• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef UPDATE_LOG_H__
16 #define UPDATE_LOG_H__
17 
18 #include <cstring>
19 #include <iostream>
20 #include <fstream>
21 #include <sstream>
22 #include <string>
23 #include <unordered_map>
24 #include "error_code.h"
25 #ifndef DIFF_PATCH_SDK
26 #include "hilog/log.h"
27 #endif
28 
29 namespace Updater {
30 #ifdef __WIN32
31 #undef ERROR
32 #endif
33 
34 constexpr size_t MAX_LOG_SPACE = 4 * 5 * 1024 * 1024;
35 constexpr int MAX_TIME_SIZE = 20;
36 #define __FILE_NAME__   (strrchr((__FILE__), '/') ? strrchr((__FILE__), '/') + 1 : (__FILE__))
37 #define LOG(level) UpdaterLogger(level).OutputUpdaterLog((__FILE_NAME__), (__LINE__))
38 #define STAGE(stage) StageLogger(stage).OutputUpdaterStage()
39 #define ERROR_CODE(code) ErrorCode(code).OutputErrorCode((__FILE_NAME__), (__LINE__), (code))
40 
41 #define UPDATER_ERROR_CHECK(ret, log, statement)  \
42     if (!(ret)) {                                 \
43         LOG(ERROR) << log;                        \
44         statement;                                \
45     }
46 
47 #define UPDATER_WARING_CHECK(ret, log, statement)  \
48     if (!(ret)) {                                  \
49         LOG(WARNING) << log;                       \
50         statement;                                 \
51     }
52 
53 #define UPDATER_INFO_CHECK(ret, log, statement)  \
54     if (!(ret)) {                                  \
55         LOG(INFO) << log;                       \
56         statement;                                 \
57     }
58 
59 #define UPDATER_ERROR_CHECK_NOT_RETURN(ret, log)   \
60     if (!(ret)) {                                  \
61         LOG(ERROR) << log;                         \
62     }
63 
64 #define UPDATER_INFO_CHECK_NOT_RETURN(ret, log)   \
65     if (!(ret)) {                                  \
66         LOG(INFO) << log;                         \
67     }
68 
69 #define UPDATER_WARNING_CHECK_NOT_RETURN(ret, log)   \
70     if (!(ret)) {                                  \
71         LOG(WARNING) << log;                         \
72     }
73 
74 #define UPDATER_FILE_CHECK(ret, log, statement)         \
75     if (!(ret)) {                                       \
76         LOG(ERROR) << log << " : " << strerror(errno);  \
77         statement;                                      \
78     }
79 
80 #define UPDATER_CHECK_FILE_OP(ret, log, fd, statement)  \
81     if (!(ret)) {                                       \
82         LOG(ERROR) << log << " : " << strerror(errno);  \
83         close(fd);                                      \
84         statement;                                      \
85     }
86 
87 #define UPDATER_POST_STAGE_ERROR_CHECK(ret, log, statement)  \
88     if (!(ret)) {                                            \
89         LOG(ERROR) << log;                                   \
90         STAGE(UPDATE_STAGE_FAIL) << "PostUpdater";           \
91         statement;                                           \
92     }
93 
94 #define UPDATER_CHECK_ONLY_RETURN(ret, statement)         \
95     if (!(ret)) {                                       \
96         statement;                                      \
97     }
98 
99 enum {
100     VERBOSE,
101     DEBUG,
102     INFO,
103     WARNING,
104     ERROR,
105     FATAL,
106 };
107 
108 enum {
109     UPDATE_STAGE_BEGIN,
110     UPDATE_STAGE_SUCCESS,
111     UPDATE_STAGE_FAIL,
112     UPDATE_STAGE_OUT,
113 };
114 
115 void SetLogLevel(int level);
116 
117 void InitUpdaterLogger(const std::string &tag, const std::string &logFile, const std::string &stageFile,
118     const std::string &errorCodeFile);
119 
120 void Logger(int level, const char* fileName, int32_t line, const char* format, ...);
121 
122 class UpdaterLogger {
123 public:
UpdaterLogger(int level)124     UpdaterLogger(int level) : level_(level) {}
125 
126     ~UpdaterLogger();
127 
128     std::ostream& OutputUpdaterLog(const std::string &path, int line);
129 private:
130     int level_;
131     std::stringstream oss_;
132     char realTime_[MAX_TIME_SIZE] = {0};
133     static inline std::unordered_map<int, std::string> logLevelMap_ = {
134         { VERBOSE, "VERBOSE" },
135         { DEBUG, "DEBUG" },
136         { INFO, "INFO" },
137         { WARNING, "WARNING" },
138         { ERROR, "ERROR" },
139         { FATAL, "FATAL" }
140     };
141 };
142 
143 class StageLogger {
144 public:
StageLogger(int stage)145     StageLogger(int stage) : stage_(stage) {}
146 
147     ~StageLogger();
148 
149     std::ostream& OutputUpdaterStage();
150 private:
151     int stage_;
152 };
153 
154 class ErrorCode {
155 public:
ErrorCode(enum UpdaterErrorCode level)156     ErrorCode(enum UpdaterErrorCode level) {}
157 
~ErrorCode()158     ~ErrorCode() {}
159 
160     std::ostream& OutputErrorCode(const std::string &path, int line, UpdaterErrorCode code);
161 };
162 } // Updater
163 #endif /* UPDATE_LOG_H__ */
164