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 "hc_log.h"
17
18 #include <inttypes.h>
19 #include "securec.h"
20
21 #define LOG_PRINT_MAX_LEN 2048
22
23 #ifdef DEV_AUTH_DEBUG_PRINTF
24
25 #include <stdio.h>
26 #include <stdlib.h>
27
28 #define DEV_AUTH_LOG_DEBUG(buf) printf("[D][DEVAUTH]: %s\n", buf)
29 #define DEV_AUTH_LOG_INFO(buf) printf("[I][DEVAUTH]: %s\n", buf)
30 #define DEV_AUTH_LOG_WARN(buf) printf("[W][DEVAUTH]: %s\n", buf)
31 #define DEV_AUTH_LOG_ERROR(buf) printf("[E][DEVAUTH]: %s\n", buf)
32
33 #else
34
35 #include "hilog/log.h"
36
37 #define DEV_AUTH_LOG_DEBUG(buf) HiLogPrint(LOG_CORE, LOG_DEBUG, DEV_AUTH_LOG_DOMAIN, "[DEVAUTH]", "%{public}s", buf)
38 #define DEV_AUTH_LOG_INFO(buf) HiLogPrint(LOG_CORE, LOG_INFO, DEV_AUTH_LOG_DOMAIN, "[DEVAUTH]", "%{public}s", buf)
39 #define DEV_AUTH_LOG_WARN(buf) HiLogPrint(LOG_CORE, LOG_WARN, DEV_AUTH_LOG_DOMAIN, "[DEVAUTH]", "%{public}s", buf)
40 #define DEV_AUTH_LOG_ERROR(buf) HiLogPrint(LOG_CORE, LOG_ERROR, DEV_AUTH_LOG_DOMAIN, "[DEVAUTH]", "%{public}s", buf)
41
42 #endif
43
44 static __thread int32_t g_logMode = 0;
45 static __thread int64_t g_traceId = 0;
46
DevAuthOutPrint(const char * buf,DevAuthLogLevel level)47 static void DevAuthOutPrint(const char *buf, DevAuthLogLevel level)
48 {
49 switch (level) {
50 case DEV_AUTH_LOG_LEVEL_DEBUG:
51 DEV_AUTH_LOG_DEBUG(buf);
52 break;
53 case DEV_AUTH_LOG_LEVEL_INFO:
54 DEV_AUTH_LOG_INFO(buf);
55 break;
56 case DEV_AUTH_LOG_LEVEL_WARN:
57 DEV_AUTH_LOG_WARN(buf);
58 break;
59 case DEV_AUTH_LOG_LEVEL_ERROR:
60 DEV_AUTH_LOG_ERROR(buf);
61 break;
62 default:
63 break;
64 }
65 }
66
DevAuthLogPrint(DevAuthLogLevel level,const char * funName,const char * fmt,...)67 void DevAuthLogPrint(DevAuthLogLevel level, const char *funName, const char *fmt, ...)
68 {
69 int32_t ulPos = 0;
70 char outStr[LOG_PRINT_MAX_LEN] = {0};
71 int32_t res;
72 if (g_logMode == TRACE_MODE) {
73 res = sprintf_s(outStr, sizeof(outStr), "<%" PRId64 ">%s: ", g_traceId, funName);
74 } else {
75 res = sprintf_s(outStr, sizeof(outStr), "%s: ", funName);
76 }
77 if (res < 0) {
78 return;
79 }
80 ulPos = strlen(outStr);
81 va_list arg;
82 va_start(arg, fmt);
83 res = vsprintf_s(&outStr[ulPos], sizeof(outStr) - ulPos, fmt, arg);
84 va_end(arg);
85 if (res < 0) {
86 return;
87 }
88 DevAuthOutPrint(outStr, level);
89 }
90
SetLogMode(LogMode mode)91 void SetLogMode(LogMode mode)
92 {
93 g_logMode = mode;
94 g_traceId = 0;
95 }
96
SetTraceId(int64_t traceId)97 void SetTraceId(int64_t traceId)
98 {
99 g_traceId = traceId;
100 }
101