Log utilities

Here you can find some tools for logging purpose :
  • The verbosity is defined with the enum Log level

  • Two loggers are available :
    • A basic Log file

    • TeeLogger, allowing to simultaneously print in the terminal and in a file

  • A Logger Manager is also provided

  • Macros to log while taking the verbosity into account


Log level

The verbosity of the log can be set using

enum class io::LogLevel

Describes the verbosity of the logger.

See also

io::SetLogLevel

Values:

enumerator OFF

No log at all.

enumerator ERROR

Only Error messages.

enumerator WARNING

Error and Warning messages.

enumerator INFO

Error, Warning and Info messages.

enumerator EXTENDED

Error, Warning, Info and Extended messages.

enumerator DEBUG

Error, Warning, Info and Debug messages.


Loggers

Log file

A logfile can be managed through

class FileLogger : public io::Logger<std::ofstream>

Log in a file.

Public Functions

explicit FileLogger(std::string const &file, LogLevel level = LogLevel::INFO)

Constructor.

Parameters:
  • file – path to the log file

  • level – initial verbosity

Throws:

if – the file can not be open

~FileLogger() override

Destructor.

TeeLogger

class TeeLogger : public io::Logger<TeeStream>

Log both in a file and a terminal.

Public Functions

explicit TeeLogger(std::string const &file, LogLevel level = LogLevel::INFO)

Constructor.

Parameters:
  • file – path to the log file

  • level – verbosity

~TeeLogger() override

Destructor.

Logger Manager

One can create, access and set the verbosity an instance of TeeLogger or Log file using the following class :

class LoggerManager

Store and give access to TeeLoggers.

Public Static Functions

static TeeLogger &GetLogger(std::string_view name = "")

Get or create a TeeLogger.

Parameters:

name – name of the logger, empty string for default name

Returns:

A reference to the logger

static void ClearLogger(std::string_view name = "")

Clear a logger.

Parameters:

name – name of the logger, empty string for default name

static void SetLogLevel(LogLevel level, std::string_view name = "")

Set the loglevel of a TeeLogger.

Parameters:
  • name – name of the logger, empty string for default name

  • level – the log level

static inline void SetDefaultLogLevel(LogLevel level)

Set the default verbosity.

Parameters:

level – the verbosity

static inline void SetDefaultName(std::string_view name)

Set the default name.

Parameters:

name – name of the logger

static FileLogger &GetFileLogger(std::string_view name)

Get or create a FileLogger.

Parameters:

name – name of the logger

Returns:

A reference to the logger

static void ClearFileLogger(std::string_view name)

Clear a FileLogger.

Parameters:

name – name of the logger

static void SetFileLogLevel(LogLevel level, std::string_view name)

Set the loglevel of a FileLogger.

Parameters:
  • name – name of the logger

  • level – the log level

static void ClearAllLoggers()

Clear every logger (file or tee)

Logger

Both the Log file and the TeeLogger inherits from the following class:

template<typename ostream>
class Logger

Interface for logger.

Template Parameters:

ostream – The type of output stream

Public Functions

inline explicit Logger(std::string const &file, LogLevel level = io::LogLevel::INFO)

Constructor.

Parameters:
  • file – path to the log file

  • level – initial verbosity

virtual ~Logger() = default

Destruct.

inline void setLogLevel(LogLevel level)

set the verbosity

inline LogLevel getLogLevel() const

set the verbosity

inline bool shouldLog(LogLevel level) const
Returns:

true if the verbosity is greater than leve

inline ostream &getLog()
Returns:

the ostream

inline void setLog(ostream &stream)

set the stream


Macros

Main macros for TeeLogger manipulation

ERORLOG

Log a message in the main io::TeeLogger, provided that the log level is >= io::LogLevel::ERROR.

Throws if:

the main io::TeeLogger has not been created

WARNLOG

Log a message in the main io::TeeLogger, provided that the log level is >= io::LogLevel::WARNING.

Throws if:

the main io::TeeLogger has not been created

INFOLOG

Log a message in the main io::TeeLogger, provided that the log level is >= io::LogLevel::INFO.

Throws if:

the main io::TeeLogger has not been created

DBUGLOG

Log a message in the main io::TeeLogger, provided that the log level is >= io::LogLevel::DEBUG.

Throws if:

the main io::TeeLogger has not been created

ERORLOG_RANGE(header, range)

Log a range with a header message in the main io::TeeLogger, provided that the log level is >= io::LogLevel::ERROR.

Parameters:
  • header – the message

  • range – the range

Throws if:

the main io::TeeLogger has not been created

WARNLOG_RANGE(header, range)

Log a range with a header message in the main io::TeeLogger, provided that the log level is >= io::LogLevel::WARNING.

