ealogger
ealogger is a c++ library that provides a blazing fast and easy to use logging mechanism
Public Member Functions | List of all members
ealogger::Logger Class Reference

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...
 

Detailed Description

ealogger main class

Author
Christian Rapp (crapp)

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.

int main() {
logger.eal_debug("My application is just awesome");
return 0;
}

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.

Constructor & Destructor Documentation

ealogger::Logger::Logger ( bool  async = true)

Logger constructor.

Parameters
asyncBoolean 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.

Member Function Documentation

void ealogger::Logger::discard_sink ( ealogger::constants::LOGGER_SINK  sink)

Discard a Sink and delete the object.

Parameters
sinkThe Sink you want to discard

You can discard a Sink. This will reduce some overhead and can speed up logging a little bit.

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.

Parameters
enabledChoose whether this sink is enabled or not
min_lvlMinimum severity for this sink
msg_templateMessage template based on conversion patterns
datetime_patternDatetime conversion patterns

Initializes a sink to write a message to the console. The layout of the message is based on msg_template.

See also
ConversionPattern and SinkConsole
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.

Parameters
enabledChoose whether this sink is enabled or not
min_lvlMinimum severity for this sink
msg_templateMessage template based on conversion patterns
datetime_patternDatetime conversion patterns
logfileLogfile to use
flush_bufferFlush 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.

Note
ealogger will not create any directories for you and you have to make sure the target location is writeable by the user that runs the application.
See also
ConversionPattern and SinkFile Logger::discard_sink
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.

Parameters
enabledChoose whether this sink is enabled or not
min_lvlMinimum severity for this sink
msg_templateMessage template based on conversion patterns
datetime_patternDatetime conversion patterns

This initializes a sink that allows you to write a message to the system syslog.

Note
This does only work on supported systems
See also
ConversionPattern and SinkSyslog
bool ealogger::Logger::is_initialized ( ealogger::constants::LOGGER_SINK  sink)

Check if a Sink has been initialized already.

Parameters
sinkThe Sink you want to check
Returns
True if the Sink has been initialized
bool ealogger::Logger::queue_empty ( )

Check if the message queue is empty.

Returns
True if the message is empty, otherwise false.
void ealogger::Logger::set_datetime_pattern ( ealogger::constants::LOGGER_SINK  sink,
std::string  datetime_pattern 
)

Set datetime conversion pattern for a Sink.

Parameters
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.

Parameters
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.

Parameters
sink
min_level
void ealogger::Logger::set_msg_template ( ealogger::constants::LOGGER_SINK  sink,
std::string  msg_template 
)

Set message template for a Sink.

Parameters
sink
msg_templateTemplate consisting of conversion patterns

You can use conversion patterns and arbitrary strings to create a message template for a Sink.

See also
ConversionPattern
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.

Parameters
msgMessage text
lvlSeverity of the message
fileFile from where the method was called
lnumberLine number
funcFunction 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

mylogger.write_log("This is a warning", ealogger::constants::LOG_LEVEL::EAL_WARNING,
__FILE__, __LINE__, __func__);
mylogger.write_log("This is a warning without line file and func",
void ealogger::Logger::write_log ( std::string  msg,
ealogger::constants::LOG_LEVEL  lvl 
)

Write a log message.

Parameters
msgMessage text
lvlSeverity of the message
Note
Using conversion patterns for source file, line number or function with this function will not give you the appropriate information as it is not available

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