From 0887c50c8fedc54d07cab6f9baafb14b5fee07c8 Mon Sep 17 00:00:00 2001 From: Jan Steemann Date: Wed, 2 Mar 2016 11:31:41 +0100 Subject: [PATCH] fixed logger initialization and shutdown --- lib/Basics/Logger.cpp | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/lib/Basics/Logger.cpp b/lib/Basics/Logger.cpp index e06e97ca40..02915ee61b 100644 --- a/lib/Basics/Logger.cpp +++ b/lib/Basics/Logger.cpp @@ -98,6 +98,12 @@ static std::atomic LoggingActive(false); static Mutex LogTopicNamesLock; static std::map LogTopicNames; +//////////////////////////////////////////////////////////////////////////////// +/// @brief mutex to initialize, flush and shutdown logging +//////////////////////////////////////////////////////////////////////////////// + +static Mutex InitializeMutex; + //////////////////////////////////////////////////////////////////////////////// /// @brief message container //////////////////////////////////////////////////////////////////////////////// @@ -1124,15 +1130,16 @@ std::vector Logger::bufferedEntries(LogLevel level, uint64_t start, /// @brief initializes the logging component //////////////////////////////////////////////////////////////////////////////// -static std::atomic IsInitialized(false); static std::atomic ShutdownInitalized(false); static std::unique_ptr LoggingThread; static void ShutdownLogging() { Logger::shutdown(true); } void Logger::initialize(bool threaded) { - if (!IsInitialized) { - IsInitialized = true; + MUTEX_LOCKER(locker, InitializeMutex); + + if (LoggingActive.load(std::memory_order_seq_cst)) { + return; } // logging is now active @@ -1159,27 +1166,22 @@ void Logger::initialize(bool threaded) { //////////////////////////////////////////////////////////////////////////////// void Logger::shutdown(bool clearBuffers) { - if (!IsInitialized) { + MUTEX_LOCKER(locker, InitializeMutex); + + if (!LoggingActive.load(std::memory_order_seq_cst)) { + // if logging not activated or already shutdown, then we can abort here return; } // logging is now inactive (this will terminate the logging thread) - LoggingActive.store(false, std::memory_order_relaxed); + LoggingActive.store(false, std::memory_order_seq_cst); // join with the logging thread if (ThreadedLogging) { - LoggingActive.store(false, std::memory_order_relaxed); - // ignore all errors for now as we cannot log them anywhere... LoggingThread->beginShutdown(); - for (size_t i = 0; i < 10; ++i) { - if (LoggingThread->isRunning()) { - break; - } - - usleep(100 * 1000); - } + LoggingThread.reset(); } // cleanup appenders @@ -1222,7 +1224,10 @@ void Logger::reopen() { //////////////////////////////////////////////////////////////////////////////// void Logger::flush() { - if (!IsInitialized) { + MUTEX_LOCKER(locker, InitializeMutex); + + if (!LoggingActive.load(std::memory_order_seq_cst)) { + // logging not (or not yet) initialized return; }