summaryrefslogtreecommitdiff
path: root/src/format.hpp
diff options
context:
space:
mode:
authorlittleblack111 <littleblack11111@gmail.com>2025-03-05 23:44:56 +0800
committerlittleblack111 <littleblack11111@gmail.com>2025-03-07 18:03:03 +0800
commitc37bd3e34f5840773949bb55848cda3af6688d20 (patch)
tree1d31ea7c3cf8811ea294da9c094027a9e805dfcb /src/format.hpp
parentaa21e4ea0687fd0c8d333161f5ec31a580512e9e (diff)
refactor: logging system
Diffstat (limited to 'src/format.hpp')
-rw-r--r--src/format.hpp76
1 files changed, 76 insertions, 0 deletions
diff --git a/src/format.hpp b/src/format.hpp
new file mode 100644
index 0000000..2630b7f
--- /dev/null
+++ b/src/format.hpp
@@ -0,0 +1,76 @@
+#pragma once
+#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
+};
+
+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);
+}
+
+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)...);
+}