1 /*
2 * Copyright (c) 2022 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 "exception_branch_checker.h"
17 #include "softbus_log.h"
18 #include "securec.h"
19
20 static constexpr int32_t LOG_BUF_LEN = 512;
21 static const char *g_logName[SOFTBUS_LOG_MODULE_MAX] = {
22 "AUTH", "TRAN", "CONN", "LNN", "DISC", "COMM"
23 };
24
SoftBusLogImpl(SoftBusLogModule module,SoftBusLogLevel level,const char * funcName,int lineNo,const char * fmt,...)25 void SoftBusLogImpl(SoftBusLogModule module, SoftBusLogLevel level, const char* funcName,
26 int lineNo, const char *fmt, ...)
27 {
28 if (module >= SOFTBUS_LOG_MODULE_MAX) {
29 HILOG_ERROR(SOFTBUS_HILOG_ID, "[COMM] log module exceed max");
30 return;
31 }
32
33 char buffer[LOG_BUF_LEN];
34
35 int usedLen = sprintf_s(buffer, LOG_BUF_LEN, "[%s][%s:%d] ", g_logName[module], funcName, lineNo);
36 if (usedLen < 0) {
37 HILOG_ERROR(SOFTBUS_HILOG_ID, "[COMM] sprintf_s log error");
38 return;
39 }
40
41 va_list arg;
42 va_start(arg, fmt);
43 int ret = vsprintf_s(buffer + usedLen, LOG_BUF_LEN - usedLen, fmt, arg);
44 va_end(arg);
45 if (ret < 0) {
46 HILOG_ERROR(SOFTBUS_HILOG_ID, "[COMM] vsprintf_s log error");
47 return;
48 }
49
50 switch (level) {
51 case SOFTBUS_LOG_DBG:
52 HILOG_DEBUG(SOFTBUS_HILOG_ID, "%{public}s", buffer);
53 break;
54 case SOFTBUS_LOG_INFO:
55 HILOG_INFO(SOFTBUS_HILOG_ID, "%{public}s", buffer);
56 break;
57 case SOFTBUS_LOG_WARN:
58 HILOG_WARN(SOFTBUS_HILOG_ID, "%{public}s", buffer);
59 break;
60 case SOFTBUS_LOG_ERROR:
61 HILOG_ERROR(SOFTBUS_HILOG_ID, "%{public}s", buffer);
62 break;
63 default:
64 break;
65 }
66
67 auto *checker = ExceptionBranchChecker::GetCurrentInstance();
68 if (checker != nullptr) {
69 checker->WriteLog(buffer);
70 }
71 }
72
GetCurrentInstance()73 ExceptionBranchChecker* ExceptionBranchChecker::GetCurrentInstance()
74 {
75 return instance_.load();
76 }
77
ExceptionBranchChecker(const std::string & branch)78 ExceptionBranchChecker::ExceptionBranchChecker(const std::string &branch)
79 : isMatched_(false), matchBranch_(branch)
80 {
81 instance_.store(this);
82 }
83
~ExceptionBranchChecker()84 ExceptionBranchChecker::~ExceptionBranchChecker()
85 {
86 instance_.store(nullptr);
87 }
88
WriteLog(const std::string & log)89 void ExceptionBranchChecker::WriteLog(const std::string& log)
90 {
91 if (log.find(matchBranch_) != std::string::npos) {
92 HILOG_ERROR(SOFTBUS_HILOG_ID, "[Unit Test] exception branch match !!");
93 isMatched_ = true;
94 }
95 }
96
GetResult() const97 bool ExceptionBranchChecker::GetResult() const
98 {
99 return isMatched_;
100 }