Commit ae30603d authored by Giovanni La Mura's avatar Giovanni La Mura
Browse files

Introduce a Logger class

parent fa52eab5
Loading
Loading
Loading
Loading

src/include/logging.h

0 → 100644
+47 −0
Original line number Diff line number Diff line
/* Distributed under the terms of GPLv3 or later. See COPYING for details. */

/*! \file logging.h
 *
 * \brief Definition of the logging system.
 */
#ifndef INCLUDE_LOGGING_H_
#define INCLUDE_LOGGING_H_

#define LOG_DEBG 0
#define LOG_INFO 1
#define LOG_WARN 2
#define LOG_ERRO 3

/*! \brief Logger class.
 *
 * Loggers are objects used to track the execution of a code, reporting activities
 * such as function calls and parameter settings. They can be used to inform the
 * user about the execution of operations at runtime (e.g. by printing messages to
 * 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.
 */
class Logger {
 protected:
  FILE *log_output;
  FILE *err_output;
  int log_threshold;

 public:
  /*! \brief Logger instance constructor.
   *
   * \param threshold: `int` Threshold of the messages to be included in log. Can
   * 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
   * (optional, default is `stdout`).
   * \param error_output: `FILE *` Pointer to an output file for error messages
   * (optional, default is `stderr`).
   */
  Logger(int threshold, FILE *logging_output=std::stdout, FILE *error_output=std::stderr);

  log(std::string message, int level);
};

#endif
+27 −0
Original line number Diff line number Diff line
/* Distributed under the terms of GPLv3 or later. See COPYING for details. */

/*! \file logging.cpp
 *
 * \brief Implementation of the logging system.
 */
#include <cstdio>
#include <string>

#ifndef INCLUDE_LOGGING_H_
#include "../include/logging.h"
#endif

using namespace std;

Logger::Logger(int threshold, FILE *logging_output, FILE *error_output) {
  log_threshold = threshold;
  log_output = logging_output;
  err_output = error_output;
}

Logger::log(std::string message, int level) {
  if (level == LOG_ERRO) err(message);
  else {
    if (level >= log_threshold) fprintf(log_output, message);
  }
}