OpenCBDC Transaction Processor
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends
logging.hpp
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#ifndef OPENCBDC_TX_SRC_COMMON_LOGGING_H_
6#define OPENCBDC_TX_SRC_COMMON_LOGGING_H_
7
8#include <chrono>
9#include <cstdint>
10#include <fstream>
11#include <iomanip>
12#include <iostream>
13#include <memory>
14#include <mutex>
15#include <optional>
16#include <sstream>
17
18namespace cbdc::logging {
20 class null_stream : public std::ostream {
21 public:
24
25 template<typename T>
26 auto operator<<(const T& /* unused */) -> null_stream& {
27 return *this;
28 }
29 };
30
34 enum class log_level : uint8_t {
36 trace,
38 debug,
40 info,
42 warn,
44 error,
46 fatal
47 };
48
51 class log {
52 public:
59 explicit log(log_level level,
60 bool use_stdout = true,
61 std::unique_ptr<std::ostream> logfile
62 = std::make_unique<null_stream>());
63
66 void set_stdout_enabled(bool stdout_enabled);
67
70 void set_logfile(std::unique_ptr<std::ostream> logfile);
71
75 void set_loglevel(log_level level);
76
78 static void flush();
79
81 template<typename... Targs>
82 void trace(Targs&&... args) {
83 write_log_statement(log_level::trace,
84 std::forward<Targs>(args)...);
85 }
86
88 template<typename... Targs>
89 void debug(Targs&&... args) {
90 write_log_statement(log_level::debug,
91 std::forward<Targs>(args)...);
92 }
93
95 template<typename... Targs>
96 void info(Targs&&... args) {
97 write_log_statement(log_level::info, std::forward<Targs>(args)...);
98 }
99
101 template<typename... Targs>
102 void warn(Targs&&... args) {
103 write_log_statement(log_level::warn, std::forward<Targs>(args)...);
104 }
105
107 template<typename... Targs>
108 void error(Targs&&... args) {
109 write_log_statement(log_level::error,
110 std::forward<Targs>(args)...);
111 }
112
115 template<typename... Targs>
116 [[noreturn]] void fatal(Targs&&... args) {
117 write_log_statement(log_level::fatal,
118 std::forward<Targs>(args)...);
119 exit(EXIT_FAILURE);
120 }
121
124 [[nodiscard]] auto get_log_level() const -> log_level;
125
126 private:
127 bool m_stdout{true};
128 log_level m_loglevel{};
129 std::mutex m_stream_mut{};
130 std::unique_ptr<std::ostream> m_logfile;
131
132 auto static to_string(log_level level) -> std::string;
133 static void write_log_prefix(std::stringstream& ss, log_level level);
134 template<typename... Targs>
135 void write_log_statement(log_level level, Targs&&... args) {
136 if(m_loglevel <= level) {
137 std::stringstream ss;
138 write_log_prefix(ss, level);
139 ((ss << " " << args), ...);
140 ss << "\n";
141 auto formatted_statement = ss.str();
142 const std::lock_guard<std::mutex> lock(m_stream_mut);
143 if(m_stdout) {
144 std::cout << formatted_statement;
145 }
146 *m_logfile << formatted_statement;
147 }
148 }
149 };
150
157 auto parse_loglevel(const std::string& level) -> std::optional<log_level>;
158}
159
160#endif // OPENCBDC_TX_SRC_COMMON_LOGGING_H_
Generalized logging class.
Definition logging.hpp:51
void set_stdout_enabled(bool stdout_enabled)
Enables or disables printing the log output to stdout.
Definition logging.cpp:18
void info(Targs &&... args)
Writes the argument list to the info log level.
Definition logging.hpp:96
static void flush()
Flushes the log buffer.
Definition logging.cpp:66
void error(Targs &&... args)
Writes the argument list to the error log level.
Definition logging.hpp:108
void trace(Targs &&... args)
Writes the argument list to the trace log level.
Definition logging.hpp:82
void warn(Targs &&... args)
Writes the argument list to the warn log level.
Definition logging.hpp:102
void debug(Targs &&... args)
Writes the argument list to the debug log level.
Definition logging.hpp:89
void fatal(Targs &&... args)
Writes the argument list to the fatal log level.
Definition logging.hpp:116
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
No-op stream destination for log output.
Definition logging.hpp:20
auto operator<<(const T &) -> null_stream &
Definition logging.hpp:26
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