dodo
0.0.1
A C++ library to create containerized Linux services
|
#include <logger.hpp>
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 Logger * | getLogger () |
Get the Logger singleton - initialize() must have been called first. More... | |
static Logger * | initialize (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, LogLevel > | levels_ |
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 Logger * | logger_ = 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... | |
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.
Destination flags.
Definition at line 59 of file logger.hpp.
|
strong |
The level of a log entry.
Definition at line 46 of file logger.hpp.
|
protected |
Construct against a config.
config | The configuration to read from. |
Definition at line 46 of file logger.cpp.
References dodo::common::Config::config_dodo_common_logger_console_level, dodo::common::Config::config_dodo_common_logger_file, dodo::common::Config::config_dodo_common_logger_syslog, dodo::common::Config::config_dodo_common_logger_syslog_facility, dodo::common::Config::config_dodo_common_logger_syslog_level, destinations_, dodo::common::Config::exists(), dodo::common::Logger::SyslogParams::facility, dodo::common::Config::getConfig(), getFileParams(), dodo::common::Config::getValue(), hostname_, Info, levels_, mutex_, rotate_throttle_counter_, StringAsLogLevel(), syslog_params_, and throw_Exception.
Referenced by initialize().
|
protectedvirtual |
Destructor.
Definition at line 71 of file logger.cpp.
References dodo::common::Logger::FileParams::file, file_params_, and mutex_.
|
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().
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.
message | The Debug log message. |
Definition at line 132 of file logger.cpp.
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.
message | The Error log message. |
Definition at line 116 of file logger.cpp.
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.
message | The fatal log message. |
Definition at line 112 of file logger.cpp.
|
protected |
Format a LogLine.
level | The logLevel of the messsage. |
message | The log message. |
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().
|
protected |
Get FileParams from the Config into file_params_.
config | The Config to get the FileParams from. |
Definition at line 180 of file logger.cpp.
References dodo::common::Logger::FileParams::active_log, dodo::common::Config::config_dodo_common_logger_file_directory, dodo::common::Config::config_dodo_common_logger_file_level, dodo::common::Config::config_dodo_common_logger_file_max_file_trail, dodo::common::Config::config_dodo_common_logger_file_max_size_mib, dodo::common::Logger::FileParams::directory, dodo::common::directoryExists(), dodo::common::directoryWritable(), dodo::common::Config::exists(), dodo::common::Logger::FileParams::file, file_params_, dodo::common::Logger::FileParams::filesize, dodo::common::Config::getAppName(), dodo::common::Config::getConfig(), dodo::common::getFileSize(), dodo::common::Config::getValue(), levels_, dodo::common::Logger::FileParams::max_file_trail, dodo::common::Logger::FileParams::max_size_mib, StringAsLogLevel(), and throw_Exception.
Referenced by Logger().
|
static |
Get the Logger singleton - initialize() must have been called first.
config | The Config to use. |
Definition at line 83 of file logger.cpp.
References logger_, and throw_Exception.
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.
message | The Info log message. |
Definition at line 124 of file logger.cpp.
Initialize the Logger singleton.
config | The Config to use. |
Definition at line 77 of file logger.cpp.
References Logger(), logger_, and throw_Exception.
Referenced by dodo::common::Application::Application().
|
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.
level | The logLevel of the messsage. |
message | The 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().
|
static |
Return the LogLevel as a string (as used in console and file).
level | The LogLevel. |
acronym | If true, return a uppercase character triplet for use in log files. |
Definition at line 228 of file logger.cpp.
References Debug, Error, Fatal, Info, Statistics, Trace, and Warning.
Referenced by formatMessage(), and log().
|
protected |
Map a LogLevel to a syslog level.
level | The LogLevel to map. |
Definition at line 140 of file logger.cpp.
References Debug, Error, Fatal, Info, Statistics, Trace, and Warning.
Referenced by log().
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.
message | The Statistics log message. |
Definition at line 128 of file logger.cpp.
References log(), and Statistics.
|
static |
Return the string as a LogLevel.
Note this will default to Info if string is not recognized.
slevel | The LogLevel as a string. |
Definition at line 217 of file logger.cpp.
References Debug, Error, Fatal, Info, Statistics, Trace, and Warning.
Referenced by getFileParams(), and Logger().
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.
message | The Trace log message. |
Definition at line 136 of file logger.cpp.
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.
message | The Warning log message. |
Definition at line 120 of file logger.cpp.
References log(), and Warning.
|
friend |
Application::~Application() will destruct this singleton.
Definition at line 263 of file logger.hpp.
|
protected |
Destinations as bit flags.
Definition at line 233 of file logger.hpp.
|
protected |
Parameters for the file Destination.
Definition at line 248 of file logger.hpp.
Referenced by checkRotate(), getFileParams(), log(), and ~Logger().
|
protected |
The hostname cached in the constructor.
Definition at line 243 of file logger.hpp.
Referenced by formatMessage(), and Logger().
|
protected |
Map Destination to LogLevel.
Definition at line 238 of file logger.hpp.
Referenced by getFileParams(), log(), and Logger().
|
staticprotected |
The singleton.
Definition at line 228 of file logger.hpp.
Referenced by getLogger(), initialize(), and dodo::common::Application::~Application().
|
staticprotected |
threads::Mutex to serialize log writing.
Definition at line 223 of file logger.hpp.
|
protected |
Track when rotation was last checked.
Definition at line 258 of file logger.hpp.
Referenced by Logger().
|
protected |
Parameters for the syslog Destination.
Definition at line 253 of file logger.hpp.