1 /* 2 * Copyright (c) 2023 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 UPDATE_LOG_H 17 #define UPDATE_LOG_H 18 19 #include <string> 20 21 #include "hilog/log.h" 22 23 namespace OHOS { 24 namespace UpdateEngine { 25 #ifdef UPDATE_SERVICE 26 static constexpr OHOS::HiviewDFX::HiLogLabel UPDATE_LABEL = {LOG_CORE, 0xD002E00, "UPDATE_SA"}; 27 #else 28 static constexpr OHOS::HiviewDFX::HiLogLabel UPDATE_LABEL = {LOG_CORE, 0xD002E00, "UPDATE_KITS"}; 29 #endif 30 31 enum class UpdateLogLevel { 32 UPDATE_DEBUG = 0, 33 UPDATE_INFO, 34 UPDATE_WARN, 35 UPDATE_ERROR, 36 UPDATE_FATAL 37 }; 38 39 struct UpdateLogContent { 40 HiviewDFX::HiLogLabel label; 41 UpdateLogLevel level; 42 std::string log; 43 std::string args; 44 std::string fileName; 45 int32_t line; 46 BuildWithArgsUpdateLogContent47 UpdateLogContent BuildWithArgs(const std::string &argsInput) const 48 { 49 return {label, level, log, argsInput, fileName, line}; 50 }; 51 BuildWithFmtAndArgsUpdateLogContent52 UpdateLogContent BuildWithFmtAndArgs(const std::string &logInput, const std::string &argsInput) const 53 { 54 return {label, level, logInput, argsInput, fileName, line}; 55 }; 56 }; 57 58 class UpdateLog { 59 public: 60 static bool JudgeLevel(const UpdateLogLevel &level); 61 static void SetLogLevel(const UpdateLogLevel &level); 62 static const UpdateLogLevel &GetLogLevel(); 63 static std::string GetBriefFileName(const std::string &file); 64 static void PrintLongLog(const UpdateLogContent &logContent); 65 66 private: 67 static void PrintLog(const UpdateLogContent &logContent); 68 static void PrintSingleLine(const UpdateLogContent &logContent); 69 static std::pair<std::string, std::string> SplitLogByFmtLabel(const std::string &log, const std::string &fmtLabel); 70 static std::string GetFmtLabel(const std::string &log); 71 static int32_t FindSubStrCount(const std::string &str, const std::string &subStr); 72 73 private: 74 static UpdateLogLevel level_; 75 }; 76 77 // 暂时记录两边日志 78 #define BASE_PRINT_LOG(label, level, hilogMethod, fileName, line, fmt, ...) \ 79 if (UpdateLog::JudgeLevel((level))) \ 80 OHOS::HiviewDFX::HiLog::hilogMethod((label), ("[%{public}s(%{public}d)] " + \ 81 std::string(fmt)).c_str(), fileName, line, ##__VA_ARGS__) 82 83 #define PRINT_LOG(label, level, hilogMethod, fmt, ...) BASE_PRINT_LOG(label, level, \ 84 hilogMethod, UpdateLog::GetBriefFileName(std::string(__FILE__)).c_str(), __LINE__, fmt, ##__VA_ARGS__) 85 86 #define PRINT_LOGD(label, fmt, ...) PRINT_LOG(label, UpdateLogLevel::UPDATE_DEBUG, Debug, fmt, ##__VA_ARGS__) 87 #define PRINT_LOGI(label, fmt, ...) PRINT_LOG(label, UpdateLogLevel::UPDATE_INFO, Info, fmt, ##__VA_ARGS__) 88 #define PRINT_LOGE(label, fmt, ...) PRINT_LOG(label, UpdateLogLevel::UPDATE_ERROR, Error, fmt, ##__VA_ARGS__) 89 90 #define ENGINE_LOGD(fmt, ...) PRINT_LOGD(UPDATE_LABEL, fmt, ##__VA_ARGS__) 91 #define ENGINE_LOGI(fmt, ...) PRINT_LOGI(UPDATE_LABEL, fmt, ##__VA_ARGS__) 92 #define ENGINE_LOGE(fmt, ...) PRINT_LOGE(UPDATE_LABEL, fmt, ##__VA_ARGS__) 93 94 #define PRINT_LONG_LOGD(label, fmt, args) UpdateLog::PrintLongLog({label, \ 95 UpdateLogLevel::UPDATE_DEBUG, std::string(fmt), std::string(args), std::string(__FILE__), __LINE__}) 96 #define PRINT_LONG_LOGI(label, fmt, args) UpdateLog::PrintLongLog({label, \ 97 UpdateLogLevel::UPDATE_INFO, std::string(fmt), std::string(args), std::string(__FILE__), __LINE__}) 98 #define PRINT_LONG_LOGE(label, fmt, args) UpdateLog::PrintLongLog({label, \ 99 UpdateLogLevel::UPDATE_ERROR, std::string(fmt), std::string(args), std::string(__FILE__), __LINE__}) 100 101 #define ENGINE_LONG_LOGD(fmt, args) PRINT_LONG_LOGD(UPDATE_LABEL, fmt, args) 102 #define ENGINE_LONG_LOGI(fmt, args) PRINT_LONG_LOGI(UPDATE_LABEL, fmt, args) 103 #define ENGINE_LONG_LOGE(fmt, args) PRINT_LONG_LOGE(UPDATE_LABEL, fmt, args) 104 } // namespace UpdateEngine 105 } // namespace OHOS 106 #endif // UPDATE_LOG_H