• 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 
26 namespace Updater {
27 #ifdef __WIN32
28 #undef ERROR
29 #endif
30 
31 constexpr size_t MIN_UPDATE_SPACE = 50 * 1024 * 1024;
32 constexpr int MAX_TIME_SIZE = 20;
33 #define UPDATER_LOG_FILE_NAME \
34     ((strrchr((__FILE_NAME__), '/') != nullptr) ? strrchr((__FILE_NAME__), '/') + 1 : (__FILE_NAME__))
35 #define LOG(level) UpdaterLogger(level).OutputUpdaterLog((UPDATER_LOG_FILE_NAME), (__LINE__))
36 #define STAGE(stage) StageLogger(stage).OutputUpdaterStage()
37 #define ERROR_CODE(code) ErrorCode(code).OutputErrorCode((UPDATER_LOG_FILE_NAME), (__LINE__), (code))
38 
39 enum {
40     DEBUG = 3,
41     INFO = 4,
42     WARNING = 5,
43     ERROR = 6,
44     FATAL = 7,
45 };
46 
47 enum {
48     UPDATE_STAGE_BEGIN,
49     UPDATE_STAGE_SUCCESS,
50     UPDATE_STAGE_FAIL,
51     UPDATE_STAGE_OUT,
52 };
53 
54 void SetLogLevel(int level);
55 
56 void InitUpdaterLogger(const std::string &tag, const std::string &logFile, const std::string &stageFile,
57     const std::string &errorCodeFile);
58 
59 extern "C" void Logger(int level, const char* fileName, int32_t line, const char* format, ...);
60 
61 extern "C" void UpdaterHiLogger(int level, const char* fileName, int32_t line, const char* format, ...);
62 
63 class UpdaterLogger {
64 public:
UpdaterLogger(int level)65     UpdaterLogger(int level) : level_(level) {}
66 
67     ~UpdaterLogger();
68 
69     std::ostream& OutputUpdaterLog(const std::string &path, int line);
70 private:
71     int level_;
72     std::stringstream oss_;
73     char realTime_[MAX_TIME_SIZE] = {0};
74     static inline std::unordered_map<int, std::string> logLevelMap_ = {
75         { DEBUG, "D" },
76         { INFO, "I" },
77         { WARNING, "W" },
78         { ERROR, "E" },
79         { FATAL, "F" }
80     };
81 };
82 
83 class StageLogger {
84 public:
StageLogger(int stage)85     StageLogger(int stage) : stage_(stage) {}
86 
87     ~StageLogger();
88 
89     std::ostream& OutputUpdaterStage();
90 private:
91     int stage_;
92 };
93 
94 class ErrorCode {
95 public:
ErrorCode(enum UpdaterErrorCode level)96     ErrorCode(enum UpdaterErrorCode level) {}
97 
~ErrorCode()98     ~ErrorCode() {}
99 
100     std::ostream& OutputErrorCode(const std::string &path, int line, UpdaterErrorCode code);
101 };
102 } // Updater
103 #endif /* UPDATE_LOG_H__ */
104