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