Refactor LogLevel handling: replace switch statements with constexpr mapping for improved maintainability and readability

This commit is contained in:
whaffman 2025-09-20 23:46:35 +02:00
parent df97596321
commit 8e0ae154b6
2 changed files with 35 additions and 37 deletions

View File

@ -32,7 +32,7 @@ IncludeCategories:
# Allow simple functions and if statements on one line # Allow simple functions and if statements on one line
AllowShortFunctionsOnASingleLine: Inline AllowShortFunctionsOnASingleLine: Inline
AllowShortIfStatementsOnASingleLine: WithoutElse AllowShortIfStatementsOnASingleLine: true
AllowShortLoopsOnASingleLine: true AllowShortLoopsOnASingleLine: true
AllowShortBlocksOnASingleLine: Empty AllowShortBlocksOnASingleLine: Empty
AllowShortCaseLabelsOnASingleLine: true AllowShortCaseLabelsOnASingleLine: true

View File

@ -1,7 +1,9 @@
#pragma once #pragma once
#include <array>
#include <cstdint> #include <cstdint>
#include <string> #include <string>
#include <string_view>
enum class LogLevel : uint8_t enum class LogLevel : uint8_t
{ {
@ -25,30 +27,43 @@ constexpr const char *ERROR_COLOR = "\033[31m"; // Red
constexpr const char *FATAL_COLOR = "\033[1;31m"; // Bold red constexpr const char *FATAL_COLOR = "\033[1;31m"; // Bold red
} // namespace LogColors } // namespace LogColors
// Constexpr bidirectional mapping
struct LogLevelMapping
{
LogLevel level;
std::string_view name;
const char *color;
};
constexpr std::array<LogLevelMapping, 6> LOG_LEVEL_MAP = {{
{ .level = LogLevel::LOGLVL_TRACE, .name = "TRACE", .color = LogColors::TRACE_COLOR },
{ .level = LogLevel::LOGLVL_DEBUG, .name = "DEBUG", .color = LogColors::DEBUG_COLOR },
{ .level = LogLevel::LOGLVL_INFO, .name = "INFO", .color = LogColors::INFO_COLOR },
{ .level = LogLevel::LOGLVL_WARN, .name = "WARN", .color = LogColors::WARN_COLOR },
{ .level = LogLevel::LOGLVL_ERROR, .name = "ERROR", .color = LogColors::ERROR_COLOR },
{ .level = LogLevel::LOGLVL_FATAL, .name = "FATAL", .color = LogColors::FATAL_COLOR }
}};
inline std::string logLevelToString(LogLevel level) inline std::string logLevelToString(LogLevel level)
{ {
switch (level) for (const auto &mapping : LOG_LEVEL_MAP)
{ {
case LogLevel::LOGLVL_TRACE: return "TRACE"; if (mapping.level == level)
case LogLevel::LOGLVL_DEBUG: return "DEBUG"; {
case LogLevel::LOGLVL_INFO: return "INFO"; return std::string(mapping.name);
case LogLevel::LOGLVL_WARN: return "WARN"; }
case LogLevel::LOGLVL_ERROR: return "ERROR";
case LogLevel::LOGLVL_FATAL: return "FATAL";
} }
return "UNKNOWN"; return "UNKNOWN";
} }
inline const char *logLevelToColor(LogLevel level) inline const char *logLevelToColor(LogLevel level)
{ {
switch (level) for (const auto &mapping : LOG_LEVEL_MAP)
{ {
case LogLevel::LOGLVL_TRACE: return LogColors::TRACE_COLOR; if (mapping.level == level)
case LogLevel::LOGLVL_DEBUG: return LogColors::DEBUG_COLOR; {
case LogLevel::LOGLVL_INFO: return LogColors::INFO_COLOR; return mapping.color;
case LogLevel::LOGLVL_WARN: return LogColors::WARN_COLOR; }
case LogLevel::LOGLVL_ERROR: return LogColors::ERROR_COLOR;
case LogLevel::LOGLVL_FATAL: return LogColors::FATAL_COLOR;
} }
return LogColors::RESET; return LogColors::RESET;
} }
@ -60,29 +75,12 @@ inline std::string logLevelToColoredString(LogLevel level)
inline LogLevel stringToLogLevel(const std::string &level) inline LogLevel stringToLogLevel(const std::string &level)
{ {
if (level == "TRACE") for (const auto &mapping : LOG_LEVEL_MAP)
{ {
return LogLevel::LOGLVL_TRACE; if (mapping.name == level)
{
return mapping.level;
} }
if (level == "DEBUG")
{
return LogLevel::LOGLVL_DEBUG;
}
if (level == "INFO")
{
return LogLevel::LOGLVL_INFO;
}
if (level == "WARN")
{
return LogLevel::LOGLVL_WARN;
}
if (level == "ERROR")
{
return LogLevel::LOGLVL_ERROR;
}
if (level == "FATAL")
{
return LogLevel::LOGLVL_FATAL;
} }
return LogLevel::LOGLVL_INFO; // Default fallback return LogLevel::LOGLVL_INFO; // Default fallback
} }