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 FOUNDATION_ACE_FRAMEWORKS_BASE_LOG_LOG_WRAPPER_H 17 #define FOUNDATION_ACE_FRAMEWORKS_BASE_LOG_LOG_WRAPPER_H 18 19 #include <cstdarg> 20 #include <cstdint> 21 #include <cstring> 22 #include <string> 23 #include "base/utils/macros.h" 24 #include "base/utils/system_properties.h" 25 26 #ifdef ACE_INSTANCE_LOG 27 #define ACE_FMT_PREFIX "[%{public}s(%{public}s)-(%{public}d)] " 28 #define ACE_LOG_ID , OHOS::Ace::LogWrapper::GetId() 29 #else 30 #define ACE_FMT_PREFIX "[%{private}s(%{private}s)] " 31 #define ACE_LOG_ID 32 #endif 33 34 #define PRINT_LOG(level, fmt, ...) \ 35 do { \ 36 if (OHOS::Ace::LogWrapper::JudgeLevel(OHOS::Ace::LogLevel::level)) { \ 37 OHOS::Ace::LogWrapper::PrintLog(OHOS::Ace::LogDomain::FRAMEWORK, OHOS::Ace::LogLevel::level, \ 38 ACE_FMT_PREFIX fmt, OHOS::Ace::LogWrapper::GetBriefFileName(__FILE__), __FUNCTION__ ACE_LOG_ID, \ 39 ##__VA_ARGS__); \ 40 } \ 41 } while (0) 42 43 #ifdef ACE_DEBUG_LOG 44 #define LOGD(fmt, ...) PRINT_LOG(DEBUG, fmt, ##__VA_ARGS__) 45 #else 46 #define LOGD(fmt, ...) ((void)0) 47 #endif 48 #define LOGI(fmt, ...) PRINT_LOG(INFO, fmt, ##__VA_ARGS__) 49 #define LOGW(fmt, ...) PRINT_LOG(WARN, fmt, ##__VA_ARGS__) 50 #define LOGE(fmt, ...) PRINT_LOG(ERROR, fmt, ##__VA_ARGS__) 51 #define LOGF(fmt, ...) PRINT_LOG(FATAL, fmt, ##__VA_ARGS__) 52 53 #define LOG_DESTROY() LOGI("destroyed") 54 #define LOG_FUNCTION() LOGD("function track: %{public}s", __FUNCTION__) 55 56 #define PRINT_APP_LOG(level, fmt, ...) \ 57 OHOS::Ace::LogWrapper::PrintLog(OHOS::Ace::LogDomain::JS_APP, OHOS::Ace::LogLevel::level, fmt, ##__VA_ARGS__) 58 59 #define APP_LOGD(fmt, ...) PRINT_APP_LOG(DEBUG, fmt, ##__VA_ARGS__) 60 #define APP_LOGI(fmt, ...) PRINT_APP_LOG(INFO, fmt, ##__VA_ARGS__) 61 #define APP_LOGW(fmt, ...) PRINT_APP_LOG(WARN, fmt, ##__VA_ARGS__) 62 #define APP_LOGE(fmt, ...) PRINT_APP_LOG(ERROR, fmt, ##__VA_ARGS__) 63 #define APP_LOGF(fmt, ...) PRINT_APP_LOG(FATAL, fmt, ##__VA_ARGS__) 64 65 namespace OHOS::Ace { 66 67 enum class LogDomain : uint32_t { 68 FRAMEWORK = 0, 69 JS_APP, 70 }; 71 72 enum class LogLevel : uint32_t { 73 DEBUG = 0, 74 INFO, 75 WARN, 76 ERROR, 77 FATAL, 78 }; 79 80 class ACE_FORCE_EXPORT LogWrapper final { 81 public: JudgeLevel(LogLevel level)82 static bool JudgeLevel(LogLevel level) 83 { 84 if (level == LogLevel::DEBUG) { 85 return SystemProperties::GetDebugEnabled(); 86 } 87 return level_ <= level; 88 } 89 SetLogLevel(LogLevel level)90 static void SetLogLevel(LogLevel level) 91 { 92 level_ = level; 93 } 94 GetLogLevel()95 static LogLevel GetLogLevel() 96 { 97 return level_; 98 } 99 GetBriefFileName(const char * name)100 static const char* GetBriefFileName(const char* name) 101 { 102 static const char separator = GetSeparatorCharacter(); 103 const char* p = strrchr(name, separator); 104 return p != nullptr ? p + 1 : name; 105 } 106 StripFormatString(const std::string & prefix,std::string & str)107 static void StripFormatString(const std::string& prefix, std::string& str) 108 { 109 for (auto pos = str.find(prefix, 0); pos != std::string::npos; pos = str.find(prefix, pos)) { 110 str.erase(pos, prefix.size()); 111 } 112 } 113 ReplaceFormatString(const std::string & prefix,const std::string & replace,std::string & str)114 static void ReplaceFormatString(const std::string& prefix, const std::string& replace, std::string& str) 115 { 116 for (auto pos = str.find(prefix, 0); pos != std::string::npos; pos = str.find(prefix, pos)) { 117 str.replace(pos, prefix.size(), replace); 118 } 119 } 120 PrintLog(LogDomain domain,LogLevel level,const char * fmt,...)121 static void PrintLog(LogDomain domain, LogLevel level, const char* fmt, ...) 122 __attribute__((__format__(os_log, 3, 4))) 123 { 124 va_list args; 125 va_start(args, fmt); 126 PrintLog(domain, level, fmt, args); 127 va_end(args); 128 } 129 130 // MUST implement these interface on each platform. 131 static char GetSeparatorCharacter(); 132 static void PrintLog(LogDomain domain, LogLevel level, const char* fmt, va_list args); 133 static int32_t GetId(); 134 135 private: 136 LogWrapper() = delete; 137 ~LogWrapper() = delete; 138 139 static LogLevel level_; 140 }; 141 142 } // namespace OHOS::Ace 143 144 #endif // FOUNDATION_ACE_FRAMEWORKS_BASE_LOG_LOG_WRAPPER_H 145