From b83f057e49e8149a3fe9b91b58713b57769c617e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Sun, 21 Jan 2024 21:48:29 +0100 Subject: [PATCH] Add logger singleton --- tprcc/include/lang/temp-lang-frontend.hpp | 23 +--------------- tprcc/include/tpr/tpr-types.hpp | 2 +- tprcc/include/tprcc/logger.hpp | 31 ++++++++++++++++++++++ tprcc/src/logger.cpp | 32 +++++++++++++++++++++++ tprcc/src/tpr/tpr-frontend.cpp | 3 ++- tprcc/src/tpr/tpr-types.cpp | 3 +-- 6 files changed, 68 insertions(+), 26 deletions(-) create mode 100644 tprcc/include/tprcc/logger.hpp create mode 100644 tprcc/src/logger.cpp diff --git a/tprcc/include/lang/temp-lang-frontend.hpp b/tprcc/include/lang/temp-lang-frontend.hpp index ad990fc..d10d8da 100644 --- a/tprcc/include/lang/temp-lang-frontend.hpp +++ b/tprcc/include/lang/temp-lang-frontend.hpp @@ -9,14 +9,6 @@ class TempLangFrontend { public: - enum class LogLevel { - DEBUG, - INFO, - WARNING, - ERROR, - }; - - TempLangFrontend(const std::string &source_file) { m_source_file = source_file; @@ -32,20 +24,7 @@ public: protected: std::string m_source_file; - void log(LogLevel lvl, const std::string &message) const - { - switch (lvl) { - case LogLevel::ERROR: - std::cerr << "[ERR]" << message << std::endl; - break; - case LogLevel::WARNING: - std::cerr << "[WARN]" << message << std::endl; - break; - default: - std::cout << message << std::endl; - break; - } - } + }; diff --git a/tprcc/include/tpr/tpr-types.hpp b/tprcc/include/tpr/tpr-types.hpp index 320cbce..72b62cb 100644 --- a/tprcc/include/tpr/tpr-types.hpp +++ b/tprcc/include/tpr/tpr-types.hpp @@ -39,7 +39,7 @@ public: bool check_command(bool print_status) const; -} +}; } diff --git a/tprcc/include/tprcc/logger.hpp b/tprcc/include/tprcc/logger.hpp new file mode 100644 index 0000000..a5e25bd --- /dev/null +++ b/tprcc/include/tprcc/logger.hpp @@ -0,0 +1,31 @@ +#ifndef _LOGGER_HPP_ +#define _LOGGER_HPP_ + +#include + +enum class LogLevel { + DEBUG, + INFO, + WARNING, + ERROR, +}; + +class Logger { +public: + Logger(Logger &other) = delete; + void operator=(const Logger &) = delete; + + void log(LogLevel lvl, const std::string &message) const; + + static Logger *get_logger(); + +protected: + /* Create a proteceted instructure, to prevent construction outside of this class */ + Logger(); + + static Logger *logger_inst; + + +}; + +#endif /* _LOGGER_HPP_ */ \ No newline at end of file diff --git a/tprcc/src/logger.cpp b/tprcc/src/logger.cpp new file mode 100644 index 0000000..baf21d2 --- /dev/null +++ b/tprcc/src/logger.cpp @@ -0,0 +1,32 @@ +#include +#include + +Logger *Logger::logger_inst = nullptr; + +Logger::Logger() +{ + +} + +Logger *Logger::get_logger() { + if (logger_inst == nullptr) { + logger_inst = new Logger(); + } + + return logger_inst; +} + +void Logger::log(LogLevel lvl, const std::string &message) const +{ + switch (lvl) { + case LogLevel::ERROR: + std::cerr << "[ERR]" << message << std::endl; + break; + case LogLevel::WARNING: + std::cerr << "[WARN]" << message << std::endl; + break; + default: + std::cout << message << std::endl; + break; + } +} \ No newline at end of file diff --git a/tprcc/src/tpr/tpr-frontend.cpp b/tprcc/src/tpr/tpr-frontend.cpp index 7a3c7bd..d1fea71 100644 --- a/tprcc/src/tpr/tpr-frontend.cpp +++ b/tprcc/src/tpr/tpr-frontend.cpp @@ -1,3 +1,4 @@ +#include #include namespace tpr { @@ -11,7 +12,7 @@ int TprFrontend::analyze() std::ifstream input_stream(m_source_file); if (!input_stream.good()) { - log(TempLangFrontend::LogLevel::ERROR, "Cannot read input file " + m_source_file); + Logger::get_logger()->log(LogLevel::ERROR, "Cannot read input file " + m_source_file); return -1; } diff --git a/tprcc/src/tpr/tpr-types.cpp b/tprcc/src/tpr/tpr-types.cpp index 0de17e7..40b3d49 100644 --- a/tprcc/src/tpr/tpr-types.cpp +++ b/tprcc/src/tpr/tpr-types.cpp @@ -42,7 +42,6 @@ bool TprCommand::is_whole_number(float num) const return false; } - bool TprCommand::check_command(bool print_status) const { for (unsigned int i = 0; i < 11; i++) { @@ -56,7 +55,7 @@ bool TprCommand::check_command(bool print_status) const for (unsigned int param_idx = 0; i < m_parameters.size(); i++) { if (is_whole_number(m_parameters[param_idx]) ^ m_specs[i].param_is_whole_num[param_idx]) { if (print_status) { - std::cerr << "[WARN]" // TODO:::: Continue here!! + // TODO:::: Continue here!! } return false; }