Vita
Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes | Protected Attributes | List of all members
vita::log Class Reference

A basic console printer with integrated logger. More...

#include <log.h>

Public Types

enum  level {
  lALL , lDEBUG , lINFO , lOUTPUT ,
  lWARNING , lERROR , lFATAL , lOFF
}
 The log level. More...
 

Public Member Functions

std::ostringstream & get (level=lOUTPUT)
 Sets the logging level of a message. More...
 
 log ()
 Creates a log object. More...
 
 log (const log &)=delete
 
logoperator= (const log &)=delete
 

Static Public Member Functions

static void setup_stream (const std::string &base)
 Sets the log::stream variable with a convenient value. More...
 

Static Public Attributes

static level reporting_level = log::lALL
 Messages with a lower level aren't logged / printed. More...
 
static std::unique_ptr< std::ostream > stream = nullptr
 Optional log stream. More...
 

Protected Attributes

std::ostringstream os
 

Detailed Description

A basic console printer with integrated logger.

Note
This is derived from the code presented in "Logging in C++" by Petru Marginean (DDJ Sep 2007)

Definition at line 31 of file log.h.

Member Enumeration Documentation

◆ level

The log level.

  • lDEBUG - Only interesting for developers
  • lINFO - I say something but I don't expect you to listen
  • lOUTPUT - Standard program's console output
  • lWARNING - I can continue but please have a look
  • lERROR - Something really wrong... but you could be lucky
  • lFATAL - The program cannot continue
  • lOFF - Disable output
Remarks
The DEBUG log level can be switched on only if the NDEBUG macro is defined.

Definition at line 47 of file log.h.

Constructor & Destructor Documentation

◆ log()

vita::log::log ( )
explicit

Creates a log object.

Definition at line 31 of file log.cc.

◆ ~log()

vita::log::~log ( )
virtual

Definition at line 54 of file log.cc.

Member Function Documentation

◆ get()

std::ostringstream & vita::log::get ( level  l = lOUTPUT)

Sets the logging level of a message.

Parameters
[in]llogging level of the following message

The following code:

log().get(level) << "Hello " << username;

creates a log object with the level logging level, fetches its std::stringstream object, formats and accumulates the user-supplied data and, finally:

  • prints the resulting string on std::cout;
  • persists the resulting string into the log file (if specified).

Definition at line 48 of file log.cc.

◆ setup_stream()

void vita::log::setup_stream ( const std::string &  base)
static

Sets the log::stream variable with a convenient value.

Parameters
[in]basebase filepath of the log (e.g. /home/doe/app)

Given the /home/doe/app argument associates the log::stream variable with the app_123_18_30_00.log file (the numbers represents the current: day of the year, hours, minutes, seconds) in the /home/doe/ directory.

Definition at line 89 of file log.cc.

Member Data Documentation

◆ os

std::ostringstream vita::log::os
protected

Definition at line 65 of file log.h.

◆ reporting_level

log::level vita::log::reporting_level = log::lALL
static

Messages with a lower level aren't logged / printed.

Definition at line 50 of file log.h.

◆ stream

std::unique_ptr< std::ostream > vita::log::stream = nullptr
static

Optional log stream.

Definition at line 53 of file log.h.


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