From c37bd3e34f5840773949bb55848cda3af6688d20 Mon Sep 17 00:00:00 2001 From: littleblack111 Date: Wed, 5 Mar 2025 23:44:56 +0800 Subject: refactor: logging system --- src/log.hpp | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) (limited to 'src/log.hpp') diff --git a/src/log.hpp b/src/log.hpp index 1ee401e..a6c6ea2 100644 --- a/src/log.hpp +++ b/src/log.hpp @@ -1,12 +1,28 @@ #pragma once - -#include +#include "format.hpp" +#include +#include +#include #include -typedef enum { - LOG_LEVEL_DEBUG = 0, - LOG_LEVEL_INFO, - LOG_LEVEL_WARN, - LOG_LEVEL_ERROR, - LOG_LEVEL_FATAL, - LOG_LEVEL_MAX -} eLogLevel; + +enum eLogLevel : int8_t { + INFO = 0, + WARN, + ERROR, + FATAL +}; + +inline bool useColors = true; + +void log(eLogLevel level, std::string str); + +template +void log(eLogLevel level, std::format_string fmt, Args &&...args) { + const auto now = std::chrono::system_clock::now(); + const auto hms = std::chrono::hh_mm_ss{now - std::chrono::floor(now)}; + std::string timeStamp = std::format("[{}] ", hms); + + std::string msg = CFormatter::fmt(eFormatType::RAW, fmt, std::forward(args)...); + + log(level, timeStamp + msg); +} -- cgit v1.2.3