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 #ifndef SOFTBUS_LOG_H
17 #define SOFTBUS_LOG_H
18
19 #include <stdint.h>
20 #include <stdio.h>
21 #include <inttypes.h>
22 #include "softbus_adapter_log.h"
23
24 #ifdef __cplusplus
25 #if __cplusplus
26 extern "C" {
27 #endif
28 #endif
29
30 #if defined(__ICCARM__) || defined(__LITEOS_M__)
31 #define SOFTBUS_DPRINTF(fd, fmt, ...)
32 #else
33 #define SOFTBUS_DPRINTF(fd, fmt, ...) dprintf(fd, fmt, ##__VA_ARGS__)
34 #endif
35
36 typedef enum {
37 SOFTBUS_LOG_AUTH,
38 SOFTBUS_LOG_TRAN,
39 SOFTBUS_LOG_CONN,
40 SOFTBUS_LOG_LNN,
41 SOFTBUS_LOG_DISC,
42 SOFTBUS_LOG_COMM,
43 SOFTBUS_LOG_MODULE_MAX,
44 } SoftBusLogModule;
45
46 void SoftBusLog(SoftBusLogModule module, SoftBusLogLevel level, const char *fmt, ...);
47 void NstackxLog(const char *moduleName, uint32_t nstackLevel, const char *format, ...);
48
49 void AnonyPacketPrintout(SoftBusLogModule module, const char *msg, const char *packet, size_t packetLen);
50
51 const char *AnonyDevId(char **outName, const char *inName);
52
53 #define UUID_ANONYMIZED_LENGTH 4
54 #define NETWORKID_ANONYMIZED_LENGTH 4
55 #define UDID_ANONYMIZED_LENGTH 4
56 #define MACADDR_ANONYMIZED_LENGTH 5
57
58 const char *Anonymizes(const char *target, const uint8_t expectAnonymizedLength);
59
AnonymizesUUID(const char * input)60 static inline const char *AnonymizesUUID(const char *input)
61 {
62 return Anonymizes(input, UUID_ANONYMIZED_LENGTH);
63 }
64
AnonymizesNetworkID(const char * input)65 static inline const char *AnonymizesNetworkID(const char *input)
66 {
67 return Anonymizes(input, NETWORKID_ANONYMIZED_LENGTH);
68 }
69
AnonymizesUDID(const char * input)70 static inline const char *AnonymizesUDID(const char *input)
71 {
72 return Anonymizes(input, UDID_ANONYMIZED_LENGTH);
73 }
74
AnonymizesMac(const char * input)75 static inline const char *AnonymizesMac(const char *input)
76 {
77 return Anonymizes(input, MACADDR_ANONYMIZED_LENGTH);
78 }
79
80 // discovery log print macro
81 #define DLOGD(fmt, ...) SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_DBG, "[%s()] " fmt, __FUNCTION__, ##__VA_ARGS__)
82 #define DLOGI(fmt, ...) SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_INFO, "[%s()] " fmt, __FUNCTION__, ##__VA_ARGS__)
83 #define DLOGW(fmt, ...) SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_WARN, "[%s()] " fmt, __FUNCTION__, ##__VA_ARGS__)
84 #define DLOGE(fmt, ...) SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_ERROR, "[%s()] " fmt, __FUNCTION__, ##__VA_ARGS__)
85
86 // connection log print macro
87 #define CLOGD(fmt, ...) SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_DBG, "[%s()] " fmt, __FUNCTION__, ##__VA_ARGS__)
88 #define CLOGI(fmt, ...) SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "[%s()] " fmt, __FUNCTION__, ##__VA_ARGS__)
89 #define CLOGW(fmt, ...) SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_WARN, "[%s()] " fmt, __FUNCTION__, ##__VA_ARGS__)
90 #define CLOGE(fmt, ...) SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "[%s()] " fmt, __FUNCTION__, ##__VA_ARGS__)
91
92 // bus center lnn log print macro
93 #define LLOGD(fmt, ...) SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_DBG, "[%s()] " fmt, __FUNCTION__, ##__VA_ARGS__)
94 #define LLOGI(fmt, ...) SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "[%s()] " fmt, __FUNCTION__, ##__VA_ARGS__)
95 #define LLOGW(fmt, ...) SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_WARN, "[%s()] " fmt, __FUNCTION__, ##__VA_ARGS__)
96 #define LLOGE(fmt, ...) SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "[%s()] " fmt, __FUNCTION__, ##__VA_ARGS__)
97
98 // transmission log print macro
99 #define TLOGD(fmt, ...) SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_DBG, "[%s()] " fmt, __FUNCTION__, ##__VA_ARGS__)
100 #define TLOGI(fmt, ...) SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "[%s()] " fmt, __FUNCTION__, ##__VA_ARGS__)
101 #define TLOGW(fmt, ...) SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_WARN, "[%s()] " fmt, __FUNCTION__, ##__VA_ARGS__)
102 #define TLOGE(fmt, ...) SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "[%s()] " fmt, __FUNCTION__, ##__VA_ARGS__)
103
104 // authority log print macro
105 #define ALOGD(fmt, ...) SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_DBG, "[%s()] " fmt, __FUNCTION__, ##__VA_ARGS__)
106 #define ALOGI(fmt, ...) SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_INFO, "[%s()] " fmt, __FUNCTION__, ##__VA_ARGS__)
107 #define ALOGW(fmt, ...) SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_WARN, "[%s()] " fmt, __FUNCTION__, ##__VA_ARGS__)
108 #define ALOGE(fmt, ...) SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_ERROR, "[%s()] " fmt, __FUNCTION__, ##__VA_ARGS__)
109
110 // common log print macro
111 #define MLOGD(fmt, ...) SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_DBG, "[%s()] " fmt, __FUNCTION__, ##__VA_ARGS__)
112 #define MLOGI(fmt, ...) SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_INFO, "[%s()] " fmt, __FUNCTION__, ##__VA_ARGS__)
113 #define MLOGW(fmt, ...) SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_WARN, "[%s()] " fmt, __FUNCTION__, ##__VA_ARGS__)
114 #define MLOGE(fmt, ...) SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "[%s()] " fmt, __FUNCTION__, ##__VA_ARGS__)
115
116 #define CHECK_AND_RETURN_RET_LOG(cond, ret, log, fmt, ...) \
117 do { \
118 if (!(cond)) { \
119 log(fmt, ##__VA_ARGS__); \
120 return ret; \
121 } \
122 } while (0)
123
124 #define CHECK_AND_RETURN_LOG(cond, log, fmt, ...) \
125 do { \
126 if (!(cond)) { \
127 log(fmt, ##__VA_ARGS__); \
128 return; \
129 } \
130 } while (0)
131
132 #define DISC_CHECK_AND_RETURN_RET_LOG(cond, ret, fmt, ...) \
133 CHECK_AND_RETURN_RET_LOG(cond, ret, DLOGE, fmt, ##__VA_ARGS__)
134 #define DISC_CHECK_AND_RETURN_LOG(cond, fmt, ...) \
135 CHECK_AND_RETURN_LOG(cond, DLOGE, fmt, ##__VA_ARGS__)
136
137 #define CONN_CHECK_AND_RETURN_RET_LOG(cond, ret, fmt, ...) \
138 CHECK_AND_RETURN_RET_LOG(cond, ret, CLOGE, fmt, ##__VA_ARGS__)
139 #define CONN_CHECK_AND_RETURN_LOG(cond, fmt, ...) \
140 CHECK_AND_RETURN_LOG(cond, CLOGE, fmt, ##__VA_ARGS__)
141
142 #define LNN_CHECK_AND_RETURN_RET_LOG(cond, ret, fmt, ...) \
143 CHECK_AND_RETURN_RET_LOG(cond, ret, LLOGE, fmt, ##__VA_ARGS__)
144 #define LNN_CHECK_AND_RETURN_LOG(cond, fmt, ...) \
145 CHECK_AND_RETURN_LOG(cond, LLOGE, fmt, ##__VA_ARGS__)
146
147 #define TRAN_CHECK_AND_RETURN_RET_LOG(cond, ret, fmt, ...) \
148 CHECK_AND_RETURN_RET_LOG(cond, ret, TLOGE, fmt, ##__VA_ARGS__)
149 #define TRAN_CHECK_AND_RETURN_LOG(cond, fmt, ...) \
150 CHECK_AND_RETURN_LOG(cond, TLOGE, fmt, ##__VA_ARGS__)
151
152 #define AUTH_CHECK_AND_RETURN_RET_LOG(cond, ret, fmt, ...) \
153 CHECK_AND_RETURN_RET_LOG(cond, ret, ALOGE, fmt, ##__VA_ARGS__)
154 #define AUTH_CHECK_AND_RETURN_LOG(cond, fmt, ...) \
155 CHECK_AND_RETURN_LOG(cond, ALOGE, fmt, ##__VA_ARGS__)
156
157 #define COMM_CHECK_AND_RETURN_RET_LOG(cond, ret, fmt, ...) \
158 CHECK_AND_RETURN_RET_LOG(cond, ret, MLOGE, fmt, ##__VA_ARGS__)
159 #define COMM_CHECK_AND_RETURN_LOG(cond, fmt, ...) \
160 CHECK_AND_RETURN_LOG(cond, MLOGE, fmt, ##__VA_ARGS__)
161
162 #ifdef __cplusplus
163 #if __cplusplus
164 }
165 #endif /* __cplusplus */
166 #endif /* __cplusplus */
167
168 #endif /* SOFTBUS_LOG_H */