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 16 #ifndef UTILS_INCLUDE_LOGGER_EXPORT_GSLOGGER_H 17 #define UTILS_INCLUDE_LOGGER_EXPORT_GSLOGGER_H 18 19 #include <any> 20 #include <cstdarg> 21 #include <sstream> 22 #include <vector> 23 24 class Gslogger : public std::stringstream { 25 public: 26 enum class LOG_LEVEL { DEBUG, INFO, WARN, ERROR, FATAL }; 27 enum class LOG_PHASE { BEGIN, END }; 28 29 using GsloggerWrapperFunc = void(*)(Gslogger &, enum LOG_PHASE phase); 30 // output GsloggerWrapperFunc 31 static void Stdout(Gslogger &logger, enum LOG_PHASE phase); 32 static void Stderr(Gslogger &logger, enum LOG_PHASE phase); 33 static void Hilog(Gslogger &logger, enum LOG_PHASE phase); 34 static void FileLog(Gslogger &logger, enum LOG_PHASE phase); 35 36 // wrapper GsloggerWrapperFunc 37 static void Func(Gslogger &logger, enum LOG_PHASE phase); // 1 38 static void FuncLine(Gslogger &logger, enum LOG_PHASE phase); // 2 39 static void FileLine(Gslogger &logger, enum LOG_PHASE phase); // 3 40 static void FileFuncLine(Gslogger &logger, enum LOG_PHASE phase); // 4 41 static void PidTid(Gslogger &logger, enum LOG_PHASE phase); // +5 42 43 Gslogger(const std::string &file, const std::string &func, int line, enum LOG_LEVEL level, ...); 44 virtual ~Gslogger() override; 45 46 const std::string &GetFile() const; 47 const std::string &GetFunc() const; 48 int GetLine() const; 49 enum LOG_LEVEL GetLevel() const; 50 va_list &GetVariousArgument(); 51 52 template<class T> GetData()53 std::shared_ptr<T> GetData() 54 { 55 using sptrT = std::shared_ptr<T>; 56 sptrT ret = nullptr; 57 auto pRet = std::any_cast<sptrT>(&data_); 58 if (pRet != nullptr) { 59 ret = *pRet; 60 } else { 61 ret = std::make_shared<T>(); 62 data_ = ret; 63 } 64 return ret; 65 } 66 67 private: 68 std::string file_; 69 std::string func_; 70 int line_; 71 enum LOG_LEVEL level_; 72 va_list vl_; 73 std::any data_; 74 std::vector<GsloggerWrapperFunc> wrappers_; 75 }; 76 77 #define LOGGER_ARG(level) __FILE__, __func__, __LINE__, (Gslogger::LOG_LEVEL::level) 78 79 // hilog 80 #define DEFINE_HILOG_LABEL(str) \ 81 namespace { constexpr const char *HILOG_LABEL = str; } 82 #define GSLOG0HI(level) Gslogger(LOGGER_ARG(level), \ 83 Gslogger::Hilog, HILOG_LABEL, NULL) 84 #define GSLOG1HI(level) Gslogger(LOGGER_ARG(level), \ 85 Gslogger::Func, Gslogger::Hilog, HILOG_LABEL, NULL) 86 #define GSLOG2HI(level) Gslogger(LOGGER_ARG(level), \ 87 Gslogger::FuncLine, Gslogger::Hilog, HILOG_LABEL, NULL) 88 #define GSLOG3HI(level) Gslogger(LOGGER_ARG(level), \ 89 Gslogger::FileLine, Gslogger::Hilog, HILOG_LABEL, NULL) 90 #define GSLOG4HI(level) Gslogger(LOGGER_ARG(level), \ 91 Gslogger::FileFuncLine, Gslogger::Hilog, HILOG_LABEL, NULL) 92 93 // stdout 94 #define GSLOG0SO(level) Gslogger(LOGGER_ARG(level), \ 95 Gslogger::Stdout, NULL) 96 #define GSLOG1SO(level) Gslogger(LOGGER_ARG(level), \ 97 Gslogger::Func, Gslogger::Stdout, NULL) 98 #define GSLOG2SO(level) Gslogger(LOGGER_ARG(level), \ 99 Gslogger::FuncLine, Gslogger::Stdout, NULL) 100 #define GSLOG3SO(level) Gslogger(LOGGER_ARG(level), \ 101 Gslogger::FileLine, Gslogger::Stdout, NULL) 102 #define GSLOG4SO(level) Gslogger(LOGGER_ARG(level), \ 103 Gslogger::FileFuncLine, Gslogger::Stdout, NULL) 104 #define GSLOG5SO(level) Gslogger(LOGGER_ARG(level), \ 105 Gslogger::PidTid, Gslogger::Stdout, NULL) 106 #define GSLOG6SO(level) Gslogger(LOGGER_ARG(level), \ 107 Gslogger::PidTid, Gslogger::Func, Gslogger::Stdout, NULL) 108 #define GSLOG7SO(level) Gslogger(LOGGER_ARG(level), \ 109 Gslogger::PidTid, Gslogger::FuncLine, Gslogger::Stdout, NULL) 110 #define GSLOG8SO(level) Gslogger(LOGGER_ARG(level), \ 111 Gslogger::PidTid, Gslogger::FileLine, Gslogger::Stdout, NULL) 112 #define GSLOG9SO(level) Gslogger(LOGGER_ARG(level), \ 113 Gslogger::PidTid, Gslogger::FileFuncLine, Gslogger::Stdout, NULL) 114 115 // stderr 116 #define GSLOG0SE(level) Gslogger(LOGGER_ARG(level), \ 117 Gslogger::Stderr, NULL) 118 #define GSLOG1SE(level) Gslogger(LOGGER_ARG(level), \ 119 Gslogger::Func, Gslogger::Stderr, NULL) 120 #define GSLOG2SE(level) Gslogger(LOGGER_ARG(level), \ 121 Gslogger::FuncLine, Gslogger::Stderr, NULL) 122 #define GSLOG3SE(level) Gslogger(LOGGER_ARG(level), \ 123 Gslogger::FileLine, Gslogger::Stderr, NULL) 124 #define GSLOG4SE(level) Gslogger(LOGGER_ARG(level), \ 125 Gslogger::FileFuncLine, Gslogger::Stderr, NULL) 126 #define GSLOG5SE(level) Gslogger(LOGGER_ARG(level), \ 127 Gslogger::PidTid, Gslogger::Stderr, NULL) 128 #define GSLOG6SE(level) Gslogger(LOGGER_ARG(level), \ 129 Gslogger::PidTid, Gslogger::Func, Gslogger::Stderr, NULL) 130 #define GSLOG7SE(level) Gslogger(LOGGER_ARG(level), \ 131 Gslogger::PidTid, Gslogger::FuncLine, Gslogger::Stderr, NULL) 132 #define GSLOG8SE(level) Gslogger(LOGGER_ARG(level), \ 133 Gslogger::PidTid, Gslogger::FileLine, Gslogger::Stderr, NULL) 134 #define GSLOG9SE(level) Gslogger(LOGGER_ARG(level), \ 135 Gslogger::PidTid, Gslogger::FileFuncLine, Gslogger::Stderr, NULL) 136 137 // filelog 138 #define DEFINE_FILE_LABEL(str) \ 139 namespace { constexpr const char *FILE_LABEL = str; } 140 #define GSLOG0F(level) Gslogger(LOGGER_ARG(level), \ 141 Gslogger::FileLog, FILE_LABEL, NULL) 142 #define GSLOG1F(level) Gslogger(LOGGER_ARG(level), \ 143 Gslogger::Func, Gslogger::FileLog, FILE_LABEL, NULL) 144 #define GSLOG2F(level) Gslogger(LOGGER_ARG(level), \ 145 Gslogger::FuncLine, Gslogger::FileLog, FILE_LABEL, NULL) 146 #define GSLOG3F(level) Gslogger(LOGGER_ARG(level), \ 147 Gslogger::FileLine, Gslogger::FileLog, FILE_LABEL, NULL) 148 #define GSLOG4F(level) Gslogger(LOGGER_ARG(level), \ 149 Gslogger::FileFuncLine, Gslogger::FileLog, FILE_LABEL, NULL) 150 #define GSLOG5F(level) Gslogger(LOGGER_ARG(level), \ 151 Gslogger::PidTid, Gslogger::FileLog, FILE_LABEL, NULL) 152 #define GSLOG6F(level) Gslogger(LOGGER_ARG(level), \ 153 Gslogger::PidTid, Gslogger::Func, Gslogger::FileLog, FILE_LABEL, NULL) 154 #define GSLOG7F(level) Gslogger(LOGGER_ARG(level), \ 155 Gslogger::PidTid, Gslogger::FuncLine, Gslogger::FileLog, FILE_LABEL, NULL) 156 #define GSLOG8F(level) Gslogger(LOGGER_ARG(level), \ 157 Gslogger::PidTid, Gslogger::FileLine, Gslogger::FileLog, FILE_LABEL, NULL) 158 #define GSLOG9F(level) Gslogger(LOGGER_ARG(level), \ 159 Gslogger::PidTid, Gslogger::FileFuncLine, Gslogger::FileLog, FILE_LABEL, NULL) 160 161 #endif // UTILS_INCLUDE_LOGGER_EXPORT_GSLOGGER_H 162