Version: SMASH-3.1
einhard::Logger< MAX > Class Template Reference

#include <einhard.hpp>

template<LogLevel MAX = ALL>
class einhard::Logger< MAX >

A Logger object can be used to output messages to stdout.

The Logger object is created with a certain verbosity. All messages of a more verbose LogLevel will be filtered out. The way the class is build this can happen at compile time up to the level restriction given by the template parameter.

The class can automatically detect non-tty output and will not colorize output in that case.

Definition at line 347 of file einhard.hpp.

Public Member Functions

 Logger (const LogLevel verbosity=WARN)
 Create a new Logger object. More...
 
 Logger (const LogLevel verbosity, const bool colorize)
 Create a new Logger object explicitly selecting whether to colorize the output or not. More...
 
void setAreaName (const char *name)
 Set an area name. More...
 
EINHARD_ALWAYS_INLINE_ void setAreaName (const std::string &name)
 
OutputFormatter trace () const
 Access to the trace message stream. More...
 
template<typename... Ts>
void trace (Ts &&... args) const noexcept
 
OutputFormatter debug () const
 Access to the debug message stream. More...
 
template<typename... Ts>
void debug (Ts &&... args) const noexcept
 
OutputFormatter info () const
 Access to the info message stream. More...
 
template<typename... Ts>
void info (Ts &&... args) const noexcept
 
OutputFormatter warn () const
 Access to the warning message stream. More...
 
template<typename... Ts>
void warn (Ts &&... args) const noexcept
 
OutputFormatter error () const
 Access to the error message stream. More...
 
template<typename... Ts>
void error (Ts &&... args) const noexcept
 
OutputFormatter fatal () const
 Access to the fatal message stream. More...
 
template<typename... Ts>
void fatal (Ts &&... args) const noexcept
 Will print stacktrace. More...
 
template<LogLevel LEVEL>
bool isEnabled () const noexcept
 
void setVerbosity (LogLevel verbosity) noexcept
 Modify the verbosity of the Logger. More...
 
LogLevel getVerbosity () const noexcept
 Retrieve the current log level. More...
 
char const * getVerbosityString () const
 Retrieve a human readable representation of the current log level. More...
 
void setColorize (bool colorize) noexcept
 Select whether the output stream should be colorized. More...
 
bool getColorize () const noexcept
 Check whether the output stream is colorized. More...
 

Private Attributes

char areaName [32 - sizeof(LogLevel) - sizeof(bool)] = {'\0'}
 
LogLevel verbosity
 
bool colorize_stdout
 
bool colorize_stderr
 

Constructor & Destructor Documentation

◆ Logger() [1/2]

template<LogLevel MAX = ALL>
einhard::Logger< MAX >::Logger ( const LogLevel  verbosity = WARN)
inline

Create a new Logger object.

The object will automatically colorize output on ttys and not colorize output on non ttys.

Definition at line 362 of file einhard.hpp.

362  : verbosity( verbosity )
363  {
364  // use some, sadly not c++-ways to figure out whether we are writing ot a terminal
365  // only colorize when we are writing ot a terminal
366  colorize_stdout = isatty( fileno( stdout ) );
367  colorize_stderr = isatty( fileno( stderr ) );
368  };
bool colorize_stderr
Definition: einhard.hpp:353
LogLevel verbosity
Definition: einhard.hpp:351
bool colorize_stdout
Definition: einhard.hpp:352

◆ Logger() [2/2]

template<LogLevel MAX = ALL>
einhard::Logger< MAX >::Logger ( const LogLevel  verbosity,
const bool  colorize 
)
inline

Create a new Logger object explicitly selecting whether to colorize the output or not.

Be aware that if output colorization is selected output will even be colorized if output is to a non tty.

Definition at line 375 of file einhard.hpp.

376  : verbosity( verbosity ), colorize_stdout( colorize ), colorize_stderr( colorize ) {};

Member Function Documentation

◆ setAreaName() [1/2]

template<LogLevel MAX = ALL>
void einhard::Logger< MAX >::setAreaName ( const char *  name)
inline

Set an area name.

This will be printed after the LogLevel to identify the place in the code where the output is coming from. This can be used to identify the different Logger objects in the log output.

Parameters
nameA string. Only the first 30, or so, characters will be used. The rest will not be displayed. You can reset the name with an empty string.
Warning
Passing a nullptr is not allowed!

Definition at line 387 of file einhard.hpp.

388  {
389  assert( name );
390  std::strncpy( &areaName[0], name, sizeof( areaName ) - 1 );
391  areaName[sizeof( areaName ) - 1] = '\0';
392  }
char areaName[32 - sizeof(LogLevel) - sizeof(bool)]
Definition: einhard.hpp:350

