From ae30603d48dd4815974abdaea0878456ee4cd922 Mon Sep 17 00:00:00 2001
From: Giovanni La Mura <giovanni.lamura@inaf.it>
Date: Wed, 13 Mar 2024 13:57:03 +0100
Subject: [PATCH] Introduce a Logger class

---
 src/include/logging.h   | 47 +++++++++++++++++++++++++++++++++++++++++
 src/libnptm/logging.cpp | 27 +++++++++++++++++++++++
 2 files changed, 74 insertions(+)
 create mode 100644 src/include/logging.h
 create mode 100644 src/libnptm/logging.cpp

diff --git a/src/include/logging.h b/src/include/logging.h
new file mode 100644
index 00000000..002e530a
--- /dev/null
+++ b/src/include/logging.h
@@ -0,0 +1,47 @@
+/* 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
diff --git a/src/libnptm/logging.cpp b/src/libnptm/logging.cpp
new file mode 100644
index 00000000..a1d0a648
--- /dev/null
+++ b/src/libnptm/logging.cpp
@@ -0,0 +1,27 @@
+/* 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);
+  }
+}
-- 
GitLab