How to enable logging within Modern C++ code

This article overviews how to enable logging within Modern C++ code.  

Logging options are illustrated via a simple example with hyperlinks to the relevant online documentation.

The available logging options include:

  • Logging Verbosity Level
  • Logging by Category
  • Logging Print Format
  • Logging to a single cumulative file or a set of files

When logging to a set of files,  the user has the option of applying various file set generation rules:

  • File Prefix
  • File Suffix
  • Maximum File Size
  • Maximum number of Files

Example:  

In this example:

  • Verbosity for all categories is initially set to STATUS_LOCAL
  • Verbosity for the COMMUNICATIONS category is changed to WARNING
  • Print Format is set to VERBOSE_TIMESTAMPED for all messages.
  • Log File Options (mutually exclusive):
    • No log file  
    • Cumulative log file   
    • File set   
// include the Logger’s header file
#include <rti/config/Logger.hpp>

// create a 'Logger' object
static rti::config::Logger& Logging = rti::config::Logger::instance();

// Specifying global logging Verbosity
Logging.verbosity(rti::config::Verbosity::STATUS_LOCAL);

// Specifying  logging by category   (optional)
Logging.verbosity_by_category(rti::config::LogCategory::COMMUNICATION, \ rti::config::Verbosity::WARNING);

// Specifying print format  (optional)
Logging.print_format(rti::config::PrintFormat::VERBOSE_TIMESTAMPED);

// Logging Option 1: No log file

// Logging Option 2: Specifying logging to a single file  
Logging.output_file("cummulative_logfile.log");

// Logging Option 3: Specifying logging to a fileset   
Logging.output_file_set("LogFile", ".log", 1000, 3);

...

Logging Option 1:    No log file specified

If no log file is specified,  the results are printed to the console (typically stdout).

Logging Option 2:   Logging to a single file

If a single log file is specified,   the named file grows without bounds and an existing file is overwritten.

Logging Option 3:  Logging to a set of files

In this example the four parameters are:

  • file_suffix = “LogFile
  • file_prefix = “.log”
  • max_byptes = 1000
  • max_files = 3

 If Option 3 is selected, the resultant file set could include LogFile1.log, LogFile2.log, and LogFile3.log.   Where each file is limited to 1000 bytes.

When the last file reaches the ‘max_bytes’ specific,  the logging operation will overwrite existing files (i.e. rollover takes place).  

Note:A Request for Future Enhancement (RFE) has been filed to allow the logging option to be halted if desired:

CORE-9849 Ability to stop logging when the final file is reached Logger's output_file_set

 
Product:
Programming Language:
Keywords: