1060ff233Sopenharmony_ci/* 2060ff233Sopenharmony_ci * Copyright (c) 2022-2024 Huawei Device Co., Ltd. 3060ff233Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4060ff233Sopenharmony_ci * you may not use this file except in compliance with the License. 5060ff233Sopenharmony_ci * You may obtain a copy of the License at 6060ff233Sopenharmony_ci * 7060ff233Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8060ff233Sopenharmony_ci * 9060ff233Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10060ff233Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11060ff233Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12060ff233Sopenharmony_ci * See the License for the specific language governing permissions and 13060ff233Sopenharmony_ci * limitations under the License. 14060ff233Sopenharmony_ci */ 15060ff233Sopenharmony_ci 16060ff233Sopenharmony_ci#include "exception_branch_checker.h" 17060ff233Sopenharmony_ci#include "hilog/log.h" 18060ff233Sopenharmony_ci#include "securec.h" 19060ff233Sopenharmony_ci 20060ff233Sopenharmony_ci#define MAX_LOG_LEN 1024 21060ff233Sopenharmony_ci#define PERMISSION_NUM 3 22060ff233Sopenharmony_ci 23060ff233Sopenharmony_cinamespace { 24060ff233Sopenharmony_cistd::vector<std::string> g_hilogPermissionList = {"{public}", "{private}", "{protect}"}; 25060ff233Sopenharmony_ci 26060ff233Sopenharmony_civoid RemoveHilogModifiers(const char *fmt, char *modifiedFmt) 27060ff233Sopenharmony_ci{ 28060ff233Sopenharmony_ci std::string strTypeFmt = fmt; 29060ff233Sopenharmony_ci std::string::size_type pos = 0; 30060ff233Sopenharmony_ci for (int32_t i = 0; i < PERMISSION_NUM; i++) { 31060ff233Sopenharmony_ci while ((pos = strTypeFmt.find(g_hilogPermissionList[i], pos)) != std::string::npos) { 32060ff233Sopenharmony_ci strTypeFmt.erase(pos, g_hilogPermissionList[i].length()); 33060ff233Sopenharmony_ci } 34060ff233Sopenharmony_ci } 35060ff233Sopenharmony_ci if (strcpy_s(modifiedFmt, strTypeFmt.length() + 1, strTypeFmt.c_str()) != EOK) { 36060ff233Sopenharmony_ci return; 37060ff233Sopenharmony_ci } 38060ff233Sopenharmony_ci} 39060ff233Sopenharmony_ci} 40060ff233Sopenharmony_ci 41060ff233Sopenharmony_ci#ifdef HILOG_FMTID 42060ff233Sopenharmony_ciint32_t HiLogPrintDictNew(const LogType type, const LogLevel level, const unsigned int domain, const char *tag, 43060ff233Sopenharmony_ci const unsigned int uuid, const unsigned int fmtOffset, const char *fmt, ...) 44060ff233Sopenharmony_ci#else 45060ff233Sopenharmony_ciint32_t HiLogPrint(LogType type, LogLevel level, unsigned int domain, const char *tag, const char *fmt, ...) 46060ff233Sopenharmony_ci#endif 47060ff233Sopenharmony_ci{ 48060ff233Sopenharmony_ci va_list args = { 0 }; 49060ff233Sopenharmony_ci char buffer[MAX_LOG_LEN] = { 0 }; 50060ff233Sopenharmony_ci char modifiedFmt[MAX_LOG_LEN] = { 0 }; 51060ff233Sopenharmony_ci 52060ff233Sopenharmony_ci RemoveHilogModifiers(fmt, modifiedFmt); 53060ff233Sopenharmony_ci 54060ff233Sopenharmony_ci va_start(args, fmt); 55060ff233Sopenharmony_ci int32_t ret = vsprintf_s(&buffer[0], sizeof(buffer), modifiedFmt, args); 56060ff233Sopenharmony_ci va_end(args); 57060ff233Sopenharmony_ci if (ret < 0) { 58060ff233Sopenharmony_ci return ret; 59060ff233Sopenharmony_ci } 60060ff233Sopenharmony_ci 61060ff233Sopenharmony_ci auto *checker = ExceptionBranchChecker::GetCurrentInstance(); 62060ff233Sopenharmony_ci if (checker != nullptr) { 63060ff233Sopenharmony_ci checker->WriteLog(buffer); 64060ff233Sopenharmony_ci } 65060ff233Sopenharmony_ci 66060ff233Sopenharmony_ci return ret; 67060ff233Sopenharmony_ci} 68060ff233Sopenharmony_ci 69060ff233Sopenharmony_ciExceptionBranchChecker* ExceptionBranchChecker::GetCurrentInstance() 70060ff233Sopenharmony_ci{ 71060ff233Sopenharmony_ci return instance_.load(); 72060ff233Sopenharmony_ci} 73060ff233Sopenharmony_ci 74060ff233Sopenharmony_ciExceptionBranchChecker::ExceptionBranchChecker(const std::string &branch) 75060ff233Sopenharmony_ci : isMatched_(false), matchBranch_(branch) 76060ff233Sopenharmony_ci{ 77060ff233Sopenharmony_ci instance_.store(this); 78060ff233Sopenharmony_ci} 79060ff233Sopenharmony_ci 80060ff233Sopenharmony_ciExceptionBranchChecker::~ExceptionBranchChecker() 81060ff233Sopenharmony_ci{ 82060ff233Sopenharmony_ci instance_.store(nullptr); 83060ff233Sopenharmony_ci} 84060ff233Sopenharmony_ci 85060ff233Sopenharmony_civoid ExceptionBranchChecker::WriteLog(const std::string& log) 86060ff233Sopenharmony_ci{ 87060ff233Sopenharmony_ci if (log.find(matchBranch_) != std::string::npos) { 88060ff233Sopenharmony_ci isMatched_ = true; 89060ff233Sopenharmony_ci } 90060ff233Sopenharmony_ci} 91060ff233Sopenharmony_ci 92060ff233Sopenharmony_cibool ExceptionBranchChecker::GetResult() const 93060ff233Sopenharmony_ci{ 94060ff233Sopenharmony_ci return isMatched_; 95060ff233Sopenharmony_ci}