From 17df58bb2bc905090830a67fe25ba30addd313e4 Mon Sep 17 00:00:00 2001 From: Odysseas Georgoudis Date: Sat, 15 Jun 2024 11:38:49 +0300 Subject: [PATCH] Prevent multiple usage of FrontendOptions --- CHANGELOG.md | 2 ++ quill/include/quill/core/ThreadContextManager.h | 14 ++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 987e318b..1d65b567 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -70,6 +70,8 @@ - Fixed multiple definitions of `quill::detail::get_error_message` ([#469](https://github.com/odygrd/quill/issues/469)) - Fixed an issue causing a `SIGABRT` when creating directories with a symlink folder path using GCC versions 8 or 9 ([#468](https://github.com/odygrd/quill/issues/468)) +- Added an assertion to prevent the use of custom `FrontendOptions` together with + default `FrontendOptions` ([#453](https://github.com/odygrd/quill/issues/453)) ## v4.4.0 diff --git a/quill/include/quill/core/ThreadContextManager.h b/quill/include/quill/core/ThreadContextManager.h index 6a8671d9..8a5dac80 100644 --- a/quill/include/quill/core/ThreadContextManager.h +++ b/quill/include/quill/core/ThreadContextManager.h @@ -335,6 +335,20 @@ class ScopedThreadContext ScopedThreadContext(QueueType queue_type, uint32_t spsc_queue_capacity, bool huge_pages_enabled) : _thread_context(std::make_shared(queue_type, spsc_queue_capacity, huge_pages_enabled)) { +#ifndef NDEBUG + // Thread-local flag to track if an instance has been created for this thread. + // This ensures that get_local_thread_context() is not called with different template arguments + // when using custom FrontendOptions. Having multiple thread contexts in a single thread is fine + // and functional but goes against the design principle of maintaining a single thread context + // per thread. + thread_local bool thread_local_instance_created = false; + + assert(!thread_local_instance_created && + R"(ScopedThreadContext can only be instantiated once per thread. It appears you may be combining default FrontendOptions with custom FrontendOptions. Ensure only one set of FrontendOptions is used to maintain a single thread context per thread.)"); + + thread_local_instance_created = true; +#endif + ThreadContextManager::instance().register_thread_context(_thread_context); }