Skip to content
Snippets Groups Projects
Commit 99990106 authored by Giovanni La Mura's avatar Giovanni La Mura
Browse files

Save last_message as pointer to string

parent 4ac15ae9
No related branches found
No related tags found
No related merge requests found
......@@ -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.
......
......@@ -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++;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment