Logger - lms-org/lms GitHub Wiki

Logging helps a user or developer to find out what the program does during runtime. Usually you are printing on the console or into a logging file. This is can be done via printf in C or std::cout in C++.

Then why do we need a logging framework? A logging framework can provide logging levels, filtering, formatted output (coloring, timestamps) and logging targets (terminal, file, socket). Therefore, use it instead of printf!

Overview

Each logging message consists of three parts: level, tag and message. The logging level describes the importance of the logging message. The tag is used for filtering. In modules the tag is always prepended by the module name.

The LMS logging framework uses the logging levels DEBUG, INFO, WARN and ERROR in this order.

Using the logger in modules

Each module provides it's own logger instance, named logger. You can use it initialize(), cycle() and deinitialize() but not in constructor and destructor.

Use the C++ stream pattern to print all kinds of values: std::string, int, long, double, short, char, char*...

// debug message
logger.debug() << "Debug message";

// info message with tag
logger.info("checkValue") << "This is a warning!";

// warning message combination of strings and numbers
int a = 5; float b = 7.3;
logger.warn() << "Current value: " << a << " Next value: " << b;

// multiline error message
logger.error() << "This is line No. " << 1 << std::endl
    << "This is the second line";

// similar to error() but calls perror() additionally
// can be useful if you use standard or linux APIs instead
// of calling the raw perror() function
logger.perror("Read from file") << "during file read";

Benchmark code blocks

You can measure the time a code blocks needs for executing by using time()/timeEnd() before and after the block. Both methods expect a string identifier that must be equals if both calls belong together meaning they enclose a block.

The following code will emit a DEBUG message containing the measured time (here it will be something around 1000000 us).

logger.time("example");

// do something time intensive (like a sleep)
sleep(1);

logger.timeEnd("example");

How to set logging filters via command line arguments

If you don't know yet, you can get all available command line arguments via

./lms --help

Do not show any logging messages on stdout

./lms --quiet

Log all messages to a given file

./lms --log-file out.log

The file out.log will be created in the build folder.

Filter by a minimum logging level

The logging filter will be installed after framework initialization.

# DEBUG messages will be ignored, INFO and above will be printed.
./lms --logging-threshold INFO

# If you don't set the --logging-min-level option
# all messages will be printed.
./lms

Allowed logging levels: ALL, DEBUG, INFO, WARN, ERROR, OFF.