From 99990106388b953c3fe39f4750d135046abfec97 Mon Sep 17 00:00:00 2001 From: Giovanni La Mura <giovanni.lamura@inaf.it> Date: Tue, 26 Mar 2024 15:44:59 +0100 Subject: [PATCH] Save last_message as pointer to string --- src/include/logging.h | 14 +++++++++++--- src/libnptm/logging.cpp | 26 ++++++++++++++++++++------ 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/src/include/logging.h b/src/include/logging.h index 9545af27..fac4d71d 100644 --- a/src/include/logging.h +++ b/src/include/logging.h @@ -27,6 +27,10 @@ * the terminal), as well as to record the execution history in appropriate log * files. The `Logger` class offers an implementation of logging system complying * with the requirements of the NP_TMcode project. + * + * The Logger class is designed to work with open files. It is a user responsibility + * to check that the required log files are properly opened before use, and closed + * thereafter, if they are not the standard `stdout` and `stderr` streams. */ class Logger { protected: @@ -35,7 +39,7 @@ class Logger { //! \brief Pointer to logging stream. FILE *log_output; //! \brief Last logged message. - std::string last_message; + std::string *last_message; //! \brief Threshold of logging level. int log_threshold; //! \brief Number of identical message repetitions. @@ -48,13 +52,17 @@ class Logger { * be `LOG_DEBG` (log everything), `LOG_INFO` (give detailed information), * `LOG_WARN` (log odd looking effects), or `LOG_ERRO` (print error messages, * `always active). The default behaviour is `LOG_WARN`. - * \param logging_output: `FILE *` Pointer to an output file for common messages + * \param logging_output: `FILE *` Pointer to an open output file for common messages * (optional, default is `stdout`). - * \param error_output: `FILE *` Pointer to an output file for error messages + * \param error_output: `FILE *` Pointer to an open output file for error messages * (optional, default is `stderr`). */ Logger(int threshold, FILE *logging_output=stdout, FILE *error_output=stderr); + /*! \brief Logger instance destroyer. + */ + ~Logger(); + /*! \brief Print a message to the error output. * * \param message: `string` The message to be printed. diff --git a/src/libnptm/logging.cpp b/src/libnptm/logging.cpp index f27bd256..67a586e4 100644 --- a/src/libnptm/logging.cpp +++ b/src/libnptm/logging.cpp @@ -14,42 +14,56 @@ using namespace std; Logger::Logger(int threshold, FILE *logging_output, FILE *error_output) { - last_message = ""; + last_message = new string(""); log_threshold = threshold; log_output = logging_output; err_output = error_output; repetitions = 0; } +Logger::~Logger() { + delete last_message; +} + void Logger::err(std::string message) { fprintf(err_output, "%s", message.c_str()); + fflush(err_output); } void Logger::flush(int level) { - string summary = "\"" + last_message + "\" issued " + to_string(repetitions); + string summary = "\"" + *last_message + "\" issued " + to_string(repetitions); if (repetitions == 1) summary += " time.\n"; else summary += " times.\n"; if (level == LOG_ERRO) err(summary); else { - if (level >= log_threshold) fprintf(log_output, "%s", summary.c_str()); + if (level >= log_threshold) { + fprintf(log_output, "%s", summary.c_str()); + fflush(log_output); + } } + delete last_message; + last_message = new string(""); repetitions = 0; } void Logger::log(std::string message, int level) { if (level == LOG_ERRO) err(message); else { - if (level >= log_threshold) fprintf(log_output, "%s", message.c_str()); + if (level >= log_threshold) { + fprintf(log_output, "%s", message.c_str()); + fflush(log_output); + } } } void Logger::push(std::string message) { if (repetitions > 0) { - if (message.compare(last_message) != 0) { + if (message.compare(*last_message) != 0) { flush(LOG_DEBG); } } log(message, LOG_DEBG); - last_message = message; + delete last_message; + last_message = new string(message); repetitions++; } -- GitLab