5#ifndef OPENCBDC_TX_SRC_COMMON_LOGGING_H_
6#define OPENCBDC_TX_SRC_COMMON_LOGGING_H_
60 bool use_stdout =
true,
61 std::unique_ptr<std::ostream> logfile
62 = std::make_unique<null_stream>());
70 void set_logfile(std::unique_ptr<std::ostream> logfile);
81 template<
typename... Targs>
84 std::forward<Targs>(args)...);
88 template<
typename... Targs>
91 std::forward<Targs>(args)...);
95 template<
typename... Targs>
96 void info(Targs&&... args) {
101 template<
typename... Targs>
107 template<
typename... Targs>
110 std::forward<Targs>(args)...);
115 template<
typename... Targs>
116 [[noreturn]]
void fatal(Targs&&... args) {
118 std::forward<Targs>(args)...);
116 [[noreturn]]
void fatal(Targs&&... args) {
…}
129 std::mutex m_stream_mut{};
130 std::unique_ptr<std::ostream> m_logfile;
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), ...);
141 auto formatted_statement = ss.str();
142 const std::lock_guard<std::mutex> lock(m_stream_mut);
144 std::cout << formatted_statement;
146 *m_logfile << formatted_statement;
157 auto parse_loglevel(
const std::string& level) -> std::optional<log_level>;
Generalized logging class.
void set_stdout_enabled(bool stdout_enabled)
Enables or disables printing the log output to stdout.
void info(Targs &&... args)
Writes the argument list to the info log level.
static void flush()
Flushes the log buffer.
void error(Targs &&... args)
Writes the argument list to the error log level.
void trace(Targs &&... args)
Writes the argument list to the trace log level.
void warn(Targs &&... args)
Writes the argument list to the warn log level.
void debug(Targs &&... args)
Writes the argument list to the debug log level.
void fatal(Targs &&... args)
Writes the argument list to the fatal log level.
log(log_level level, bool use_stdout=true, std::unique_ptr< std::ostream > logfile=std::make_unique< null_stream >())
Creates a new log instance.
auto get_log_level() const -> log_level
Returns the current log level of the logger.
void set_loglevel(log_level level)
Changes the log level threshold.
void set_logfile(std::unique_ptr< std::ostream > logfile)
Changes the logfile output to another destination.
No-op stream destination for log output.
auto operator<<(const T &) -> null_stream &
null_stream()
Constructor. Sets the instance's stream buffer to nullptr.
log_level
Set of possible log levels.
@ 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.