From 3972c5f76483bf5f64bcf19685ee391c4d6b1a9b Mon Sep 17 00:00:00 2001 From: littleblack111 Date: Fri, 7 Mar 2025 17:55:15 +0800 Subject: refactor(format,log): better/simpler way to do them --- src/format.hpp | 90 ++++++++++++++++------------------------------------------ src/log.cpp | 26 ----------------- src/log.hpp | 29 ++++++++----------- 3 files changed, 36 insertions(+), 109 deletions(-) delete mode 100644 src/log.cpp (limited to 'src') diff --git a/src/format.hpp b/src/format.hpp index 2630b7f..6dc5e4d 100644 --- a/src/format.hpp +++ b/src/format.hpp @@ -2,75 +2,33 @@ #include #include -enum class eFormatType { - RAW, // No formatting at all - STANDARD, // Standard message format - SYSTEM, // System message format - ERROR, // Error message format - CHAT, // Chat message format (username: message) - INFO, // Info level log format - WARN, // Warning level log format - FATAL // Fatal level log format +enum eFormatType : int8_t { + NONE, + LOG, + WARN, + ERR, + SYS, + CHAT, }; -class CFormatter { - public: - template - static std::string fmt(eFormatType type, std::format_string fmt, Args &&...args, bool addNewline = true) { - std::string formatted = std::vformat(fmt.get(), std::make_format_args(args...)); - if (addNewline && !formatted.empty() && formatted.back() != '\n') - formatted += '\n'; - - switch (type) { - case eFormatType::RAW: - return formatted; - case eFormatType::STANDARD: - return "[MSG] " + formatted; - case eFormatType::SYSTEM: - return "[SYS] " + formatted; - case eFormatType::ERROR: - return "[ERR] " + formatted; - case eFormatType::CHAT: - return formatted; // handled by chatFmt - case eFormatType::INFO: - return "[INFO] " + formatted; - case eFormatType::WARN: - return "[WARN] " + formatted; - case eFormatType::FATAL: - return "[FATAL] " + formatted; - default: - return formatted; - } - } - - template - static std::string chatFmt(const std::string &username, std::format_string fmt, Args &&...args) { - std::string formatted = std::vformat(fmt.get(), std::make_format_args(args...)); - return std::format("{}: {}", username, formatted); - } -}; - -template -inline std::string fmtRaw(std::format_string fmt, Args &&...args, bool addNewline = true) { - return CFormatter::fmt(eFormatType::RAW, fmt, std::forward(args)..., addNewline); -} - -template -inline std::string fmtStd(std::format_string fmt, Args &&...args, bool addNewline = true) { - return CFormatter::fmt(eFormatType::STANDARD, fmt, std::forward(args)..., addNewline); -} - -template -inline std::string fmtSys(std::format_string fmt, Args &&...args, bool addNewline = true) { - return CFormatter::fmt(eFormatType::SYSTEM, fmt, std::forward(args)..., addNewline); -} - -template -inline std::string fmtErr(std::format_string fmt, Args &&...args, bool addNewline = true) { - return CFormatter::fmt(eFormatType::ERROR, fmt, std::forward(args)..., addNewline); +namespace NFormatter { +inline std::string fmt(eFormatType type, std::string str) { + std::string prefix; + switch (type) { + case LOG: prefix = "[LOG] "; break; + case WARN: prefix = "[WARN] "; break; + case ERR: prefix = "[ERROR] "; break; + case SYS: prefix = "[SYSTEM] "; break; + case CHAT: prefix = "[CHAT] "; break; + default: prefix = ""; break; + } + if (!str.empty() && str.back() != '\n' && !prefix.empty()) + str += '\n'; + return prefix + str; } template -inline std::string fmtChat(const std::string &username, std::format_string fmt, Args &&...args) { - return CFormatter::chatFmt(username, fmt, std::forward(args)...); +std::string fmt(eFormatType type, std::format_string fmt, Args &&...args) { + return NFormatter::fmt(type, std::format(fmt, std::forward(args)...)); } +} // namespace NFormatter diff --git a/src/log.cpp b/src/log.cpp deleted file mode 100644 index 8cd3ded..0000000 --- a/src/log.cpp +++ /dev/null @@ -1,26 +0,0 @@ -#include "log.hpp" - -void log(eLogLevel level, std::string str) { - // Use CFormatter for the prefix part - std::string prefixed; - switch (level) { - case INFO: - prefixed = CFormatter::fmt(eFormatType::STANDARD, "{}", str); - str = useColors ? "\033[1;32m" + prefixed + "\033[0m" : prefixed; // green - break; - case WARN: - prefixed = CFormatter::fmt(eFormatType::SYSTEM, "{}", str); - str = useColors ? "\033[1;33m" + prefixed + "\033[0m" : prefixed; // yellow - break; - case ERROR: - prefixed = CFormatter::fmt(eFormatType::ERROR, "{}", str); - str = useColors ? "\033[1;31m" + prefixed + "\033[0m" : prefixed; // red - break; - case FATAL: - prefixed = CFormatter::fmt(eFormatType::ERROR, "{}", str); - str = useColors ? "\033[1;35m" + prefixed + "\033[0m" : prefixed; // magenta - break; - } - - std::println("{}", str); // Changed to print since we're managing the newline ourselves -} diff --git a/src/log.hpp b/src/log.hpp index a6c6ea2..8cfc917 100644 --- a/src/log.hpp +++ b/src/log.hpp @@ -1,28 +1,23 @@ #pragma once #include "format.hpp" -#include #include #include #include -enum eLogLevel : int8_t { - INFO = 0, - WARN, - ERROR, - FATAL -}; - -inline bool useColors = true; +inline void log(FILE* _stream, eFormatType type, std::string str) { + std::println(_stream, "{}", NFormatter::fmt(type, str)); +} -void log(eLogLevel level, std::string str); +inline void log(eFormatType type, std::string str) { + log(stdout, type, 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)...); +void log(FILE* _stream, eFormatType type, std::format_string fmt, Args &&...args) { + log(_stream, type, std::format(fmt, std::forward(args)...)); +} - log(level, timeStamp + msg); +template +void log(eFormatType type, std::format_string fmt, Args &&...args) { + log(stdout, type, std::format(fmt, std::forward(args)...)); } -- cgit v1.2.3