Bredbandskollen CLI  1.2
Asynchronous network task engine
Public Member Functions | Static Public Member Functions | Protected Member Functions | List of all members
Logger Class Reference

A simple logger. All classes that want to write to the global log file should inherit from this class. More...

#include <logger.h>

Inheritance diagram for Logger:
Inheritance graph
[legend]

Public Member Functions

 Logger (std::string label)
 
std::string label () const
 Return the object's log label.
 
void resetLabel (const std::string &new_label)
 Modify the object's log label.
 

Static Public Member Functions

static void setLogFile (std::ostream &stream)
 Set global log destination. More...
 
static void reopenLogFile (const std::string &filename)
 If current log is a file (ofstream), reopen it with new filename:
 
static void setLogLimit (unsigned int loglines=0, unsigned int warnlines=0, unsigned int errlines=0)
 Set max number of lines of info/warn/err log. More...
 
static void sayTime (std::ostream &stream)
 Write current local time to the given stream.
 
static bool inError ()
 Return true if any error has been logged (globally since start)
 
static std::ostream & err_log (const std::string &label)
 Write a line of error log. More...
 
static std::ostream & warn_log (const std::string &label)
 Write a line of warning log. More...
 
static std::ostream & log (const std::string &label)
 Write a line of info log. More...
 
static void flushLogFile ()
 
static void pauseLogging ()
 Disable all log output until next call to Logger::setLogFile.
 
static double secondsSince (const TimePoint &t)
 
static double secondsTo (const TimePoint &t)
 
static int64_t msSince (const TimePoint &t)
 
static int64_t msTo (const TimePoint &t)
 
static bool hasExpired (const TimePoint &t)
 Return true if current time is after the given TimePoint.
 
static TimePoint timeNow ()
 Return current time.
 
static TimePoint timeAfter (double s)
 Return current time plus s seconds.
 
static TimePoint timeMax ()
 Return a very distant time.
 
static std::chrono::microseconds toUs (double t)
 Convert s (seconds) to std::chrono::microseconds.
 
static std::string dateString (time_t t=0)
 Return local time, formatted as 2023-10-14T09:38:47+0200.
 
static std::string dateString2 (time_t t=0)
 Return local time, formatted as Sat, 14 Oct 2023 09:38:47.
 
static std::string createHashKey (unsigned int length=20)
 Return a random string. More...
 

Protected Member Functions

std::ostream & errno_log () const
 Write a line of error log after a failed system call has set the global errno to a non-zero value. More...
 
std::ostream & err_log () const
 Write a line of error log. More...
 
std::ostream & warn_log () const
 Write a line of warning log. More...
 
std::ostream & log () const
 Write a line of info log. More...
 
std::ostream & dbg_log () const
 Write a line of debug log. More...
 

Detailed Description

A simple logger. All classes that want to write to the global log file should inherit from this class.

By default, logs will be written to cerr. To log elsewhere, you must call the static function Logger::setLogFile with a stream object (e.g. an std::ofstream or an std::ostringstream) which the logs will be written to. The stream will be used globally. You must make sure the global stream is never destroyed, at least not before Logger::setLogFile has been called with another stream.

Constructor & Destructor Documentation

◆ Logger()

Logger::Logger ( std::string  label)
inline

Each object of the Logger class (or its subclasses) have a log label, which will often be thought of as the name of the object.

Member Function Documentation

◆ createHashKey()

std::string Logger::createHashKey ( unsigned int  length = 20)
static

Return a random string.

Create string of length random hex chars from system's random number generator. The length should be a multiple of 4.

◆ dbg_log()

std::ostream& Logger::dbg_log ( ) const
inlineprotected

Write a line of debug log.

Access the current global debug log stream. A line feed and a preamble will be written to the stream. Then send whetever you want to the log stream using the standard std::ostream API.

Note! The global debug log stream will be "disabled" (i.e. set to a dummy stream) unless compiler macro TASKRUNNER_LOGDBG is defined.

◆ err_log() [1/2]

std::ostream& Logger::err_log ( ) const
inlineprotected

Write a line of error log.

