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 "softbus_log.h"
17
18 #include <securec.h>
19 #include <string.h>
20
21 #include "softbus_feature_config.h"
22
23 #define LOG_NAME_MAX_LEN 5
24 #define LOG_PRINT_MAX_LEN 256
25
26 // anonymize should mask more than half of the string
27 #define EXPECTED_ANONYMIZED_TIMES 2
28
29 static int32_t g_logLevel;
30
31 typedef struct {
32 SoftBusLogModule mod;
33 char name[LOG_NAME_MAX_LEN];
34 } LogInfo;
35
36 static LogInfo g_logInfo[SOFTBUS_LOG_MODULE_MAX] = {
37 {SOFTBUS_LOG_AUTH, "AUTH"},
38 {SOFTBUS_LOG_TRAN, "TRAN"},
39 {SOFTBUS_LOG_CONN, "CONN"},
40 {SOFTBUS_LOG_LNN, "LNN"},
41 {SOFTBUS_LOG_DISC, "DISC"},
42 {SOFTBUS_LOG_COMM, "COMM"},
43 };
44
SoftBusLog(SoftBusLogModule module,SoftBusLogLevel level,const char * fmt,...)45 void SoftBusLog(SoftBusLogModule module, SoftBusLogLevel level, const char *fmt, ...)
46 {
47 uint32_t ulPos;
48 char szStr[LOG_PRINT_MAX_LEN] = {0};
49 va_list arg;
50 int32_t ret;
51
52 if (module >= SOFTBUS_LOG_MODULE_MAX || level >= SOFTBUS_LOG_LEVEL_MAX) {
53 HILOG_ERROR(SOFTBUS_HILOG_ID, "[COMM]softbus log type or module error");
54 return;
55 }
56
57 SoftbusGetConfig(SOFTBUS_INT_ADAPTER_LOG_LEVEL, (unsigned char *)&g_logLevel, sizeof(g_logLevel));
58 if ((int32_t)level < g_logLevel) {
59 return;
60 }
61
62 ret = sprintf_s(szStr, sizeof(szStr), "[%s]", g_logInfo[module].name);
63 if (ret < 0) {
64 HILOG_ERROR(SOFTBUS_HILOG_ID, "[COMM]softbus log error");
65 return;
66 }
67 ulPos = strlen(szStr);
68 (void)memset_s(&arg, sizeof(va_list), 0, sizeof(va_list));
69 va_start(arg, fmt);
70 ret = vsprintf_s(&szStr[ulPos], sizeof(szStr) - ulPos, fmt, arg);
71 va_end(arg);
72 if (ret < 0) {
73 HILOG_WARN(SOFTBUS_HILOG_ID, "[COMM]softbus log len error");
74 return;
75 }
76 SoftBusOutPrint(szStr, level);
77
78 return;
79 }
80
Anonymizes(const char * target,const uint8_t expectAnonymizedLength)81 const char *Anonymizes(const char *target, const uint8_t expectAnonymizedLength)
82 {
83 if (target == NULL) {
84 return "NULL";
85 }
86 if (expectAnonymizedLength == 0) {
87 return "BADLENGTH";
88 }
89 size_t targetLen = strlen(target);
90 if (targetLen / expectAnonymizedLength < EXPECTED_ANONYMIZED_TIMES) {
91 return "TOOSHORT";
92 }
93
94 return target + (targetLen - expectAnonymizedLength);
95 }
96