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 #define SoftBusLog(Module, Level, Fmt, Args...) SoftBusLogImpl(Module, Level, __func__, __LINE__, Fmt, ##Args)
47
48 void SoftBusLogImpl(SoftBusLogModule module, SoftBusLogLevel level, const char* funcName,
49 int lineNo, const char *fmt, ...);
50 void NstackxLog(const char *moduleName, uint32_t nstackLevel, const char *format, ...);
51
52 void AnonyPacketPrintout(SoftBusLogModule module, const char *msg, const char *packet, size_t packetLen);
53
54 const char *AnonyDevId(char **outName, const char *inName);
55
56 /* Note: need call SoftBusFree(*outputStr) */
57 const char *ToSecureStrDeviceID(const char *deviceId, char **outputStr);
58
59 #define UUID_ANONYMIZED_LENGTH 4
60 #define NETWORKID_ANONYMIZED_LENGTH 4
61 #define UDID_ANONYMIZED_LENGTH 4
62 #define MACADDR_ANONYMIZED_LENGTH 5
63 #define IP_ANONYMIZED_LENGTH 4
64
65 const char *Anonymizes(const char *target, const uint8_t expectAnonymizedLength);
66
AnonymizesUUID(const char * input)67 static inline const char *AnonymizesUUID(const char *input)
68 {
69 return Anonymizes(input, UUID_ANONYMIZED_LENGTH);
70 }
71
AnonymizesNetworkID(const char * input)72 static inline const char *AnonymizesNetworkID(const char *input)
73 {
74 return Anonymizes(input, NETWORKID_ANONYMIZED_LENGTH);
75 }
76
AnonymizesUDID(const char * input)77 static inline const char *AnonymizesUDID(const char *input)
78 {
79 return Anonymizes(input, UDID_ANONYMIZED_LENGTH);
80 }
81
AnonymizesMac(const char * input)82 static inline const char *AnonymizesMac(const char *input)
83 {
84 return Anonymizes(input, MACADDR_ANONYMIZED_LENGTH);
85 }
86
AnonymizesIp(const char * input)87 inline const char *AnonymizesIp(const char *input)
88 {
89 if (input == NULL) {
90 return "NULL";
91 }
92 return Anonymizes(input, IP_ANONYMIZED_LENGTH);
93 }
94
95 // discovery log print macro
96 #define DLOGD(fmt, ...) SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_DBG, fmt, ##__VA_ARGS__)
97 #define DLOGI(fmt, ...) SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_INFO, fmt, ##__VA_ARGS__)
98 #define DLOGW(fmt, ...) SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_WARN, fmt, ##__VA_ARGS__)
99 #define DLOGE(fmt, ...) SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_ERROR, fmt, ##__VA_ARGS__)
100
101 // connection log print macro
102 #define CLOGD(fmt, ...) SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_DBG, fmt, ##__VA_ARGS__)
103 #define CLOGI(fmt, ...) SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, fmt, ##__VA_ARGS__)
104 #define CLOGW(fmt, ...) SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_WARN, fmt, ##__VA_ARGS__)
105 #define CLOGE(fmt, ...) SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, fmt, ##__VA_ARGS__)
106
107 // bus center lnn log print macro
108 #define LLOGD(fmt, ...) SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_DBG, fmt, ##__VA_ARGS__)
109 #define LLOGI(fmt, ...) SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, fmt, ##__VA_ARGS__)
110 #define LLOGW(fmt, ...) SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_WARN, fmt, ##__VA_ARGS__)
111 #define LLOGE(fmt, ...) SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, fmt, ##__VA_ARGS__)
112
113 // transmission log print macro
114 #define TLOGD(fmt, ...) SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_DBG, fmt, ##__VA_ARGS__)
115 #define TLOGI(fmt, ...) SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, fmt, ##__VA_ARGS__)
116 #define TLOGW(fmt, ...) SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_WARN, fmt, ##__VA_ARGS__)
117 #define TLOGE(fmt, ...) SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, fmt, ##__VA_ARGS__)
118
119 // authority log print macro
120 #define ALOGD(fmt, ...) SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_DBG, fmt, ##__VA_ARGS__)
121 #define ALOGI(fmt, ...) SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_INFO, fmt, ##__VA_ARGS__)
122 #define ALOGW(fmt, ...) SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_WARN, fmt, ##__VA_ARGS__)
123 #define ALOGE(fmt, ...) SoftBusLog(SOFTBUS_LOG_AUTH, SOFTBUS_LOG_ERROR, fmt, ##__VA_ARGS__)
124
125 // common log print macro
126 #define MLOGD(fmt, ...) SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_DBG, fmt, ##__VA_ARGS__)
127 #define MLOGI(fmt, ...) SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_INFO, fmt, ##__VA_ARGS__)
128 #define MLOGW(fmt, ...) SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_WARN, fmt, ##__VA_ARGS__)
129 #define MLOGE(fmt, ...) SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, fmt, ##__VA_ARGS__)
130
131 #define CHECK_AND_RETURN_RET_LOG(cond, ret, log, fmt, ...) \
132 do { \
133 if (!(cond)) { \
134 log(fmt, ##__VA_ARGS__); \
135 return ret; \
136 } \
137 } while (0)
138
139 #define CHECK_AND_RETURN_LOG(cond, log, fmt, ...) \
140 do { \
141 if (!(cond)) { \
142 log(fmt, ##__VA_ARGS__); \
143 return; \
144 } \
145 } while (0)
146
147 #define DISC_CHECK_AND_RETURN_RET_LOG(cond, ret, fmt, ...) \
148 CHECK_AND_RETURN_RET_LOG(cond, ret, DLOGE, fmt, ##__VA_ARGS__)
149 #define DISC_CHECK_AND_RETURN_LOG(cond, fmt, ...) \
150 CHECK_AND_RETURN_LOG(cond, DLOGE, fmt, ##__VA_ARGS__)
151
152 #define CONN_CHECK_AND_RETURN_RET_LOG(cond, ret, fmt, ...) \
153 CHECK_AND_RETURN_RET_LOG(cond, ret, CLOGE, fmt, ##__VA_ARGS__)
154 #define CONN_CHECK_AND_RETURN_LOG(cond, fmt, ...) \
155 CHECK_AND_RETURN_LOG(cond, CLOGE, fmt, ##__VA_ARGS__)
156
157 #define LNN_CHECK_AND_RETURN_RET_LOG(cond, ret, fmt, ...) \
158 CHECK_AND_RETURN_RET_LOG(cond, ret, LLOGE, fmt, ##__VA_ARGS__)
159 #define LNN_CHECK_AND_RETURN_LOG(cond, fmt, ...) \
160 CHECK_AND_RETURN_LOG(cond, LLOGE, fmt, ##__VA_ARGS__)
161
162 #define TRAN_CHECK_AND_RETURN_RET_LOG(cond, ret, fmt, ...) \
163 CHECK_AND_RETURN_RET_LOG(cond, ret, TLOGE, fmt, ##__VA_ARGS__)
164 #define TRAN_CHECK_AND_RETURN_LOG(cond, fmt, ...) \
165 CHECK_AND_RETURN_LOG(cond, TLOGE, fmt, ##__VA_ARGS__)
166
167 #define AUTH_CHECK_AND_RETURN_RET_LOG(cond, ret, fmt, ...) \
168 CHECK_AND_RETURN_RET_LOG(cond, ret, ALOGE, fmt, ##__VA_ARGS__)
169 #define AUTH_CHECK_AND_RETURN_LOG(cond, fmt, ...) \
170 CHECK_AND_RETURN_LOG(cond, ALOGE, fmt, ##__VA_ARGS__)
171
172 #define COMM_CHECK_AND_RETURN_RET_LOG(cond, ret, fmt, ...) \
173 CHECK_AND_RETURN_RET_LOG(cond, ret, MLOGE, fmt, ##__VA_ARGS__)
174 #define COMM_CHECK_AND_RETURN_LOG(cond, fmt, ...) \
175 CHECK_AND_RETURN_LOG(cond, MLOGE, fmt, ##__VA_ARGS__)
176
177 #ifdef __cplusplus
178 #if __cplusplus
179 }
180 #endif /* __cplusplus */
181 #endif /* __cplusplus */
182
183 #endif /* SOFTBUS_LOG_H */