Skip to content

Logging Messages

Filip Dutescu edited this page Mar 27, 2020 · 17 revisions

There are 2 ways of logging messages using SBLogger:

  1. Through method calls, as seen in the Logger Methods section,
  2. Through predefined macros, as seen in the Logger Predefined Macros section

All of those methods allow for the usage of various defined placeholders, with some of those being exclusive to one of the methods (more in the Placeholders section, further below).

Each of the loggers ensure thread-safety when writing to streams or manipulating them. Any of the following methods enforces compliance with this behaviour.

Logger Methods

All loggers have the following general methods for printing and formatting messages (inherited from sblogger::Logger):

  • void Write(...) - write the message const std::string& message after replacing all placeholders with the respective parameter value (ex.: "{0}" will be changed to the value of the first parameter after the string)
  • void WriteLine([...]) - write the message const std::string& message after replacing all placeholders with the respective parameter value (ex.: "{0}" will be changed to the value of the first parameter after the string) and append the newline character (system dependent, define system macros for proper support, check the Cross-Platform Info)
  • void Trace(...) - same as the sblogger::Write(...) method previously mentioned, with the addition that output from the call will only appear if the log level (at the time of the call) is at most sblogger::LogLevel::TRACE
  • void Debug(...) - same as the sblogger::Write(...) method, with the addition that output from the call will only appear if the log level (at the time of the call) is at most sblogger::LogLevel::DEBUG
  • void Info(...) - same as the sblogger::Write(...) method, with the addition that output from the call will only appear if the log level (at the time of the call) is at most sblogger::LogLevel::INFO
  • void Warn(...) - same as the sblogger::Write(...) method, with the addition that output from the call will only appear if the log level (at the time of the call) is at most sblogger::LogLevel::WARN
  • void Error(...) - same as the sblogger::Write(...) method, with the addition that output from the call will only appear if the log level (at the time of the call) is at most sblogger::LogLevel::ERROR
  • void Critical(...) - same as the sblogger::Write(...) method, with the addition that output from the call will only appear if the log level (at the time of the call) is at most sblogger::LogLevel::CRITICAL
  • int Indent()/int Dedent() - increase/decrease indent by 1
  • void Flush() - flushes the stream

Note: sblogger::Write and sblogger::WriteLine methods use a logging level of sblogger::LogLevel::TRACE.

Note: In order to set the logging level, you can do it either at compile or at run time. More information concerning them can be found either in the Default Log Level.

sblogger::StreamLogger contains an additional method:

  • void SetStreamType(sblogger::StreamType streamType) - change the current stream type to a different sblogger::StreamType

sblogger::FileLogger (and thus sblogger::DailyLogger as well) also contains an additional method:

  • void ClearLogs() - removes all content from the log file

Logging Objects

In order to log entire objects sent as parameters, one needs to override operator<< between the desired class and std::ostream. After that, instances can be passed as parameters to any of the aforementioned methods, as follows:

...
#include "SmallBetterLogger.h"
...
#include <iostream>
...
class MyClass
{
...
};
std::ostream& operator<<(std::ostream& out, const MyClass& myObj)
{
  // Send desired info to 'out', i.e.:
  // out << myObj.GetMyData();
  ...

  return out;
}

int main()
{
  ...
  MyClass myObj();
  ...
  sblogger::StreamLogger logger();
  ...
  logger.WriteLine("{0}", myObj);
  ...
}

Output:

# Assuming the operator added "This is my object" as data to std::ostream
This is my object

Logger Predefined Macros

