15 #include <vsg/core/Inherit.h>
16 #include <vsg/io/stream.h>
19 #include <string_view>
44 Level level = LOGGER_INFO;
49 virtual void flush() {}
51 inline void debug(
char* message) { debug(std::string_view(message)); }
52 inline void debug(
const char* message) { debug(std::string_view(message)); }
53 inline void debug(std::string& message) { debug(std::string_view(message)); }
54 inline void debug(
const std::string& message) { debug(std::string_view(message)); }
56 void debug(
const std::string_view& str)
58 if (level > LOGGER_DEBUG)
return;
60 std::scoped_lock<std::mutex> lock(_mutex);
61 debug_implementation(str);
64 template<
typename... Args>
65 void debug(Args&&... args)
67 if (level > LOGGER_DEBUG)
return;
69 std::scoped_lock<std::mutex> lock(_mutex);
72 (_stream << ... << args);
74 debug_implementation(_stream.str());
77 inline void info(
char* message) { info(std::string_view(message)); }
78 inline void info(
const char* message) { info(std::string_view(message)); }
79 inline void info(std::string& message) { info(std::string_view(message)); }
80 inline void info(
const std::string& message) { info(std::string_view(message)); }
82 void info(
const std::string_view& str)
84 if (level > LOGGER_INFO)
return;
86 std::scoped_lock<std::mutex> lock(_mutex);
87 info_implementation(str);
90 template<
typename... Args>
91 void info(Args&&... args)
93 if (level > LOGGER_INFO)
return;
95 std::scoped_lock<std::mutex> lock(_mutex);
98 (_stream << ... << args);
100 info_implementation(_stream.str());
103 inline void warn(
char* message) { warn(std::string_view(message)); }
104 inline void warn(
const char* message) { warn(std::string_view(message)); }
105 inline void warn(std::string& message) { warn(std::string_view(message)); }
106 inline void warn(
const std::string& message) { warn(std::string_view(message)); }
108 void warn(
const std::string_view& str)
110 if (level > LOGGER_WARN)
return;
112 std::scoped_lock<std::mutex> lock(_mutex);
113 warn_implementation(str);
116 template<
typename... Args>
117 void warn(Args&&... args)
119 if (level > LOGGER_WARN)
return;
121 std::scoped_lock<std::mutex> lock(_mutex);
124 (_stream << ... << args);
126 warn_implementation(_stream.str());
129 inline void error(
char* message) { error(std::string_view(message)); }
130 inline void error(
const char* message) { error(std::string_view(message)); }
131 inline void error(std::string& message) { error(std::string_view(message)); }
132 inline void error(
const std::string& message) { error(std::string_view(message)); }
134 void error(
const std::string_view& str)
136 if (level > LOGGER_DEBUG)
return;
138 std::scoped_lock<std::mutex> lock(_mutex);
139 error_implementation(str);
142 template<
typename... Args>
143 void error(Args&&... args)
145 if (level > LOGGER_ERROR)
return;
147 std::scoped_lock<std::mutex> lock(_mutex);
150 (_stream << ... << args);
152 error_implementation(_stream.str());
155 inline void fatal(
char* message) { fatal(std::string_view(message)); }
156 inline void fatal(
const char* message) { fatal(std::string_view(message)); }
157 inline void fatal(std::string& message) { fatal(std::string_view(message)); }
158 inline void fatal(
const std::string& message) { fatal(std::string_view(message)); }
160 void fatal(
const std::string_view& str)
162 if (level > LOGGER_DEBUG)
return;
164 std::scoped_lock<std::mutex> lock(_mutex);
165 fatal_implementation(str);
168 template<
typename... Args>
169 void fatal(Args&&... args)
171 if (level > LOGGER_ERROR)
return;
173 std::scoped_lock<std::mutex> lock(_mutex);
176 (_stream << ... << args);
178 fatal_implementation(_stream.str());
181 using PrintToStreamFunction = std::function<void(std::ostream&)>;
199 inline void log(Level msg_level,
char* message) {
log(msg_level, std::string_view(message)); }
200 inline void log(Level msg_level,
const char* message) { log(msg_level, std::string_view(message)); }
201 inline void log(Level msg_level, std::string& message) { log(msg_level, std::string_view(message)); }
202 inline void log(Level msg_level,
const std::string& message) { log(msg_level, std::string_view(message)); }
204 void log(Level msg_level,
const std::string_view& message);
207 template<
typename... Args>
208 void log(Level msg_level, Args... args)
210 if (level > msg_level)
return;
212 std::scoped_lock<std::mutex> lock(_mutex);
215 (_stream << ... << args);
219 case (LOGGER_DEBUG): debug_implementation(_stream.str());
break;
220 case (LOGGER_INFO): info_implementation(_stream.str());
break;
221 case (LOGGER_WARN): warn_implementation(_stream.str());
break;
222 case (LOGGER_ERROR): error_implementation(_stream.str());
break;
223 case (LOGGER_FATAL): fatal_implementation(_stream.str());
break;
229 void log_stream(Level msg_level, PrintToStreamFunction print);
235 std::ostringstream _stream;
237 virtual void debug_implementation(
const std::string_view& message) = 0;
238 virtual void info_implementation(
const std::string_view& message) = 0;
239 virtual void warn_implementation(
const std::string_view& message) = 0;
240 virtual void error_implementation(
const std::string_view& message) = 0;
241 virtual void fatal_implementation(
const std::string_view& message) = 0;
247 template<
typename... Args>
248 void debug(Args&&... args)
254 inline void debug_stream(Logger::PrintToStreamFunction print)
261 template<
typename... Args>
262 void info(Args&&... args)
268 inline void info_stream(Logger::PrintToStreamFunction print)
274 template<
typename... Args>
275 void warn(Args&&... args)
281 inline void warn_stream(Logger::PrintToStreamFunction print)
287 template<
typename... Args>
288 void error(Args&&... args)
294 inline void error_stream(Logger::PrintToStreamFunction print)
300 template<
typename... Args>
301 void fatal(Args&&... args)
307 inline void fatal_stream(Logger::PrintToStreamFunction print)
313 template<
typename... Args>
314 void log(Logger::Level msg_level, Args&&... args)
320 inline void log_stream(Logger::Level msg_level, Logger::PrintToStreamFunction print)
331 std::string debugPrefix =
"debug: ";
332 std::string infoPrefix =
"info: ";
333 std::string warnPrefix =
"Warning: ";
334 std::string errorPrefix =
"ERROR: ";
335 std::string fatalPrefix =
"FATAL: ";
337 void flush()
override;
340 void debug_implementation(
const std::string_view& message)
override;
341 void info_implementation(
const std::string_view& message)
override;
342 void warn_implementation(
const std::string_view& message)
override;
343 void error_implementation(
const std::string_view& message)
override;
344 void fatal_implementation(
const std::string_view& message)
override;
359 std::string debugPrefix =
"debug: ";
360 std::string infoPrefix =
"info: ";
361 std::string warnPrefix =
"Warning: ";
362 std::string errorPrefix =
"ERROR: ";
363 std::string fatalPrefix =
"FATAL: ";
365 void flush()
override;
368 void print_id(std::ostream& out, std::thread::id
id);
370 void debug_implementation(
const std::string_view& message)
override;
371 void info_implementation(
const std::string_view& message)
override;
372 void warn_implementation(
const std::string_view& message)
override;
373 void error_implementation(
const std::string_view& message)
override;
374 void fatal_implementation(
const std::string_view& message)
override;
376 std::map<std::thread::id, std::string> _threadPrefixes;
389 void debug_implementation(
const std::string_view&)
override;
390 void info_implementation(
const std::string_view&)
override;
391 void warn_implementation(
const std::string_view&)
override;
392 void error_implementation(
const std::string_view&)
override;
393 void fatal_implementation(
const std::string_view&)
override;
thread safe, pure virtual Logger base class that provides extensible message logging facilities
Definition: Logger.h:27
void error_stream(PrintToStreamFunction print)
thread safe access to stream for writing error output.
void debug_stream(PrintToStreamFunction print)
thread safe access to stream for writing debug output.
void log(Level msg_level, Args... args)
pass message to debug()/info()/warn()/error() based on specified level
Definition: Logger.h:208
void fatal_stream(PrintToStreamFunction print)
thread safe access to stream for writing fatal output and throwing vsg::Exception
void info_stream(PrintToStreamFunction print)
thread safe access to stream for writing info output.
static ref_ptr< Logger > & instance()
Logger singleton, defaults to using vsg::StdLogger.
void log(Level msg_level, char *message)
pass message to debug()/info()/warn()/error() based on specified level
Definition: Logger.h:199
void warn_stream(PrintToStreamFunction print)
thread safe access to stream for writing warn output.
void log_stream(Level msg_level, PrintToStreamFunction print)
thread safe access to stream for writing error output.
default Logger that sends debug and info messages to std:cout, and warn and error messages to std::ce...
Definition: Logger.h:327
void setThreadPrefix(std::thread::id id, const std::string &str)
assign prefix for std::thread::id. The id can be obtained from std::thread::get_id() i....