13 std::unique_ptr<std::ostream> logfile)
14 : m_stdout(use_stdout),
16 m_logfile(std::move(logfile)) {}
19 m_stdout = stdout_enabled;
23 m_logfile = std::move(logfile);
34 auto log::to_string(
log_level level) -> std::string {
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);
57 = std::chrono::time_point_cast<std::chrono::milliseconds>(now);
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) <<
"]";
67 std::cout << std::flush;
71 if(level ==
"TRACE") {
74 if(level ==
"DEBUG") {
83 if(level ==
"ERROR") {
86 if(level ==
"FATAL") {
void set_stdout_enabled(bool stdout_enabled)
Enables or disables printing the log output to stdout.
static void flush()
Flushes the log buffer.
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.
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.