A toolkit for working with phylogenetic data.
v0.19.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Logging Class Reference

#include <genesis/utils/core/logging.hpp>

Detailed Description

Logging class with easy and fast usage.

The basic usage of this class is to invoke the macros for the different types of log messages and send a stream of messages to them:

LOG_DBG << "you are here";
LOG_ERR << "there was an error: " << 42;

The provided types of macros are: LOG_ERR, LOG_WARN, LOG_INFO, LOG_DBG, LOG_DBG1, LOG_DBG2, LOG_DBG3, LOG_DBG4 for all levels of logging explained in LoggingLevel.

The details that are logged with each message can be changed via accessing Logging::details – see LoggingDetails for more on that.

In order to use this class, at least one output stream has to be added first by invoking either AddOutputStream or AddOutputFile.

The depths of logging can be changed in order to reduce the amount of written messages. First, at compile time the macro constant LOG_LEVEL_MAX can be set (e.g. in the Makefile) to the highest level that shall be logged. All log invoakations with a higher level are never called, and moreover will be pruned from the code by most compilers completetly. This makes the log class fast – instead of deleting all log invokations by hand, it is sufficient to set the constant to a low level. Second, the depths of logging can be changed dynamically at run time by setting Logging::max_level to the desired value. Of course, this value cannot be higher than LOG_LEVEL_MAX, because those logs are already pruned by the compiler.

There are also three more special log types that create a different output than the previously mentioned types: LOG_BOLD, LOG_TIME and LOG_PROG. See their respective documentation for more information.

The inner working of this class is as follows: Upon invokation via one of the macros, an instance is created that stays alive only for the rest of the source code line. In this line, the log message is inserted to the buffer stream. After the line finishes, the object is automatically destroyed, so that its destructor is called. In the destructor, the message is composed and logged to all given output streams.

Caveat: Because the macro contains conditions depending on the log level, do not use code in a log line that changes the program state. Therefore, instead of

LOG_INFO << "this is iteration " << ++i;

use

++i;
LOG_INFO << "this is iteration " << i;

because the former will not work when the max log level is below info level.

Definition at line 351 of file logging.hpp.

Public Member Functions

 Logging ()
 
 Logging (const Logging &)
 
 ~Logging ()
 Destructor that is invoked at the end of each log line and does the actual output. More...
 
std::ostringstream & get (const std::string &file, const int line, const std::string &function, const LoggingLevel level)
 Getter for the singleton instance of log, is called by the standard macros. More...
 
std::ostringstream & get (const std::string &file, const int line, const std::string &function, const LoggingLevel level, const LoggingDetails dets)
 Getter for the singleton instance of log, is called by special macros that change the details of the log message. More...
 
Loggingoperator= (const Logging &)=delete
 

Static Public Member Functions

static std::string level_to_string (const LoggingLevel level)
 Return a string representation of a log level. More...
 
static void log_debug (const std::string &msg)
 
static void log_debug_1 (const std::string &msg)
 
static void log_debug_2 (const std::string &msg)
 
static void log_debug_3 (const std::string &msg)
 
static void log_debug_4 (const std::string &msg)
 
static void log_error (const std::string &msg)
 
static void log_info (const std::string &msg)
 
static void log_to_file (const std::string &fn)
 Add an output file to which log messages are written. More...
 
static void log_to_stdout ()
 Add stdout as output stream to which log messages are written. More...
 
static void log_to_stream (std::ostream &os)
 Add an output stream to which log messages are written. More...
 
static void log_warning (const std::string &msg)
 
static LoggingLevel max_level ()
 Get the highest log level that is reported. More...
 
static void max_level (const LoggingLevel level)
 Set the highest log level that is reported. More...
 
static int report_percentage ()
 Get the current percentage for reporting LOG_PROG messages. More...
 
static void report_percentage (const int percentage)
 set the percentage for reporting LOG_PROG messages. More...
 

Public Types

enum  LoggingLevel {
  kNone = 0, kError, kWarning, kInfo,
  kProgress, kDebug, kDebug1, kDebug2,
  kDebug3, kDebug4
}
 Levels of severity used for logging. More...
 

Static Public Attributes

static std::string debug_indent = " "
 Indention string for Debug Levels 1-4. More...
 
static LoggingDetails details
 Settings for which information is included with each log message. See LoggingDetails for usage. More...
 

Protected Attributes

std::ostringstream buff_
 
LoggingDetails details_
 
std::string file_
 
std::string function_
 
LoggingLevel level_
 
int line_
 

Static Protected Attributes

static long count_ = 0
 
static clock_t last_clock_ = 0
 
static LoggingLevel max_level_ = kDebug4
 
static std::vector
< std::ostream * > 
ostreams_
 
static int report_percentage_ = 5
 

Constructor & Destructor Documentation

Logging ( )
inline

Definition at line 402 of file logging.hpp.

~Logging ( )

Destructor that is invoked at the end of each log line and does the actual output.

Definition at line 173 of file logging.cpp.

Logging ( const Logging )
inline

Definition at line 408 of file logging.hpp.

Member Function Documentation

std::ostringstream & get ( const std::string &  file,
const int  line,
const std::string &  function,
const LoggingLevel  level 
)

