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 <string> 20 #include "hilog/log.h" 21 22 namespace OHOS { 23 namespace EventFwk { 24 #ifndef LOG_DOMAIN 25 #define LOG_DOMAIN 0xD001100 26 #endif 27 #ifndef EVENT_LOG_TAG 28 #define EVENT_LOG_TAG NULL 29 #endif 30 31 enum class EventLogLevel { DEBUG = 0, INFO, WARN, ERROR, FATAL }; 32 33 static constexpr OHOS::HiviewDFX::HiLogLabel Event_LABEL = {LOG_CORE, LOG_DOMAIN, EVENT_LOG_TAG}; 34 35 class EventLogWrapper { 36 public: 37 static bool JudgeLevel(const EventLogLevel &level); 38 SetLogLevel(const EventLogLevel & level)39 static void SetLogLevel(const EventLogLevel &level) 40 { 41 level_ = level; 42 } 43 GetLogLevel()44 static const EventLogLevel &GetLogLevel() 45 { 46 return level_; 47 } 48 49 static std::string GetBriefFileName(const char *str); 50 51 private: 52 static EventLogLevel level_; 53 }; 54 55 #define PRINT_LOG(LEVEL, Level, fmt, ...) \ 56 if (EventLogWrapper::JudgeLevel(EventLogLevel::LEVEL)) \ 57 OHOS::HiviewDFX::HiLog::Level(Event_LABEL, \ 58 "[%{public}s:(%{public}s):%{public}d] " fmt, \ 59 EventLogWrapper::GetBriefFileName(__FILE__).c_str(), \ 60 __FUNCTION__, \ 61 __LINE__, \ 62 ##__VA_ARGS__) 63 64 #define EVENT_LOGD(fmt, ...) PRINT_LOG(DEBUG, Debug, fmt, ##__VA_ARGS__) 65 #define EVENT_LOGI(fmt, ...) PRINT_LOG(INFO, Info, fmt, ##__VA_ARGS__) 66 #define EVENT_LOGW(fmt, ...) PRINT_LOG(WARN, Warn, fmt, ##__VA_ARGS__) 67 #define EVENT_LOGE(fmt, ...) PRINT_LOG(ERROR, Error, fmt, ##__VA_ARGS__) 68 #define EVENT_LOGF(fmt, ...) PRINT_LOG(FATAL, Fatal, fmt, ##__VA_ARGS__) 69 } // namespace EventFwk 70 } // namespace OHOS 71 #endif // FOUNDATION_EVENT_COMMON_LOG_INCLUDE_EVENT_LOG_WRAPPER_H 72