elanthis / easylogger

Simple "good enough" C++ logging framework

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

easylogger

easylogger is a very simple C++ logging framework, designed to be "good enough" for most people. If it doesn't suit a particular application's needs, the application should try a more featureful logging framework, such as log4cxx.

installation

easylogger is distributed as a single header file, easylogger.h. No other libraries or packages need to be installed to use easylogger. A fairly complete STL implementation is required, however, along with a standards compliant C++ compiler.

usage

All classes reside in the easylogger namespace.

There is only one primary class that the user must be aware of, which is the Logger class. Objects of Logger are used along with a set of macros to perform all logging.

To create a new log, create a Logger object with a log name.

easylogger::Logger NETWORK("NETWORK");

You can then write message to the NETWORK log using one of LOG_TRACE, LOG_DEBUG, LOG_INFO, LOG_WARNING, LOG_ERROR, or LOG_FATAL.

LOG_TRACE(NETWORK, "Trace message to NETWORK log");
LOG_WARNING(NETWORK, "Warning message to NETWORK log");

You can inherit logs from each other, creating a hierarchy. Pass the parent log as the second argument to the Logger constructor.

easylogger::Logger CONNECT("NETWORK.CONNECT", NETWORK);

LOG_INFO(CONNECT, "Connection received");

Inherited logs will cause all messages to be sent to the parent log. This is useful for classification of logs, as well as allowing some logs to be directed to alternative output streams.

A Logger instance with no parent by default will log all messages to std::cerr. Logger instances with a parent have no associated stream by default.

You can set the associated stream on a Logger instance using the Logger::Stream(std::ostream&) method. Any std::ostream derivative is acceptable.

std::ofstream network_log("network.log");
NETWORK.Stream(network_log);

std::ofstream connect_log("connect.log");
CONNECT.Stream(connect_log);

With the above example, all network log messages will be written to network.log. Additionally, any logs on the CONNECT log will also be written to connect.log.

Each log has a minimum log level. Only messages of that level or higher will be processed. The default log level is INFO. This can be changed by using the Logger::Level(LogLevel) method, giving it one of LEVEL_TRACE, LEVEL_DEBUG, LEVEL_INFO, LEVEL_WARNING, LEVEL_ERROR, or LEVEL_FATAL.

NETWORK.Level(easylogger::LEVEL_DEBUG);

Logging any message with the level FATAL will cause the application to abort immediately via std::abort().

Finally, there is a set of assertion macros that can be used for checking invariants.

easylogger::Logger MAIN("MAIN");

EASY_ASSERT(MAIN, param > 42, "param must be more than 42");
ASSERT_EQ(MAIN, left, right, "left must be equal to right");
ASSERT_NE(MAIN, left, right, "left must not equal right");
ASSERT_TRUE(MAIN, param, "param must be true");
ASSERT_FALSE(MAIN, param, "param must be false");

About

Simple "good enough" C++ logging framework

License:MIT License


Languages

Language:C++ 100.0%