Access the current global error log stream. A line feed and a preamble will be written to the stream. Then send whetever you want to the log stream using the standard std::ostream API.

Note! The global error log stream will be "disabled" (i.e. set to a dummy stream) unless compiler macro TASKRUNNER_LOGERR is defined.

May be used in any non-static member of any subclass. Example:

err_log() << "Child task " << t->label() << " failed."; 

◆ err_log() [2/2]

static std::ostream& Logger::err_log ( const std::string &  label)
inlinestatic

Write a line of error log.

Access the current global error log stream. A line feed and a preamble will be written to the stream. Then send whetever you want to the log stream using the standard std::ostream API.

In non-static members of subclasses to Logger, the method Logger::err_log() should be used instead of this function.

◆ errno_log()

std::ostream & Logger::errno_log ( ) const
protected

Write a line of error log after a failed system call has set the global errno to a non-zero value.

Access the current global error log stream. A line feed and a preamble, including the latest OS error, will be written to the stream.

Note! The global error stream will be "disabled" (i.e. set to a dummy stream) unless compiler macro TASKRUNNER_LOGERR is defined.

◆ flushLogFile()

static void Logger::flushLogFile ( )
inlinestatic

Anything written to the global log may be buffered for quite some time, and thus not visible in the destination file. This method will flush the buffer and write an extra empty line.

Calling this often may be bad for performance.

◆ log() [1/2]

std::ostream& Logger::log ( ) const
inlineprotected

Write a line of info log.

Access the current global info log stream. A line feed and a preamble will be written to the stream. Then send whetever you want to the log stream using the standard std::ostream API.

Note! The global info log stream will be "disabled" (i.e. set to a dummy stream) unless compiler macro TASKRUNNER_LOGINFO is defined.

◆ log() [2/2]

static std::ostream& Logger::log ( const std::string &  label)
inlinestatic

Write a line of info log.

Access the current global info log stream. A line feed and a preamble will be written to the stream. Then send whetever you want to the log stream using the standard std::ostream API.

In non-static members of subclasses to Logger, the method Logger::warn_log() should be used instead of this function.

◆ msSince()

int64_t Logger::msSince ( const TimePoint t)
static

Return number of milliseconds since the given TimePoint. The returned value might be negative.

◆ msTo()

int64_t Logger::msTo ( const TimePoint t)
static

Return number of milliseconds until the given TimePoint. The returned value might be negative.

◆ secondsSince()

double Logger::secondsSince ( const TimePoint t)
static

Return number of seconds since the given TimePoint. The returned value might be negative.

◆ secondsTo()

double Logger::secondsTo ( const TimePoint t)
static

Return number of seconds until the given TimePoint. The returned value might be negative.

◆ setLogFile()

void Logger::setLogFile ( std::ostream &  stream)
static

Set global log destination.

The given stream will be the destination of all subsequent log calls globally. You must make sure the global stream never is destroyed, at least not until Logger::setLogFile is called with another stream.

◆ setLogLimit()

void Logger::setLogLimit ( unsigned int  loglines = 0,
unsigned int  warnlines = 0,
unsigned int  errlines = 0 
)
static

Set max number of lines of info/warn/err log.

If 0, reset to previous (non-zero) max number of lines. After the limit has been reached, no more lines for that log level will be printed until the limit has been reset.

◆ warn_log() [1/2]

std::ostream& Logger::warn_log ( ) const
inlineprotected

Write a line of warning log.

Access the current global warning log stream. A line feed and a preamble will be written to the stream. Then send whetever you want to the log stream using the standard std::ostream API.

Note! The global warning log stream will be "disabled" (i.e. set to a dummy stream) unless compiler macro TASKRUNNER_LOGWARN is defined.

◆ warn_log() [2/2]

static std::ostream& Logger::warn_log ( const std::string &  label)
inlinestatic

Write a line of warning log.

Access the current global warning log stream. A line feed and a preamble will be written to the stream. Then send whetever you want to the log stream using the standard std::ostream API.

In non-static members of subclasses to Logger, the method Logger::warn_log() should be used instead of this function.


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