CppNCorr
C++ ncorr Digital Image Correlation engine
Loading...
Searching...
No Matches
log.h
Go to the documentation of this file.
1#pragma once
29#include <fstream>
30#include <mutex>
31#include <ostream>
32#include <sstream>
33#include <string>
34
35namespace ncorr {
36namespace log {
37
39enum class Level { Trace = 0, Debug = 1, Info = 2, Warn = 3, Error = 4, Off = 5 };
40
43Level level_from_string(const std::string& s, Level fallback = Level::Info);
44
46const char* level_name(Level l);
47
51class Logger {
52 public:
53 static Logger& instance();
54
56 void set_file_level(Level l);
57 Level console_level() const;
58 Level file_level() const;
59
62 bool set_file(const std::string& path);
63
65 void set_console_enabled(bool on);
66
69 void set_verbose_format(bool on);
70
73 void configure_from_env();
74
77 bool enabled(Level l);
78
80 void write(Level l, const char* file, int line, const std::string& msg);
81
82 private:
83 Logger();
84 void ensure_env();
85
86 mutable std::mutex mtx_;
87 Level console_level_ = Level::Info;
88 Level file_level_ = Level::Debug;
89 bool console_enabled_ = true;
90 bool verbose_format_ = false;
91 bool color_ = false;
92 bool env_done_ = false;
93 std::ofstream file_;
94};
95
97void set_level(Level l);
98
101void set_debug(bool on);
102
104bool set_file(const std::string& path);
105
107inline bool enabled(Level l) {
108 return Logger::instance().enabled(l);
109}
110
115class Stream {
116 public:
117 Stream(Level level, const char* file, int line) : level_(level), file_(file), line_(line) {}
118 ~Stream() { Logger::instance().write(level_, file_, line_, oss_.str()); }
119
120 Stream(const Stream&) = delete;
121 Stream& operator=(const Stream&) = delete;
122
123 template <typename T>
124 Stream& operator<<(const T& v) {
125 oss_ << v;
126 return *this;
127 }
129 Stream& operator<<(std::ostream& (*manip)(std::ostream&)) {
130 oss_ << manip;
131 return *this;
132 }
133
134 private:
135 Level level_;
136 const char* file_;
137 int line_;
138 std::ostringstream oss_;
139};
140
144class Voidify {
145 public:
146 Voidify() = default;
148};
149
150} // namespace log
151} // namespace ncorr
152
153// Stream-style logging macros. When the level is disabled, the right-hand side
154// (message construction) is never evaluated.
155#define NLOG_AT(lvl) \
156 !::ncorr::log::enabled(lvl) \
157 ? (void)0 \
158 : ::ncorr::log::Voidify() & ::ncorr::log::Stream((lvl), __FILE__, __LINE__)
159
160#define NLOG_TRACE NLOG_AT(::ncorr::log::Level::Trace)
161#define NLOG_DEBUG NLOG_AT(::ncorr::log::Level::Debug)
162#define NLOG_INFO NLOG_AT(::ncorr::log::Level::Info)
163#define NLOG_WARN NLOG_AT(::ncorr::log::Level::Warn)
164#define NLOG_ERROR NLOG_AT(::ncorr::log::Level::Error)
Process-wide singleton logger.
Definition log.h:51
void configure_from_env()
Apply NCORR_LOG_LEVEL / NCORR_LOG_FILE / NCORR_LOG_CONSOLE.
Definition log.cpp:147
void set_file_level(Level l)
Definition log.cpp:167
void write(Level l, const char *file, int line, const std::string &msg)
Emit a fully-formed message (trailing newlines are trimmed) at l.
Definition log.cpp:209
Level console_level() const
Definition log.cpp:172
static Logger & instance()
Definition log.cpp:131
bool enabled(Level l)
True if a message at l would reach any sink.
Definition log.cpp:200
void set_console_enabled(bool on)
Enable or disable console output entirely.
Definition log.cpp:190
void set_console_level(Level l)
Definition log.cpp:162
Level file_level() const
Definition log.cpp:177
bool set_file(const std::string &path)
Open (or replace) the file sink.
Definition log.cpp:182
void set_verbose_format(bool on)
Include timestamp + source location in console output too (the file sink always includes them).
Definition log.cpp:195
RAII stream builder; flushes its accumulated text to the logger when it is destroyed at the end of th...
Definition log.h:115
Stream & operator=(const Stream &)=delete
Stream(Level level, const char *file, int line)
Definition log.h:117
Stream(const Stream &)=delete
Stream & operator<<(const T &v)
Definition log.h:124
Stream & operator<<(std::ostream &(*manip)(std::ostream &))
Support stream manipulators such as std::endl / std::setprecision.
Definition log.h:129
Helper that turns the conditional logging expression back into a void statement (glog idiom).
Definition log.h:144
void operator&(Stream &)
Definition log.h:147
bool enabled(Level l)
True if a message at l would be emitted by any sink.
Definition log.h:107
void set_level(Level l)
Set the console threshold (convenience wrapper).
Definition log.cpp:235
void set_debug(bool on)
Lower the console threshold to Debug when on is true (used to honour the engine's existing debug flag...
Definition log.cpp:239
Level level_from_string(const std::string &s, Level fallback=Level::Info)
Parse a level name (case-insensitive): trace, debug, info, warn|warning, error, off.
Definition log.cpp:102
Level
Severity levels, ordered from most to least verbose.
Definition log.h:39
const char * level_name(Level l)
Short, fixed-width display name for a level (e.g. "INFO ").
Definition log.cpp:113
bool set_file(const std::string &path)
Convenience: open a log file sink (full Debug detail).
Definition log.cpp:245