aboutsummaryrefslogtreecommitdiff
path: root/NorthstarDLL/include/spdlog/async.h
diff options
context:
space:
mode:
authorBobTheBob <32057864+BobTheBob9@users.noreply.github.com>2022-08-24 00:32:31 +0100
committerGitHub <noreply@github.com>2022-08-24 00:32:31 +0100
commitf9bc3c9d1834cb8bd5f872b749b057c33e8b0102 (patch)
treee96e2da0d95798dd42eddf644a82a74555db858f /NorthstarDLL/include/spdlog/async.h
parent812893d8219daa60f5b5b7fd22cbd6b175603399 (diff)
downloadNorthstarLauncher-f9bc3c9d1834cb8bd5f872b749b057c33e8b0102.tar.gz
NorthstarLauncher-f9bc3c9d1834cb8bd5f872b749b057c33e8b0102.zip
Adjust folder structure (#242)
* Adjust folder structure * change launcher directory name
Diffstat (limited to 'NorthstarDLL/include/spdlog/async.h')
-rw-r--r--NorthstarDLL/include/spdlog/async.h93
1 files changed, 93 insertions, 0 deletions
diff --git a/NorthstarDLL/include/spdlog/async.h b/NorthstarDLL/include/spdlog/async.h
new file mode 100644
index 00000000..f7956305
--- /dev/null
+++ b/NorthstarDLL/include/spdlog/async.h
@@ -0,0 +1,93 @@
+// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
+// Distributed under the MIT License (http://opensource.org/licenses/MIT)
+
+#pragma once
+
+//
+// Async logging using global thread pool
+// All loggers created here share same global thread pool.
+// Each log message is pushed to a queue along with a shared pointer to the
+// logger.
+// If a logger deleted while having pending messages in the queue, it's actual
+// destruction will defer
+// until all its messages are processed by the thread pool.
+// This is because each message in the queue holds a shared_ptr to the
+// originating logger.
+
+#include <spdlog/async_logger.h>
+#include <spdlog/details/registry.h>
+#include <spdlog/details/thread_pool.h>
+
+#include <memory>
+#include <mutex>
+#include <functional>
+
+namespace spdlog {
+
+namespace details {
+static const size_t default_async_q_size = 8192;
+}
+
+// async logger factory - creates async loggers backed with thread pool.
+// if a global thread pool doesn't already exist, create it with default queue
+// size of 8192 items and single thread.
+template<async_overflow_policy OverflowPolicy = async_overflow_policy::block>
+struct async_factory_impl
+{
+ template<typename Sink, typename... SinkArgs>
+ static std::shared_ptr<async_logger> create(std::string logger_name, SinkArgs &&...args)
+ {
+ auto &registry_inst = details::registry::instance();
+
+ // create global thread pool if not already exists..
+
+ auto &mutex = registry_inst.tp_mutex();
+ std::lock_guard<std::recursive_mutex> tp_lock(mutex);
+ auto tp = registry_inst.get_tp();
+ if (tp == nullptr)
+ {
+ tp = std::make_shared<details::thread_pool>(details::default_async_q_size, 1);
+ registry_inst.set_tp(tp);
+ }
+
+ auto sink = std::make_shared<Sink>(std::forward<SinkArgs>(args)...);
+ auto new_logger = std::make_shared<async_logger>(std::move(logger_name), std::move(sink), std::move(tp), OverflowPolicy);
+ registry_inst.initialize_logger(new_logger);
+ return new_logger;
+ }
+};
+
+using async_factory = async_factory_impl<async_overflow_policy::block>;
+using async_factory_nonblock = async_factory_impl<async_overflow_policy::overrun_oldest>;
+
+template<typename Sink, typename... SinkArgs>
+inline std::shared_ptr<spdlog::logger> create_async(std::string logger_name, SinkArgs &&...sink_args)
+{
+ return async_factory::create<Sink>(std::move(logger_name), std::forward<SinkArgs>(sink_args)...);
+}
+
+template<typename Sink, typename... SinkArgs>
+inline std::shared_ptr<spdlog::logger> create_async_nb(std::string logger_name, SinkArgs &&...sink_args)
+{
+ return async_factory_nonblock::create<Sink>(std::move(logger_name), std::forward<SinkArgs>(sink_args)...);
+}
+
+// set global thread pool.
+inline void init_thread_pool(size_t q_size, size_t thread_count, std::function<void()> on_thread_start)
+{
+ auto tp = std::make_shared<details::thread_pool>(q_size, thread_count, on_thread_start);
+ details::registry::instance().set_tp(std::move(tp));
+}
+
+// set global thread pool.
+inline void init_thread_pool(size_t q_size, size_t thread_count)
+{
+ init_thread_pool(q_size, thread_count, [] {});
+}
+
+// get the global thread pool.
+inline std::shared_ptr<spdlog::details::thread_pool> thread_pool()
+{
+ return details::registry::instance().get_tp();
+}
+} // namespace spdlog