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#include "update_log.h"
17190978c3Sopenharmony_ci
18190978c3Sopenharmony_cinamespace OHOS {
19190978c3Sopenharmony_cinamespace UpdateEngine {
20190978c3Sopenharmony_ciUpdateLogLevel UpdateLog::level_ = UpdateLogLevel::UPDATE_INFO;
21190978c3Sopenharmony_ciconstexpr int32_t COUNT_ONE = 1;
22190978c3Sopenharmony_ciconstexpr int32_t LONG_LOG_LEN = 900;
23190978c3Sopenharmony_ci
24190978c3Sopenharmony_cibool UpdateLog::JudgeLevel(const UpdateLogLevel &level)
25190978c3Sopenharmony_ci{
26190978c3Sopenharmony_ci    const UpdateLogLevel &curLevel = GetLogLevel();
27190978c3Sopenharmony_ci    if (level <= curLevel) {
28190978c3Sopenharmony_ci        return true;
29190978c3Sopenharmony_ci    }
30190978c3Sopenharmony_ci    return true;
31190978c3Sopenharmony_ci}
32190978c3Sopenharmony_ci
33190978c3Sopenharmony_civoid UpdateLog::SetLogLevel(const UpdateLogLevel &level)
34190978c3Sopenharmony_ci{
35190978c3Sopenharmony_ci    level_ = level;
36190978c3Sopenharmony_ci}
37190978c3Sopenharmony_ci
38190978c3Sopenharmony_ciconst UpdateLogLevel &UpdateLog::GetLogLevel()
39190978c3Sopenharmony_ci{
40190978c3Sopenharmony_ci    return level_;
41190978c3Sopenharmony_ci}
42190978c3Sopenharmony_ci
43190978c3Sopenharmony_cistd::string UpdateLog::GetBriefFileName(const std::string &file)
44190978c3Sopenharmony_ci{
45190978c3Sopenharmony_ci    auto pos = file.find_last_of("/");
46190978c3Sopenharmony_ci    if (pos != std::string::npos) {
47190978c3Sopenharmony_ci        return file.substr(pos + 1);
48190978c3Sopenharmony_ci    }
49190978c3Sopenharmony_ci    pos = file.find_last_of("\\");
50190978c3Sopenharmony_ci    if (pos != std::string::npos) {
51190978c3Sopenharmony_ci        return file.substr(pos + 1);
52190978c3Sopenharmony_ci    }
53190978c3Sopenharmony_ci    return file;
54190978c3Sopenharmony_ci}
55190978c3Sopenharmony_ci
56190978c3Sopenharmony_civoid UpdateLog::PrintLongLog(const uint32_t subModuleTag, const UpdateLogContent &logContent)
57190978c3Sopenharmony_ci{
58190978c3Sopenharmony_ci    std::string fmtLabel = GetFmtLabel(logContent.log);
59190978c3Sopenharmony_ci    std::pair<std::string, std::string> splitLogPair = SplitLogByFmtLabel(logContent.log, fmtLabel);
60190978c3Sopenharmony_ci
61190978c3Sopenharmony_ci    PrintLog(subModuleTag, logContent.BuildWithFmtAndArgs(PUBLIC_FMT_LABEL, splitLogPair.first));     // log前缀不做打印控制
62190978c3Sopenharmony_ci    PrintLog(subModuleTag, logContent.BuildWithFmtAndArgs(fmtLabel, logContent.args));                // args采用fmt进行控制
63190978c3Sopenharmony_ci    PrintLog(subModuleTag, logContent.BuildWithFmtAndArgs(PUBLIC_FMT_LABEL, splitLogPair.second));    // log后缀不做打印控制
64190978c3Sopenharmony_ci}
65190978c3Sopenharmony_ci
66190978c3Sopenharmony_civoid UpdateLog::PrintLog(const uint32_t subModuleTag, const UpdateLogContent &logContent)
67190978c3Sopenharmony_ci{
68190978c3Sopenharmony_ci    int32_t printPos = 0;
69190978c3Sopenharmony_ci    int32_t len = static_cast<int32_t>(logContent.args.length());
70190978c3Sopenharmony_ci    while (printPos < len) {
71190978c3Sopenharmony_ci        int32_t printLen = std::min(len - printPos, LONG_LOG_LEN);
72190978c3Sopenharmony_ci        PrintSingleLine(subModuleTag, logContent.BuildWithArgs(logContent.args.substr(printPos, printLen)));
73190978c3Sopenharmony_ci        printPos += printLen;
74190978c3Sopenharmony_ci    }
75190978c3Sopenharmony_ci}
76190978c3Sopenharmony_ci
77190978c3Sopenharmony_civoid UpdateLog::PrintSingleLine(const uint32_t subModuleTag, const UpdateLogContent &logContent)
78190978c3Sopenharmony_ci{
79190978c3Sopenharmony_ci    // BASE_PRINT_LOG的第三个参数是hilog方法名,即hilogMethod
80190978c3Sopenharmony_ci    std::string fmtLabel = GetFmtLabel(logContent.log);
81190978c3Sopenharmony_ci    switch (logContent.level) {
82190978c3Sopenharmony_ci        case UpdateLogLevel::UPDATE_DEBUG:
83190978c3Sopenharmony_ci            LONG_PRINT_HILOG(LOG_DEBUG, subModuleTag, logContent.log,
84190978c3Sopenharmony_ci                UpdateLog::GetBriefFileName(logContent.fileName).c_str(), logContent.line, logContent.args.c_str());
85190978c3Sopenharmony_ci            break;
86190978c3Sopenharmony_ci        case UpdateLogLevel::UPDATE_INFO:
87190978c3Sopenharmony_ci            LONG_PRINT_HILOG(LOG_INFO, subModuleTag, logContent.log,
88190978c3Sopenharmony_ci                UpdateLog::GetBriefFileName(logContent.fileName).c_str(), logContent.line, logContent.args.c_str());
89190978c3Sopenharmony_ci            break;
90190978c3Sopenharmony_ci        case UpdateLogLevel::UPDATE_ERROR:
91190978c3Sopenharmony_ci            LONG_PRINT_HILOG(LOG_ERROR, subModuleTag, logContent.log,
92190978c3Sopenharmony_ci                UpdateLog::GetBriefFileName(logContent.fileName).c_str(), logContent.line, logContent.args.c_str());
93190978c3Sopenharmony_ci            break;
94190978c3Sopenharmony_ci        default:
95190978c3Sopenharmony_ci            break;
96190978c3Sopenharmony_ci    }
97190978c3Sopenharmony_ci}
98190978c3Sopenharmony_ci
99190978c3Sopenharmony_cistd::pair<std::string, std::string> UpdateLog::SplitLogByFmtLabel(const std::string &log, const std::string &fmtLabel)
100190978c3Sopenharmony_ci{
101190978c3Sopenharmony_ci    if (fmtLabel.empty()) {
102190978c3Sopenharmony_ci        // 如果log中没有%{public|private}s,则把log全部内容作为前缀字符串,后缀字符串为空
103190978c3Sopenharmony_ci        return std::make_pair(log, "");
104190978c3Sopenharmony_ci    }
105190978c3Sopenharmony_ci    return std::make_pair(log.substr(0, log.find(fmtLabel, 0)), log.substr(log.find(fmtLabel, 0) +
106190978c3Sopenharmony_ci        fmtLabel.length()));
107190978c3Sopenharmony_ci}
108190978c3Sopenharmony_ci
109190978c3Sopenharmony_cistd::string UpdateLog::GetFmtLabel(const std::string &log)
110190978c3Sopenharmony_ci{
111190978c3Sopenharmony_ci    if (FindSubStrCount(log, DEFAULT_LABEL) != COUNT_ONE) {
112190978c3Sopenharmony_ci        // 如果log中%字符出现次数不为一个,说明log格式有误,返回空的fmtLabel
113190978c3Sopenharmony_ci        return "";
114190978c3Sopenharmony_ci    }
115190978c3Sopenharmony_ci    if (FindSubStrCount(log, DEFAULT_FMT_LABEL) == COUNT_ONE) {
116190978c3Sopenharmony_ci        return DEFAULT_FMT_LABEL;
117190978c3Sopenharmony_ci    }
118190978c3Sopenharmony_ci    if (FindSubStrCount(log, PRIVATE_FMT_LABEL) == COUNT_ONE) {
119190978c3Sopenharmony_ci        return PRIVATE_FMT_LABEL;
120190978c3Sopenharmony_ci    }
121190978c3Sopenharmony_ci    if (FindSubStrCount(log, PUBLIC_FMT_LABEL) == COUNT_ONE) {
122190978c3Sopenharmony_ci        return PUBLIC_FMT_LABEL;
123190978c3Sopenharmony_ci    }
124190978c3Sopenharmony_ci    return "";
125190978c3Sopenharmony_ci}
126190978c3Sopenharmony_ci
127190978c3Sopenharmony_ciint32_t UpdateLog::FindSubStrCount(const std::string &str, const std::string &subStr)
128190978c3Sopenharmony_ci{
129190978c3Sopenharmony_ci    int32_t count = 0;
130190978c3Sopenharmony_ci    for (size_t pos = 0; (pos = str.find(subStr, pos)) != std::string::npos; pos++) {
131190978c3Sopenharmony_ci        count++;
132190978c3Sopenharmony_ci    }
133190978c3Sopenharmony_ci    return count;
134190978c3Sopenharmony_ci}
135190978c3Sopenharmony_ci} // namespace UpdateEngine
136190978c3Sopenharmony_ci} // namespace OHOS
137