A library for working with phylogenetic and population genetic data.
v0.27.0
logging.hpp File Reference

Detailed Description

Provides easy and fast logging functionality.

For more information on the logging, see Logging class.

Definition in file logging.hpp.

#include "genesis/utils/core/std.hpp"
#include <cmath>
#include <iosfwd>
#include <memory>
#include <sstream>
#include <string>
#include <vector>

Go to the source code of this file.

Classes

class  Logging
 Logging class with easy and fast usage. More...
 
struct  LoggingDetails
 POD stuct containing the settings for which information is included with each logging message. More...
 
class  LoggingScopeLevel
 Class that sets the Logging Level to a value von construction and set it back on destruction. This is used by the log scope level macro. More...
 

Namespaces

 genesis
 Container namespace for all symbols of genesis in order to keep them separate when used as a library.
 
 genesis::utils
 

Macros

#define GENESIS_LOG(level)
 
#define GENESIS_LOG_DETAILS(level, ...)
 
#define LOG_BOLD
 Logging of a message that is always displayed. More...
 
#define LOG_DBG   GENESIS_LOG(genesis::utils::Logging::kDebug)
 Log a debug message. See genesis::utils::LoggingLevel. More...
 
#define LOG_DBG1   GENESIS_LOG(genesis::utils::Logging::kDebug1)
 Log a debug message. See genesis::utils::LoggingLevel. More...
 
#define LOG_DBG2   GENESIS_LOG(genesis::utils::Logging::kDebug2)
 Log a debug message. See genesis::utils::LoggingLevel. More...
 
#define LOG_DBG3   GENESIS_LOG(genesis::utils::Logging::kDebug3)
 Log a debug message. See genesis::utils::LoggingLevel. More...
 
#define LOG_DBG4   GENESIS_LOG(genesis::utils::Logging::kDebug4)
 Log a debug message. See genesis::utils::LoggingLevel. More...
 
#define LOG_ERR   GENESIS_LOG(genesis::utils::Logging::kError)
 Log an error. See genesis::utils::LoggingLevel. More...
 
#define LOG_INFO   GENESIS_LOG(genesis::utils::Logging::kInfo)
 Log an info message. See genesis::utils::LoggingLevel. More...
 
#define LOG_LEVEL_MAX   genesis::utils::Logging::kDebug4
 Static maximal logging level. More...
 
#define LOG_MSG   GENESIS_LOG(genesis::utils::Logging::kMessage)
 Log an info message. See genesis::utils::LoggingLevel. More...
 
#define LOG_MSG1   GENESIS_LOG(genesis::utils::Logging::kMessage1)
 Log an info message. See genesis::utils::LoggingLevel. More...
 
#define LOG_MSG2   GENESIS_LOG(genesis::utils::Logging::kMessage2)
 Log an info message. See genesis::utils::LoggingLevel. More...
 
#define LOG_MSG3   GENESIS_LOG(genesis::utils::Logging::kMessage3)
 Log an info message. See genesis::utils::LoggingLevel. More...
 
#define LOG_MSG4   GENESIS_LOG(genesis::utils::Logging::kMessage4)
 Log an info message. See genesis::utils::LoggingLevel. More...
 
#define LOG_PROG(value, quantity)
 Logging of a progress message. More...
 
#define LOG_SCOPE_LEVEL(level)   genesis::utils::LoggingScopeLevel genesis_logging_scope_level_temp_object(level);
 This macro sets the logging level to a certain value for this scope and sets it back to the original value on exiting the scope. More...
 
#define LOG_TIME
 Logging of a message with timing information. More...
 
#define LOG_WARN   GENESIS_LOG(genesis::utils::Logging::kWarning)
 Log a warning. See genesis::utils::LoggingLevel. More...
 

Functions

long logging_progress_value (long value=-1)
 Hack function to make sure that the value arugment in LOG_PROG is only evaluated once. More...
 

Macro Definition Documentation

◆ GENESIS_LOG

#define GENESIS_LOG (   level)
Value:
if (level > LOG_LEVEL_MAX) ; \
else if (level > genesis::utils::Logging::max_level()) ; \
else genesis::utils::Logging().get(__FILE__, __LINE__, GENESIS_FUNC, level)

Definition at line 80 of file logging.hpp.

◆ GENESIS_LOG_DETAILS

#define GENESIS_LOG_DETAILS (   level,
  ... 
)
Value:
if (level > LOG_LEVEL_MAX) ; \
else if (level > genesis::utils::Logging::max_level()) ; \
else genesis::utils::Logging().get(__FILE__, __LINE__, GENESIS_FUNC, level, {__VA_ARGS__})

Definition at line 86 of file logging.hpp.

◆ LOG_BOLD

#define LOG_BOLD
Value:
false, false, false, false, false, false, false, false, false)

Logging of a message that is always displayed.

It does not include any details with its message stream (thus, is independent of Logging::details) and is always logged (independent of the max levels). This is for example used to log the program header and footer on startup and termination.

Definition at line 143 of file logging.hpp.

◆ LOG_DBG

Log a debug message. See genesis::utils::LoggingLevel.

Definition at line 118 of file logging.hpp.

◆ LOG_DBG1

Log a debug message. See genesis::utils::LoggingLevel.

Definition at line 121 of file logging.hpp.

◆ LOG_DBG2

Log a debug message. See genesis::utils::LoggingLevel.

Definition at line 124 of file logging.hpp.

◆ LOG_DBG3

Log a debug message. See genesis::utils::LoggingLevel.

Definition at line 127 of file logging.hpp.

