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 OS_ACCOUNT_FRAMEWORKS_COMMON_LOG_INCLUDE_ACCOUNT_LOG_WRAPPER_H 17 #define OS_ACCOUNT_FRAMEWORKS_COMMON_LOG_INCLUDE_ACCOUNT_LOG_WRAPPER_H 18 19 #include <string> 20 #include "hilog/log.h" 21 22 namespace OHOS { 23 namespace AccountSA { 24 #ifdef __FILE_NAME__ 25 #define LOG_FILE_NAME __FILE_NAME__ 26 #else 27 #define LOG_FILE_NAME (__builtin_strrchr(__FILE__, '/') ? __builtin_strrchr(__FILE__, '/') + 1 : __FILE__) 28 #endif 29 30 enum class AccountLogLevel { DEBUG = 0, INFO, WARN, ERROR, FATAL }; 31 constexpr OHOS::HiviewDFX::HiLogLabel ACCOUNT_LABEL = {LOG_CORE, LOG_DOMAIN, ACCOUNT_LOG_TAG}; 32 33 class AccountLogWrapper { 34 public: 35 static bool JudgeLevel(const AccountLogLevel &level); 36 SetLogLevel(const AccountLogLevel & level)37 static void SetLogLevel(const AccountLogLevel &level) 38 { 39 level_ = level; 40 } 41 GetLogLevel()42 static const AccountLogLevel &GetLogLevel() 43 { 44 return level_; 45 } 46 47 static std::string GetBriefFileName(const std::string &file); 48 49 private: 50 static AccountLogLevel level_; 51 }; 52 53 #define ARGS(fmt, ...) "[%{public}s@%{public}s:%{public}d] " fmt, __FUNCTION__, LOG_FILE_NAME, __LINE__, ##__VA_ARGS__ 54 #define ACCOUNT_LOGD(fmt, ...) \ 55 ((void)HILOG_IMPL(ACCOUNT_LABEL.type, LOG_DEBUG, ACCOUNT_LABEL.domain, ACCOUNT_LABEL.tag, \ 56 "[%{public}s@%{public}s:%{public}d] " fmt, __FUNCTION__, LOG_FILE_NAME, __LINE__, ##__VA_ARGS__)) 57 #define ACCOUNT_LOGI(fmt, ...) \ 58 ((void)HILOG_IMPL(ACCOUNT_LABEL.type, LOG_INFO, ACCOUNT_LABEL.domain, ACCOUNT_LABEL.tag, \ 59 "[%{public}s@%{public}s:%{public}d] " fmt, __FUNCTION__, LOG_FILE_NAME, __LINE__, ##__VA_ARGS__)) 60 #define ACCOUNT_LOGW(fmt, ...) \ 61 ((void)HILOG_IMPL(ACCOUNT_LABEL.type, LOG_WARN, ACCOUNT_LABEL.domain, ACCOUNT_LABEL.tag, \ 62 "[%{public}s@%{public}s:%{public}d] " fmt, __FUNCTION__, LOG_FILE_NAME, __LINE__, ##__VA_ARGS__)) 63 #define ACCOUNT_LOGE(fmt, ...) \ 64 ((void)HILOG_IMPL(ACCOUNT_LABEL.type, LOG_ERROR, ACCOUNT_LABEL.domain, ACCOUNT_LABEL.tag, \ 65 "[%{public}s@%{public}s:%{public}d] " fmt, __FUNCTION__, LOG_FILE_NAME, __LINE__, ##__VA_ARGS__)) 66 #define ACCOUNT_LOGF(fmt, ...) \ 67 ((void)HILOG_IMPL(ACCOUNT_LABEL.type, LOG_FATAL, ACCOUNT_LABEL.domain, ACCOUNT_LABEL.tag, \ 68 "[%{public}s@%{public}s:%{public}d] " fmt, __FUNCTION__, LOG_FILE_NAME, __LINE__, ##__VA_ARGS__)) 69 } // namespace AccountSA 70 } // namespace OHOS 71 72 #endif // OS_ACCOUNT_FRAMEWORKS_COMMON_LOG_INCLUDE_ACCOUNT_LOG_WRAPPER_H 73