◆ setAreaName() [2/2]

template<LogLevel MAX = ALL>
EINHARD_ALWAYS_INLINE_ void einhard::Logger< MAX >::setAreaName ( const std::string &  name)
inline

Definition at line 394 of file einhard.hpp.

395  {
396  setAreaName(name.c_str());
397  }
void setAreaName(const char *name)
Set an area name.
Definition: einhard.hpp:387

◆ trace() [1/2]

template<LogLevel MAX = ALL>
OutputFormatter einhard::Logger< MAX >::trace ( ) const
inline

Access to the trace message stream.

Definition at line 410 of file einhard.hpp.

411  {
412  return {isEnabled<TRACE>(), colorize_stdout, areaName,
413  std::integral_constant<LogLevel, TRACE>()};
414  }

◆ trace() [2/2]

template<LogLevel MAX = ALL>
template<typename... Ts>
void einhard::Logger< MAX >::trace ( Ts &&...  args) const
inlinenoexcept

Definition at line 416 of file einhard.hpp.

417  {
418  if( isEnabled<TRACE>() )
419  {
420  UnconditionalOutput o{colorize_stdout, areaName,
421  std::integral_constant<LogLevel, TRACE>()};
422  auto &&unused = {&( o << args )...};
423  o.doCleanup();
424  }
425  }

◆ debug() [1/2]

template<LogLevel MAX = ALL>
OutputFormatter einhard::Logger< MAX >::debug ( ) const
inline

Access to the debug message stream.

Definition at line 437 of file einhard.hpp.

438  {
439  return {isEnabled<DEBUG>(), colorize_stdout, areaName,
440  std::integral_constant<LogLevel, DEBUG>()};
441  }

◆ debug() [2/2]

template<LogLevel MAX = ALL>
template<typename... Ts>
void einhard::Logger< MAX >::debug ( Ts &&...  args) const
inlinenoexcept

Definition at line 442 of file einhard.hpp.

443  {
444  if( isEnabled<DEBUG>() )
445  {
446  UnconditionalOutput o{colorize_stdout, areaName,
447  std::integral_constant<LogLevel, DEBUG>()};
448  auto &&unused = {&( o << args )...};
449  o.doCleanup();
450  }
451  }

◆ info() [1/2]

template<LogLevel MAX = ALL>
OutputFormatter einhard::Logger< MAX >::info ( ) const
inline

Access to the info message stream.

Definition at line 454 of file einhard.hpp.

455  {
456  return {isEnabled<INFO>(), colorize_stdout, areaName,
457  std::integral_constant<LogLevel, INFO>()};
458  }

◆ info() [2/2]

template<LogLevel MAX = ALL>
template<typename... Ts>
void einhard::Logger< MAX >::info ( Ts &&...  args) const
inlinenoexcept

Definition at line 459 of file einhard.hpp.

460  {
461  if( isEnabled<INFO>() )
462  {
463  UnconditionalOutput o{colorize_stdout, areaName,
464  std::integral_constant<LogLevel, INFO>()};
465  auto &&unused = {&( o << args )...};
466  o.doCleanup();
467  }
468  }

◆ warn() [1/2]

template<LogLevel MAX = ALL>
OutputFormatter einhard::Logger< MAX >::warn ( ) const
inline

Access to the warning message stream.

Definition at line 470 of file einhard.hpp.

471  {
472  return {isEnabled<WARN>(), colorize_stderr, areaName,
473  std::integral_constant<LogLevel, WARN>(), stderr};
474  }

◆ warn() [2/2]

template<LogLevel MAX = ALL>
template<typename... Ts>
void einhard::Logger< MAX >::warn ( Ts &&...  args) const
inlinenoexcept

Definition at line 475 of file einhard.hpp.

476  {
477  if( isEnabled<WARN>() )
478  {
479  UnconditionalOutput o{colorize_stderr, areaName,
480  std::integral_constant<LogLevel, WARN>()};
481  auto &&unused = {&( o << args )...};
482  o.doCleanup(stderr);
483  }
484  }

◆ error() [1/2]

template<LogLevel MAX = ALL>
OutputFormatter einhard::Logger< MAX >::error ( ) const
inline

Access to the error message stream.

Definition at line 486 of file einhard.hpp.

487  {
488  return {isEnabled<ERROR>(), colorize_stderr, areaName,
489  std::integral_constant<LogLevel, ERROR>(), stderr};
490  }

◆ error() [2/2]

template<LogLevel MAX = ALL>
template<typename... Ts>
void einhard::Logger< MAX >::error ( Ts &&...  args) const
inlinenoexcept

