69 #include "stacktrace.h" 77 #define EINHARD_ALWAYS_INLINE_ __attribute__((always_inline)) 79 #define EINHARD_ALWAYS_INLINE_ 86 #define EINHARD_NO_THREAD_LOCAL 1 142 template <typename Parent> class
Color 157 return Parent::ANSI();
170 #define _COLOR( name, code ) \ 173 static char const *ANSI() noexcept \ 175 return "\33[" code "m"; \ 178 typedef Color<name##_t_> name 210 std::ostream &( * )( std::ostream & ) ) noexcept
221 #ifdef EINHARD_NO_THREAD_LOCAL 223 std::ostringstream realOut;
230 bool resetColor =
false;
233 template <LogLevel VERBOSITY>
235 std::integral_constant<LogLevel, VERBOSITY> )
236 : colorize( colorize_ )
238 doInit<VERBOSITY>( areaName );
245 *out << col.ansiCode();
264 void doCleanup(std::FILE *outfile = stdout) noexcept;
270 template <LogLevel VERBOSITY>
void doInit(
const char *areaName );
271 void checkColorReset();
288 template <LogLevel VERBOSITY>
290 const char *areaName,
291 std::integral_constant<LogLevel, VERBOSITY>,
292 std::FILE *outfile_ = stdout )
297 doInit<VERBOSITY>( areaName );
333 doCleanup( outfile );
347 template<LogLevel MAX = ALL>
class Logger 350 char areaName[32 -
sizeof(
LogLevel ) -
sizeof(
bool )] = {
'\0'};
366 colorize_stdout = isatty( fileno( stdout ) );
367 colorize_stderr = isatty( fileno( stderr ) );
376 : verbosity( verbosity ), colorize_stdout( colorize ), colorize_stderr( colorize ) {};
390 std::strncpy( &areaName[0], name,
sizeof( areaName ) - 1 );
391 areaName[
sizeof( areaName ) - 1] =
'\0';
396 setAreaName(name.c_str());
406 template <
typename... Ts>
void trace( Ts &&... )
const noexcept
412 return {isEnabled<TRACE>(), colorize_stdout, areaName,
413 std::integral_constant<LogLevel, TRACE>()};
416 template <
typename... Ts>
void trace( Ts &&... args ) const noexcept
418 if( isEnabled<TRACE>() )
421 std::integral_constant<LogLevel, TRACE>()};
422 auto &&unused = {&( o << args )...};
433 template <
typename... Ts>
void debug( Ts &&... )
const noexcept
439 return {isEnabled<DEBUG>(), colorize_stdout, areaName,
440 std::integral_constant<LogLevel, DEBUG>()};
442 template <
typename... Ts>
void debug( Ts &&... args ) const noexcept
444 if( isEnabled<DEBUG>() )
447 std::integral_constant<LogLevel, DEBUG>()};
448 auto &&unused = {&( o << args )...};
456 return {isEnabled<INFO>(), colorize_stdout, areaName,
457 std::integral_constant<LogLevel, INFO>()};
459 template <
typename... Ts>
void info( Ts &&... args ) const noexcept
461 if( isEnabled<INFO>() )
464 std::integral_constant<LogLevel, INFO>()};
465 auto &&unused = {&( o << args )...};
472 return {isEnabled<WARN>(), colorize_stderr, areaName,
473 std::integral_constant<LogLevel, WARN>(), stderr};
475 template <
typename... Ts>
void warn( Ts &&... args ) const noexcept
477 if( isEnabled<WARN>() )
480 std::integral_constant<LogLevel, WARN>()};
481 auto &&unused = {&( o << args )...};
488 return {isEnabled<ERROR>(), colorize_stderr, areaName,
489 std::integral_constant<LogLevel, ERROR>(), stderr};
491 template <
typename... Ts>
void error( Ts &&... args ) const noexcept
493 if( isEnabled<ERROR>() )
496 std::integral_constant<LogLevel, ERROR>()};
497 auto &&unused = {&( o << args )...};
504 return {isEnabled<FATAL>(), colorize_stderr, areaName,
505 std::integral_constant<LogLevel, FATAL>(), stderr};
508 template <
typename... Ts>
void fatal( Ts &&... args ) const noexcept
510 if( isEnabled<FATAL>() )
513 std::integral_constant<LogLevel, FATAL>()};
514 auto &&unused = {&( o << args )...};
516 print_stacktrace(stderr, 63, 2);
520 template <LogLevel LEVEL>
bool isEnabled() const noexcept
527 return ( MAX <= LEVEL && verbosity <= LEVEL );
537 this->verbosity = verbosity;
547 return this->verbosity;
561 this->colorize_stdout = colorize;
562 this->colorize_stderr = colorize;
569 return this->colorize_stdout || this->colorize_stderr;
void trace(Ts &&...args) const noexcept
OutputFormatter trace() const
Access to the trace message stream.
void setAreaName(const char *name)
Set an area name.
Logger(const LogLevel verbosity, const bool colorize)
Create a new Logger object explicitly selecting whether to colorize the output or not...
bool getColorize() const noexcept
Check whether the output stream is colorized.
bool isEnabled() const noexcept
The lowes severity for messages describing the program flow.
const char * colorForLogLevel() noexcept
OutputFormatter info() const
Access to the info message stream.
EINHARD_ALWAYS_INLINE_ void setAreaName(const std::string &name)
#define _COLOR(name, code)
EINHARD_ALWAYS_INLINE_ UnconditionalOutput & operator<<(std::ostream &(*manip)(std::ostream &))
If selected no messages will be output.
OutputFormatter debug() const
Access to the debug message stream.
OutputFormatter error() const
Access to the error message stream.
EINHARD_ALWAYS_INLINE_ UnconditionalOutput & operator<<(const T &msg)
#define EINHARD_ALWAYS_INLINE_
Messages of informational nature, expected processing time e.g.
void error(Ts &&...args) const noexcept
EINHARD_ALWAYS_INLINE_ UnconditionalOutput(bool colorize_, const char *areaName, std::integral_constant< LogLevel, VERBOSITY >)
EINHARD_ALWAYS_INLINE_ UnconditionalOutput(bool colorize_)
EINHARD_ALWAYS_INLINE_ Color< Parent > operator~() const noexcept
With the ~ operator the color modifier affects the rest of the stream (or until another color object ...
LogLevel getVerbosity() const noexcept
Retrieve the current log level.
EINHARD_ALWAYS_INLINE_ bool resetColor() const noexcept
const char * getLogLevelString() noexcept
Retrieve a human readable representation of the given log level value.
Logger(const LogLevel verbosity=WARN)
Create a new Logger object.
char const * getVerbosityString() const
Retrieve a human readable representation of the current log level.
EINHARD_ALWAYS_INLINE_ Color() noexcept
The default color modifier only affects the next object in the stream.
EINHARD_ALWAYS_INLINE_ Color(bool r) noexcept
void fatal(Ts &&...args) const noexcept
Will print stacktrace.
This namespace contains all objects required for logging using Einhard.
void setColorize(bool colorize) noexcept
Select whether the output stream should be colorized.
A Logger object can be used to output messages to stdout.
OutputFormatter fatal() const
Access to the fatal message stream.
void info(Ts &&...args) const noexcept
void setVerbosity(LogLevel verbosity) noexcept
Modify the verbosity of the Logger.
Messages that indicate terminal application failure.
A stream modifier that allows to colorize the log output.
LogLevel
Specification of the message severity.
void warn(Ts &&...args) const noexcept
UnconditionalOutput & operator<<(const Color< T > &col)
LogLevel getLogLevel(const std::string &level)
Compares the string level against the strings for LogLevel and returns the one it matches...
EINHARD_ALWAYS_INLINE_ char const * ansiCode() const noexcept
void debug(Ts &&...args) const noexcept
OutputFormatter warn() const
Access to the warning message stream.
void doCleanup(std::FILE *outfile=stdout) noexcept
char const VERSION[]
Version string of the Einhard library.