Jazz 1.25.+
Loading...
Searching...
No Matches
Public Member Functions | Private Member Functions | Private Attributes
jazz_elements::Logger Class Reference

A simple logger. More...

#include <utils.h>

Public Member Functions

 Logger (const char *output_file_name)
 
 Logger (ConfigFile config, const char *config_key)
 
 ~Logger ()
 
int get_output_file_name (char *buff, int buff_size)
 
void log (int loglevel, const char *message)
 
void log_printf (int loglevel, const char *fmt,...)
 
void log_printf (int loglevel, const char *fmt, va_list args)
 

Private Member Functions

void InitLogger ()
 

Private Attributes

char file_name [MAX_FILENAME_LENGTH]
 The name of the log file.
 
std::ofstream f_stream
 The stream to the log file.
 
std::filebuf * f_buff
 The buffer for the stream.
 
TimePoint big_bang
 The time when the logger was created.
 

Detailed Description

A simple logger.

This objects logs events one line per event. It prefixes the time since the logger was created, the trace level and the thread id as in " 0.224036 : 02 : 2872 : jzzAPI started.". A printf style version supports printing variables using variadic arguments.

Constructor & Destructor Documentation

◆ Logger() [1/2]

jazz_elements::Logger::Logger ( const char *  output_file_name)

Initialize the Logger (Method 1: by directly giving it the output_file_name).

Parameters
output_file_nameThe name of the file to log to.

Stores a copy of the file name, Calls InitLogger() for the rest of the initialization.

◆ Logger() [2/2]

jazz_elements::Logger::Logger ( ConfigFile  config,
const char *  config_key 
)

Initialize the Logger (Method 2: with a configuration file and a configuration key).

Parameters
configThe configuration file.
config_keyThe configuration key to be searched.

Stores a copy of the file name, Calls InitLogger() for the rest of the initialization.

◆ ~Logger()

jazz_elements::Logger::~Logger ( )

Close the output file in the Logger.

.. and clears the file_name (that can be queried via get_output_file_name())

Member Function Documentation

◆ get_output_file_name()

int jazz_elements::Logger::get_output_file_name ( char *  buff,
int  buff_size 
)

Close the output file in the Logger.

Parameters
buffThe configuration key to be searched.
buff_sizeValue to be returned only when the function returns true.
Returns
Zero if opening the file failed, the length of the name instead.

◆ log()

void jazz_elements::Logger::log ( int  loglevel,
const char *  message 
)

Create a log event with a static message.

Parameters
loglevelThe trace level. One of:

LOG_DEBUG 1 - Just for checking, normally not logged, should not exist when NDEBUG. In that case, it becomes a LOG_WARN to force removing it. LOG_INFO 2 - A good, non trivial, non frequent event to discard trouble. E.g., "Jazz successfully installed host xxx", "backup completed ok." LOG_MISS 3 - A function returned an error status. This may still be normal. E.g., "configuration key xxx cannot be converted to integer." LOG_WARN 4 - A warning. More serious than the previous. Should not happen. It is desirable to treat the existence of a warning as a bug. LOG_ERROR 5 - Something known to be a requisite is failing. The program or task halts due to this.

Parameters
messageA message that can be up to 256 - LEFTAUTO characters (see source for details).
If loglevel >= LOG_WARN, the output also goes to stderr.

◆ log_printf() [1/2]

void jazz_elements::Logger::log_printf ( int  loglevel,
const char *  fmt,
  ... 
)

Create a log event with a printf style string including a variadic list of parameters.

Parameters
loglevelThe trace level. (see Logger::log())
fmtThe printf-style format string.
...The list of parameters as a variadic list of parameters.
It is a wrapper function calling Logger::log_printf(). (See Logger::log() for details.)

NOTE: This does not check buffer allocation! Use it for short results.

◆ log_printf() [2/2]

void jazz_elements::Logger::log_printf ( int  loglevel,
const char *  fmt,
va_list  args 
)

Create a log event with a printf style string including a variadic list of parameters.

Parameters
loglevelThe trace level. (see Logger::log())
fmtThe printf-style format string.
argsThe list of parameters as a va_list.
It is a wrapper function calling Logger::log(). (See Logger::log() for details.)

NOTE: This does not check buffer allocation! Use it for short results.

◆ InitLogger()

void jazz_elements::Logger::InitLogger ( )
private

Common part of the constructor has been emancipated as a private argument-less method to avoid repeating code.

Sets the stopwatch origin in big_bang. Tries to open the file .. .. if successful, logs out a new execution message with level LOG_INFO .. if failed, clears the file_name (that can be queried via get_output_file_name())


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