Definition at line 491 of file einhard.hpp.

492  {
493  if( isEnabled<ERROR>() )
494  {
495  UnconditionalOutput o{colorize_stderr, areaName,
496  std::integral_constant<LogLevel, ERROR>()};
497  auto &&unused = {&( o << args )...};
498  o.doCleanup(stderr);
499  }
500  }

◆ fatal() [1/2]

template<LogLevel MAX = ALL>
OutputFormatter einhard::Logger< MAX >::fatal ( ) const
inline

Access to the fatal message stream.

Definition at line 502 of file einhard.hpp.

503  {
504  return {isEnabled<FATAL>(), colorize_stderr, areaName,
505  std::integral_constant<LogLevel, FATAL>(), stderr};
506  }

◆ fatal() [2/2]

template<LogLevel MAX = ALL>
template<typename... Ts>
void einhard::Logger< MAX >::fatal ( Ts &&...  args) const
inlinenoexcept

Will print stacktrace.

Definition at line 508 of file einhard.hpp.

509  {
510  if( isEnabled<FATAL>() )
511  {
512  UnconditionalOutput o{colorize_stderr, areaName,
513  std::integral_constant<LogLevel, FATAL>()};
514  auto &&unused = {&( o << args )...};
515  o.doCleanup(stderr);
516  print_stacktrace(stderr, 63, 2);
517  }
518  }

◆ isEnabled()

template<LogLevel MAX = ALL>
template<LogLevel LEVEL>
bool einhard::Logger< MAX >::isEnabled ( ) const
inlinenoexcept

Definition at line 520 of file einhard.hpp.

521  {
522 #ifdef NDEBUG
523  if( LEVEL == DEBUG || LEVEL == TRACE ) {
524  return false;
525  }
526 #endif
527  return ( MAX <= LEVEL && verbosity <= LEVEL );
528  }
@ TRACE
The lowes severity for messages describing the program flow.
Definition: einhard.hpp:107
@ DEBUG
Debug messages.
Definition: einhard.hpp:108

◆ setVerbosity()

template<LogLevel MAX = ALL>
void einhard::Logger< MAX >::setVerbosity ( LogLevel  verbosity)
inlinenoexcept

Modify the verbosity of the Logger.

Be aware that the verbosity can not be increased over the level given by the template parameter

Definition at line 535 of file einhard.hpp.

536  {
537  this->verbosity = verbosity;
538  }

◆ getVerbosity()

template<LogLevel MAX = ALL>
LogLevel einhard::Logger< MAX >::getVerbosity ( ) const
inlinenoexcept

Retrieve the current log level.

If you want to check whether messages of a certain LogLevel will be output the method isEnabled() should be preffered.

Definition at line 545 of file einhard.hpp.

546  {
547  return this->verbosity;
548  }

◆ getVerbosityString()

template<LogLevel MAX = ALL>
char const* einhard::Logger< MAX >::getVerbosityString ( ) const
inline

Retrieve a human readable representation of the current log level.

Definition at line 552 of file einhard.hpp.

553  {
555  }
const char * getLogLevelString() noexcept
Retrieve a human readable representation of the given log level value.

◆ setColorize()

template<LogLevel MAX = ALL>
void einhard::Logger< MAX >::setColorize ( bool  colorize)
inlinenoexcept

Select whether the output stream should be colorized.

Definition at line 559 of file einhard.hpp.

560  {
561  this->colorize_stdout = colorize;
562  this->colorize_stderr = colorize;
563  }

◆ getColorize()

template<LogLevel MAX = ALL>
bool einhard::Logger< MAX >::getColorize ( ) const
inlinenoexcept

Check whether the output stream is colorized.

Definition at line 567 of file einhard.hpp.

568  {
569  return this->colorize_stdout || this->colorize_stderr;
570  }

Member Data Documentation

◆ areaName

template<LogLevel MAX = ALL>
char einhard::Logger< MAX >::areaName[32 - sizeof(LogLevel) - sizeof(bool)] = {'\0'}
private

Definition at line 350 of file einhard.hpp.

◆ verbosity

template<LogLevel MAX = ALL>
LogLevel einhard::Logger< MAX >::verbosity
private

Definition at line 351 of file einhard.hpp.

◆ colorize_stdout

template<LogLevel MAX = ALL>
bool einhard::Logger< MAX >::colorize_stdout
private

Definition at line 352 of file einhard.hpp.

◆ colorize_stderr

template<LogLevel MAX = ALL>
bool einhard::Logger< MAX >::colorize_stderr
private

Definition at line 353 of file einhard.hpp.


The documentation for this class was generated from the following file: