code review

This commit is contained in:
m-aXimilian
2025-06-14 16:55:45 +02:00
parent 2180f07ea9
commit 896ddfb720
2 changed files with 32 additions and 29 deletions
+30 -25
View File
@@ -1,13 +1,13 @@
#include "SpdLogger.hpp" #include "SpdLogger.hpp"
#include <spdlog/common.h> #include <spdlog/common.h>
#include <spdlog/spdlog.h>
#include <spdlog/sinks/basic_file_sink.h> #include <spdlog/sinks/basic_file_sink.h>
#include <memory> #include <spdlog/spdlog.h>
#include <sstream>
#include <string>
#include "ILog.hpp"
#include <memory>
#include <string>
#include "ILog.hpp"
using namespace pixelarium::utils::log; using namespace pixelarium::utils::log;
@@ -19,53 +19,58 @@ SpdLogger::SpdLogger(const std::string& file_sink, const std::string& name)
logger_->info("Logger initiated"); logger_->info("Logger initiated");
} }
void SpdLogger::Info(const std::string& msg) void SpdLogger::Info(const std::string& msg) { this->logger_->info(msg); }
{ void SpdLogger::Debug(const std::string& msg) { this->logger_->debug(msg); }
this->logger_->info(msg); void SpdLogger::Warn(const std::string& msg) { this->logger_->warn(msg); }
}
void SpdLogger::Debug(const std::string& msg)
{
this->logger_->debug(msg);
}
void SpdLogger::Warn(const std::string& msg)
{
this->logger_->warn(msg);
}
void SpdLogger::Error(const std::string& msg) { this->logger_->error(msg); } void SpdLogger::Error(const std::string& msg) { this->logger_->error(msg); }
void SpdLogger::ChangeLevel(LogLevel lvl) void SpdLogger::ChangeLevel(LogLevel lvl)
{ {
std::stringstream st{}; constexpr auto LogLevelToString = [](LogLevel l) -> const char*
st << std::format("with argument {}", static_cast<int>(lvl)); {
switch (l)
{
case LogLevel::Trace:
return "Trace";
case LogLevel::Debug:
return "Debug";
case LogLevel::Info:
return "Info";
case LogLevel::Warn:
return "Warn";
case LogLevel::Error:
return "Error";
default:
return "Not Found";
}
};
switch (lvl) switch (lvl)
{ {
case LogLevel::Trace: case LogLevel::Trace:
this->logger_->set_level(spdlog::level::trace); this->logger_->set_level(spdlog::level::trace);
spdlog::flush_on(spdlog::level::trace); spdlog::flush_on(spdlog::level::trace);
st << "Trace";
break; break;
case LogLevel::Info: case LogLevel::Info:
this->logger_->set_level(spdlog::level::info); this->logger_->set_level(spdlog::level::info);
spdlog::flush_on(spdlog::level::info); spdlog::flush_on(spdlog::level::info);
st << "Info";
break; break;
case LogLevel::Warn: case LogLevel::Warn:
this->logger_->set_level(spdlog::level::warn); this->logger_->set_level(spdlog::level::warn);
spdlog::flush_on(spdlog::level::warn); spdlog::flush_on(spdlog::level::warn);
st << "Warn";
break; break;
case LogLevel::Error: case LogLevel::Error:
this->logger_->set_level(spdlog::level::err); this->logger_->set_level(spdlog::level::err);
spdlog::flush_on(spdlog::level::err); spdlog::flush_on(spdlog::level::err);
st << "Error";
break; break;
case LogLevel::Debug: case LogLevel::Debug:
default: default:
this->logger_->set_level(spdlog::level::debug); this->logger_->set_level(spdlog::level::debug);
spdlog::flush_on(spdlog::level::debug); spdlog::flush_on(spdlog::level::debug);
st << "Debug";
} }
// you will only get this message for log levels <= info! I.e., not for error or warning. // you will only get this message for log levels <= info! I.e., not for error or warning.
this->logger_->info(std::format("{}: Changed log level {}", __FUNCTION__, st.str()).c_str()); this->logger_->info(
std::format("{}: Changed log level to {}({})", __FUNCTION__, LogLevelToString(lvl), static_cast<int>(lvl))
.c_str());
} }
+2 -4
View File
@@ -8,7 +8,5 @@
#define LOGLEVELSELECT "Log Level" #define LOGLEVELSELECT "Log Level"
#define SHOWIMGUIDEMOS "ImGui Demos" #define SHOWIMGUIDEMOS "ImGui Demos"
namespace inline constexpr const char* LOGLEVELS[] = {"Trace", "Debug", "Info", "Warning", "Error"};
{
const char* LOGLEVELS[] = {"Trace", "Debug", "Info", "Warning", "Error"};
}