• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <securec.h>
17 #include <stdbool.h>
18 #include <stdio.h>
19 
20 #include "hilog_inner.h"
21 #include "hilog/log_c.h"
22 
23 #define MAX_LOG_LEN 4096 /* maximum length of a log, include '\0' */
24 #define MAX_TAG_LEN 32   /* log tag size, include '\0' */
25 
OH_LOG_Print(LogType type,LogLevel level,unsigned int domain,const char * tag,const char * fmt,...)26 int OH_LOG_Print(LogType type, LogLevel level, unsigned int domain, const char *tag, const char *fmt, ...)
27 {
28     int ret;
29     va_list ap;
30     va_start(ap, fmt);
31     ret = HiLogPrintArgs(type, level, domain, tag, fmt, ap);
32     va_end(ap);
33     return ret;
34 }
35 
OH_LOG_PrintMsg(LogType type,LogLevel level,unsigned int domain,const char * tag,const char * message)36 int OH_LOG_PrintMsg(LogType type, LogLevel level, unsigned int domain, const char *tag, const char *message)
37 {
38     return OH_LOG_Print(type, level, domain, tag, "%{public}s", message);
39 }
40 
OH_LOG_PrintMsgByLen(LogType type,LogLevel level,unsigned int domain,const char * tag,size_t tagLen,const char * message,size_t messageLen)41 int OH_LOG_PrintMsgByLen(LogType type, LogLevel level, unsigned int domain, const char *tag, size_t tagLen,
42     const char *message, size_t messageLen)
43 {
44     if (tag == NULL || message == NULL) {
45         return -1;
46     }
47     size_t copyTagLen = tagLen < MAX_TAG_LEN - 1 ? tagLen : MAX_TAG_LEN - 1;
48     size_t copyMsgLen = messageLen < MAX_LOG_LEN - 1 ? messageLen : MAX_LOG_LEN - 1;
49     char newTag[copyTagLen + 1];
50     char newMessage[copyMsgLen + 1];
51     (void)memset_s(newTag, copyTagLen + 1, 0, copyTagLen + 1);
52     (void)memset_s(newMessage, copyMsgLen + 1, 0, copyMsgLen + 1);
53     if (strncpy_s(newTag, copyTagLen + 1, tag, copyTagLen) < 0 ||
54         strncpy_s(newMessage, copyMsgLen + 1, message, copyMsgLen) < 0) {
55         return -1;
56     }
57     return OH_LOG_Print(type, level, domain, newTag, "%{public}s", newMessage);
58 }
59 
OH_LOG_VPrint(LogType type,LogLevel level,unsigned int domain,const char * tag,const char * fmt,va_list ap)60 int OH_LOG_VPrint(LogType type, LogLevel level, unsigned int domain, const char *tag, const char *fmt, va_list ap)
61 {
62     return HiLogPrintArgs(type, level, domain, tag, fmt, ap);
63 }
64 
OH_LOG_IsLoggable(unsigned int domain,const char * tag,LogLevel level)65 bool OH_LOG_IsLoggable(unsigned int domain, const char *tag, LogLevel level)
66 {
67     return HiLogIsLoggable(domain, tag, level);
68 }
69 
OH_LOG_SetCallback(LogCallback callback)70 void OH_LOG_SetCallback(LogCallback callback)
71 {
72     return LOG_SetCallback(callback);
73 }
74 
OH_LOG_SetMinLogLevel(LogLevel level)75 void OH_LOG_SetMinLogLevel(LogLevel level)
76 {
77     return HiLogSetAppMinLogLevel(level);
78 }
79