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