• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
SoftBusLog(SoftBusLogModule module,SoftBusLogLevel level,const char * fmt,...)25 void SoftBusLog(SoftBusLogModule module, SoftBusLogLevel level, const char *fmt, ...)
26 {
27     if (module >= SOFTBUS_LOG_MODULE_MAX) {
28         HILOG_ERROR(SOFTBUS_HILOG_ID, "[COMM] log module exceed max");
29         return;
30     }
31 
32     char buffer[LOG_BUF_LEN];
33 
34     int usedLen = sprintf_s(buffer, LOG_BUF_LEN, "[%s] ", g_logName[module]);
35     if (usedLen < 0) {
36         HILOG_ERROR(SOFTBUS_HILOG_ID, "[COMM] sprintf_s log error");
37         return;
38     }
39 
40     va_list arg;
41     va_start(arg, fmt);
42     int ret = vsprintf_s(buffer + usedLen, LOG_BUF_LEN - usedLen, fmt, arg);
43     va_end(arg);
44     if (ret < 0) {
45         HILOG_ERROR(SOFTBUS_HILOG_ID, "[COMM] vsprintf_s log error");
46         return;
47     }
48 
49     switch (level) {
50         case SOFTBUS_LOG_DBG:
51             HILOG_DEBUG(SOFTBUS_HILOG_ID, "%{public}s", buffer);
52             break;
53         case SOFTBUS_LOG_INFO:
54             HILOG_INFO(SOFTBUS_HILOG_ID, "%{public}s", buffer);
55             break;
56         case SOFTBUS_LOG_WARN:
57             HILOG_WARN(SOFTBUS_HILOG_ID, "%{public}s", buffer);
58             break;
59         case SOFTBUS_LOG_ERROR:
60             HILOG_ERROR(SOFTBUS_HILOG_ID, "%{public}s", buffer);
61             break;
62         default:
63             break;
64     }
65 
66     auto *checker = ExceptionBranchChecker::GetCurrentInstance();
67     if (checker != nullptr) {
68         checker->WriteLog(buffer);
69     }
70 }
71 
GetCurrentInstance()72 ExceptionBranchChecker* ExceptionBranchChecker::GetCurrentInstance()
73 {
74     return instance_.load();
75 }
76 
ExceptionBranchChecker(const std::string & branch)77 ExceptionBranchChecker::ExceptionBranchChecker(const std::string &branch)
78     : isMatched_(false), matchBranch_(branch)
79 {
80     instance_.store(this);
81 }
82 
~ExceptionBranchChecker()83 ExceptionBranchChecker::~ExceptionBranchChecker()
84 {
85     instance_.store(nullptr);
86 }
87 
WriteLog(const std::string & log)88 void ExceptionBranchChecker::WriteLog(const std::string& log)
89 {
90     if (log.find(matchBranch_) != std::string::npos) {
91         HILOG_ERROR(SOFTBUS_HILOG_ID, "[Unit Test] exception branch match !!");
92         isMatched_ = true;
93     }
94 }
95 
GetResult() const96 bool ExceptionBranchChecker::GetResult() const
97 {
98     return isMatched_;
99 }