1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
| #include "util.hpp" #include "level.hpp" #include "message.hpp" #include "formatter.hpp" #include "sink.hpp" #include "looper.hpp" #include <vector> #include <list> #include <atomic> #include <unordered_map> #include <cstdarg> #include <type_traits>
namespace suplog{ class Logger{ public: enum class Type{ LOGGER_SYNC = 0, LOGGER_ASYNC }; using ptr = std::shared_ptr<Logger>;
Logger(const std::string& name, Formatter::ptr formatter, std::vector<LogSink::ptr> &sinks, LogLevel::Level level = LogLevel::Level::DEBUG ):_name(name),_level(level),_formatter(formatter), _sinks(sinks.begin(),sinks.end()) {}
std::string loggerName(){ return _name; } LogLevel::Level loggerLevel(){ return _level; }
void debug(const char*file,size_t line,const char*fmt,...) { if(shouldLog(LogLevel::Level::DEBUG) == false) return;
va_list al; va_start(al,fmt); log(LogLevel::Level::DEBUG,file,line,fmt,al); va_end(al); }
void info(const char*file,size_t line,const char*fmt,...) { if(shouldLog(LogLevel::Level::INFO) == false) return;
va_list al; va_start(al,fmt); log(LogLevel::Level::INFO,file,line,fmt,al); va_end(al); }
void warn(const char*file,size_t line,const char*fmt,...) { if(shouldLog(LogLevel::Level::WARN) == false) return;
va_list al; va_start(al,fmt); log(LogLevel::Level::WARN,file,line,fmt,al); va_end(al); }
void error(const char*file,size_t line,const char*fmt,...) { if(shouldLog(LogLevel::Level::ERROR) == false) return;
va_list al; va_start(al,fmt); log(LogLevel::Level::WARN,file,line,fmt,al); va_end(al); }
void fatal(const char*file,size_t line,const char*fmt,...) { if(shouldLog(LogLevel::Level::FATAL) == false) return;
va_list al; va_start(al,fmt); log(LogLevel::Level::FATAL,file,line,fmt,al); va_end(al); }
public: class Builder { public: using ptr = std::shared_ptr<Builder>;
Builder():_level(LogLevel::Level::DEBUG), _logger_type(Logger::Type::LOGGER_SYNC) {}
void buildLoggerName(const std::string& name){ _logger_name = name; return; }
void buildLoggerLevel(LogLevel::Level level){ _level = level; return; }
void buildLoggerType(Logger::Type type){ _logger_type = type; return; }
void buidFormatter(const Formatter::ptr& formatter){ _formatter = formatter; return; }
void buidFormatter(const std::string& formatStr){ auto formatter = std::make_shared<suplog::Formatter>(formatStr); _formatter = formatter; return; }
template<typename SinkType,typename ...Args> void buildSink(Args &&...args){ auto sink = SinkFactory::create<SinkType>(std::forward<Args>(args)...); _sinks.push_back(sink); return; }
void clearSink(){ _sinks.clear(); return; }
virtual Logger::ptr build() = 0; protected: Logger::Type _logger_type; std::string _logger_name; LogLevel::Level _level; Formatter::ptr _formatter; std::vector<LogSink::ptr> _sinks; };
protected: bool shouldLog(LogLevel::Level level){ return level >= _level; }
void log(LogLevel::Level level,const char*file, size_t line,const char*fmt,va_list al) { char *buf; std::string msg; int len = vasprintf(&buf,fmt,al); if(len < 0) msg = "格式化日志消息失败!!"; else { msg.assign(buf,len); free(buf); }
LogMsg logmsg(_name,file,line,std::move(msg),level); std::string str; str = _formatter->format(logmsg); logIt(std::move(str)); }
virtual void logIt(const std::string &msg) = 0;
protected: std::mutex _mutex; std::string _name; Formatter::ptr _formatter; std::atomic<LogLevel::Level> _level; std::vector<LogSink::ptr> _sinks; };
|