CPPxDIC
C++ Digital Image Correlation library for fingertip 3D reconstruction
Loading...
Searching...
No Matches
logging.h
Go to the documentation of this file.
1
27#ifndef CPPXDIC_LOGGING_H
28#define CPPXDIC_LOGGING_H
29
30#include <fstream>
31#include <mutex>
32#include <ostream>
33#include <sstream>
34#include <string>
35
36namespace cppxdic {
37namespace log {
38
40enum class Level { Trace = 0, Debug = 1, Info = 2, Warn = 3, Error = 4, Off = 5 };
41
44Level levelFromString(const std::string& s, Level fallback = Level::Info);
45
47const char* levelName(Level l);
48
52class Logger {
53public:
54 static Logger& instance();
55
56 void setConsoleLevel(Level l);
57 void setFileLevel(Level l);
58 Level consoleLevel() const;
59 Level fileLevel() const;
60
63 bool setFile(const std::string& path);
64
66 void setConsoleEnabled(bool on);
67
70 void setVerboseFormat(bool on);
71
74 void configureFromEnv();
75
78 bool enabled(Level l);
79
81 void write(Level l, const char* file, int line, const std::string& msg);
82
83private:
84 Logger();
85 void ensureEnv();
86
87 mutable std::mutex mtx_;
88 Level console_level_ = Level::Info;
89 Level file_level_ = Level::Debug;
90 bool console_enabled_ = true;
91 bool verbose_format_ = false;
92 bool color_ = false;
93 bool env_done_ = false;
94 std::ofstream file_;
95};
96
98struct Options {
100 bool console_level_set = false;
101 std::string log_file;
102 bool debug = false;
103 bool quiet = false;
104 int verbose = 0;
105};
106
110void configureFromOptions(const Options& opts);
111
113void setLevel(Level l);
114bool setFile(const std::string& path);
115inline bool enabled(Level l) {
116 return Logger::instance().enabled(l);
117}
118
123class Stream {
124public:
125 Stream(Level level, const char* file, int line) : level_(level), file_(file), line_(line) {}
126 ~Stream() { Logger::instance().write(level_, file_, line_, oss_.str()); }
127
128 Stream(const Stream&) = delete;
129 Stream& operator=(const Stream&) = delete;
130
131 template <typename T>
132 Stream& operator<<(const T& v) {
133 oss_ << v;
134 return *this;
135 }
137 Stream& operator<<(std::ostream& (*manip)(std::ostream&)) {
138 oss_ << manip;
139 return *this;
140 }
141
142private:
143 Level level_;
144 const char* file_;
145 int line_;
146 std::ostringstream oss_;
147};
148
152class Voidify {
153public:
154 Voidify() = default;
156};
157
158} // namespace log
159} // namespace cppxdic
160
161// Stream-style logging macros. When the level is disabled, the right-hand side
162// (message construction) is never evaluated.
163#define LOG_AT(lvl) \
164 !::cppxdic::log::enabled(lvl) \
165 ? (void)0 \
166 : ::cppxdic::log::Voidify() & ::cppxdic::log::Stream((lvl), __FILE__, __LINE__)
167
168#define LOG_TRACE LOG_AT(::cppxdic::log::Level::Trace)
169#define LOG_DEBUG LOG_AT(::cppxdic::log::Level::Debug)
170#define LOG_INFO LOG_AT(::cppxdic::log::Level::Info)
171#define LOG_WARN LOG_AT(::cppxdic::log::Level::Warn)
172#define LOG_ERROR LOG_AT(::cppxdic::log::Level::Error)
173
174#endif // CPPXDIC_LOGGING_H
Process-wide singleton logger.
Definition logging.h:52
Level fileLevel() const
Definition logging.cpp:169
void setVerboseFormat(bool on)
Include timestamp + source location in console output too (the file sink always includes them).
Definition logging.cpp:187
void setFileLevel(Level l)
Definition logging.cpp:159
void setConsoleLevel(Level l)
Definition logging.cpp:154
bool setFile(const std::string &path)
Open (or replace) the file sink.
Definition logging.cpp:174
bool enabled(Level l)
True if a message at l would reach any sink.
Definition logging.cpp:192
Level consoleLevel() const
Definition logging.cpp:164
void configureFromEnv()
Apply CPPXDIC_LOG_LEVEL / CPPXDIC_LOG_FILE / CPPXDIC_LOG_CONSOLE.
Definition logging.cpp:141
static Logger & instance()
Definition logging.cpp:125
void write(Level l, const char *file, int line, const std::string &msg)
Emit a fully-formed message (trailing newlines trimmed) at l.
Definition logging.cpp:201
void setConsoleEnabled(bool on)
Enable or disable console output entirely.
Definition logging.cpp:182
RAII stream builder; flushes accumulated text to the logger when it is destroyed at the end of the fu...
Definition logging.h:123
Stream(Level level, const char *file, int line)
Definition logging.h:125
Stream & operator=(const Stream &)=delete
Stream(const Stream &)=delete
Stream & operator<<(const T &v)
Definition logging.h:132
Stream & operator<<(std::ostream &(*manip)(std::ostream &))
Support stream manipulators such as std::endl / std::setprecision.
Definition logging.h:137
Helper that turns the conditional logging expression into a void statement (glog idiom).
Definition logging.h:152
void operator&(Stream &)
Definition logging.h:155
bool enabled(Level l)
Definition logging.h:115
void configureFromOptions(const Options &opts)
Apply env vars, then the supplied options (CLI/config), to the singleton.
Definition logging.cpp:233
void setLevel(Level l)
Convenience accessors.
Definition logging.cpp:225
Level
Severity levels, ordered from most to least verbose.
Definition logging.h:40
bool setFile(const std::string &path)
Definition logging.cpp:229
Level levelFromString(const std::string &s, Level fallback=Level::Info)
Parse a level name (case-insensitive): trace, debug, info, warn|warning, error, off.
Definition logging.cpp:96
const char * levelName(Level l)
Short, fixed-width display name for a level (e.g. "INFO ").
Definition logging.cpp:107
DataSerializer - Abstract base class for format-agnostic DIC data I/O.
Aggregated options resolved from env/config/CLI, applied in one call.
Definition logging.h:98
bool console_level_set
true if an explicit level was chosen
Definition logging.h:100
std::string log_file
empty = no file sink
Definition logging.h:101
int verbose
-V count: lowers threshold per step
Definition logging.h:104
bool debug
debug mode (Debug level + verbose format)
Definition logging.h:102
bool quiet
force console threshold up to Warn
Definition logging.h:103