File Logger.h

Copyright

This code is licensed under the 3-clause BSD license.

Copyright ETH Zurich, Laboratory for Physical Chemistry, Reiher Group.

See LICENSE.txt for details.

namespace Scine

This header file contains functions that allow for common notation for common things that can be done at a different degree of derivatives.

This header contains alias definitions defining which classes to use for the different degrees of derivatives.

namespace Utils
class Log
#include <Logger.h>

Static class for logging.

By default, logging is disabled. Logging can be started with startConsoleLogging() or startFileLogging(). It is possible to have several files for logging. The calls to Log are thread-safe.

Public Types

enum severity_level

Enum for the log entry severity.

Values:

trace
debug
info
warning
error
fatal

Public Functions

Log()

Disabled constructor: this is a static class.

Public Static Functions

static void disableLogging()

This function disables all logging.

static void enableLogging()

This function reenables all logging if disableLogging() was called previously.

static void flush()

Flush the log messages.

It can be that log messages are temporarily stored in a buffer. This forces output to the respective stream.

static void startFileLogging(std::string filename, severity_level minimalSeverity = defaultMinimalSeverity_, bool autoFlush = false)

Start writing log to a file.

Parameters
  • filename: The path to the file to be logged to.

  • minimalSeverity: Log messages will be written only when their severity is at least as important as this.

  • autoFlush: Ff true, after each log message the stream will be flushed (at higher computational cost).

static void startFileLogging(std::string filename, const std::string &minimalSeverity, bool autoFlush = false)

Start writing log to a file.

Parameters
  • filename: The path to the file to be logged to.

  • minimalSeverity: Log messages will be written only when their severity is at least as important as this.

  • autoFlush: Ff true, after each log message the stream will be flushed (at higher computational cost).

static void startConsoleLogging(const std::string &minimalSeverity)

(Re)starts logging to the console.

Parameters
  • minimalSeverity: Log messages will be written only when their severity is at least as important as this. The severity levels are written in UniversalSettings::LogLevels.

static void startConsoleLogging(severity_level minimalSeverity = defaultMinimalSeverity_)

(Re)starts logging to the console.

Parameters
  • minimalSeverity: Log messages will be written only when their severity is at least as important as this.

static void stopConsoleLogging()

Stops logging to the console.

static ChainedLogger trace()

Logging function to log statementes of the severity: trace.

Function returning a logger. Allows direct logging with the left-shift operator:

Log::trace() << "Information to log"; 

Return

ChainedLogger The logger to be logged with.

static ChainedLogger debug()

Logging function to log statementes of the severity: debug.

Function returning a logger, the function0 allow dircet logging via the leftshift operator:

Log::debug() << "Information to log"; 

Return

ChainedLogger The logger to be logged with.

static ChainedLogger info()

Logging function to log statementes of the severity: info.

Function returning a logger, the function0 allow dircet logging via the leftshift operator:

Log::info() << "Information to log"; 

Return

ChainedLogger The logger to be logged with.

static ChainedLogger warning()

Logging function to log statementes of the severity: warning.

Function returning a logger, the function0 allow dircet logging via the leftshift operator:

Log::warning() << "Information to log"; 

Return

ChainedLogger The logger to be logged with.

static ChainedLogger error()

Logging function to log statementes of the severity: error.

Function returning a logger, the function0 allow dircet logging via the leftshift operator:

Log::error() << "Information to log"; 

Return

ChainedLogger The logger to be logged with.

static ChainedLogger fatal()

Logging function to log statementes of the severity: fatal.

Function returning a logger, the function0 allow dircet logging via the leftshift operator:

Log::fatal() << "Information to log"; 

Return

ChainedLogger The logger to be logged with.

template<typename String>
static void trace(String &&s)

Logging function to log statementes of the severity: trace.

Template Parameters
  • String:

Parameters
  • s: The statement to be logged.

template<typename String>
static void debug(String &&s)

Logging function to log statementes of the severity: debug.

Template Parameters
  • String:

Parameters
  • s: The statement to be logged.

template<typename String>
static void info(String &&s)

Logging function to log statementes of the severity: info.

Template Parameters
  • String:

Parameters
  • s: The statement to be logged.

template<typename String>
static void warning(String &&s)

Logging function to log statementes of the severity: warning.

Template Parameters
  • String:

Parameters
  • s: The statement to be logged.

template<typename String>
static void error(String &&s)

Logging function to log statementes of the severity: error.

Template Parameters
  • String:

Parameters
  • s: The statement to be logged.

template<typename String>
static void fatal(String &&s)

Logging function to log statementes of the severity: fatal.

Template Parameters
  • String:

Parameters
  • s: The statement to be logged.

static severity_level stringToSeverity(const std::string &minimalSeverity)

Method to convert a string to the corresponding severity_level.

possible strings are debug trace info warning error fatal

Private Static Attributes

std::unique_ptr<Impl> pImpl_
const severity_level defaultMinimalSeverity_ = severity_level::info
std::mutex m_
class ChainedLogger

Structure allowing to chain output elements to be logged.

The boost macro is called in the destructor to avoid creating one log entry by chained output element.

Public Functions

ChainedLogger(severity_level l)

Construct a new ChainedLogger object.

Parameters
  • l: The severty level of this logger.

ChainedLogger(ChainedLogger &&t)

Default move constructor.

~ChainedLogger()

Custom destructor.

template<typename K>
std::ostream &operator<<(K &&k)

Overriding the left shift operator.

Return

std::ostream& The stream logged to.

Template Parameters
  • K: The type of the message.

Parameters
  • k: The message.

Public Members

const severity_level level
std::ostringstream os