Add logger singleton

This commit is contained in:
Mario Hüttel 2024-01-21 21:48:29 +01:00
parent fc2744d7fa
commit b83f057e49
6 changed files with 68 additions and 26 deletions

View File

@ -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;
}
}
};

View File

@ -39,7 +39,7 @@ public:
bool check_command(bool print_status) const;
}
};
}

View File

@ -0,0 +1,31 @@
#ifndef _LOGGER_HPP_
#define _LOGGER_HPP_
#include <string>
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_ */

32
tprcc/src/logger.cpp Normal file
View File

@ -0,0 +1,32 @@
#include <tprcc/logger.hpp>
#include <iostream>
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;
}
}

View File

@ -1,3 +1,4 @@
#include <tprcc/logger.hpp>
#include <tpr/tpr-frontend.hpp>
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;
}

View File

@ -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;
}