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