ealogger
ealogger is a c++ library that provides a blazing fast and easy to use logging mechanism
|
ealogger main class More...
#include <ealogger.h>
Public Member Functions | |
Logger (bool async=true) | |
Logger constructor. More... | |
void | write_log (std::string msg, ealogger::constants::LOG_LEVEL lvl, std::string file, int lnumber, std::string func) |
Write a log message. More... | |
void | write_log (std::string msg, ealogger::constants::LOG_LEVEL lvl) |
Write a log message. More... | |
void | init_syslog_sink (bool enabled=true, ealogger::constants::LOG_LEVEL min_lvl=ealogger::constants::LOG_LEVEL::EAL_DEBUG, std::string msg_template="%s: %m", std::string datetime_pattern="%F %T") |
Init a syslog Sink. More... | |
void | init_console_sink (bool enabled=true, ealogger::constants::LOG_LEVEL min_lvl=ealogger::constants::LOG_LEVEL::EAL_DEBUG, std::string msg_template="%d %s: %m", std::string datetime_pattern="%F %T") |
Initialize the console Sink. More... | |
void | init_file_sink (bool enabled=true, ealogger::constants::LOG_LEVEL min_lvl=ealogger::constants::LOG_LEVEL::EAL_DEBUG, std::string msg_template="%d %s [%f:%l] %m", std::string datetime_pattern="%F %T", std::string logfile="ealogger_logfile.log", bool flush_buffer=false) |
Initialize the simple file Sink. More... | |
void | discard_sink (ealogger::constants::LOGGER_SINK sink) |
Discard a Sink and delete the object. More... | |
bool | is_initialized (ealogger::constants::LOGGER_SINK sink) |
Check if a Sink has been initialized already. More... | |
void | set_msg_template (ealogger::constants::LOGGER_SINK sink, std::string msg_template) |
Set message template for a Sink. More... | |
void | set_datetime_pattern (ealogger::constants::LOGGER_SINK sink, std::string datetime_pattern) |
Set datetime conversion pattern for a Sink. More... | |
void | set_enabled (ealogger::constants::LOGGER_SINK sink, bool enabled) |
Activate or deactivate a Sink. More... | |
void | set_min_lvl (ealogger::constants::LOGGER_SINK sink, ealogger::constants::LOG_LEVEL min_level) |
Set the minimum log message severity for a Sink. More... | |
bool | queue_empty () |
Check if the message queue is empty. More... | |
ealogger main class
The Logger class provides all the functionality you need for your application to log messages. It can be as simple as in the following example.
This will print My application is awesome to the console.
ealogger uses sinks to write to different targets. Each Sink supports different message conversion patterns and a datetime pattern. Each sink can be enabled or disabled independently and the minimum severity can be set. You have to use Logger::init_console_* to init a sink. This methods define sane defaults for their options. But of course you can change them to whatever you want.
The Methods set_msg_template, set_datetime_pattern, set_enabled and set_min_lvl allow you to change the configuration of a sink. If the sink supports more options you have to use the dedicated init method to reinitialize the sink.
To make it easy to write messages with a specific severity there are some macro functions for each log level and one for stacktrace (eal_debug(msg) eal_info(msg) eal_warn(msg) eal_error(msg) eal_fatal(msg) eal_stack()) Logger::write_log allows you to write log messages without using these macros.
ealogger and its sinks are threadsafe. Meaning if you use the same instance all over your application it will make sure only one message at a time is written to an iostream for example and the internal message queue is synchronized.
ealogger::Logger::Logger | ( | bool | async = true | ) |
Logger constructor.
async | Boolean if activated ealogger uses a background thread to write messages to a Sink |
Use the Parameter async to activate a background logger thread. This way logging will no longer slow down your application which is important for high performance or time critical events. The only overhead is creating a LogMessage object and pushing it on a std::queue.
void ealogger::Logger::discard_sink | ( | ealogger::constants::LOGGER_SINK | sink | ) |
void ealogger::Logger::init_console_sink | ( | bool | enabled = true , |
ealogger::constants::LOG_LEVEL | min_lvl = ealogger::constants::LOG_LEVEL::EAL_DEBUG , |
||
std::string | msg_template = "%d %s: %m" , |
||
std::string | datetime_pattern = "%F %T" |
||
) |
Initialize the console Sink.
enabled | Choose whether this sink is enabled or not |
min_lvl | Minimum severity for this sink |
msg_template | Message template based on conversion patterns |
datetime_pattern | Datetime conversion patterns |
Initializes a sink to write a message to the console. The layout of the message is based on msg_template
.
void ealogger::Logger::init_file_sink | ( | bool | enabled = true , |
ealogger::constants::LOG_LEVEL | min_lvl = ealogger::constants::LOG_LEVEL::EAL_DEBUG , |
||
std::string | msg_template = "%d %s [%f:%l] %m" , |
||
std::string | datetime_pattern = "%F %T" , |
||
std::string | logfile = "ealogger_logfile.log" , |
||
bool | flush_buffer = false |
||
) |
Initialize the simple file Sink.
enabled | Choose whether this sink is enabled or not |
min_lvl | Minimum severity for this sink |
msg_template | Message template based on conversion patterns |
datetime_pattern | Datetime conversion patterns |
logfile | Logfile to use |
flush_buffer | Flush the ofstream buffer with every message |
This method initializes a file sink. Using a file sink you can write to a logfile that was specified with logfile
. The file will be created if it does not exist otherwise new messages will be appended.
void ealogger::Logger::init_syslog_sink | ( | bool | enabled = true , |
ealogger::constants::LOG_LEVEL | min_lvl = ealogger::constants::LOG_LEVEL::EAL_DEBUG , |
||
std::string | msg_template = "%s: %m" , |
||
std::string | datetime_pattern = "%F %T" |
||
) |
Init a syslog Sink.
enabled | Choose whether this sink is enabled or not |
min_lvl | Minimum severity for this sink |
msg_template | Message template based on conversion patterns |
datetime_pattern | Datetime conversion patterns |
This initializes a sink that allows you to write a message to the system syslog.
bool ealogger::Logger::is_initialized | ( | ealogger::constants::LOGGER_SINK | sink | ) |
bool ealogger::Logger::queue_empty | ( | ) |
Check if the message queue is empty.
void ealogger::Logger::set_datetime_pattern | ( | ealogger::constants::LOGGER_SINK | sink, |
std::string | datetime_pattern | ||
) |
Set datetime conversion pattern for a Sink.
sink | |
datetime_pattern |
Date time conversion is based on std::strftime You can use all the conversion specifiers mentioned there.
void ealogger::Logger::set_enabled | ( | ealogger::constants::LOGGER_SINK | sink, |
bool | enabled | ||
) |
Activate or deactivate a Sink.
sink | |
enabled |
void ealogger::Logger::set_min_lvl | ( | ealogger::constants::LOGGER_SINK | sink, |
ealogger::constants::LOG_LEVEL | min_level | ||
) |
Set the minimum log message severity for a Sink.
sink | |
min_level |
void ealogger::Logger::set_msg_template | ( | ealogger::constants::LOGGER_SINK | sink, |
std::string | msg_template | ||
) |
Set message template for a Sink.
sink | |
msg_template | Template consisting of conversion patterns |
You can use conversion patterns and arbitrary strings to create a message template for a Sink.
void ealogger::Logger::write_log | ( | std::string | msg, |
ealogger::constants::LOG_LEVEL | lvl, | ||
std::string | file, | ||
int | lnumber, | ||
std::string | func | ||
) |
Write a log message.
msg | Message text |
lvl | Severity of the message |
file | File from where the method was called |
lnumber | Line number |
func | Function name |
This method is called by the macros that are defined in this header file for the different log levels. You can of course call this method yourself
void ealogger::Logger::write_log | ( | std::string | msg, |
ealogger::constants::LOG_LEVEL | lvl | ||
) |
Write a log message.
msg | Message text |
lvl | Severity of the message |