From 7547e6bf07072eb74b842f06643b9da3a8ecdec1 Mon Sep 17 00:00:00 2001 From: maj00r Date: Sun, 15 Nov 2026 23:32:38 +0100 Subject: [PATCH] fix(physicslog): a log whose file can't be written keeps its lines and header, and the writer is stopped once on every way out A failed write put its lines back, ahead of those since, so a file held by another program for a while gets its header and every line once it can be written; up to 4 MB kept per log. Stopping is joined once, and not from the writer's own thread. main stops the logs after an error it caught too, and the test watchdog flushes with a time limit, so a disk that doesn't answer doesn't keep the run from ending. --- EU07.cpp | 3 ++ simulation/simulationtest.cpp | 5 ++- tests/unit/test_physicslog.cpp | 60 +++++++++++++++++++++++++++- utilities/physicslog.cpp | 73 ++++++++++++++++++++++++++++------ utilities/physicslog.h | 11 +++-- 5 files changed, 134 insertions(+), 18 deletions(-) diff --git a/EU07.cpp b/EU07.cpp index 4ccbd6f56..1c2c36aab 100644 --- a/EU07.cpp +++ b/EU07.cpp @@ -242,6 +242,9 @@ int main(int argc, char *argv[]) MessageBoxA(nullptr, msg.c_str(), "Simulator crashed :(", MB_ICONERROR); } #endif + // the last two seconds of the physics logs, also after an error caught above; Application.exit() stops them + // otherwise, and a second stop only writes what came since + PhysicsLogs.stop(); } #ifndef _WIN32 fflush(stdout); diff --git a/simulation/simulationtest.cpp b/simulation/simulationtest.cpp index 8f1856663..11aca23c4 100644 --- a/simulation/simulationtest.cpp +++ b/simulation/simulationtest.cpp @@ -262,8 +262,9 @@ void test_manager::check_wall_clock(double const Elapsed) // but which doesn't close ends with its result if (false == conclude(test_result::timeout, std::format("still running after {:.0f} s of real time (test.wall_timeout)", limit))) ErrorLog(std::format("test: the simulator was still running {:.0f} s of real time after the start, ended", limit)); - // what was logged last gets written, unless the log is held up by a hung thread - PhysicsLogs.flush(); + // what was logged last gets written, unless the log is held up by a hung thread, or the physics logs by a disk + // which doesn't answer: the run ends anyway + PhysicsLogs.flush_for(std::chrono::seconds{2}); StopLogService(); std::this_thread::sleep_for(std::chrono::milliseconds(250)); std::_Exit(exit_code()); diff --git a/tests/unit/test_physicslog.cpp b/tests/unit/test_physicslog.cpp index afcd0f8c7..3189c424f 100644 --- a/tests/unit/test_physicslog.cpp +++ b/tests/unit/test_physicslog.cpp @@ -83,7 +83,8 @@ TEST_CASE("physicslog: no file is kept open between the writings, and a log not logs.begin("SM42-001", header); logs.flush(); - // Windows doesn't let a file open in a program be removed + // Windows doesn't let a file open in a program be removed. Linux does, so there it checks nothing; the log is + // written again below anyway std::error_code error; CHECK(std::filesystem::remove(folder.path() / "SM42-001.dat", error)); CHECK_FALSE(error); @@ -120,6 +121,63 @@ TEST_CASE("physicslog: stopping writes what is kept at once, and what comes afte CHECK(folder.read("EN57-1001") == std::string{header} + "0.0500 0.0000\r\n0.1000 0.0000\r\n"); } +TEST_CASE("physicslog: the lines of a log whose file can't be written are kept, with its header, for the next try") { + log_folder folder{"failed"}; + physicslog_writer logs{folder.path(), std::chrono::hours{1}}; + + // a folder in the way of the file: it can't be opened, as one held by another program + std::filesystem::create_directories(folder.path() / "EU07-005.dat"); + logs.begin("EU07-005", header); + logs.append("EU07-005", "0.0500 0.0000\r\n"); + logs.flush(); + logs.append("EU07-005", "0.1000 0.5000\r\n"); + logs.flush(); + CHECK(std::filesystem::is_directory(folder.path() / "EU07-005.dat")); + + std::filesystem::remove(folder.path() / "EU07-005.dat"); + logs.append("EU07-005", "0.1500 1.0000\r\n"); + logs.flush(); + CHECK(folder.read("EU07-005") == std::string{header} + "0.0500 0.0000\r\n0.1000 0.5000\r\n0.1500 1.0000\r\n"); +} + +TEST_CASE("physicslog: a log begun anew while its file can't be written drops what was kept for it") { + log_folder folder{"failedbegun"}; + physicslog_writer logs{folder.path(), std::chrono::hours{1}}; + + std::filesystem::create_directories(folder.path() / "SU45-001.dat"); + logs.begin("SU45-001", header); + logs.append("SU45-001", "0.0500 0.0000\r\n"); + logs.flush(); + std::filesystem::remove(folder.path() / "SU45-001.dat"); + logs.begin("SU45-001", header); + logs.append("SU45-001", "0.0500 2.0000\r\n"); + logs.flush(); + CHECK(folder.read("SU45-001") == std::string{header} + "0.0500 2.0000\r\n"); +} + +TEST_CASE("physicslog: stopping twice, from two threads at once, writes once and ends") { + log_folder folder{"stoptwice"}; + physicslog_writer logs{folder.path(), std::chrono::milliseconds{20}}; + + logs.begin("EP09-001", header); + logs.append("EP09-001", "0.0500 0.0000\r\n"); + { + std::jthread other{[&logs] { logs.stop(); }}; + logs.stop(); + } + CHECK(folder.read("EP09-001") == std::string{header} + "0.0500 0.0000\r\n"); +} + +TEST_CASE("physicslog: a flush with a time limit writes, as flush does") { + log_folder folder{"flushfor"}; + physicslog_writer logs{folder.path(), std::chrono::hours{1}}; + + logs.begin("ET41-001", header); + logs.append("ET41-001", "0.0500 0.0000\r\n"); + CHECK(logs.flush_for(std::chrono::seconds{1})); + CHECK(folder.read("ET41-001") == std::string{header} + "0.0500 0.0000\r\n"); +} + TEST_CASE("physicslog: what is kept is written when the writer goes") { log_folder folder{"end"}; { diff --git a/utilities/physicslog.cpp b/utilities/physicslog.cpp index cb57d5ca3..8a9ec2434 100644 --- a/utilities/physicslog.cpp +++ b/utilities/physicslog.cpp @@ -17,6 +17,13 @@ http://mozilla.org/MPL/2.0/. physicslog_writer PhysicsLogs; +namespace { + +// the most of a log kept while its file can't be written; the oldest lines go beyond it +constexpr std::size_t keptwhilefailing{4 * 1024 * 1024}; + +} // namespace + physicslog_writer::physicslog_writer(std::filesystem::path Folder, std::chrono::milliseconds const Interval) : m_folder{std::move(Folder)}, m_interval{Interval} {} physicslog_writer::~physicslog_writer() { @@ -27,7 +34,8 @@ void physicslog_writer::begin(std::string const &Name, std::string_view const He std::scoped_lock lock{m_mutex}; auto &lines{m_pending[Name]}; lines.fresh = true; - lines.text.assign(Header); + lines.header.assign(Header); + lines.text.clear(); start(); } @@ -38,20 +46,36 @@ void physicslog_writer::append(std::string const &Name, std::string_view const L } void physicslog_writer::flush() { + std::scoped_lock files{m_filemutex}; write(); } +bool physicslog_writer::flush_for(std::chrono::milliseconds const Timeout) { + std::unique_lock files{m_filemutex, Timeout}; + if (false == files.owns_lock()) + return false; + write(); + return true; +} + void physicslog_writer::stop() { + std::jthread thread; { std::scoped_lock lock{m_mutex}; m_stopped = true; + // taken by the first to stop, so it's joined once + thread = std::move(m_thread); } - // started only before m_stopped was set, so no one else touches it now - if (m_thread.joinable()) { - m_thread.request_stop(); - m_thread.join(); + if (thread.joinable()) { + if (thread.get_id() == std::this_thread::get_id()) { + // stopped from the thread itself, by the end of a run which failed in it: the files may be held by it + thread.detach(); + return; + } + thread.request_stop(); + thread.join(); } - write(); + flush(); } void physicslog_writer::start() { @@ -68,12 +92,11 @@ void physicslog_writer::run(std::stop_token const &Stop) { if (m_wake.wait_for(lock, Stop, m_interval, [] { return false; }) || Stop.stop_requested()) return; } - write(); + flush(); } } void physicslog_writer::write() { - std::scoped_lock files{m_filemutex}; std::map taken; { std::scoped_lock lock{m_mutex}; @@ -83,16 +106,42 @@ void physicslog_writer::write() { return; std::error_code error; std::filesystem::create_directories(m_folder, error); - for (auto const &[name, lines] : taken) { + std::map failed; + for (auto &[name, lines] : taken) { if (lines.text.empty() && false == lines.fresh) continue; auto const path{m_folder / (name + ".dat")}; // in text mode, as the logs were always written: on Windows their lines, which end with "\r\n", end with "\r\r\n" std::ofstream file{path, lines.fresh ? std::ios::out | std::ios::trunc : std::ios::out | std::ios::app}; - if (file.is_open()) + if (file.is_open()) { + if (lines.fresh) + file << lines.header; file << lines.text; + } file.close(); - if (file.fail() && m_failed.insert(name).second) - ErrorLog("Bad physics log: can't write " + path.string()); + if (false == file.fail()) { + if (m_failed.erase(name) > 0) + WriteLog("Physics log: " + path.string() + " written again"); + continue; + } + if (m_failed.insert(name).second) + ErrorLog("Bad physics log: can't write " + path.string() + ", its lines are kept for the next try"); + failed.emplace(name, std::move(lines)); + } + if (failed.empty()) + return; + // tried again the next time, before what came since; a file held by another program for a while doesn't lose + // them, nor its header. a log begun anew since drops them + std::scoped_lock lock{m_mutex}; + for (auto &[name, lines] : failed) { + auto &now{m_pending[name]}; + if (now.fresh) + continue; + lines.text.append(now.text); + if (lines.text.size() > keptwhilefailing) { + auto const cut{lines.text.find('\n', lines.text.size() - keptwhilefailing)}; + lines.text.erase(0, cut == std::string::npos ? lines.text.size() : cut + 1); + } + now = std::move(lines); } } diff --git a/utilities/physicslog.h b/utilities/physicslog.h index 2963c29a9..024ae8fba 100644 --- a/utilities/physicslog.h +++ b/utilities/physicslog.h @@ -22,7 +22,8 @@ http://mozilla.org/MPL/2.0/. // the physics logs, /.dat. the lines given are kept, and written every two seconds by a thread of its // own, each file opened only for the moment it is written to: the logs of hundreds of vehicles hold no files open, and -// the simulation doesn't wait for the disk. a crash loses the last two seconds of them +// the simulation doesn't wait for the disk. a crash loses the last two seconds of real time of them. lines whose file +// can't be written, held by another program say, are kept for the next try class physicslog_writer { public: explicit physicslog_writer(std::filesystem::path Folder = "physicslog", std::chrono::milliseconds Interval = std::chrono::seconds{2}); @@ -38,18 +39,22 @@ public: void append(std::string const &Name, std::string_view Line); // writes what is kept, now void flush(); + // as flush, unless the files are being written for longer than Timeout: then false, and nothing done + bool flush_for(std::chrono::milliseconds Timeout); // writes what is kept and ends the thread. what is given after is written by a flush, or at the end void stop(); private: struct pending { - bool fresh{false}; // its file is to be emptied first + bool fresh{false}; // its file is to be emptied first, and the header written into it + std::string header; std::string text; }; // the thread, on the first line given, unless stopped. called with m_mutex held void start(); void run(std::stop_token const &Stop); + // called with m_filemutex held void write(); std::filesystem::path const m_folder; @@ -59,7 +64,7 @@ private: bool m_stopped{false}; std::condition_variable_any m_wake; std::jthread m_thread; - std::mutex m_filemutex; // the files are written one batch at a time, so the lines of a log stay in order + std::timed_mutex m_filemutex; // the files are written one batch at a time, so the lines of a log stay in order std::set m_failed; // the logs whose file couldn't be written, told once };