diff options
| author | littleblack111 <littleblack11111@gmail.com> | 2025-03-07 17:55:15 +0800 |
|---|---|---|
| committer | littleblack111 <littleblack11111@gmail.com> | 2025-03-07 18:03:03 +0800 |
| commit | 3972c5f76483bf5f64bcf19685ee391c4d6b1a9b (patch) | |
| tree | db258338afafa632d3f51baed3665c3c1f1dbb05 /src/format.hpp | |
| parent | c37bd3e34f5840773949bb55848cda3af6688d20 (diff) | |
refactor(format,log): better/simpler way to do them
Diffstat (limited to 'src/format.hpp')
| -rw-r--r-- | src/format.hpp | 90 |
1 files changed, 24 insertions, 66 deletions
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 <format> #include <string> -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 <typename... Args> - static std::string fmt(eFormatType type, std::format_string<Args...> 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 <typename... Args> - static std::string chatFmt(const std::string &username, std::format_string<Args...> fmt, Args &&...args) { - std::string formatted = std::vformat(fmt.get(), std::make_format_args(args...)); - return std::format("{}: {}", username, formatted); - } -}; - -template <typename... Args> -inline std::string fmtRaw(std::format_string<Args...> fmt, Args &&...args, bool addNewline = true) { - return CFormatter::fmt(eFormatType::RAW, fmt, std::forward<Args>(args)..., addNewline); -} - -template <typename... Args> -inline std::string fmtStd(std::format_string<Args...> fmt, Args &&...args, bool addNewline = true) { - return CFormatter::fmt(eFormatType::STANDARD, fmt, std::forward<Args>(args)..., addNewline); -} - -template <typename... Args> -inline std::string fmtSys(std::format_string<Args...> fmt, Args &&...args, bool addNewline = true) { - return CFormatter::fmt(eFormatType::SYSTEM, fmt, std::forward<Args>(args)..., addNewline); -} - -template <typename... Args> -inline std::string fmtErr(std::format_string<Args...> fmt, Args &&...args, bool addNewline = true) { - return CFormatter::fmt(eFormatType::ERROR, fmt, std::forward<Args>(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 <typename... Args> -inline std::string fmtChat(const std::string &username, std::format_string<Args...> fmt, Args &&...args) { - return CFormatter::chatFmt(username, fmt, std::forward<Args>(args)...); +std::string fmt(eFormatType type, std::format_string<Args...> fmt, Args &&...args) { + return NFormatter::fmt(type, std::format(fmt, std::forward<Args>(args)...)); } +} // namespace NFormatter |
