Skip to content

Commit

Permalink
Avoid processing log statement if logger has been destructed in the m…
Browse files Browse the repository at this point in the history
…iddle of logging
  • Loading branch information
SergeyRyabinin committed Jan 29, 2024
1 parent 669654e commit 44e798f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ namespace Aws
std::mutex m_logQueueMutex;
std::condition_variable m_queueSignal;
Aws::Vector<Aws::String> m_queuedLogMessages;
bool m_stopLogging;
bool m_stopLogging = false;
bool m_loggingThreadStopped = false;

private:
LogSynchronizationData(const LogSynchronizationData& rhs) = delete;
Expand Down
18 changes: 18 additions & 0 deletions src/aws-cpp-sdk-core/source/utils/logging/DefaultLogSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ static void LogThread(DefaultLogSystem::LogSynchronizationData* syncData, std::s
logFile->flush();
}
}

{
std::unique_lock<std::mutex> locker(syncData->m_logQueueMutex);
syncData->m_loggingThreadStopped = true;
syncData->m_queueSignal.notify_one();
}
}

DefaultLogSystem::DefaultLogSystem(LogLevel logLevel, const std::shared_ptr<Aws::OStream>& logFile) :
Expand All @@ -90,12 +96,24 @@ DefaultLogSystem::~DefaultLogSystem()
m_syncData.m_queueSignal.notify_one();
}

// explicitly wait for logging thread to finish
{
std::unique_lock<std::mutex> locker(m_syncData.m_logQueueMutex);
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<std::mutex> 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)
{
Expand Down

0 comments on commit 44e798f

Please sign in to comment.