aboutsummaryrefslogtreecommitdiff
path: root/src/openvic/utility/Logger.hpp
diff options
context:
space:
mode:
author Hop311 <Hop3114@gmail.com>2023-05-24 10:12:37 +0200
committer GitHub <noreply@github.com>2023-05-24 10:12:37 +0200
commit8710cb9b7967de2201b5c440593aab2df310f33d (patch)
tree60380b625c5e49ac535fd1208939511eb15ac5e3 /src/openvic/utility/Logger.hpp
parent35b038fe91d4f3c4b3ffdc4f48385ac3b2082664 (diff)
parent3daa1e17cdfa302b4436d7635ad3a7d1a6f4da1c (diff)
Merge pull request #6 from OpenVicProject/date-fix
Date fix + Logger queuing
Diffstat (limited to 'src/openvic/utility/Logger.hpp')
-rw-r--r--src/openvic/utility/Logger.hpp23
1 files changed, 15 insertions, 8 deletions
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 <functional>
+#include <queue>
#include <sstream>
#ifdef __cpp_lib_source_location
#include <source_location>
@@ -33,6 +34,7 @@ namespace OpenVic {
class Logger {
using log_func_t = std::function<void(std::string&&)>;
+ using log_queue_t = std::queue<std::string>;
#ifdef __cpp_lib_source_location
using source_location = std::source_location;
@@ -44,14 +46,18 @@ namespace OpenVic {
template<typename... Ts>
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>(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>(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 <typename... Ts> \
struct name { \
name(Ts&&... ts, source_location const& location = source_location::current()) { \
- log<Ts...>{ name##_func, std::forward<Ts>(ts)..., location }; \
+ log<Ts...>{ name##_func, name##_queue, std::forward<Ts>(ts)..., location }; \
} \
}; \
template <typename... Ts> \