For all the aforementioned methods of logging there are also compile-time ways of outputting messages to streams. There are predefined macros for each method, with the exception of sblogger::Indent, sblogger::Dedent and sblogger::Flush (and each logger's specific methods).

  • SBLOGGER_WRITE(x, ...) - write the message const std::string& message after replacing all placeholders with the respective parameter value (ex.: "{0}" will be changed to the value of the first parameter after the string)
  • SBLOGGER_WRITELINE(x, ...) - same as SBLOGGER_WRITE(x, ...), but appends the newline character (system dependent, define system macros for proper support, check the Cross-Platform Info)
  • SBLOGGER_TRACE(x, ...) - same as the SBLOGGER_WRITE(x, ...) method previously mentioned, with the addition that output from the call will only appear if the log level (at the time of the call) is at most sblogger::LogLevel::TRACE
  • SBLOGGER_DEBUG(x, ...) - same as the SBLOGGER_WRITE(x, ...) method previously mentioned, with the addition that output from the call will only appear if the log level (at the time of the call) is at most sblogger::LogLevel::DEBUG
  • SBLOGGER_INFO(x, ...) - same as the SBLOGGER_WRITE(x, ...) method previously mentioned, with the addition that output from the call will only appear if the log level (at the time of the call) is at most sblogger::LogLevel::INFO
  • SBLOGGER_WARN(x, ...) - same as the SBLOGGER_WRITE(x, ...) method previously mentioned, with the addition that output from the call will only appear if the log level (at the time of the call) is at most sblogger::LogLevel::WARN
  • SBLOGGER_ERROR(x, ...) - same as the SBLOGGER_WRITE(x, ...) method previously mentioned, with the addition that output from the call will only appear if the log level (at the time of the call) is at most sblogger::LogLevel::ERROR
  • SBLOGGER_CRITICAL(x, ...) - same as the SBLOGGER_WRITE(x, ...) method previously mentioned, with the addition that output from the call will only appear if the log level (at the time of the call) is at most sblogger::LogLevel::CRITICAL

Note: The x found in the macro parameters denotes the message that would be passed in the method calls, shown previously. In the case of the predefined macros it is mandatory to have it.

Note: The predefined macros also expose placeholders for the file, line and function information. Please check the Placeholders section for more info related to placeholders.

Another important aspect concerning this way of logging is that those macros are available based on whether or not the SBLOGGER_LOG_LEVEL macro is defined (which is defined and initialized by default with the SBLOGGER_LEVEL_TRACE value).


Placeholders

SBLogger provides a good amount of placeholders at your disposal, in order to allow easier creation and formatting of logs. They vary from date related placeholders (such as for the current date or time) to colour placeholders (which allow you to add colours for sblogger::StreamLogger generated output). Below you can find a short summary of the most relevant of those placeholders:

Placeholder Meaning Other
{i} Replace with the value of the ith parameter from the method call If parameter i does not exist, the placeholder won't be replaced
%[^]tr Replace with the string "Trace" Using ^ (%^tr) will use the string "TRACE"
%[^]dbg Replace with the string "Debug" Using ^ (%^dbg) will use the string "DEBUG"
%[^]inf Replace with the string "Info" Using ^ (%^inf) will use the string "INFO"
%[^]wn Replace with the string "Warn" Using ^ (%^wn) will use the string "WARN"
%[^]er Replace with the string "Error" Using ^ (%^er) will use the string "ERROR"
%[^]crt Replace with the string "Critical" Using ^ (%^crt) will use the string "CRITICAL"
%[^]lvl Replace with the aforementioned string that corresponds to the current level Using ^ (%^lvl) will use the uppercase string
%src Replace with the name of the current file Only works using the predefined macros for logging
%fsrc Replace with the complete path and name of the current file Only works using the predefined macros for logging
%ln Replace with the current line of the file that the call is made in Only works using the predefined macros for logging
%fnc Replace with the name of the current function (no return type params. etc.) Only works using the predefined macros for logging
Time & Date strftime or std::chrono::format i.e.: F% T% gives 2020-03-26 00:00:00
%{reset} Resets the STDOUT/STDERR/STDLOG colour to the original one Only works using the sblogger::StreamLogger, with colours enabled
%{black} Set STDOUT/STDERR/STDLOG foreground colour to black Only works using the sblogger::StreamLogger, with colours enabled
%{red} Set STDOUT/STDERR/STDLOG foreground colour to red Only works using the sblogger::StreamLogger, with colours enabled
%{green} Set STDOUT/STDERR/STDLOG foreground colour to green Only works using the sblogger::StreamLogger, with colours enabled
%{yellow} Set STDOUT/STDERR/STDLOG foreground colour to yellow Only works using the sblogger::StreamLogger, with colours enabled
%{blue} Set STDOUT/STDERR/STDLOG foreground colour to blue Only works using the sblogger::StreamLogger, with colours enabled
%{magenta} Set STDOUT/STDERR/STDLOG foreground colour to magenta Only works using the sblogger::StreamLogger, with colours enabled
%{cyan} Set STDOUT/STDERR/STDLOG foreground colour to cyan Only works using the sblogger::StreamLogger, with colours enabled
%{white} Set STDOUT/STDERR/STDLOG foreground colour to white Only works using the sblogger::StreamLogger, with colours enabled
%{bg-black} Set STDOUT/STDERR/STDLOG background colour to black Only works using the sblogger::StreamLogger, with colours enabled
%{bg-red} Set STDOUT/STDERR/STDLOG background colour to red Only works using the sblogger::StreamLogger, with colours enabled
%{bg-green} Set STDOUT/STDERR/STDLOG background colour to green Only works using the sblogger::StreamLogger, with colours enabled
%{bg-yellow} Set STDOUT/STDERR/STDLOG background colour to yellow Only works using the sblogger::StreamLogger, with colours enabled
%{bg-blue} Set STDOUT/STDERR/STDLOG background colour to blue Only works using the sblogger::StreamLogger, with colours enabled
%{bg-magenta} Set STDOUT/STDERR/STDLOG background colour to magenta Only works using the sblogger::StreamLogger, with colours enabled
%{bg-cyan} Set STDOUT/STDERR/STDLOG background colour to cyan Only works using the sblogger::StreamLogger, with colours enabled
%{bg-white} Set STDOUT/STDERR/STDLOG background colour to white Only works using the sblogger::StreamLogger, with colours enabled

**Note: ** Check ANSI Escape Codes for 3/4 Bit Colors for the actual values of the colours defined about.