dodo  0.0.1
A C++ library to create containerized Linux services
dodo::common::Logger Class Reference

A Logger interface. More...

#include <logger.hpp>

Collaboration diagram for dodo::common::Logger:

Data Structures

struct  FileParams
 File Logging parameters. More...
 
struct  SyslogParams
 syslog logging parameters. More...
 

Public Types

enum  Destination { Console = 1, File = 2, Syslog = 4 }
 Destination flags. More...
 
enum  LogLevel {
  LogLevel::Fatal, LogLevel::Error, LogLevel::Warning, LogLevel::Info,
  LogLevel::Statistics, LogLevel::Debug, LogLevel::Trace
}
 The level of a log entry. More...
 

Public Member Functions

void debug (const std::string &message)
 Log a Debug log entry. More...
 
void error (const std::string &message)
 Log a Error log entry. More...
 
void fatal (const std::string &message)
 Log a Fatal log entry. More...
 
void info (const std::string &message)
 Log a Info log entry. More...
 
void statistics (const std::string &message)
 Log a Statistics log entry. More...
 
void trace (const std::string &message)
 Log a Trace log entry. More...
 
void warning (const std::string &message)
 Log a Warning log entry. More...
 

Static Public Member Functions

static LoggergetLogger ()
 Get the Logger singleton - initialize() must have been called first. More...
 
static Loggerinitialize (const Config &config)
 Initialize the Logger singleton. More...
 
static std::string LogLevelAsString (LogLevel level, bool acronym)
 Return the LogLevel as a string (as used in console and file). More...
 
static LogLevel StringAsLogLevel (const std::string slevel)
 Return the string as a LogLevel. More...
 

Protected Member Functions

 Logger (const Config &config)
 Construct against a config. More...
 
virtual ~Logger ()
 Destructor. More...
 
void checkRotate ()
 Check and rotate the log file if it exceeds the size limit, delete older logs if needed. More...
 
std::string formatMessage (LogLevel level, const std::string &message)
 Format a LogLine. More...
 
void getFileParams (const Config &config)
 Get FileParams from the Config into file_params_. More...
 
void log (LogLevel level, const std::string &message)
 Log a log entry. More...
 
int mapLeveltoSyslog (LogLevel level)
 Map a LogLevel to a syslog level. More...
 

Protected Attributes

uint8_t destinations_
 Destinations as bit flags. More...
 
FileParams file_params_
 Parameters for the file Destination. More...
 
std::string hostname_
 The hostname cached in the constructor. More...
 
std::map< uint8_t, LogLevellevels_
 Map Destination to LogLevel. More...
 
size_t rotate_throttle_counter_
 Track when rotation was last checked. More...
 
SyslogParams syslog_params_
 Parameters for the syslog Destination. More...
 

Static Protected Attributes

static Loggerlogger_ = nullptr
 The singleton. More...
 
static threads::Mutex mutex_
 threads::Mutex to serialize log writing. More...
 

Friends

class Application
 Application::~Application() will destruct this singleton. More...
 

Detailed Description

A Logger interface.

Calls to log( LogLevel level, const std::string message ) are serialized internally and thus thread-safe.

Definition at line 40 of file logger.hpp.

Member Enumeration Documentation

◆ Destination

Destination flags.

Definition at line 59 of file logger.hpp.

◆ LogLevel

The level of a log entry.

Enumerator
Fatal 

The program could not continue.

Error 

The program signaled an error.

Warning 

The program signaled a warning.

Info 

The program signaled an informational message.

Statistics 

The program signaled runtime statistics.

Debug 

The program produced debug info.

Trace 

The program produced trace info.

Definition at line 46 of file logger.hpp.

Constructor & Destructor Documentation

◆ Logger()

◆ ~Logger()

dodo::common::Logger::~Logger ( )
protectedvirtual

Destructor.

Definition at line 71 of file logger.cpp.

References dodo::common::Logger::FileParams::file, file_params_, and mutex_.

Member Function Documentation

◆ checkRotate()

void dodo::common::Logger::checkRotate ( )
protected

