1 /*
2 * Copyright (c) 2021 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 #include "log/log.h"
16 #include <chrono>
17 #include <cstdarg>
18 #include <memory>
19 #include <unordered_map>
20 #include <vector>
21 #include "securec.h"
22
23 namespace Updater {
24 static std::ofstream g_updaterLog;
25 static std::ofstream g_updaterStage;
26 static std::ofstream g_errorCode;
27 static std::string g_logTag;
28 static int g_logLevel = INFO;
29 #ifndef DIFF_PATCH_SDK
30 static OHOS::HiviewDFX::HiLogLabel g_logLabel = {LOG_CORE, 0XD002E01, "UPDATER"};
31 #endif
32
InitUpdaterLogger(const std::string & tag,const std::string & logFile,const std::string & stageFile,const std::string & errorCodeFile)33 void InitUpdaterLogger(const std::string &tag, const std::string &logFile, const std::string &stageFile,
34 const std::string &errorCodeFile)
35 {
36 g_logTag = tag;
37 #ifndef DIFF_PATCH_SDK
38 g_logLabel.tag = g_logTag.c_str();
39 #endif
40 g_updaterLog.open(logFile.c_str(), std::ios::app | std::ios::out);
41 g_updaterStage.open(stageFile.c_str(), std::ios::app | std::ios::out);
42 g_errorCode.open(errorCodeFile.c_str(), std::ios::app | std::ios::out);
43 }
44
~UpdaterLogger()45 UpdaterLogger::~UpdaterLogger()
46 {
47 std::string str = oss_.str();
48 if (g_logLevel > level_) {
49 std::cout << std::endl << std::flush;
50 return;
51 }
52 #ifndef DIFF_PATCH_SDK
53 switch (level_) {
54 case static_cast<int>(INFO):
55 OHOS::HiviewDFX::HiLog::Info(g_logLabel, "%{public}s", str.c_str());
56 break;
57 case static_cast<int>(ERROR):
58 OHOS::HiviewDFX::HiLog::Error(g_logLabel, "%{public}s", str.c_str());
59 break;
60 case static_cast<int>(DEBUG):
61 OHOS::HiviewDFX::HiLog::Debug(g_logLabel, "%{public}s", str.c_str());
62 break;
63 case static_cast<int>(WARNING):
64 OHOS::HiviewDFX::HiLog::Warn(g_logLabel, "%{public}s", str.c_str());
65 break;
66 default:
67 break;
68 }
69 #endif
70 oss_.str("");
71 oss_ << std::endl << std::flush;
72 if (g_updaterLog.is_open()) {
73 g_updaterLog << realTime_ << " " << "[" << logLevelMap_[level_] << "]" <<
74 g_logTag << " " << str << std::endl << std::flush;
75 }
76 }
77
~StageLogger()78 StageLogger::~StageLogger()
79 {
80 if (g_updaterStage.is_open()) {
81 g_updaterStage << std::endl << std::flush;
82 } else {
83 std::cout << std::endl << std::flush;
84 }
85 }
86
SetLogLevel(int level)87 void SetLogLevel(int level)
88 {
89 g_logLevel = level;
90 }
91
OutputUpdaterLog(const std::string & path,int line)92 std::ostream& UpdaterLogger::OutputUpdaterLog(const std::string &path, int line)
93 {
94 auto sysTime = std::chrono::system_clock::now();
95 auto currentTime = std::chrono::system_clock::to_time_t(sysTime);
96 struct tm *localTime = std::localtime(¤tTime);
97 if (localTime != nullptr) {
98 std::strftime(realTime_, sizeof(realTime_), "%Y-%m-%d %H:%M:%S", localTime);
99 }
100 if (g_logLevel <= level_) {
101 return oss_ << path << " " << line << " : ";
102 }
103 return std::cout;
104 }
105
OutputUpdaterStage()106 std::ostream& StageLogger::OutputUpdaterStage()
107 {
108 std::unordered_map<int, std::string> updaterStageMap = {
109 { UPDATE_STAGE_BEGIN, "BEGIN" },
110 { UPDATE_STAGE_SUCCESS, "SUCCESS" },
111 { UPDATE_STAGE_FAIL, "FAIL" },
112 { UPDATE_STAGE_OUT, "OUT" }
113 };
114 char realTime[MAX_TIME_SIZE] = {0};
115 auto sysTime = std::chrono::system_clock::now();
116 auto currentTime = std::chrono::system_clock::to_time_t(sysTime);
117 struct tm *localTime = std::localtime(¤tTime);
118 if (localTime != nullptr) {
119 std::strftime(realTime, sizeof(realTime), "%Y-%m-%d %H:%M:%S", localTime);
120 }
121
122 if (g_updaterLog.is_open()) {
123 if (stage_ == UPDATE_STAGE_OUT) {
124 return g_updaterStage << realTime << " " << g_logTag << " ";
125 }
126 if (auto it = updaterStageMap.find(stage_); it != updaterStageMap.end()) {
127 return g_updaterStage << realTime << " " << g_logTag << " status is : " <<
128 it->second << ", stage is ";
129 }
130 }
131 return std::cout;
132 }
133
Logger(int level,const char * fileName,int32_t line,const char * format,...)134 void Logger(int level, const char* fileName, int32_t line, const char* format, ...)
135 {
136 static std::vector<char> buff(1024); // 1024 : max length of buff
137 va_list list;
138 va_start(list, format);
139 int size = vsnprintf_s(reinterpret_cast<char*>(buff.data()), buff.capacity(), buff.capacity(), format, list);
140 va_end(list);
141 if (size < EOK) {
142 UpdaterLogger(level).OutputUpdaterLog(fileName, line) << "vsnprintf_s failed";
143 return;
144 }
145 std::string str(buff.data(), size);
146 UpdaterLogger(level).OutputUpdaterLog(fileName, line) << str;
147 }
148
OutputErrorCode(const std::string & path,int line,UpdaterErrorCode code)149 std::ostream& ErrorCode::OutputErrorCode(const std::string &path, int line, UpdaterErrorCode code)
150 {
151 char realTime[MAX_TIME_SIZE] = {0};
152 auto sysTime = std::chrono::system_clock::now();
153 auto currentTime = std::chrono::system_clock::to_time_t(sysTime);
154 struct tm *localTime = std::localtime(¤tTime);
155 if (localTime != nullptr) {
156 std::strftime(realTime, sizeof(realTime), "%Y-%m-%d %H:%M:%S", localTime);
157 }
158 if (g_errorCode.is_open()) {
159 return g_errorCode << realTime << " " << path << " " << line << " , error code is : " << code << std::endl;
160 }
161 return std::cout;
162 }
163 } // Updater
164