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 #ifndef FOUNDATION_RESOURCESCHEDULE_WORKSCHEDULER_UTILS_HILOG_H 16 #define FOUNDATION_RESOURCESCHEDULE_WORKSCHEDULER_UTILS_HILOG_H 17 18 #include <string> 19 20 #include "hilog/log.h" 21 22 namespace OHOS { 23 namespace WorkScheduler { 24 #ifndef WORKSCHEDULER_DOMAIN_ID 25 #define WORKSCHEDULER_DOMAIN_ID 0xD001712 26 #endif 27 28 #ifndef WORKSCHEDULER_MGR_LOG_TAG 29 #define WORKSCHEDULER_MGR_LOG_TAG "WORK_SCHEDULER" 30 #endif 31 32 static constexpr OHOS::HiviewDFX::HiLogLabel WORKSCHEDULER_LABEL = {LOG_CORE, 33 WORKSCHEDULER_DOMAIN_ID, WORKSCHEDULER_MGR_LOG_TAG}; 34 35 enum class WorkSchedLogLevel : uint8_t { DEBUG = 0, INFO, WARN, ERROR, FATAL }; 36 37 class WorkSchedHilog { 38 public: 39 WorkSchedHilog() = delete; 40 ~WorkSchedHilog() = delete; 41 42 /** 43 * @brief Judge level. 44 * 45 * @param level The level. 46 * @return True if success,else false. 47 */ 48 static bool JudgeLevel(const WorkSchedLogLevel &level); 49 50 /** 51 * @brief Set log level. 52 * 53 * @param level The level. 54 */ SetLogLevel(const WorkSchedLogLevel & level)55 static void SetLogLevel(const WorkSchedLogLevel &level) 56 { 57 level_ = level; 58 } 59 60 /** 61 * @brief Get log level. 62 * 63 * @return Level. 64 */ GetLogLevel()65 static const WorkSchedLogLevel &GetLogLevel() 66 { 67 return level_; 68 } 69 70 /** 71 * @brief Get brief file name. 72 * 73 * @param str The str. 74 * @return Brief file name. 75 */ 76 static std::string GetBriefFileName(const char *str); 77 78 private: 79 static WorkSchedLogLevel level_; 80 }; 81 82 #define WS_PRINT_LOG(LEVEL, Level, fmt, ...) \ 83 if (WorkScheduler::WorkSchedHilog::JudgeLevel(WorkScheduler::WorkSchedLogLevel::LEVEL)) \ 84 OHOS::HiviewDFX::HiLog::Level(WorkScheduler::WORKSCHEDULER_LABEL, \ 85 "[%{public}s(%{public}s):%{public}d] " fmt, \ 86 WorkScheduler::WorkSchedHilog::GetBriefFileName(__FILE__).c_str(), \ 87 __FUNCTION__, \ 88 __LINE__, \ 89 ##__VA_ARGS__) 90 91 #define WS_HILOGD(fmt, ...) WS_PRINT_LOG(DEBUG, Debug, fmt, ##__VA_ARGS__) 92 #define WS_HILOGI(fmt, ...) WS_PRINT_LOG(INFO, Info, fmt, ##__VA_ARGS__) 93 #define WS_HILOGW(fmt, ...) WS_PRINT_LOG(WARN, Warn, fmt, ##__VA_ARGS__) 94 #define WS_HILOGE(fmt, ...) WS_PRINT_LOG(ERROR, Error, fmt, ##__VA_ARGS__) 95 #define WS_HILOGF(fmt, ...) WS_PRINT_LOG(FATAL, Fatal, fmt, ##__VA_ARGS__) 96 } // namespace WorkScheduler 97 } // namespace OHOS 98 #endif // FOUNDATION_RESOURCESCHEDULE_WORKSCHEDULER_UTILS_HILOG_H