diff --git a/src/aws-cpp-sdk-core/include/aws/core/utils/logging/DefaultLogSystem.h b/src/aws-cpp-sdk-core/include/aws/core/utils/logging/DefaultLogSystem.h index dbeed83cb1a..afe4ff08b9a 100644 --- a/src/aws-cpp-sdk-core/include/aws/core/utils/logging/DefaultLogSystem.h +++ b/src/aws-cpp-sdk-core/include/aws/core/utils/logging/DefaultLogSystem.h @@ -58,12 +58,13 @@ namespace Aws struct LogSynchronizationData { public: - LogSynchronizationData() : m_stopLogging(false) {} + LogSynchronizationData() : m_stopLogging(false), m_loggingThreadStopped(false) {} std::mutex m_logQueueMutex; std::condition_variable m_queueSignal; Aws::Vector m_queuedLogMessages; - bool m_stopLogging; + bool m_stopLogging = false; + bool m_loggingThreadStopped = false; private: LogSynchronizationData(const LogSynchronizationData& rhs) = delete; diff --git a/src/aws-cpp-sdk-core/source/utils/logging/DefaultLogSystem.cpp b/src/aws-cpp-sdk-core/source/utils/logging/DefaultLogSystem.cpp index 580b8ba4078..5a948777d3f 100644 --- a/src/aws-cpp-sdk-core/source/utils/logging/DefaultLogSystem.cpp +++ b/src/aws-cpp-sdk-core/source/utils/logging/DefaultLogSystem.cpp @@ -64,6 +64,12 @@ static void LogThread(DefaultLogSystem::LogSynchronizationData* syncData, std::s logFile->flush(); } } + + { + std::unique_lock locker(syncData->m_logQueueMutex); + syncData->m_loggingThreadStopped = true; + syncData->m_queueSignal.notify_one(); + } } DefaultLogSystem::DefaultLogSystem(LogLevel logLevel, const std::shared_ptr& logFile) : @@ -90,12 +96,27 @@ DefaultLogSystem::~DefaultLogSystem() m_syncData.m_queueSignal.notify_one(); } + // explicitly wait for logging thread to finish + { + std::unique_lock locker(m_syncData.m_logQueueMutex); + if (!m_syncData.m_loggingThreadStopped) + { + m_syncData.m_queueSignal.wait_for(locker, + std::chrono::milliseconds(500), + [&](){ return m_syncData.m_loggingThreadStopped; }); + } + } + m_loggingThread.join(); } void DefaultLogSystem::ProcessFormattedStatement(Aws::String&& statement) { std::lock_guard locker(m_syncData.m_logQueueMutex); + if (m_syncData.m_stopLogging) + { + return; + } m_syncData.m_queuedLogMessages.emplace_back(std::move(statement)); if(m_syncData.m_queuedLogMessages.size() >= BUFFERED_MSG_COUNT) {