1 /* 2 * Copyright (c) 2021-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 #ifndef BASE_ACCOUNT_COMMON_LOG_INCLUDE_ACCOUNT_LOG_WRAPPER_H 17 #define BASE_ACCOUNT_COMMON_LOG_INCLUDE_ACCOUNT_LOG_WRAPPER_H 18 19 #include <string> 20 #include "hilog/log.h" 21 22 23 namespace OHOS { 24 namespace AccountSA { 25 enum class AccountLogLevel { DEBUG = 0, INFO, WARN, ERROR, FATAL }; 26 27 static constexpr OHOS::HiviewDFX::HiLogLabel ACCOUNT_LABEL = {LOG_CORE, LOG_DOMAIN, ACCOUNT_LOG_TAG}; 28 29 class AccountLogWrapper { 30 public: 31 static bool JudgeLevel(const AccountLogLevel &level); 32 SetLogLevel(const AccountLogLevel & level)33 static void SetLogLevel(const AccountLogLevel &level) 34 { 35 level_ = level; 36 } 37 GetLogLevel()38 static const AccountLogLevel &GetLogLevel() 39 { 40 return level_; 41 } 42 43 static std::string GetBriefFileName(const std::string &file); 44 45 private: 46 static AccountLogLevel level_; 47 }; 48 49 #define PRINT_LOG(LEVEL, Level, fmt, ...) \ 50 if (AccountLogWrapper::JudgeLevel(AccountLogLevel::LEVEL)) \ 51 OHOS::HiviewDFX::HiLog::Level(ACCOUNT_LABEL, \ 52 "[%{public}s(%{public}s:%{public}d)] " fmt, \ 53 AccountLogWrapper::GetBriefFileName(std::string(__FILE__)).c_str(), \ 54 __FUNCTION__, \ 55 __LINE__, \ 56 ##__VA_ARGS__) 57 58 #define ACCOUNT_LOGD(fmt, ...) PRINT_LOG(DEBUG, Debug, fmt, ##__VA_ARGS__) 59 #define ACCOUNT_LOGI(fmt, ...) PRINT_LOG(INFO, Info, fmt, ##__VA_ARGS__) 60 #define ACCOUNT_LOGW(fmt, ...) PRINT_LOG(WARN, Warn, fmt, ##__VA_ARGS__) 61 #define ACCOUNT_LOGE(fmt, ...) PRINT_LOG(ERROR, Error, fmt, ##__VA_ARGS__) 62 #define ACCOUNT_LOGF(fmt, ...) PRINT_LOG(FATAL, Fatal, fmt, ##__VA_ARGS__) 63 } // namespace AccountSA 64 } // namespace OHOS 65 66 #endif // BASE_ACCOUNT_COMMON_LOG_INCLUDE_ACCOUNT_LOG_WRAPPER_H 67