From fcd89862cb93f3251846ee9f9c6730ad80f9b8b4 Mon Sep 17 00:00:00 2001 From: James Taylor <1166237+jamestaylr@users.noreply.github.com> Date: Thu, 7 Aug 2025 08:07:57 -0700 Subject: [PATCH] Avoid microsecond format string parsing Fixes a bug in the implementation of https://github.com/MisterTea/EternalTerminal/pull/680. Log creation uses [`std::strftime`](https://en.cppreference.com/w/cpp/chrono/c/strftime) which does not support the `%f` format string. Instead, build the microseconds outside of `strftime` and append it after. After the fix, the file names are now correclty appending microseconds (after testing): ``` test_prefix-2025-08-03_21-37-11.810495 test_prefix-stderr-2025-08-03_21-37-11.810495 ``` --- src/base/LogHandler.cpp | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/base/LogHandler.cpp b/src/base/LogHandler.cpp index 993287680..897269a55 100644 --- a/src/base/LogHandler.cpp +++ b/src/base/LogHandler.cpp @@ -28,13 +28,22 @@ void LogHandler::setupLogFiles(el::Configurations *defaultConf, const string &path, const string &filenamePrefix, bool logToStdout, bool redirectStderrToFile, bool appendPid, string maxlogsize) { - time_t rawtime; - struct tm *timeinfo; + auto now = std::chrono::system_clock::now(); + time_t rawtime = std::chrono::system_clock::to_time_t(now); + struct tm *timeinfo = std::localtime(&rawtime); + char buffer[80]; - time(&rawtime); - timeinfo = localtime(&rawtime); - strftime(buffer, sizeof(buffer), "%Y-%m-%d_%H-%M-%S.%f", timeinfo); + strftime(buffer, sizeof(buffer), "%Y-%m-%d_%H-%M-%S", timeinfo); string current_time(buffer); + + auto duration = now.time_since_epoch(); + auto microseconds = + std::chrono::duration_cast(duration) % + std::chrono::seconds(1); + std::stringstream ss; + ss << std::setw(6) << std::setfill('0') << microseconds.count(); + current_time += "." + ss.str(); + string logFilename = filenamePrefix + "-" + current_time; string stderrFilename = filenamePrefix + "-stderr-" + current_time; if (appendPid) {