OpenCBDC Transaction Processor
Loading...
Searching...
No Matches
logging.cpp
Go to the documentation of this file.
1// Copyright (c) 2021 MIT Digital Currency Initiative,
2// Federal Reserve Bank of Boston
3// Distributed under the MIT software license, see the accompanying
4// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6#include "logging.hpp"
7
8namespace cbdc::logging {
9 null_stream::null_stream() : std::ostream(nullptr) {}
10
12 bool use_stdout,
13 std::unique_ptr<std::ostream> logfile)
14 : m_stdout(use_stdout),
15 m_loglevel(level),
16 m_logfile(std::move(logfile)) {}
17
18 void log::set_stdout_enabled(bool stdout_enabled) {
19 m_stdout = stdout_enabled;
20 }
21
22 void log::set_logfile(std::unique_ptr<std::ostream> logfile) {
23 m_logfile = std::move(logfile);
24 }
25
27 m_loglevel = level;
28 }
29
30 auto log::get_log_level() const -> log_level {
31 return m_loglevel;
32 }
33
34 auto log::to_string(log_level level) -> std::string {
35 switch(level) {
37 return "TRACE";
39 return "DEBUG";
40 case log_level::info:
41 return "INFO ";
42 case log_level::warn:
43 return "WARN ";
45 return "ERROR";
47 return "FATAL";
48 default:
49 return "NONE ";
50 }
51 }
52
53 void log::write_log_prefix(std::stringstream& ss, log_level level) {
54 auto now = std::chrono::system_clock::now();
55 auto now_t = std::chrono::system_clock::to_time_t(now);
56 auto now_ms
57 = std::chrono::time_point_cast<std::chrono::milliseconds>(now);
58
59 static constexpr int msec_per_sec = 1000;
60 auto const now_ms_f = now_ms.time_since_epoch().count() % msec_per_sec;
61 ss << std::put_time(std::localtime(&now_t), "[%Y-%m-%d %H:%M:%S.")
62 << std::setfill('0') << std::setw(3) << now_ms_f << "] ["
63 << to_string(level) << "]";
64 }
65
66 void log::flush() {
67 std::cout << std::flush;
68 }
69
70 auto parse_loglevel(const std::string& level) -> std::optional<log_level> {
71 if(level == "TRACE") {
72 return log_level::trace;
73 }
74 if(level == "DEBUG") {
75 return log_level::debug;
76 }
77 if(level == "INFO") {
78 return log_level::info;
79 }
80 if(level == "WARN") {
81 return log_level::warn;
82 }
83 if(level == "ERROR") {
84 return log_level::error;
85 }
86 if(level == "FATAL") {
87 return log_level::fatal;
88 }
89 return std::nullopt;
90 }
91}
void set_stdout_enabled(bool stdout_enabled)
Enables or disables printing the log output to stdout.
Definition logging.cpp:18
static void flush()
Flushes the log buffer.
Definition logging.cpp:66
log(log_level level, bool use_stdout=true, std::unique_ptr< std::ostream > logfile=std::make_unique< null_stream >())
Creates a new log instance.
Definition logging.cpp:11
auto get_log_level() const -> log_level
Returns the current log level of the logger.
Definition logging.cpp:30
void set_loglevel(log_level level)
Changes the log level threshold.
Definition logging.cpp:26
void set_logfile(std::unique_ptr< std::ostream > logfile)
Changes the logfile output to another destination.
Definition logging.cpp:22
null_stream()
Constructor. Sets the instance's stream buffer to nullptr.
Definition logging.cpp:9
log_level
Set of possible log levels.
Definition logging.hpp:34
@ trace
Fine-grained, fully verbose operating information.
@ warn
Potentially unintended, unexpected, or undesirable behavior.
@ debug
Diagnostic information.
@ info
General information about the state of the system.
@ error
Serious, critical errors.
@ fatal
Only fatal errors.
auto parse_loglevel(const std::string &level) -> std::optional< log_level >
Parses a capitalized string into a log level.
Definition logging.cpp:70