From 0a1c4fd42505a5b80b0a6e7fb2da7a87205fa14d Mon Sep 17 00:00:00 2001 From: maj00r Date: Mon, 16 Nov 2026 13:00:16 +0100 Subject: [PATCH] fix(physicslog): what no file name may have on Windows is replaced in the name of a log, on every system A spawned vehicle is named :, and its log went to physicslog/:.dat. On NTFS that isn't even an error: it is the stream .dat of the file , which nothing lists, so the log was written and no one could find it. On Linux the file was there, under a name Windows can't open. The name of the file of a log has < > : " / \ | ? * and the control characters replaced by _, on every system, so the logs are named alike everywhere (spawn_ezt:12 is spawn_ezt_12.dat), and a _ before a name Windows keeps for a device (con, nul, com1..., with an extension too). The bytes of 1250 stay. The black box tells the log it begins by the same name. Co-Authored-By: Claude Opus 5.5 --- blackbox_doc.md | 5 ++++- simulation/blackboxrecorder.cpp | 2 +- tests/unit/test_physicslog.cpp | 23 +++++++++++++++++++++++ utilities/physicslog.cpp | 26 ++++++++++++++++++++++++-- utilities/physicslog.h | 19 +++++++++++-------- 5 files changed, 63 insertions(+), 12 deletions(-) diff --git a/blackbox_doc.md b/blackbox_doc.md index 8b4e23e0f..5dc8f7b71 100644 --- a/blackbox_doc.md +++ b/blackbox_doc.md @@ -16,7 +16,10 @@ recording started at), next to the `.dat` logs of the vehicles when those are on With the physics logs off it still gives the `.dat` log of each vehicle the user was in, as it goes: `physicslog/.dat`, the same file name, the same columns and the same line every 0.05 s as -`physicslog yes` would write. A log is the driver's, named after the vehicle that driver began in: it +`physicslog yes` would write. In the name of a file, of these logs and of those of `physicslog yes`, the +characters no file name may have on Windows, `< > : " / \ | ? *` and the control characters, are `_`, on +every system: a spawned vehicle, named `:`, has its log as `_.dat`. A name +Windows keeps for a device (`con`, `nul`, `com1` and the like) gets a `_` before it. A log is the driver's, named after the vehicle that driver began in: it begins the first time the user is with that driver, and goes on while the user looks around outside, or moves to another cab of the unit or another vehicle of the same train. Taking a vehicle of another driver goes on in that driver's log, begun then if it is the first time. Back with a driver, the log goes on after diff --git a/simulation/blackboxrecorder.cpp b/simulation/blackboxrecorder.cpp index efd92c3fc..b30d8a6c7 100644 --- a/simulation/blackboxrecorder.cpp +++ b/simulation/blackboxrecorder.cpp @@ -513,7 +513,7 @@ void blackbox_recorder::keep_tape_line() { auto const &name{driver->physicslog_name()}; if (m_tapes.insert(name).second) { PhysicsLogs.begin(name, TController::physicslog_header()); - WriteLog(std::format("Black box: the physics log \"physicslog/{}.dat\" begun", name)); + WriteLog(std::format("Black box: the physics log \"{}\" begun", PhysicsLogs.shown(name))); } PhysicsLogs.append(name, driver->physicslog_line()); } diff --git a/tests/unit/test_physicslog.cpp b/tests/unit/test_physicslog.cpp index b61755d4d..7a341b4f1 100644 --- a/tests/unit/test_physicslog.cpp +++ b/tests/unit/test_physicslog.cpp @@ -183,6 +183,29 @@ TEST_CASE("physicslog: a vehicle named in the code page of its scenery has its l CHECK(log_folder::read_file(logs.file(name)) == std::string{header} + "0.0500 0.0000\r\n"); } +TEST_CASE("physicslog: what no file name may have on Windows is replaced in the name of the file, on every system") { + log_folder folder{"filename"}; + physicslog_writer logs{folder.path(), std::chrono::hours{1}}; + + // a spawned vehicle's name has a ':', which on NTFS would write into a stream of the file "spawn_ezt" + CHECK(logs.file("spawn_ezt:12").filename() == "spawn_ezt_12.dat"); + CHECK(logs.file("ac\"d/e\\f|g?h*i\tj").filename() == "a_b_c_d_e_f_g_h_i_j.dat"); + CHECK(logs.file("EN57-1001 'a'.2").filename() == "EN57-1001 'a'.2.dat"); + // the devices of Windows, with an extension too, aren't files + CHECK(logs.file("nul").filename() == "_nul.dat"); + CHECK(logs.file("Com1.2").filename() == "_Com1.2.dat"); + CHECK(logs.file("con ").filename() == "_con .dat"); + CHECK(logs.file("console").filename() == "console.dat"); + CHECK(logs.file("com0").filename() == "com0.dat"); + CHECK(logs.shown("spawn_ezt:12") == folder.path().generic_string() + "/spawn_ezt_12.dat"); + + logs.begin("spawn_ezt:12", header); + logs.append("spawn_ezt:12", "0.0500 0.0000\r\n"); + logs.flush(); + CHECK(folder.read("spawn_ezt_12") == std::string{header} + "0.0500 0.0000\r\n"); + CHECK(false == std::filesystem::exists(folder.path() / "spawn_ezt")); +} + 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}}; diff --git a/utilities/physicslog.cpp b/utilities/physicslog.cpp index 25ab2556d..208fc7b01 100644 --- a/utilities/physicslog.cpp +++ b/utilities/physicslog.cpp @@ -12,10 +12,12 @@ http://mozilla.org/MPL/2.0/. #include "utilities/Logs.h" #include +#include #include #include #include #include +#include #include #include @@ -36,6 +38,26 @@ void keep_last(std::string &Text, std::size_t const Most) { Text.erase(0, cut == std::string::npos ? Text.size() : cut + 1); } +// the name of the file of the log Name, its extension too. what no file name may have on Windows is replaced, on every +// system, so the logs are named alike everywhere: a spawned vehicle is named :, and on NTFS "a:1.dat" +// isn't even an error but the stream 1.dat of the file a, which nothing lists +std::string file_name(std::string const &Name) { + std::string name{Name}; + for (auto &character : name) { + if (static_cast(character) < 0x20 || std::string_view{R"(<>:"/\|?*)"}.find(character) != std::string_view::npos) + character = '_'; + } + // a device rather than a file, with any extension: con.dat is the console. told by the name before its first dot + auto stem{name.substr(0, name.find('.'))}; + while (false == stem.empty() && stem.back() == ' ') + stem.pop_back(); + std::ranges::transform(stem, stem.begin(), [](unsigned char const Character) { return static_cast(std::toupper(Character)); }); + auto const numbered{stem.size() == 4 && (stem.starts_with("COM") || stem.starts_with("LPT")) && stem[3] >= '1' && stem[3] <= '9'}; + if (numbered || stem == "CON" || stem == "PRN" || stem == "AUX" || stem == "NUL") + name.insert(0, 1, '_'); + return name + ".dat"; +} + // told in the log of the simulator, if there's the memory for it void tell(std::string const &Text) noexcept { try { @@ -115,7 +137,7 @@ void physicslog_writer::stop() { } std::filesystem::path physicslog_writer::file(std::string const &Name) const { - auto const name{Name + ".dat"}; + auto const name{file_name(Name)}; #ifdef _WIN32 // in the code page of the system, as the standard library would convert it, but with the bytes not of it replaced // instead of an exception thrown: a vehicle of a scenery written in 1250 on a Windows set to UTF-8, say @@ -130,7 +152,7 @@ std::filesystem::path physicslog_writer::file(std::string const &Name) const { } std::string physicslog_writer::shown(std::string const &Name) const { - return m_shownfolder + '/' + Name + ".dat"; + return m_shownfolder + '/' + file_name(Name); } void physicslog_writer::start() { diff --git a/utilities/physicslog.h b/utilities/physicslog.h index 59ed34828..0f397d27c 100644 --- a/utilities/physicslog.h +++ b/utilities/physicslog.h @@ -20,10 +20,11 @@ http://mozilla.org/MPL/2.0/. #include #include -// 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 real time of them. lines whose file -// can't be written, held by another program say, are kept for the next try: the last 64 MB of them all at most +// the physics logs, /.dat, what no file name may have on Windows replaced in the name (a spawned vehicle's +// has a ':'), the same on every system. 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 real time of them. lines whose file can't +// be written, held by another program say, are kept for the next try: the last 64 MB of them all at most class physicslog_writer { public: explicit physicslog_writer(std::filesystem::path Folder = "physicslog", std::chrono::milliseconds Interval = std::chrono::seconds{2}); @@ -44,9 +45,13 @@ public: 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(); - // the file of the log Name, made of its bytes as they are: those of the code page of a scenery (1250) too, on a - // Windows set to UTF-8, where the standard library throws making a path of them. there they become U+FFFD + // the file of the log Name: /.dat, with < > : " / \ | ? * and the control characters of the name + // replaced by _, and _ before a name Windows keeps for a device (con, nul, com1...). the bytes of the code page of a + // scenery (1250) stay as they are, also on a Windows set to UTF-8, where the standard library would throw making a + // path of them: there they become U+FFFD std::filesystem::path file(std::string const &Name) const; + // the same, for the log of the simulator + std::string shown(std::string const &Name) const; private: struct pending { @@ -65,8 +70,6 @@ private: void write(); // the lines of the log Name written into its file. returns: true if they all were bool write_log(std::string const &Name, pending const &Lines) const; - // the file of the log Name, for the log of the simulator - std::string shown(std::string const &Name) const; std::filesystem::path const m_folder; std::string const m_shownfolder;