1 | #include <fmt/core.h> |
2 |
|
3 | #include "ZydecoCommon.hpp" |
4 | #include "Logger.hpp" |
5 |
|
6 | static Logger LOGGER("LOGGER"); |
7 |
|
8 | // static initialize members |
9 | std::ostream *Logger::s_ostream = nullptr; |
10 | Logger::Verbosity Logger::s_maxVerbosity = Logger::DEBUG; |
11 |
|
12 |
|
13 | static const char *VERBOSITY_STRINGS[] = { |
14 | "", // DISABLED |
15 | "\e[1;031m[ERROR]\e[0m", |
16 | "\e[1;033m[WARNING]\e[0m", |
17 | "\e[1;034m[INFO]\e[0m", |
18 | "\e[1;035m[DEBUG]\e[0m", |
19 | "\e[1;036m[VERBOSE]\e[0m", |
20 | "\e[1;037m[TRACE]\e[0m", |
21 | }; |
22 |
|
23 | // ctor |
24 | Logger::Logger(std::string logger_name): |
25 | m_loggerName(logger_name) |
26 | { |
27 |
|
28 | } |
29 |
|
30 | // should be called once |
31 | void Logger::InitializeLogging(Verbosity max_verbosity, std::ostream *stream) |
32 | { |
33 | if (stream != nullptr) |
34 | { |
35 | s_maxVerbosity = max_verbosity; |
36 | s_ostream = stream; |
37 |
|
38 | LOGGER.Log(INFO, "Initialized logger with max verbosity {0}", VERBOSITY_STRINGS[max_verbosity]); |
39 | } |
40 | } |
41 |
|
42 | // output debug message |
43 | void Logger::Log(Verbosity verbosity, std::string message) |
44 | { |
45 | if (s_ostream != nullptr && verbosity <= s_maxVerbosity) |
46 | { |
47 | *s_ostream << VERBOSITY_STRINGS[verbosity] << " \e[090m" << m_loggerName << "\e[0m: " << message << "\n"; |
48 | } |
49 | } |
50 |
|