diff options
| author | hachem <im@hachem.wtf> | 2026-08-20 01:57:44 +0200 |
|---|---|---|
| committer | hachem <im@hachem.wtf> | 2026-08-20 02:10:49 +0200 |
| commit | 7d39ee93a6d354e727c7534a18663f1d00d46b0f (patch) | |
| tree | a188023b61d14dcb859e08e6f8f8dd30df11780f /src/core/log.h | |
| parent | dce67620b95a9555c7db950196b22f1bb88a307b (diff) | |
refactor: trailing return types in core
Diffstat (limited to 'src/core/log.h')
| -rw-r--r-- | src/core/log.h | 63 |
1 files changed, 32 insertions, 31 deletions
diff --git a/src/core/log.h b/src/core/log.h index 1477226..db6b4b4 100644 --- a/src/core/log.h +++ b/src/core/log.h @@ -23,67 +23,70 @@ namespace Donut Fatal = 5 }; - class Logger + class Logger { public: - static void init(); - static void shutdown(); + Logger(); + ~Logger(); + + static auto init() -> void; + static auto shutdown() -> void; - static Ref<Logger> get_logger(); + static auto get_logger() -> Ref<Logger>; template<typename... Args> - static void trace(const std::string_view& format, const Args&... args) + static auto trace(const std::string_view& format, const Args&... args) -> void { auto logger = get_logger(); if (logger) logger->log_message(LogLevel::trace, format, args...); } template<typename... Args> - static void info(const std::string_view& format, const Args&... args) + static auto info(const std::string_view& format, const Args&... args) -> void { auto logger = get_logger(); if (logger) logger->log_message(LogLevel::Info, format, args...); } template<typename... Args> - static void warn(const std::string_view& format, const Args&... args) + static auto warn(const std::string_view& format, const Args&... args) -> void { auto logger = get_logger(); if (logger) logger->log_message(LogLevel::Warn, format, args...); } template<typename... Args> - static void error(const std::string_view& format, const Args&... args) + static auto error(const std::string_view& format, const Args&... args) -> void { auto logger = get_logger(); if (logger) logger->log_message(LogLevel::Err, format, args...); } template<typename... Args> - static void fatal(const std::string_view& format, const Args&... args) + static auto fatal(const std::string_view& format, const Args&... args) -> void { auto logger = get_logger(); if (logger) logger->log_message(LogLevel::Fatal, format, args...); } template<typename... Args> - void log_message(LogLevel level, const std::string_view& format, const Args&... args) + auto log_message(LogLevel level, const std::string_view& format, const Args&... args) -> void { - if (level < m_log_level) + if (level < m_log_level) return; std::lock_guard<std::mutex> lock(m_mutex); std::string message = format_string(format, args...); std::string full_message = get_time_stamp() + " [" + get_log_level_string(level) + "] " + message; - - if (m_console_output) + + if (m_console_output) { set_console_color(level); std::cout << full_message << std::endl; reset_console_color(); } - - if (m_file_output && m_log_file.is_open()) + + if (m_file_output && m_log_file.is_open()) { m_log_file << full_message << std::endl; m_log_file.flush(); @@ -91,44 +94,42 @@ namespace Donut } template<typename... Args> - std::string format_string(const std::string_view& format, const Args&... args) + auto format_string(const std::string_view& format, const Args&... args) -> std::string { std::string result = format.data(); std::vector<std::string> arg_strings = { to_string(args)... }; - + size_t arg_index = 0; size_t pos = 0; - + while ((pos = result.find("{}", pos)) != std::string::npos && arg_index < arg_strings.size()) { result.replace(pos, 2, arg_strings[arg_index]); pos += arg_strings[arg_index].length(); arg_index++; } - + return result; } template<typename T> - std::string to_string(const T& value) + auto to_string(const T& value) -> std::string { std::stringstream ss; ss << value; return ss.str(); } - void set_log_level(LogLevel level) { m_log_level = level; } - void enable_console_output(bool enable) { m_console_output = enable; } - void enable_file_output(bool enable) { m_file_output = enable; } - void set_log_file(const std::string& filename); - public: - Logger(); - ~Logger(); + auto set_log_level(LogLevel level) -> void { m_log_level = level; } + auto enable_console_output(bool enable) -> void { m_console_output = enable; } + auto enable_file_output(bool enable) -> void { m_file_output = enable; } + auto set_log_file(const std::string& filename) -> void; + + auto get_time_stamp() -> std::string; + auto get_log_level_string(LogLevel level) -> std::string; + auto set_console_color(LogLevel level) -> void; + auto reset_console_color() -> void; - std::string get_time_stamp(); - std::string get_log_level_string(LogLevel level); - void set_console_color(LogLevel level); - void reset_console_color(); private: LogLevel m_log_level; bool m_console_output; |