Parameters:
  • header – the message

  • range – the range

Throws if:

the main io::TeeLogger has not been created

INFOLOG_RANGE(header, range)

Log a range with a header message in the main io::TeeLogger, provided that the log level is >= io::LogLevel::INFO.

Parameters:
  • header – the message

  • range – the range

Throws if:

the main io::TeeLogger has not been created

DBUGLOG_RANGE(header, range)

Log a range with a header message in the main io::TeeLogger, provided that the log level is >= io::LogLevel::DEBUG.

Parameters:
  • header – the message

  • range – the range

Throws if:

the main io::TeeLogger has not been created

Main macros for Log file manipulation

EROR(log)

Log a message in a stream, provided that the log level is >= io::LogLevel::ERROR.

Parameters:
  • log – the log

WARN(log)

Log a message in a stream, provided that the log level is >= io::LogLevel::WARNING.

Parameters:
  • log – the log

INFO(log)

Log a message in a stream, provided that the log level is >= io::LogLevel::INFO.

Parameters:
  • log – the log

DBUG(log)

Log a message in a stream, provided that the log level is >= io::LogLevel::DEBUG.

Parameters:
  • log – the log

EROR_RANGE(log, header, range)

Log a range with a header message in the main io::TeeLogger, provided that the log level is >= io::LogLevel::ERROR.

Parameters:
  • log – the log

  • header – the message

  • range – the range

WARN_RANGE(log, header, range)

Log a range with a header message in the main io::TeeLogger, provided that the log level is >= io::LogLevel::WARNING.

Parameters:
  • log – the log

  • header – the message

  • range – the range

INFO_RANGE(log, header, range)

Log a range with a header message in the main io::TeeLogger, provided that the log level is >= io::LogLevel::INFO.

Parameters:
  • log – the log

  • header – the message

  • range – the range

DBUG_RANGE(log, header, range)

Log a range with a header message in the main io::TeeLogger, provided that the log level is >= io::LogLevel::DEBUG.

Parameters:
  • log – the log

  • header – the message

  • range – the range

Full reference

log utilities

Defines

COND_STATEMENT(cond)

Execute the following instruction provided that a condition is satified.

Parameters:
  • cond – the condition

    COND_STATEMENT(cond) instruction
    
    expands to
    if(!cond){} else instruction
    

EROR(log)

Log a message in a stream, provided that the log level is >= io::LogLevel::ERROR.

Parameters:
  • log – the log

WARN(log)

Log a message in a stream, provided that the log level is >= io::LogLevel::WARNING.

Parameters:
  • log – the log

INFO(log)

Log a message in a stream, provided that the log level is >= io::LogLevel::INFO.

Parameters:
  • log – the log

EXTD(log)

Log a message in a stream, provided that the log level is >= io::LogLevel::EXTENDED.

Parameters:
  • log – the log

DBUG(log)

Log a message in a stream, provided that the log level is >= io::LogLevel::DEBUG.

Parameters:
  • log – the log

ERORLOG

Log a message in the main io::TeeLogger, provided that the log level is >= io::LogLevel::ERROR.

Throws if:

the main io::TeeLogger has not been created

WARNLOG

Log a message in the main io::TeeLogger, provided that the log level is >= io::LogLevel::WARNING.

Throws if:

the main io::TeeLogger has not been created

INFOLOG

Log a message in the main io::TeeLogger, provided that the log level is >= io::LogLevel::INFO.

Throws if:

the main io::TeeLogger has not been created

EXTDLOG

Log a message in the main io::TeeLogger, provided that the log level is >= io::LogLevel::EXTENDED.

Throws if:

the main io::TeeLogger has not been created

DBUGLOG

Log a message in the main io::TeeLogger, provided that the log level is >= io::LogLevel::DEBUG.

Throws if:

the main io::TeeLogger has not been created

EROR_RANGE(log, header, range)

Log a range with a header message in the main io::TeeLogger, provided that the log level is >= io::LogLevel::ERROR.

Parameters:
  • log – the log

  • header – the message

  • range – the range

WARN_RANGE(log, header, range)

Log a range with a header message in the main io::TeeLogger, provided that the log level is >= io::LogLevel::WARNING.

Parameters:
  • log – the log

  • header – the message

  • range – the range

INFO_RANGE(log, header, range)

Log a range with a header message in the main io::TeeLogger, provided that the log level is >= io::LogLevel::INFO.

Parameters:
  • log – the log

  • header – the message

  • range – the range

EXTD_RANGE(log, header, range)

Log a range with a header message in the main io::TeeLogger, provided that the log level is >= io::LogLevel::EXTENDED.

