1 /*
2 * Copyright (C) 2021 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 "nstackx_log.h"
17 #include "securec.h"
18 #ifdef ENABLE_DFINDER_HILOG
19 #include "hilog/log.h"
20 #endif
21
22 __attribute__((format(printf, 3, 4)))
PrintfImpl(const char * moduleName,uint32_t logLevel,const char * format,...)23 void PrintfImpl(const char *moduleName, uint32_t logLevel, const char *format, ...)
24 {
25 va_list args;
26
27 va_start(args, format);
28 printf("%u %s: ", logLevel, moduleName);
29 vprintf(format, args);
30 va_end(args);
31 }
32
33 #ifdef ENABLE_DFINDER_HILOG
34 #define LOG_PRINT_MAX_LEN 256
HiLogPrintWrapper(const char * tag,uint32_t domain,const char * buf,uint32_t logLevel)35 static void HiLogPrintWrapper(const char *tag, uint32_t domain, const char *buf, uint32_t logLevel)
36 {
37 LogLevel hiLogLevel = LOG_ERROR;
38 switch (logLevel) {
39 case NSTACKX_LOG_LEVEL_DEBUG:
40 hiLogLevel = LOG_DEBUG;
41 break;
42 case NSTACKX_LOG_LEVEL_INFO:
43 hiLogLevel = LOG_INFO;
44 break;
45 case NSTACKX_LOG_LEVEL_WARNING:
46 hiLogLevel = LOG_WARN;
47 break;
48 case NSTACKX_LOG_LEVEL_ERROR:
49 hiLogLevel = LOG_ERROR;
50 break;
51 case NSTACKX_LOG_LEVEL_FATAL:
52 hiLogLevel = LOG_FATAL;
53 break;
54 default:
55 break;
56 }
57 if (tag == NULL || strlen(tag) == 0) {
58 HiLogPrint(LOG_CORE, hiLogLevel, domain, NSTACKX_DEFAULT_TAG, "%{public}s", buf);
59 } else {
60 HiLogPrint(LOG_CORE, hiLogLevel, domain, tag, "%{public}s", buf);
61 }
62 }
63
NstackxHiLogImpl(const char * tag,uint32_t domain,const char * moduleName,uint32_t logLevel,const char * format,...)64 __attribute__((format(printf, 5, 6))) void NstackxHiLogImpl(const char *tag,
65 uint32_t domain,
66 const char *moduleName,
67 uint32_t logLevel,
68 const char *format,
69 ...)
70 {
71 uint32_t ulPos;
72 char szStr[LOG_PRINT_MAX_LEN] = {0};
73 va_list args;
74 int32_t ret;
75
76 ret = sprintf_s(szStr, sizeof(szStr), "%u %s: ", logLevel, moduleName);
77 if (ret < 0) {
78 HILOG_ERROR(LOG_CORE, "[DISC]softbus log error");
79 return;
80 }
81 ulPos = strlen(szStr);
82 (void)memset_s(&args, sizeof(va_list), 0, sizeof(va_list));
83 va_start(args, format);
84 ret = vsprintf_s(&szStr[ulPos], sizeof(szStr) - ulPos, format, args);
85 va_end(args);
86 if (ret < 0) {
87 HILOG_ERROR(LOG_CORE, "[DISC]softbus log len error");
88 return;
89 }
90 HiLogPrintWrapper(tag, domain, szStr, logLevel);
91 return;
92 }
93 #endif
94