Check and rotate the log file if it exceeds the size limit, delete older logs if needed.

Definition at line 153 of file logger.cpp.

References dodo::common::Logger::FileParams::active_log, dodo::common::Logger::FileParams::directory, dodo::common::Logger::FileParams::file, file_params_, dodo::common::Logger::FileParams::filesize, dodo::common::formatDateTimeUTC(), dodo::common::getFileSize(), dodo::common::Logger::FileParams::max_file_trail, and dodo::common::Logger::FileParams::max_size_mib.

Referenced by log().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ debug()

void dodo::common::Logger::debug ( const std::string &  message)

Log a Debug log entry.

These are only logged when LogLevel >= Debug. Calling is thread-safe.

Parameters
messageThe Debug log message.

Definition at line 132 of file logger.cpp.

References Debug, and log().

Here is the call graph for this function:

◆ error()

void dodo::common::Logger::error ( const std::string &  message)

Log a Error log entry.

These are only logged when LogLevel >= Error. Calling is thread-safe.

Parameters
messageThe Error log message.

Definition at line 116 of file logger.cpp.

References Error, and log().

Here is the call graph for this function:

◆ fatal()

void dodo::common::Logger::fatal ( const std::string &  message)

Log a Fatal log entry.

These are always logged as Fatal is the lowest LogLevel. Calling is thread-safe.

Parameters
messageThe fatal log message.

Definition at line 112 of file logger.cpp.

References Fatal, and log().

Here is the call graph for this function:

◆ formatMessage()

std::string dodo::common::Logger::formatMessage ( LogLevel  level,
const std::string &  message 
)
protected

Format a LogLine.

Parameters
levelThe logLevel of the messsage.
messageThe log message.
Returns
A formatted log line.

Definition at line 205 of file logger.cpp.

References dodo::common::formatDateTimeUTC(), dodo::common::Config::getAppName(), dodo::common::Config::getConfig(), hostname_, and LogLevelAsString().

Referenced by log().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getFileParams()

◆ getLogger()

Logger * dodo::common::Logger::getLogger ( )
static

Get the Logger singleton - initialize() must have been called first.

Parameters
configThe Config to use.
Returns
A pointer to the singleton.

Definition at line 83 of file logger.cpp.

References logger_, and throw_Exception.

◆ info()

void dodo::common::Logger::info ( const std::string &  message)

Log a Info log entry.

These are only logged when LogLevel >= Info. Calling is thread-safe.

Parameters
messageThe Info log message.

Definition at line 124 of file logger.cpp.

References Info, and log().

Here is the call graph for this function:

◆ initialize()

Logger * dodo::common::Logger::initialize ( const Config config)
static

Initialize the Logger singleton.

Parameters
configThe Config to use.
Returns
A pointer to the singleton.

Definition at line 77 of file logger.cpp.

References Logger(), logger_, and throw_Exception.

Referenced by dodo::common::Application::Application().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ log()

void dodo::common::Logger::log ( LogLevel  level,
const std::string &  message 
)
protected

Log a log entry.

The entry is only written when level <= the loglevel specified in the Config, and silently ignored otherwise. Calling is thread-safe, but the lock is not acquired at all if the entry is ignored by level.

Parameters
levelThe logLevel of the messsage.
messageThe log message.

Definition at line 88 of file logger.cpp.

References checkRotate(), destinations_, dodo::common::Logger::SyslogParams::facility, dodo::common::Logger::FileParams::file, file_params_, dodo::common::Logger::FileParams::filesize, formatMessage(), dodo::common::Config::getAppName(), dodo::common::Config::getConfig(), Info, levels_, LogLevelAsString(), mapLeveltoSyslog(), mutex_, and syslog_params_.

Referenced by dodo::common::Application::Application(), debug(), error(), fatal(), info(), statistics(), trace(), and warning().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ LogLevelAsString()

std::string dodo::common::Logger::LogLevelAsString ( Logger::LogLevel  level,
bool  acronym 
)
static

Return the LogLevel as a string (as used in console and file).