Parameters:
  • log – the log

  • header – the message

  • range – the range

DBUG_RANGE(log, header, range)

Log a range with a header message in the main io::TeeLogger, provided that the log level is >= io::LogLevel::DEBUG.

Parameters:
  • log – the log

  • header – the message

  • range – the range

ERORLOG_RANGE(header, range)

Log a range with a header message in the main io::TeeLogger, provided that the log level is >= io::LogLevel::ERROR.

Parameters:
  • header – the message

  • range – the range

Throws if:

the main io::TeeLogger has not been created

WARNLOG_RANGE(header, range)

Log a range with a header message in the main io::TeeLogger, provided that the log level is >= io::LogLevel::WARNING.

Parameters:
  • header – the message

  • range – the range

Throws if:

the main io::TeeLogger has not been created

INFOLOG_RANGE(header, range)

Log a range with a header message in the main io::TeeLogger, provided that the log level is >= io::LogLevel::INFO.

Parameters:
  • header – the message

  • range – the range

Throws if:

the main io::TeeLogger has not been created

EXTDLOG_RANGE(header, range)

Log a range with a header message in the main io::TeeLogger, provided that the log level is >= io::LogLevel::EXTENDED.

Parameters:
  • header – the message

  • range – the range

Throws if:

the main io::TeeLogger has not been created

DBUGLOG_RANGE(header, range)

Log a range with a header message in the main io::TeeLogger, provided that the log level is >= io::LogLevel::DEBUG.

Parameters:
  • header – the message

  • range – the range

Throws if:

the main io::TeeLogger has not been created

PRINT_PERCENTAGE(val)

print a numeric value as a percentage, with a precision of 2

Parameters:
  • val – the value

PRINT_ENUM(val)

print an enum value created with MAKE_DTO_ENUM as a string

Parameters:
  • val – the value

namespace io

Functions

template<typename Range>
std::ostream &print_range(std::ostream &os, Range const &range)

Print every object of a range in a stream.

Template Parameters:

Range – the type of range

Parameters:
  • os – the ostream

  • range – the range of object

template<typename T>
std::ostream &print(std::ostream &os, T const &obj)

Print an object in a stream.

Template Parameters:

T – the type of object

Parameters:
  • os – the ostream

  • obj – the object

template<typename T>
std::ostream &print(std::ostream &os, const std::vector<T> &range)

Print a range of object in a stream.

Template Parameters:

T – the type of object

Parameters:
  • os – the ostream

  • range – the object

template<typename T>
std::ostream &print(std::ostream &os, std::reference_wrapper<T> const &obj)

Print an object in a stream.

Template Parameters:

T – the type of object

Parameters:
  • os – the ostream

  • obj – the object

template<typename U, typename V>
std::ostream &print(std::ostream &os, const std::pair<U, V> &p)

Print a pair of object in a stream.

Template Parameters:
  • U – the first type of object in the pair

  • V – the second type of object in the pair

Parameters:
  • os – the ostream

  • p – the pair

class LoggerManager
#include <log.hh>

Store and give access to TeeLoggers.

Public Static Functions

static TeeLogger &GetLogger(std::string_view name = "")

Get or create a TeeLogger.

Parameters:

name – name of the logger, empty string for default name

Returns:

A reference to the logger

static void ClearLogger(std::string_view name = "")

Clear a logger.

Parameters:

name – name of the logger, empty string for default name

static void SetLogLevel(LogLevel level, std::string_view name = "")

Set the loglevel of a TeeLogger.

Parameters:
  • name – name of the logger, empty string for default name

  • level – the log level

static inline void SetDefaultLogLevel(LogLevel level)

Set the default verbosity.

Parameters:

level – the verbosity

static inline void SetDefaultName(std::string_view name)

Set the default name.

Parameters:

name – name of the logger

static FileLogger &GetFileLogger(std::string_view name)

Get or create a FileLogger.

Parameters:

name – name of the logger

Returns:

A reference to the logger

static void ClearFileLogger(std::string_view name)

Clear a FileLogger.

Parameters:

name – name of the logger

static void SetFileLogLevel(LogLevel level, std::string_view name)

Set the loglevel of a FileLogger.

Parameters:
  • name – name of the logger

  • level – the log level

static void ClearAllLoggers()

Clear every logger (file or tee)

Private Static Attributes

static std::unordered_map<std::string, TeeLogger, string_hash, std::equal_to<>> _tee_loggers

static storage of the tee logger

static std::string _logfile_name = std::string("default")

default log file name

static LogLevel _log_level = LogLevel::INFO

default log level

static std::unordered_map<std::string, FileLogger, string_hash, std::equal_to<>> _file_loggers

static storage of the file logger