1190978c3Sopenharmony_ci/*
2190978c3Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3190978c3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4190978c3Sopenharmony_ci * you may not use this file except in compliance with the License.
5190978c3Sopenharmony_ci * You may obtain a copy of the License at
6190978c3Sopenharmony_ci *
7190978c3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8190978c3Sopenharmony_ci *
9190978c3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10190978c3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11190978c3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12190978c3Sopenharmony_ci * See the License for the specific language governing permissions and
13190978c3Sopenharmony_ci * limitations under the License.
14190978c3Sopenharmony_ci */
15190978c3Sopenharmony_ci
16190978c3Sopenharmony_ci#ifndef UPDATE_LOG_H
17190978c3Sopenharmony_ci#define UPDATE_LOG_H
18190978c3Sopenharmony_ci
19190978c3Sopenharmony_ci#include <cstdint>
20190978c3Sopenharmony_ci#include <string>
21190978c3Sopenharmony_ci
22190978c3Sopenharmony_ci#include "hilog/log.h"
23190978c3Sopenharmony_ci
24190978c3Sopenharmony_cinamespace OHOS {
25190978c3Sopenharmony_cinamespace UpdateEngine {
26190978c3Sopenharmony_ciconst std::string DEFAULT_LABEL = "%";
27190978c3Sopenharmony_ciconst std::string DEFAULT_FMT_LABEL = "%s";
28190978c3Sopenharmony_ciconst std::string PRIVATE_FMT_LABEL = "%{private}s";
29190978c3Sopenharmony_ciconst std::string PUBLIC_FMT_LABEL = "%{public}s";
30190978c3Sopenharmony_ci
31190978c3Sopenharmony_cistatic constexpr uint32_t  UPDATER_SA_DOMAIN_ID = 0xD002E00;
32190978c3Sopenharmony_cienum UpdaterModuleTags {
33190978c3Sopenharmony_ci    UPDATE_SA_TAG = 0,
34190978c3Sopenharmony_ci    UPDATE_KITS_TAG,
35190978c3Sopenharmony_ci    UPDATE_FIRMWARE_TAG,
36190978c3Sopenharmony_ci    UPDATE_MODULEMGR_TAG,
37190978c3Sopenharmony_ci    UPDATE_MODULE_MAX
38190978c3Sopenharmony_ci};
39190978c3Sopenharmony_ci
40190978c3Sopenharmony_cistatic constexpr OHOS::HiviewDFX::HiLogLabel UPDATE_LABEL[UPDATE_MODULE_MAX] = {
41190978c3Sopenharmony_ci    { LOG_CORE, UPDATER_SA_DOMAIN_ID, "UPDATE_SERVICE_SA" },
42190978c3Sopenharmony_ci    { LOG_CORE, UPDATER_SA_DOMAIN_ID, "UPDATE_SERVICE_KITS" },
43190978c3Sopenharmony_ci    { LOG_CORE, UPDATER_SA_DOMAIN_ID, "UPDATE_SERVICE_FIRMWARE" },
44190978c3Sopenharmony_ci    { LOG_CORE, UPDATER_SA_DOMAIN_ID, "UPDATE_SERVICE_MODULE_MGR" }
45190978c3Sopenharmony_ci};
46190978c3Sopenharmony_ci
47190978c3Sopenharmony_cienum class UpdateLogLevel {
48190978c3Sopenharmony_ci    UPDATE_DEBUG = 0,
49190978c3Sopenharmony_ci    UPDATE_INFO,
50190978c3Sopenharmony_ci    UPDATE_WARN,
51190978c3Sopenharmony_ci    UPDATE_ERROR,
52190978c3Sopenharmony_ci    UPDATE_FATAL
53190978c3Sopenharmony_ci};
54190978c3Sopenharmony_ci
55190978c3Sopenharmony_cistruct UpdateLogContent {
56190978c3Sopenharmony_ci    HiviewDFX::HiLogLabel label;
57190978c3Sopenharmony_ci    UpdateLogLevel level;
58190978c3Sopenharmony_ci    std::string log;
59190978c3Sopenharmony_ci    std::string args;
60190978c3Sopenharmony_ci    std::string fileName;
61190978c3Sopenharmony_ci    int32_t line;
62190978c3Sopenharmony_ci
63190978c3Sopenharmony_ci    UpdateLogContent BuildWithArgs(const std::string &argsInput) const
64190978c3Sopenharmony_ci    {
65190978c3Sopenharmony_ci        return {label, level, log, argsInput, fileName, line};
66190978c3Sopenharmony_ci    };
67190978c3Sopenharmony_ci
68190978c3Sopenharmony_ci    UpdateLogContent BuildWithFmtAndArgs(const std::string &logInput, const std::string &argsInput) const
69190978c3Sopenharmony_ci    {
70190978c3Sopenharmony_ci        return {label, level, logInput, argsInput, fileName, line};
71190978c3Sopenharmony_ci    };
72190978c3Sopenharmony_ci};
73190978c3Sopenharmony_ci
74190978c3Sopenharmony_ci#ifdef UPDATE_SERVICE
75190978c3Sopenharmony_ciconstexpr int32_t UPDATE_LOG_TAG_ID = UPDATE_SA_TAG;
76190978c3Sopenharmony_ci#else
77190978c3Sopenharmony_ciconstexpr int32_t UPDATE_LOG_TAG_ID = UPDATE_KITS_TAG;
78190978c3Sopenharmony_ci#endif
79190978c3Sopenharmony_ci
80190978c3Sopenharmony_ciclass UpdateLog {
81190978c3Sopenharmony_cipublic:
82190978c3Sopenharmony_ci    static bool JudgeLevel(const UpdateLogLevel &level);
83190978c3Sopenharmony_ci    static void SetLogLevel(const UpdateLogLevel &level);
84190978c3Sopenharmony_ci    static const UpdateLogLevel &GetLogLevel();
85190978c3Sopenharmony_ci    static std::string GetBriefFileName(const std::string &file);
86190978c3Sopenharmony_ci    static void PrintLongLog(const uint32_t subModuleTag, const UpdateLogContent &logContent);
87190978c3Sopenharmony_ci
88190978c3Sopenharmony_ciprivate:
89190978c3Sopenharmony_ci    static void PrintLog(const uint32_t subModuleTag, const UpdateLogContent &logContent);
90190978c3Sopenharmony_ci    static void PrintSingleLine(const uint32_t subModuleTag, const UpdateLogContent &logContent);
91190978c3Sopenharmony_ci    static std::pair<std::string, std::string> SplitLogByFmtLabel(const std::string &log, const std::string &fmtLabel);
92190978c3Sopenharmony_ci    static std::string GetFmtLabel(const std::string &log);
93190978c3Sopenharmony_ci    static int32_t FindSubStrCount(const std::string &str, const std::string &subStr);
94190978c3Sopenharmony_ci
95190978c3Sopenharmony_ciprivate:
96190978c3Sopenharmony_ci    static UpdateLogLevel level_;
97190978c3Sopenharmony_ci};
98190978c3Sopenharmony_ci
99190978c3Sopenharmony_ci#define R_FILENAME    (__builtin_strrchr(__FILE__, '/') ? __builtin_strrchr(__FILE__, '/') + 1 : __FILE__)
100190978c3Sopenharmony_ci
101190978c3Sopenharmony_ci#define LONG_PRINT_HILOG(level, subtag, fmtlabel, fileName, line, fmt, ...) \
102190978c3Sopenharmony_ci    if (fmtlabel == PUBLIC_FMT_LABEL) { \
103190978c3Sopenharmony_ci        BASE_PRINT_LOG(level, subtag, fileName, line, "%{public}s", fmt, ##__VA_ARGS__); \
104190978c3Sopenharmony_ci    } else if (fmtlabel == PRIVATE_FMT_LABEL) { \
105190978c3Sopenharmony_ci        BASE_PRINT_LOG(level, subtag, fileName, line, "%{private}s", fmt, ##__VA_ARGS__); \
106190978c3Sopenharmony_ci    } else { \
107190978c3Sopenharmony_ci        BASE_PRINT_LOG(level, subtag, fileName, line, "%s", fmt, ##__VA_ARGS__); \
108190978c3Sopenharmony_ci    }
109190978c3Sopenharmony_ci
110190978c3Sopenharmony_ci#define BASE_PRINT_LOG(level, subtag, fileName, line, fmt, ...) \
111190978c3Sopenharmony_ci    (void)HILOG_IMPL(LOG_CORE, level, UPDATE_LABEL[subtag].domain, UPDATE_LABEL[subtag].tag, \
112190978c3Sopenharmony_ci    "[%{public}s(%{public}d)]" fmt, fileName, line, ##__VA_ARGS__)
113190978c3Sopenharmony_ci
114190978c3Sopenharmony_ci#define PRINT_LOGE(subtag, fmt, ...) BASE_PRINT_LOG(LOG_ERROR, subtag, R_FILENAME, __LINE__, fmt, ##__VA_ARGS__)
115190978c3Sopenharmony_ci#define PRINT_LOGI(subtag, fmt, ...) BASE_PRINT_LOG(LOG_INFO, subtag, R_FILENAME, __LINE__, fmt, ##__VA_ARGS__)
116190978c3Sopenharmony_ci#define PRINT_LOGD(subtag, fmt, ...) BASE_PRINT_LOG(LOG_DEBUG, subtag, R_FILENAME, __LINE__, fmt, ##__VA_ARGS__)
117190978c3Sopenharmony_ci
118190978c3Sopenharmony_ci#define ENGINE_LOGE(fmt, ...) PRINT_LOGE(UPDATE_LOG_TAG_ID, fmt, ##__VA_ARGS__)
119190978c3Sopenharmony_ci#define ENGINE_LOGI(fmt, ...) PRINT_LOGI(UPDATE_LOG_TAG_ID, fmt, ##__VA_ARGS__)
120190978c3Sopenharmony_ci#define ENGINE_LOGD(fmt, ...) PRINT_LOGD(UPDATE_LOG_TAG_ID, fmt, ##__VA_ARGS__)
121190978c3Sopenharmony_ci
122190978c3Sopenharmony_ci#define PRINT_LONG_LOGD(subtag, label, fmt, args) UpdateLog::PrintLongLog(subtag, {label,    \
123190978c3Sopenharmony_ci    UpdateLogLevel::UPDATE_DEBUG, std::string(fmt), std::string(args), std::string(__FILE__), __LINE__})
124190978c3Sopenharmony_ci#define PRINT_LONG_LOGI(subtag, label, fmt, args) UpdateLog::PrintLongLog(subtag, {label,    \
125190978c3Sopenharmony_ci    UpdateLogLevel::UPDATE_INFO, std::string(fmt), std::string(args), std::string(__FILE__), __LINE__})
126190978c3Sopenharmony_ci#define PRINT_LONG_LOGE(subtag, label, fmt, args) UpdateLog::PrintLongLog(subtag, {label,    \
127190978c3Sopenharmony_ci    UpdateLogLevel::UPDATE_ERROR, std::string(fmt), std::string(args), std::string(__FILE__), __LINE__})
128190978c3Sopenharmony_ci
129190978c3Sopenharmony_ci#define ENGINE_LONG_LOGD(fmt, args) PRINT_LONG_LOGD(UPDATE_LOG_TAG_ID, UPDATE_LABEL[UPDATE_LOG_TAG_ID], fmt, args)
130190978c3Sopenharmony_ci#define ENGINE_LONG_LOGI(fmt, args) PRINT_LONG_LOGI(UPDATE_LOG_TAG_ID, UPDATE_LABEL[UPDATE_LOG_TAG_ID], fmt, args)
131190978c3Sopenharmony_ci#define ENGINE_LONG_LOGE(fmt, args) PRINT_LONG_LOGE(UPDATE_LOG_TAG_ID, UPDATE_LABEL[UPDATE_LOG_TAG_ID], fmt, args)
132190978c3Sopenharmony_ci} // namespace UpdateEngine
133190978c3Sopenharmony_ci} // namespace OHOS
134190978c3Sopenharmony_ci#endif // UPDATE_LOG_H