![]() |
Jazz 1.25.+
|
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. | |
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.
jazz_elements::Logger::Logger | ( | const char * | output_file_name | ) |
Initialize the Logger (Method 1: by directly giving it the output_file_name).
output_file_name | The name of the file to log to. |
Stores a copy of the file name, Calls InitLogger() for the rest of the initialization.
jazz_elements::Logger::Logger | ( | ConfigFile | config, |
const char * | config_key | ||
) |
Initialize the Logger (Method 2: with a configuration file and a configuration key).
config | The configuration file. |
config_key | The configuration key to be searched. |
Stores a copy of the file name, Calls InitLogger() for the rest of the initialization.
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())
int jazz_elements::Logger::get_output_file_name | ( | char * | buff, |
int | buff_size | ||
) |
Close the output file in the Logger.
buff | The configuration key to be searched. |
buff_size | Value to be returned only when the function returns true. |
void jazz_elements::Logger::log | ( | int | loglevel, |
const char * | message | ||
) |
Create a log event with a static message.
loglevel | The 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.
message | A message that can be up to 256 - LEFTAUTO characters (see source for details). If loglevel >= LOG_WARN, the output also goes to stderr. |
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.
loglevel | The trace level. (see Logger::log()) |
fmt | The 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. |
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.
loglevel | The trace level. (see Logger::log()) |
fmt | The printf-style format string. |
args | The 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. |
|
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())