◆ LOG_DBG4

Log a debug message. See genesis::utils::LoggingLevel.

Definition at line 130 of file logging.hpp.

◆ LOG_ERR

Log an error. See genesis::utils::LoggingLevel.

Definition at line 94 of file logging.hpp.

◆ LOG_INFO

Log an info message. See genesis::utils::LoggingLevel.

Definition at line 100 of file logging.hpp.

◆ LOG_LEVEL_MAX

#define LOG_LEVEL_MAX   genesis::utils::Logging::kDebug4

Static maximal logging level.

Everything above this level will be pruned by the compiler.

Definition at line 66 of file logging.hpp.

◆ LOG_MSG

Log an info message. See genesis::utils::LoggingLevel.

Definition at line 103 of file logging.hpp.

◆ LOG_MSG1

Log an info message. See genesis::utils::LoggingLevel.

Definition at line 106 of file logging.hpp.

◆ LOG_MSG2

Log an info message. See genesis::utils::LoggingLevel.

Definition at line 109 of file logging.hpp.

◆ LOG_MSG3

Log an info message. See genesis::utils::LoggingLevel.

Definition at line 112 of file logging.hpp.

◆ LOG_MSG4

Log an info message. See genesis::utils::LoggingLevel.

Definition at line 115 of file logging.hpp.

◆ LOG_PROG

#define LOG_PROG (   value,
  quantity 
)
Value:
else if( (long) genesis::utils::logging_progress_value(value) % \
(((long) (quantity) * genesis::utils::Logging::report_percentage() / 100) > 0 ? \
((long) (quantity) * genesis::utils::Logging::report_percentage() / 100) : 1) != 0 ) ; \
(int) round(100.0 * (double) genesis::utils::logging_progress_value() / ((quantity) > 0 ? (quantity) : 1)) << "% "

Logging of a progress message.

This special logging mechanism provides an easy way to report progress while running long operations. It uses LoggingLevel::kProgress for its messages.

It takes two arguments, the first one being the number of the current iteration of the loop, the second the total number of iterations, both are casted to long.

The integer value of Logging::report_percentage can be used to control how often progress is reported in terms of a percentage counter. Default is 5, meaning every 5% progress is reported.

The trailing message is optional – when left out, simply the percentage number will be logged. However, the statement must always be ended by a semicolon.

The default use case looks like this:

Logging::report_percentage(2);
for (int i = 0; i < n; ++i) {
    // long loop from 0 to n
    // do stuff...
    LOG_PROG(i, n) << "of the loop finished.";
}

This will report the progress of the loop every 2 percent with a message like this:

0% of the loop finished.
2% of the loop finished.
4% of the loop finished.
...

It is also possible to increment the iteration counter inline:

LOG_PROG(++i, n) << "of stuff finished.";

It automatically checks whether it is time to report, so that the user does not have to take care of this.

Caveat: For small number of iterations (<100) it is not entirely accurate. Because of the involved integer modulo calculations, it will trigger too many messages. However, as this type of logging is usually used for loops with many iterations, this should rarely be an issue.

There is a slight overhead of ~60ms per 1mio invocations because of the needed calculations.

Definition at line 232 of file logging.hpp.

◆ LOG_SCOPE_LEVEL

#define LOG_SCOPE_LEVEL (   level)    genesis::utils::LoggingScopeLevel genesis_logging_scope_level_temp_object(level);

This macro sets the logging level to a certain value for this scope and sets it back to the original value on exiting the scope.

This is useful to disable debug messages in a function while still beging able to activate them when needed. The macro creates class instance of LoggingScopeLevel with a long and hopefully unique name.

Definition at line 165 of file logging.hpp.

◆ LOG_TIME

#define LOG_TIME
Value:
false, false, false, false , true, false, false, false, false)

Logging of a message with timing information.

It includes the run time difference to the last log message in seconds as its only detail (independent of Logging::details). This is particularly useful for timing and profiling code sections. Its level is Logging::kDebug, so that in can be easily turned of for production code.

Definition at line 154 of file logging.hpp.

◆ LOG_WARN

Log a warning. See genesis::utils::LoggingLevel.

Definition at line 97 of file logging.hpp.

LOG_LEVEL_MAX
#define LOG_LEVEL_MAX
Static maximal logging level.
Definition: logging.hpp:66
GENESIS_FUNC
#define GENESIS_FUNC
Definition: std.hpp:53
GENESIS_LOG_DETAILS
#define GENESIS_LOG_DETAILS(level,...)
Definition: logging.hpp:86
genesis::utils::Logging::get
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.
Definition: logging.cpp:279
genesis::utils::Logging::kProgress
@ kProgress
Progess, used in long executing functions. See LOG_PROG.
Definition: logging.hpp:419
genesis::utils::logging_progress_value
long logging_progress_value(long value=-1)
Hack function to make sure that the value arugment in LOG_PROG is only evaluated once.
Definition: logging.hpp:179
genesis::utils::Logging::max_level
static LoggingLevel max_level()
Get the highest log level that is reported.
Definition: logging.hpp:491
genesis::utils::Logging::report_percentage
static int report_percentage()
Get the current percentage for reporting LOG_PROG messages.
Definition: logging.hpp:498
genesis::utils::Logging
Logging class with easy and fast usage.
Definition: logging.hpp:386
genesis::utils::Logging::kNone
@ kNone
Special messages that are always logged, e.g. program header.
Definition: logging.hpp:407
genesis::utils::Logging::kDebug
@ kDebug
Basic debugging message. See LOG_DBG.
Definition: logging.hpp:437