1 /* 2 * Copyright (c) 2021 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 FOUNDATION_EVENT_COMMON_LOG_INCLUDE_EVENT_LOG_WRAPPER_H 17 #define FOUNDATION_EVENT_COMMON_LOG_INCLUDE_EVENT_LOG_WRAPPER_H 18 19 #include <inttypes.h> 20 #include <string> 21 #include "hilog/log.h" 22 23 namespace OHOS { 24 namespace EventFwk { 25 #ifndef EVENT_LOG_DOMAIN 26 #define EVENT_LOG_DOMAIN 0xD001200 27 #endif 28 #ifndef EVENT_LOG_TAG 29 #define EVENT_LOG_TAG "Ces" 30 #endif 31 32 enum class EventLogLevel { 33 DEBUG = 0, 34 INFO, 35 WARN, 36 ERROR, 37 FATAL 38 }; 39 40 static constexpr OHOS::HiviewDFX::HiLogLabel Event_LABEL = { LOG_CORE, EVENT_LOG_DOMAIN, EVENT_LOG_TAG }; 41 42 class EventLogWrapper { 43 public: 44 /** 45 * Judges the level of the log. 46 * 47 * @param level Indicates the level of the log. 48 * @return Returns true if success; false otherwise. 49 */ 50 static bool JudgeLevel(const EventLogLevel &level); 51 52 /** 53 * Sets the level of the log. 54 * 55 * @param level Indicates the level of the log. 56 */ SetLogLevel(const EventLogLevel & level)57 static void SetLogLevel(const EventLogLevel &level) 58 { 59 level_ = level; 60 } 61 62 /** 63 * Gets the level of the log. 64 * 65 * @return Returns the level of the log. 66 */ GetLogLevel()67 static const EventLogLevel &GetLogLevel() 68 { 69 return level_; 70 } 71 72 /** 73 * Gets the brief name of the file. 74 * 75 * @param str Indicates the full name of the file. 76 * @return Returns the full name of the file. 77 */ 78 static std::string GetBriefFileName(const char *str); 79 80 private: 81 static EventLogLevel level_; 82 }; 83 84 #define PRINT_LOG(LEVEL, Level, fmt, ...) \ 85 if (EventLogWrapper::JudgeLevel(EventLogLevel::LEVEL)) \ 86 OHOS::HiviewDFX::HiLog::Level(Event_LABEL, \ 87 "[%{public}s:(%{public}s):%{public}d] " fmt, \ 88 EventLogWrapper::GetBriefFileName(__FILE__).c_str(), \ 89 __FUNCTION__, \ 90 __LINE__, \ 91 ##__VA_ARGS__) 92 93 #define EVENT_LOGD(fmt, ...) PRINT_LOG(DEBUG, Debug, fmt, ##__VA_ARGS__) 94 #define EVENT_LOGI(fmt, ...) PRINT_LOG(INFO, Info, fmt, ##__VA_ARGS__) 95 #define EVENT_LOGW(fmt, ...) PRINT_LOG(WARN, Warn, fmt, ##__VA_ARGS__) 96 #define EVENT_LOGE(fmt, ...) PRINT_LOG(ERROR, Error, fmt, ##__VA_ARGS__) 97 #define EVENT_LOGF(fmt, ...) PRINT_LOG(FATAL, Fatal, fmt, ##__VA_ARGS__) 98 } // namespace EventFwk 99 } // namespace OHOS 100 #endif // FOUNDATION_EVENT_COMMON_LOG_INCLUDE_EVENT_LOG_WRAPPER_H 101