Parameters
levelThe LogLevel.
acronymIf true, return a uppercase character triplet for use in log files.
Returns
A string representation of the LogLevel.

Definition at line 228 of file logger.cpp.

References Debug, Error, Fatal, Info, Statistics, Trace, and Warning.

Referenced by formatMessage(), and log().

Here is the caller graph for this function:

◆ mapLeveltoSyslog()

int dodo::common::Logger::mapLeveltoSyslog ( LogLevel  level)
protected

Map a LogLevel to a syslog level.

Parameters
levelThe LogLevel to map.
Returns
The mapped syslog level.

Definition at line 140 of file logger.cpp.

References Debug, Error, Fatal, Info, Statistics, Trace, and Warning.

Referenced by log().

Here is the caller graph for this function:

◆ statistics()

void dodo::common::Logger::statistics ( const std::string &  message)

Log a Statistics log entry.

These are only logged when LogLevel >= Statistics. Calling is thread-safe.

Parameters
messageThe Statistics log message.

Definition at line 128 of file logger.cpp.

References log(), and Statistics.

Here is the call graph for this function:

◆ StringAsLogLevel()

Logger::LogLevel dodo::common::Logger::StringAsLogLevel ( const std::string  slevel)
static

Return the string as a LogLevel.

Note this will default to Info if string is not recognized.

Parameters
slevelThe LogLevel as a string.
Returns
The LogLevel.

Definition at line 217 of file logger.cpp.

References Debug, Error, Fatal, Info, Statistics, Trace, and Warning.

Referenced by getFileParams(), and Logger().

Here is the caller graph for this function:

◆ trace()

void dodo::common::Logger::trace ( const std::string &  message)

Log a Trace log entry.

These are only logged when LogLevel >= Trace. Calling is thread-safe.

Parameters
messageThe Trace log message.

Definition at line 136 of file logger.cpp.

References log(), and Trace.

Here is the call graph for this function:

◆ warning()

void dodo::common::Logger::warning ( const std::string &  message)

Log a Warning log entry.

These are only logged when LogLevel >= Warning. Calling is thread-safe.

Parameters
messageThe Warning log message.

Definition at line 120 of file logger.cpp.

References log(), and Warning.

Here is the call graph for this function:

Friends And Related Function Documentation

◆ Application

friend class Application
friend

Application::~Application() will destruct this singleton.

Definition at line 263 of file logger.hpp.

Field Documentation

◆ destinations_

uint8_t dodo::common::Logger::destinations_
protected

Destinations as bit flags.

Definition at line 233 of file logger.hpp.

Referenced by log(), and Logger().

◆ file_params_

FileParams dodo::common::Logger::file_params_
protected

Parameters for the file Destination.

Definition at line 248 of file logger.hpp.

Referenced by checkRotate(), getFileParams(), log(), and ~Logger().

◆ hostname_

std::string dodo::common::Logger::hostname_
protected

The hostname cached in the constructor.

Definition at line 243 of file logger.hpp.

Referenced by formatMessage(), and Logger().

◆ levels_

std::map<uint8_t,LogLevel> dodo::common::Logger::levels_
protected

Map Destination to LogLevel.

Definition at line 238 of file logger.hpp.

Referenced by getFileParams(), log(), and Logger().

◆ logger_

Logger * dodo::common::Logger::logger_ = nullptr
staticprotected

The singleton.

Definition at line 228 of file logger.hpp.

Referenced by getLogger(), initialize(), and dodo::common::Application::~Application().

◆ mutex_

threads::Mutex dodo::common::Logger::mutex_
staticprotected

threads::Mutex to serialize log writing.

Definition at line 223 of file logger.hpp.

Referenced by log(), Logger(), and ~Logger().

◆ rotate_throttle_counter_

size_t dodo::common::Logger::rotate_throttle_counter_
protected

Track when rotation was last checked.

Definition at line 258 of file logger.hpp.

Referenced by Logger().

◆ syslog_params_

SyslogParams dodo::common::Logger::syslog_params_
protected

Parameters for the syslog Destination.

Definition at line 253 of file logger.hpp.

Referenced by log(), and Logger().


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