[wpiutil] DataLog: Ensure file is written on shutdown (#6087)

Previously the thread could end without the file being written.
This commit is contained in:
Peter Johnson
2023-12-23 13:40:51 -08:00
committed by GitHub
parent c29e8c66cf
commit 21d1972d7a
3 changed files with 24 additions and 6 deletions

View File

@@ -179,7 +179,7 @@ DataLog::DataLog(wpi::Logger& msglog,
DataLog::~DataLog() {
{
std::scoped_lock lock{m_mutex};
m_state = kShutdown;
m_shutdown = true;
m_doFlush = true;
}
m_cond.notify_all();
@@ -419,7 +419,7 @@ void DataLog::WriterThreadMain(std::string_view dir) {
uintmax_t written = 0;
std::unique_lock lock{m_mutex};
while (m_state != kShutdown) {
do {
bool doFlush = false;
auto timeoutTime = std::chrono::steady_clock::now() + periodTime;
if (m_cond.wait_until(lock, timeoutTime) == std::cv_status::timeout) {
@@ -557,7 +557,7 @@ void DataLog::WriterThreadMain(std::string_view dir) {
}
toWrite.resize(0);
}
}
} while (!m_shutdown);
}
void DataLog::WriterThreadMain(
@@ -580,7 +580,7 @@ void DataLog::WriterThreadMain(
std::vector<Buffer> toWrite;
std::unique_lock lock{m_mutex};
while (m_state != kShutdown) {
do {
bool doFlush = false;
auto timeoutTime = std::chrono::steady_clock::now() + periodTime;
if (m_cond.wait_until(lock, timeoutTime) == std::cv_status::timeout) {
@@ -614,7 +614,7 @@ void DataLog::WriterThreadMain(
}
toWrite.resize(0);
}
}
} while (!m_shutdown);
write({}); // indicate EOF
}