From 3daa1e17cdfa302b4436d7635ad3a7d1a6f4da1c Mon Sep 17 00:00:00 2001 From: Hop311 Date: Wed, 24 May 2023 00:21:41 +0100 Subject: Date fix + Logger queuing --- src/openvic/utility/Logger.hpp | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) (limited to 'src/openvic/utility/Logger.hpp') diff --git a/src/openvic/utility/Logger.hpp b/src/openvic/utility/Logger.hpp index ac82445..417aba7 100644 --- a/src/openvic/utility/Logger.hpp +++ b/src/openvic/utility/Logger.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include #include #ifdef __cpp_lib_source_location #include @@ -33,6 +34,7 @@ namespace OpenVic { class Logger { using log_func_t = std::function; + using log_queue_t = std::queue; #ifdef __cpp_lib_source_location using source_location = std::source_location; @@ -44,14 +46,18 @@ namespace OpenVic { template struct log { - log(log_func_t log_func, Ts&&... ts, source_location const& location) { + log(log_func_t log_func, log_queue_t& log_queue, Ts&&... ts, source_location const& location) { + std::stringstream stream; + stream << "\n" << get_filename(location.file_name()) << "(" + << location.line() << ") `" << location.function_name() << "`: "; + ((stream << std::forward(ts)), ...); + stream << std::endl; + log_queue.push(stream.str()); if (log_func) { - std::stringstream stream; - stream << "\n" << get_filename(location.file_name()) << "(" - << location.line() << ") `" << location.function_name() << "`: "; - ((stream << std::forward(ts)), ...); - stream << std::endl; - log_func(stream.str()); + do { + log_func(std::move(log_queue.front())); + log_queue.pop(); + } while (!log_queue.empty()); } } }; @@ -59,6 +65,7 @@ namespace OpenVic { #define LOG_FUNC(name) \ private: \ static log_func_t name##_func; \ + static log_queue_t name##_queue; \ public: \ static void set_##name##_func(log_func_t log_func) { \ name##_func = log_func; \ @@ -66,7 +73,7 @@ namespace OpenVic { template \ struct name { \ name(Ts&&... ts, source_location const& location = source_location::current()) { \ - log{ name##_func, std::forward(ts)..., location }; \ + log{ name##_func, name##_queue, std::forward(ts)..., location }; \ } \ }; \ template \ -- cgit v1.2.3-56-ga3b1