Getter for the singleton instance of log, is called by the standard macros.

It returns the string stream buffer used to capture the log messages.

Definition at line 263 of file logging.cpp.

std::ostringstream & get ( const std::string &  file,
const int  line,
const std::string &  function,
const LoggingLevel  level,
const LoggingDetails  dets 
)

Getter for the singleton instance of log, is called by special macros that change the details of the log message.

It stores some relevant information and returns the string stream buffer used to capture the log messages.

Definition at line 278 of file logging.cpp.

std::string level_to_string ( const LoggingLevel  level)
static

Return a string representation of a log level.

Definition at line 117 of file logging.cpp.

static void log_debug ( const std::string &  msg)
inlinestatic

Definition at line 478 of file logging.hpp.

static void log_debug_1 ( const std::string &  msg)
inlinestatic

Definition at line 483 of file logging.hpp.

static void log_debug_2 ( const std::string &  msg)
inlinestatic

Definition at line 488 of file logging.hpp.

static void log_debug_3 ( const std::string &  msg)
inlinestatic

Definition at line 493 of file logging.hpp.

static void log_debug_4 ( const std::string &  msg)
inlinestatic

Definition at line 498 of file logging.hpp.

static void log_error ( const std::string &  msg)
inlinestatic

Definition at line 463 of file logging.hpp.

static void log_info ( const std::string &  msg)
inlinestatic

Definition at line 473 of file logging.hpp.

void log_to_file ( const std::string &  fn)
static

Add an output file to which log messages are written.

This creates a stream to the file.

Definition at line 154 of file logging.cpp.

void log_to_stdout ( )
static

Add stdout as output stream to which log messages are written.

Definition at line 128 of file logging.cpp.

void log_to_stream ( std::ostream &  os)
static

Add an output stream to which log messages are written.

Definition at line 144 of file logging.cpp.

static void log_warning ( const std::string &  msg)
inlinestatic

Definition at line 468 of file logging.hpp.

static LoggingLevel max_level ( )
inlinestatic

Get the highest log level that is reported.

Definition at line 440 of file logging.hpp.

void max_level ( const LoggingLevel  level)
static

Set the highest log level that is reported.

Invocations of log with higher levels will create no output. It creates a warning if the set level is higher than the static compile time level set by LOG_LEVEL_MAX.

Definition at line 86 of file logging.cpp.

Logging& operator= ( const Logging )
delete
static int report_percentage ( )
inlinestatic

Get the current percentage for reporting LOG_PROG messages.

Definition at line 447 of file logging.hpp.

void report_percentage ( const int  percentage)
static

set the percentage for reporting LOG_PROG messages.

Definition at line 99 of file logging.cpp.

Member Enumeration Documentation

Levels of severity used for logging.

The levels are in ascending order and are used both to signal what kind of message is being logged and to provide a threshold for less important messages that can be filtered out, for example debug messages in the production build of the program. Because some of the filtering is already done at compile time, log messages with a level higher than LOG_LEVEL_MAX do not produce any overhead. See also Logging class for more on this.

Enumerator
kNone 

Special messages that are always logged, e.g. program header.

kError 

Errors, usually non-recoverable. See LOG_ERR.

kWarning 

Warnings if somthing went wront, but program can continue. See LOG_WARN.

kInfo 

Infos, for example when a file was written. See LOG_INFO.

kProgress 

Progess, used in long executing functions. See LOG_PROG.

kDebug 

Basic debugging message. See LOG_DBG.

kDebug1 

Debugging message with indent level 1 (e.g. for loops). See LOG_DBG1.

kDebug2 

Debugging message with indent level 2. See LOG_DBG2.

kDebug3 

Debugging message with indent level 3. See LOG_DBG3.

kDebug4 

Debugging message with indent level 4. See LOG_DBG4.

Definition at line 370 of file logging.hpp.

Member Data Documentation

std::ostringstream buff_
protected

Definition at line 509 of file logging.hpp.

long count_ = 0
staticprotected

Definition at line 523 of file logging.hpp.

std::string debug_indent = " "
static

Indention string for Debug Levels 1-4.

Definition at line 457 of file logging.hpp.

LoggingDetails details
static
Initial value:
= {
false,
false,
false,
false,
false,
false,
false,
false,
true
}

Settings for which information is included with each log message. See LoggingDetails for usage.

Definition at line 437 of file logging.hpp.

LoggingDetails details_
protected

Definition at line 514 of file logging.hpp.

std::string file_
protected

Definition at line 510 of file logging.hpp.

std::string function_
protected

Definition at line 512 of file logging.hpp.

clock_t last_clock_ = 0
staticprotected

Definition at line 526 of file logging.hpp.

LoggingLevel level_
protected

Definition at line 513 of file logging.hpp.

int line_
protected

Definition at line 511 of file logging.hpp.

Logging::LoggingLevel max_level_ = kDebug4
staticprotected

Definition at line 517 of file logging.hpp.

std::vector< std::ostream * > ostreams_
staticprotected

Definition at line 529 of file logging.hpp.

int report_percentage_ = 5
staticprotected

Definition at line 520 of file logging.hpp.


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