From 0488519f39e320d8aa17de544cf3886a6869eeb0 Mon Sep 17 00:00:00 2001 From: maj00r Date: Sat, 14 Nov 2026 00:04:54 +0100 Subject: [PATCH 01/14] fix: the simulator opens more than 512 files at once, and a shader it can't open says why With "physicslog yes" every driver keeps its .dat open, and a large scenery (Tomaszewo: some 500 vehicles, cars of the roads included) took all 512 streams the MSVC runtime gives: the next fopen failed and the simulator stopped at "cannot read shader: shaders/map.vert" on opening the map. The limit is raised to 8192 at the start (the soft limit of open files up to the hard one on Linux), and a shader file that doesn't open is told as such, with the system's reason, rather than as one that can't be read. --- EU07.cpp | 24 ++++++++++++++++++++++++ gl/shader.cpp | 8 ++++++++ 2 files changed, 32 insertions(+) diff --git a/EU07.cpp b/EU07.cpp index 20f24aa5a..136882299 100644 --- a/EU07.cpp +++ b/EU07.cpp @@ -27,6 +27,11 @@ Stele, firleju, szociu, hunter, ZiomalCl, OLI_EU and others #include #include #include +#ifdef _WIN32 +#include +#else +#include +#endif #ifdef WITHDUMPGEN #ifdef _WIN32 #include @@ -136,10 +141,29 @@ void finish_log() end_test_run("the simulator stopped before the test ended"); } +// every vehicle with a driver keeps its physics log open for the whole run, and on a scenery with some five hundred +// of them the logs took all the files the C library of Windows lets a process have open by default (512): the +// files opened after them, the shaders of the map among them, couldn't be read. so the limit is raised to the most +// either system allows +void raise_open_files_limit() +{ +#ifdef _WIN32 + _setmaxstdio(8192); +#else + rlimit limit{}; + if (getrlimit(RLIMIT_NOFILE, &limit) == 0 && limit.rlim_cur < limit.rlim_max) + { + limit.rlim_cur = limit.rlim_max; + setrlimit(RLIMIT_NOFILE, &limit); + } +#endif +} + } // namespace int main(int argc, char *argv[]) { + raise_open_files_limit(); #ifdef WITHDUMPGEN #ifdef _WIN32 SetUnhandledExceptionFilter(CrashHandler); diff --git a/gl/shader.cpp b/gl/shader.cpp index 31a3e6a87..c276cdef8 100644 --- a/gl/shader.cpp +++ b/gl/shader.cpp @@ -1,8 +1,10 @@ #include "stdafx.h" +#include #include #include #include +#include #include "shader.h" #include "glsl_common.h" #include "utilities/Logs.h" @@ -58,6 +60,12 @@ std::string gl::shader::read_file(const std::string &filename) const std::ifstream f; f.open(filename); + if (false == f.is_open()) + { + // with the reason the system gives: a file which is there but can't be opened (too many files open) looks + // the same as a missing one otherwise + throw shader_exception("cannot open shader: " + filename + " (" + std::generic_category().message(errno) + ")"); + } stream << f.rdbuf(); f.close(); From 5ca2f7e38fc59cc77c74a8975f84381998e61a7f Mon Sep 17 00:00:00 2001 From: maj00r Date: Sat, 14 Nov 2026 00:04:55 +0100 Subject: [PATCH 02/14] feat(blackbox): the black box on its own, writing the tapes of the user's vehicles at the end "blackbox yes" in eu07.ini records the black box with the physics logs on or off, "blackbox no" never; with no entry it goes with "physicslog yes" as before. With the physics logs off, the recorder keeps the line of the physics log of the driver the user is with, every 0.05 s, and writes it to physicslog/.dat when the recording closes: the speed recorder's tape of the vehicles the user was in, without every vehicle keeping a log open and written twenty times a second. The line and the header of the log are made by one function of the driver, which its own log writes too, and the file is opened the same way, so the two are the same to the byte: the readers of these logs (the old Hasler tape viewer, the starter) read them as before. --- blackbox_doc.md | 15 ++++-- simulation/blackboxrecorder.cpp | 42 ++++++++++++++- simulation/blackboxrecorder.h | 8 +++ utilities/Globals.cpp | 8 ++- utilities/Globals.h | 4 +- vehicle/Driver.cpp | 95 +++++++++++++++++---------------- vehicle/Driver.h | 8 +++ 7 files changed, 126 insertions(+), 54 deletions(-) diff --git a/blackbox_doc.md b/blackbox_doc.md index c58cae100..80ddbacf8 100644 --- a/blackbox_doc.md +++ b/blackbox_doc.md @@ -7,10 +7,17 @@ done at the same moment, every command the user gave, when the autopilot took ov the timetable and how the train kept to it at each station, and where it was told to sound the horn. From it another program (the starter, a viewer) can replay the session on the map, or set the user's driving against the AI's. -It is written along with the physics logs: `physicslog yes` in `eu07.ini`, not in an editor session. -`blackbox no` turns it off while the physics logs are kept (it is on by default). The file goes next to -them, `physicslog/_.blackbox` (the local time the recording started at). -The `.dat` logs stay as they are. +`blackbox yes` in `eu07.ini` writes it, with the physics logs on or off; `blackbox no` never does. With no +`blackbox` entry it is written along with the physics logs, `physicslog yes`. It is never written in an +editor session. The file goes to `physicslog/_.blackbox` (the local time the +recording started at), next to the `.dat` logs of the vehicles when those are on; they stay as they are. + +With the physics logs off it still gives the `.dat` log of each vehicle the user was in, written when the +recording ends: `physicslog/.dat`, the same file name, the same columns and the same line every +0.05 s as `physicslog yes` would write, but only of the times the user was there. A vehicle with no driver +of its own takes the log of the driver of its train. So the speed recorder's tape can be read from them as +before, without the simulator keeping a log of every vehicle open all the time. A simulator that crashes +leaves the recording, but not these logs. The recording starts once the scenery is loaded and ends when the simulator closes. Going into the editor (F11) ends it too, since the editor may take away the tracks and the vehicles it refers to; no diff --git a/simulation/blackboxrecorder.cpp b/simulation/blackboxrecorder.cpp index d886bddf0..b5465053b 100644 --- a/simulation/blackboxrecorder.cpp +++ b/simulation/blackboxrecorder.cpp @@ -42,6 +42,7 @@ namespace { // [s] between the samples of the user's train, the looks at everything else and the full looks at the vehicles constexpr double cabinterval{0.1}; +constexpr double tapeinterval{0.05}; // as the physics logs (deltalog of Driver.cpp) constexpr double lookinterval{1.0}; constexpr double slowlook{20.0}; // [ms] a look around taking longer is told in the log, part by part constexpr double keyframeinterval{60.0}; @@ -233,7 +234,8 @@ void blackbox_recorder::update(double const Deltatime) { if (false == simulation::is_ready) return; m_started = true; - if (false == WriteLogFlag || false == Global.BlackBox || Global.editor_session) + // "blackbox yes" or "no" of eu07.ini decides by itself, with no entry it goes with the physics logs + if (false == Global.BlackBox.value_or(WriteLogFlag) || Global.editor_session) return; open(); } @@ -246,6 +248,11 @@ void blackbox_recorder::update(double const Deltatime) { write_cab(); m_cabdue = std::max(m_cabdue + cabinterval, m_time); } + // with the physics logs on, each driver writes its own all the time + if (false == WriteLogFlag && m_time >= m_tapedue) { + keep_tape_line(); + m_tapedue = std::max(m_tapedue + tapeinterval, m_time); + } if (m_time >= m_cameradue) { write_camera(); m_cameradue = std::max(m_cameradue + camerainterval, m_time); @@ -273,6 +280,7 @@ void blackbox_recorder::close() { m_writer->end(m_time); m_writer.reset(); m_file.close(); + write_tapes(); using milliseconds = std::chrono::duration; WriteLog(std::format("Black box: recording closed; {} looks around, {:.3f} ms each on average, the longest {:.3f} ms", m_looks, m_looks > 0 ? milliseconds{m_looktime}.count() / m_looks : 0.0, milliseconds{m_looklongest}.count())); @@ -478,6 +486,38 @@ void blackbox_recorder::write_cab() { m_writer->cab(sample); } +void blackbox_recorder::keep_tape_line() { + auto const *train{simulation::Train}; + if (train == nullptr || train->Dynamic() == nullptr) + return; + auto const *vehicle{train->Dynamic()}; + // the driver of the vehicle, whose log it would be: it keeps the name of its first vehicle, so a change of the cab + // in a unit goes on in the same log. in a vehicle without one, the driver of its train + auto const *driver{vehicle->Mechanik != nullptr ? vehicle->Mechanik : vehicle->ctOwner}; + if (driver == nullptr) + return; + m_tapes[driver->physicslog_name()] += driver->physicslog_line(); +} + +void blackbox_recorder::write_tapes() { + for (auto const &[name, lines] : m_tapes) { + // opened as the driver opens its log, so the file is the same to the byte + auto const filename{"physicslog/" + name + ".dat"}; + std::fstream file{filename, std::ios::in | std::ios::out | std::ios::trunc}; + if (false == file.is_open()) { + ErrorLog(std::format("Black box: can't create the physics log \"{}\"", filename)); + continue; + } + file << TController::physicslog_header() << lines; + file.close(); + if (file.fail()) + ErrorLog(std::format("Black box: the physics log \"{}\" wasn't written whole", filename)); + else + WriteLog(std::format("Black box: the physics log \"{}\" written from the recording", filename)); + } + m_tapes.clear(); +} + void blackbox_recorder::look_around(bool const Keyframe) { auto const &time{simulation::Time.data()}; m_writer->clock(m_time, static_cast(time.wYear), static_cast(simulation::Time.year_day()), second_of_day()); diff --git a/simulation/blackboxrecorder.h b/simulation/blackboxrecorder.h index 677c680b0..b7715e04c 100644 --- a/simulation/blackboxrecorder.h +++ b/simulation/blackboxrecorder.h @@ -15,6 +15,7 @@ http://mozilla.org/MPL/2.0/. #include #include #include +#include #include #include #include @@ -86,6 +87,10 @@ private: void write_map(); static std::vector> stop_point_headings(); void write_cab(); + // with the physics logs off, the line of the log of the driver the user is with, kept for the end + void keep_tape_line(); + // the kept lines, each driver's to its log, physicslog/.dat, as the physics logs would have it + void write_tapes(); // the clock, the user's vehicle, the vehicles, the switches and the signals void look_around(bool Keyframe); void look_at_vehicles(bool Keyframe); @@ -109,6 +114,9 @@ private: bool m_started{false}; // tried to start, made or not double m_time{0.0}; double m_cabdue{0.0}; + double m_tapedue{0.0}; + // the lines of the physics log by the name of its file, of the times the user was with its driver + std::map m_tapes; double m_lookdue{0.0}; double m_keyframedue{0.0}; std::unordered_map m_vehicles; diff --git a/utilities/Globals.cpp b/utilities/Globals.cpp index a2cb6f59d..9c0c10279 100644 --- a/utilities/Globals.cpp +++ b/utilities/Globals.cpp @@ -540,7 +540,9 @@ bool global_settings::ConfigParseSimulation(cParser& Parser, const std::string& if (token == "blackbox") { - ParseOne(Parser, BlackBox); + bool blackbox{false}; + ParseOne(Parser, blackbox); + BlackBox = blackbox; return true; } @@ -1735,7 +1737,9 @@ global_settings::export_as_text( std::ostream &Output ) const { export_as_text( Output, "sound.volume.ambient", EnvironmentAmbientVolume ); export_as_text( Output, "sound.asyncload", AudioAsyncLoad ); export_as_text( Output, "physicslog", WriteLogFlag ); - export_as_text( Output, "blackbox", BlackBox ); + if( BlackBox ) { + export_as_text( Output, "blackbox", *BlackBox ); + } export_as_text( Output, "fullphysics", FullPhysics ); export_as_text( Output, "debuglog", iWriteLogEnabled ); export_as_text( Output, "multiplelogs", MultipleLogs ); diff --git a/utilities/Globals.h b/utilities/Globals.h index 8d32a463c..c4a9a7267 100644 --- a/utilities/Globals.h +++ b/utilities/Globals.h @@ -113,7 +113,9 @@ struct global_settings { bool priorityLoadText3D{false}; // ladowanie T3D priorytetowo int iWriteLogEnabled{ 3 }; // maska bitowa: 1-zapis do pliku, 2-okienko, 4-nazwy torów bool MultipleLogs{ false }; - bool BlackBox{ true }; // with the physics logs on, the black box of the session is written as well (blackbox_doc.md) + // the black box of the session (blackbox_doc.md): "blackbox yes" writes it whatever the physics logs, "blackbox no" + // never, and with no entry it is written along with the physics logs + std::optional BlackBox; bool ShowSystemConsole{ false }; // toggle visibility of the OS console window with log output unsigned int DisabledLogTypes{ 0 }; bool ParserLogIncludes{ true }; diff --git a/vehicle/Driver.cpp b/vehicle/Driver.cpp index eb74344bf..8255a93cc 100644 --- a/vehicle/Driver.cpp +++ b/vehicle/Driver.cpp @@ -2344,16 +2344,7 @@ TController::TController(bool AI, TDynamicObject *NewControll, bool InitPsyche, LogFile.open( std::string( "physicslog/" + VehicleName + ".dat" ), std::ios::in | std::ios::out | std::ios::trunc ); #if LOGPRESS == 0 - LogFile - << "Time[s] Velocity[m/s] Acceleration[m/ss] " - << "Coupler.Dist[m] Coupler.Force[N] TractionForce[kN] FrictionForce[kN] BrakeForce[kN] " - << "BrakePress[MPa] PipePress[MPa] MotorCurrent[A] " - << "MCP SCP BCP LBP Direction Command CVal1 CVal2 " - << "Security Wheelslip " - << "EngineTemp[Deg] OilTemp[Deg] WaterTemp[Deg] WaterAuxTemp[Deg] " - << "VehicleVelocity[km/h]" - << "\r\n"; - LogFile << std::fixed << std::setprecision( 4 ); + LogFile << physicslog_header(); #endif #if LOGPRESS == 1 LogFile << string( "t\tVel\tAcc\tPP\tVVP\tBP\tBVP\tCVP" ).c_str() << "\n"; @@ -5367,47 +5358,59 @@ bool TController::PutCommand( std::string NewCommand, double NewValue1, double N return false; // nierozpoznana - wysłać bezpośrednio do pojazdu }; +std::string_view TController::physicslog_header() { + // the columns of the readers of these logs, the Hasler tape of the starter among them, stay where they are: + // a new one goes at the end + return "Time[s] Velocity[m/s] Acceleration[m/ss] " + "Coupler.Dist[m] Coupler.Force[N] TractionForce[kN] FrictionForce[kN] BrakeForce[kN] " + "BrakePress[MPa] PipePress[MPa] MotorCurrent[A] " + "MCP SCP BCP LBP Direction Command CVal1 CVal2 " + "Security Wheelslip " + "EngineTemp[Deg] OilTemp[Deg] WaterTemp[Deg] WaterAuxTemp[Deg] " + "VehicleVelocity[km/h]" + "\r\n"; +} + +std::string TController::physicslog_line() const { + std::ostringstream line; + line << std::fixed << std::setprecision( 4 ) + << ElapsedTime << " " + << fabs(11.31 * mvOccupied->WheelDiameter * mvOccupied->nrot) << " " + << mvControlling->AccS << " " + << mvOccupied->Couplers[1].Dist << " " + << mvOccupied->Couplers[1].CForce << " " + << mvOccupied->Ft << " " + << mvOccupied->Ff << " " + << mvOccupied->Fb << " " + << mvOccupied->BrakePress << " " + << mvOccupied->PipePress << " " + << mvControlling->Im << " " + << int(mvControlling->MainCtrlPos) << " " + << int(mvControlling->ScndCtrlPos) << " " + << mvOccupied->fBrakeCtrlPos << " " + << mvOccupied->LocalBrakePosA << " " + << int(mvControlling->DirActive) << " " + << ( mvOccupied->CommandIn.Command.empty() ? "none" : mvOccupied->CommandIn.Command.c_str() ) << " " + << mvOccupied->CommandIn.Value1 << " " + << mvOccupied->CommandIn.Value2 << " " + << int(mvControlling->SecuritySystem.is_blinking()) << " " + << int(mvControlling->SlippingWheels) << " " + << mvControlling->dizel_heat.Ts << " " + << mvControlling->dizel_heat.To << " " + << mvControlling->dizel_heat.temperatura1 << " " + << mvControlling->dizel_heat.temperatura2 << " " + // vehicle speed [km/h], the second column is the wheel speed, which stays at 0 while the wheels are locked in a skid + << mvOccupied->V * 3.6 + << "\r\n"; + return line.str(); +} + void TController::PhysicsLog() { // zapis logu - na razie tylko wypisanie parametrów if (LogFile.is_open()) { #if LOGPRESS == 0 -/* -<< "Time[s] Velocity[m/s] Acceleration[m/ss] " -<< "Coupler.Dist[m] Coupler.Force[N] TractionForce[kN] FrictionForce[kN] BrakeForce[kN] " -<< "BrakePress[MPa] PipePress[MPa] MotorCurrent[A] " -<< "MCP SCP BCP LBP DmgFlag Command CVal1 CVal2 " -<< "EngineTemp[Deg] OilTemp[Deg] WaterTemp[Deg] WaterAuxTemp[Deg]" -*/ - LogFile - << ElapsedTime << " " - << fabs(11.31 * mvOccupied->WheelDiameter * mvOccupied->nrot) << " " - << mvControlling->AccS << " " - << mvOccupied->Couplers[1].Dist << " " - << mvOccupied->Couplers[1].CForce << " " - << mvOccupied->Ft << " " - << mvOccupied->Ff << " " - << mvOccupied->Fb << " " - << mvOccupied->BrakePress << " " - << mvOccupied->PipePress << " " - << mvControlling->Im << " " - << int(mvControlling->MainCtrlPos) << " " - << int(mvControlling->ScndCtrlPos) << " " - << mvOccupied->fBrakeCtrlPos << " " - << mvOccupied->LocalBrakePosA << " " - << int(mvControlling->DirActive) << " " - << ( mvOccupied->CommandIn.Command.empty() ? "none" : mvOccupied->CommandIn.Command.c_str() ) << " " - << mvOccupied->CommandIn.Value1 << " " - << mvOccupied->CommandIn.Value2 << " " - << int(mvControlling->SecuritySystem.is_blinking()) << " " - << int(mvControlling->SlippingWheels) << " " - << mvControlling->dizel_heat.Ts << " " - << mvControlling->dizel_heat.To << " " - << mvControlling->dizel_heat.temperatura1 << " " - << mvControlling->dizel_heat.temperatura2 << " " - // vehicle speed [km/h], the second column is the wheel speed, which stays at 0 while the wheels are locked in a skid - << mvOccupied->V * 3.6 - << "\r\n"; + LogFile << physicslog_line(); #endif #if LOGPRESS == 1 LogFile << ElapsedTime << "\t" << fabs(11.31 * mvOccupied->WheelDiameter * mvOccupied->nrot) diff --git a/vehicle/Driver.h b/vehicle/Driver.h index cfa27757e..02b2c60e2 100644 --- a/vehicle/Driver.h +++ b/vehicle/Driver.h @@ -10,6 +10,7 @@ http://mozilla.org/MPL/2.0/. #pragma once #include +#include #include "utilities/Classes.h" #include "utilities/utilities.h" #include "MOVER.h" @@ -730,6 +731,13 @@ private: public: TDynamicObject const *Vehicle() const { return pVehicle; } + // the physics log (physicslog/.dat): the name of its file without the extension, its first line and + // a line of it as the vehicle is now, each with its line end. the black box keeps the lines of the vehicle the + // user is in, to write its log at the end when the logs of all the vehicles are off + std::string const &physicslog_name() const { + return VehicleName; } + static std::string_view physicslog_header(); + std::string physicslog_line() const; TDynamicObject *Vehicle( end const Side ) const { return pVehicles[ Side ]; } private: From 03a3b4c09a0e010756daebb0c3637974185173e3 Mon Sep 17 00:00:00 2001 From: maj00r Date: Sat, 14 Nov 2026 00:48:17 +0100 Subject: [PATCH 03/14] feat(physicslog): the logs written every two seconds by a thread of their own, no file kept open; the black box's tapes as it goes "physicslog yes" kept the .dat of every vehicle with a driver open for the whole run and flushed it twenty times a second from the simulation's thread: some 580 open files on Tomaszewo. The lines are kept now and a thread of their own writes them every two seconds, each file opened only for the moment it is written to. The files, their lines and their text mode stay as they were. - The black box, with the physics logs off, writes the tapes of the user's vehicles through the same writer as it goes, not at the end. A log begins when the user is first in the vehicle and goes on while the user looks around outside, until another vehicle is taken; back in it, the log goes on after the gap. The vehicle's own driver keeps the log only if it is the primary one, as a passive one's clock stands still; otherwise the driver of its train does. - What is kept is written when the simulator closes, after the black box, at the end of a test run, and when the test watchdog ends a run. A crash loses the last two seconds. - On Tomaszewo with the black box alone, the one tape grows every two seconds. With "physicslog yes", the 581 logs grow and the simulator holds as many handles as without them; writing them takes the thread about 1.9 s, which the closing waits for once. --- CMakeLists.txt | 1 + EU07.cpp | 11 ++- application/application.cpp | 4 + blackbox_doc.md | 15 ++-- simulation/blackboxrecorder.cpp | 30 ++------ simulation/blackboxrecorder.h | 12 +-- simulation/simulationtest.cpp | 2 + tests/unit/test_physicslog.cpp | 131 ++++++++++++++++++++++++++++++++ utilities/physicslog.cpp | 98 ++++++++++++++++++++++++ utilities/physicslog.h | 67 ++++++++++++++++ vehicle/Driver.cpp | 35 ++++----- vehicle/Driver.h | 2 +- 12 files changed, 347 insertions(+), 61 deletions(-) create mode 100644 tests/unit/test_physicslog.cpp create mode 100644 utilities/physicslog.cpp create mode 100644 utilities/physicslog.h diff --git a/CMakeLists.txt b/CMakeLists.txt index e97e41d03..3c12c51a5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -546,6 +546,7 @@ set(CORE_SOURCES "scripting/scriptscreendrawing.cpp" "simulation/simulationtestrules.cpp" "vehicle/blackbox.cpp" +"utilities/physicslog.cpp" "vehicle/enginestart.cpp" "vehicle/brakesetting.cpp" "vehicle/handover.cpp" diff --git a/EU07.cpp b/EU07.cpp index 136882299..4ccbd6f56 100644 --- a/EU07.cpp +++ b/EU07.cpp @@ -22,6 +22,7 @@ Stele, firleju, szociu, hunter, ZiomalCl, OLI_EU and others #include "simulation/simulationtest.h" #include "utilities/Globals.h" #include "utilities/Logs.h" +#include "utilities/physicslog.h" #include #include #include @@ -112,6 +113,8 @@ void finish_log() [[noreturn]] void end_test_run(std::string_view const Reason) { simulation::test().conclude(simulation::test_result::error, Reason); + // the last two seconds of the physics logs, which the runner of the tests reads + PhysicsLogs.stop(); finish_log(); fflush(stdout); fflush(stderr); @@ -141,10 +144,10 @@ void finish_log() end_test_run("the simulator stopped before the test ended"); } -// every vehicle with a driver keeps its physics log open for the whole run, and on a scenery with some five hundred -// of them the logs took all the files the C library of Windows lets a process have open by default (512): the -// files opened after them, the shaders of the map among them, couldn't be read. so the limit is raised to the most -// either system allows +// the physics logs used to keep a file open for every vehicle with a driver, and on a scenery with some five hundred +// of them they took all the files the C library of Windows lets a process have open by default (512): the files +// opened after them, the shaders of the map among them, couldn't be read. they keep none open now (physicslog.h), +// but the limit is raised to the most either system allows still, for whatever else opens many files at once void raise_open_files_limit() { #ifdef _WIN32 diff --git a/application/application.cpp b/application/application.cpp index 96f578514..ba62fe467 100644 --- a/application/application.cpp +++ b/application/application.cpp @@ -34,6 +34,7 @@ http://mozilla.org/MPL/2.0/. #include "utilities/dictionary.h" #include "version_info.h" #include "simulation/simulationtest.h" +#include "utilities/physicslog.h" #include #include #include @@ -745,6 +746,9 @@ void eu07_application::exit() track_sleepers::stop_background(); step("the black box"); simulation::Blackbox.close(); + step("the physics logs"); + // what is kept of them, the black box's written last + PhysicsLogs.stop(); step("the modes"); for (auto &mode : m_modes) mode.reset(); diff --git a/blackbox_doc.md b/blackbox_doc.md index 80ddbacf8..ea7322ca8 100644 --- a/blackbox_doc.md +++ b/blackbox_doc.md @@ -12,12 +12,15 @@ starter, a viewer) can replay the session on the map, or set the user's driving editor session. The file goes to `physicslog/_.blackbox` (the local time the recording started at), next to the `.dat` logs of the vehicles when those are on; they stay as they are. -With the physics logs off it still gives the `.dat` log of each vehicle the user was in, written when the -recording ends: `physicslog/.dat`, the same file name, the same columns and the same line every -0.05 s as `physicslog yes` would write, but only of the times the user was there. A vehicle with no driver -of its own takes the log of the driver of its train. So the speed recorder's tape can be read from them as -before, without the simulator keeping a log of every vehicle open all the time. A simulator that crashes -leaves the recording, but not these logs. +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 begins when the user first is in the vehicle, and goes on while the +user looks around outside it, until the user takes another vehicle, whose log begins then. Back in a +vehicle, its log goes on after the gap in its time. A vehicle with no driver of its own takes the log of the +driver of its train. So the speed recorder's tape can be read from them as before, also while the +simulator runs, without a log of every vehicle. Like those of `physicslog yes`, they are written every +two seconds, each file opened only for the moment it is written to; a crash loses the last two seconds +of them. The recording starts once the scenery is loaded and ends when the simulator closes. Going into the editor (F11) ends it too, since the editor may take away the tracks and the vehicles it refers to; no diff --git a/simulation/blackboxrecorder.cpp b/simulation/blackboxrecorder.cpp index b5465053b..141ff070e 100644 --- a/simulation/blackboxrecorder.cpp +++ b/simulation/blackboxrecorder.cpp @@ -23,6 +23,7 @@ http://mozilla.org/MPL/2.0/. #include "world/Track.h" #include "utilities/Globals.h" #include "utilities/Logs.h" +#include "utilities/physicslog.h" #include "utilities/utilities.h" #include @@ -280,7 +281,6 @@ void blackbox_recorder::close() { m_writer->end(m_time); m_writer.reset(); m_file.close(); - write_tapes(); using milliseconds = std::chrono::duration; WriteLog(std::format("Black box: recording closed; {} looks around, {:.3f} ms each on average, the longest {:.3f} ms", m_looks, m_looks > 0 ? milliseconds{m_looktime}.count() / m_looks : 0.0, milliseconds{m_looklongest}.count())); @@ -492,30 +492,16 @@ void blackbox_recorder::keep_tape_line() { return; auto const *vehicle{train->Dynamic()}; // the driver of the vehicle, whose log it would be: it keeps the name of its first vehicle, so a change of the cab - // in a unit goes on in the same log. in a vehicle without one, the driver of its train - auto const *driver{vehicle->Mechanik != nullptr ? vehicle->Mechanik : vehicle->ctOwner}; + // in a unit goes on in the same log. a passive one stands still (its clock too), so then the driver of the train + auto const *driver{vehicle->Mechanik != nullptr && vehicle->Mechanik->primary() ? vehicle->Mechanik : vehicle->ctOwner}; if (driver == nullptr) return; - m_tapes[driver->physicslog_name()] += driver->physicslog_line(); -} - -void blackbox_recorder::write_tapes() { - for (auto const &[name, lines] : m_tapes) { - // opened as the driver opens its log, so the file is the same to the byte - auto const filename{"physicslog/" + name + ".dat"}; - std::fstream file{filename, std::ios::in | std::ios::out | std::ios::trunc}; - if (false == file.is_open()) { - ErrorLog(std::format("Black box: can't create the physics log \"{}\"", filename)); - continue; - } - file << TController::physicslog_header() << lines; - file.close(); - if (file.fail()) - ErrorLog(std::format("Black box: the physics log \"{}\" wasn't written whole", filename)); - else - WriteLog(std::format("Black box: the physics log \"{}\" written from the recording", filename)); + 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)); } - m_tapes.clear(); + PhysicsLogs.append(name, driver->physicslog_line()); } void blackbox_recorder::look_around(bool const Keyframe) { diff --git a/simulation/blackboxrecorder.h b/simulation/blackboxrecorder.h index b7715e04c..093f22d1d 100644 --- a/simulation/blackboxrecorder.h +++ b/simulation/blackboxrecorder.h @@ -15,9 +15,9 @@ http://mozilla.org/MPL/2.0/. #include #include #include -#include #include #include +#include #include #include #include @@ -87,10 +87,10 @@ private: void write_map(); static std::vector> stop_point_headings(); void write_cab(); - // with the physics logs off, the line of the log of the driver the user is with, kept for the end + // with the physics logs off, the line of the log of the driver the user is with, to its log, physicslog/.dat, + // as the physics logs would have it. a log is begun at the first line of the recording, and goes on while the user + // looks around outside, until the user takes another vehicle; back in the vehicle, it goes on after the gap void keep_tape_line(); - // the kept lines, each driver's to its log, physicslog/.dat, as the physics logs would have it - void write_tapes(); // the clock, the user's vehicle, the vehicles, the switches and the signals void look_around(bool Keyframe); void look_at_vehicles(bool Keyframe); @@ -115,8 +115,8 @@ private: double m_time{0.0}; double m_cabdue{0.0}; double m_tapedue{0.0}; - // the lines of the physics log by the name of its file, of the times the user was with its driver - std::map m_tapes; + // the names of the logs begun by the recording + std::set m_tapes; double m_lookdue{0.0}; double m_keyframedue{0.0}; std::unordered_map m_vehicles; diff --git a/simulation/simulationtest.cpp b/simulation/simulationtest.cpp index 89a8a1046..8f1856663 100644 --- a/simulation/simulationtest.cpp +++ b/simulation/simulationtest.cpp @@ -15,6 +15,7 @@ http://mozilla.org/MPL/2.0/. #include "simulation/simulation.h" #include "utilities/Globals.h" #include "utilities/Logs.h" +#include "utilities/physicslog.h" #include "utilities/utilities.h" #include "vehicle/DynObj.h" #include "world/Event.h" @@ -262,6 +263,7 @@ void test_manager::check_wall_clock(double const Elapsed) 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(); 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 new file mode 100644 index 000000000..afcd0f8c7 --- /dev/null +++ b/tests/unit/test_physicslog.cpp @@ -0,0 +1,131 @@ +#include "stdafx.h" +#include "doctest.h" + +#include "utilities/physicslog.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +// the physics logs are kept and written by a thread of their own every so often, each file opened only to be written to + +namespace { + +// a folder of its own for the logs of a test, in the build folder of the tests, removed with the object. the writer +// makes it itself +class log_folder { +public: + explicit log_folder(std::string const &Name) : m_path{std::filesystem::path{EU07_UNIT_TESTS_SCRATCH} / std::format("eu07_physicslog_test_{}", Name)} { + std::filesystem::remove_all(m_path); + } + ~log_folder() { + std::error_code error; + std::filesystem::remove_all(m_path, error); + } + log_folder(log_folder const &) = delete; + log_folder &operator=(log_folder const &) = delete; + + std::filesystem::path const &path() const { return m_path; } + // the log as it stands, read in text mode as it is written: what was given to it + std::string read(std::string const &Name) const { + std::ifstream file{m_path / (Name + ".dat")}; + if (false == file.is_open()) + return {}; + std::ostringstream text; + text << file.rdbuf(); + return text.str(); + } + // waits up to five seconds for the log to read Text + bool reads(std::string const &Name, std::string const &Text) const { + auto const until{std::chrono::steady_clock::now() + std::chrono::seconds{5}}; + while (read(Name) != Text && std::chrono::steady_clock::now() < until) + std::this_thread::sleep_for(std::chrono::milliseconds{10}); + return read(Name) == Text; + } + +private: + std::filesystem::path m_path; +}; + +constexpr auto header{"Time[s] Velocity[m/s]\r\n"}; + +} // namespace + +TEST_CASE("physicslog: a log begun is emptied and given its header, and its lines follow it at each writing") { + log_folder folder{"begin"}; + physicslog_writer logs{folder.path(), std::chrono::hours{1}}; + + logs.begin("EU07-424", header); + logs.append("EU07-424", "0.0500 0.0000\r\n"); + CHECK(folder.read("EU07-424").empty()); + logs.flush(); + CHECK(folder.read("EU07-424") == std::string{header} + "0.0500 0.0000\r\n"); + + logs.append("EU07-424", "0.1000 1.5000\r\n"); + logs.flush(); + CHECK(folder.read("EU07-424") == std::string{header} + "0.0500 0.0000\r\n0.1000 1.5000\r\n"); + + // begun again: the lines not written yet are dropped along with those written + logs.append("EU07-424", "0.1500 3.0000\r\n"); + logs.begin("EU07-424", header); + logs.flush(); + CHECK(folder.read("EU07-424") == header); +} + +TEST_CASE("physicslog: no file is kept open between the writings, and a log not begun is added to as it stands") { + log_folder folder{"closed"}; + physicslog_writer logs{folder.path(), std::chrono::hours{1}}; + + logs.begin("SM42-001", header); + logs.flush(); + // Windows doesn't let a file open in a program be removed + std::error_code error; + CHECK(std::filesystem::remove(folder.path() / "SM42-001.dat", error)); + CHECK_FALSE(error); + + logs.append("SM42-001", "0.0500 0.0000\r\n"); + logs.flush(); + CHECK(folder.read("SM42-001") == "0.0500 0.0000\r\n"); +} + +TEST_CASE("physicslog: the thread writes the logs by itself") { + log_folder folder{"thread"}; + physicslog_writer logs{folder.path(), std::chrono::milliseconds{20}}; + + logs.begin("ET22-001", header); + logs.append("ET22-001", "0.0500 0.0000\r\n"); + CHECK(folder.reads("ET22-001", std::string{header} + "0.0500 0.0000\r\n")); + logs.append("ET22-001", "0.1000 0.0000\r\n"); + CHECK(folder.reads("ET22-001", std::string{header} + "0.0500 0.0000\r\n0.1000 0.0000\r\n")); +} + +TEST_CASE("physicslog: stopping writes what is kept at once, and what comes after waits for a writing") { + log_folder folder{"stop"}; + physicslog_writer logs{folder.path(), std::chrono::hours{1}}; + + logs.begin("EN57-1001", header); + logs.append("EN57-1001", "0.0500 0.0000\r\n"); + logs.stop(); + CHECK(folder.read("EN57-1001") == std::string{header} + "0.0500 0.0000\r\n"); + + logs.append("EN57-1001", "0.1000 0.0000\r\n"); + std::this_thread::sleep_for(std::chrono::milliseconds{50}); + CHECK(folder.read("EN57-1001") == std::string{header} + "0.0500 0.0000\r\n"); + logs.flush(); + CHECK(folder.read("EN57-1001") == std::string{header} + "0.0500 0.0000\r\n0.1000 0.0000\r\n"); +} + +TEST_CASE("physicslog: what is kept is written when the writer goes") { + log_folder folder{"end"}; + { + physicslog_writer logs{folder.path(), std::chrono::hours{1}}; + logs.begin("ST44-001", header); + logs.append("ST44-001", "0.0500 0.0000\r\n"); + } + CHECK(folder.read("ST44-001") == std::string{header} + "0.0500 0.0000\r\n"); +} diff --git a/utilities/physicslog.cpp b/utilities/physicslog.cpp new file mode 100644 index 000000000..cb57d5ca3 --- /dev/null +++ b/utilities/physicslog.cpp @@ -0,0 +1,98 @@ +/* +This Source Code Form is subject to the +terms of the Mozilla Public License, v. +2.0. If a copy of the MPL was not +distributed with this file, You can +obtain one at +http://mozilla.org/MPL/2.0/. +*/ + +#include "stdafx.h" +#include "utilities/physicslog.h" +#include "utilities/Logs.h" + +#include +#include +#include + +physicslog_writer PhysicsLogs; + +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() { + stop(); +} + +void physicslog_writer::begin(std::string const &Name, std::string_view const Header) { + std::scoped_lock lock{m_mutex}; + auto &lines{m_pending[Name]}; + lines.fresh = true; + lines.text.assign(Header); + start(); +} + +void physicslog_writer::append(std::string const &Name, std::string_view const Line) { + std::scoped_lock lock{m_mutex}; + m_pending[Name].text.append(Line); + start(); +} + +void physicslog_writer::flush() { + write(); +} + +void physicslog_writer::stop() { + { + std::scoped_lock lock{m_mutex}; + m_stopped = true; + } + // 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(); + } + write(); +} + +void physicslog_writer::start() { + if (m_stopped || m_thread.joinable()) + return; + m_thread = std::jthread([this](std::stop_token const &Stop) { run(Stop); }); +} + +void physicslog_writer::run(std::stop_token const &Stop) { + while (false == Stop.stop_requested()) { + { + std::unique_lock lock{m_mutex}; + // woken early only to stop, which writes the rest itself + if (m_wake.wait_for(lock, Stop, m_interval, [] { return false; }) || Stop.stop_requested()) + return; + } + write(); + } +} + +void physicslog_writer::write() { + std::scoped_lock files{m_filemutex}; + std::map taken; + { + std::scoped_lock lock{m_mutex}; + taken.swap(m_pending); + } + if (taken.empty()) + return; + std::error_code error; + std::filesystem::create_directories(m_folder, error); + for (auto const &[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()) + file << lines.text; + file.close(); + if (file.fail() && m_failed.insert(name).second) + ErrorLog("Bad physics log: can't write " + path.string()); + } +} diff --git a/utilities/physicslog.h b/utilities/physicslog.h new file mode 100644 index 000000000..2963c29a9 --- /dev/null +++ b/utilities/physicslog.h @@ -0,0 +1,67 @@ +/* +This Source Code Form is subject to the +terms of the Mozilla Public License, v. +2.0. If a copy of the MPL was not +distributed with this file, You can +obtain one at +http://mozilla.org/MPL/2.0/. +*/ + +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include +#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 them +class physicslog_writer { +public: + explicit physicslog_writer(std::filesystem::path Folder = "physicslog", std::chrono::milliseconds Interval = std::chrono::seconds{2}); + physicslog_writer(physicslog_writer const &) = delete; + physicslog_writer &operator=(physicslog_writer const &) = delete; + // stops, writing what is kept + ~physicslog_writer(); + + // starts the log Name anew: its file is emptied and Header written into it, the next time the logs are written. + // the lines of it not written yet are dropped + void begin(std::string const &Name, std::string_view Header); + // Line at the end of the log Name. a log not begun is added to as its file stands + void append(std::string const &Name, std::string_view Line); + // writes what is kept, now + void flush(); + // 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 + 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); + void write(); + + std::filesystem::path const m_folder; + std::chrono::milliseconds const m_interval; + std::mutex m_mutex; // the lines kept and the thread + std::map m_pending; + 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::set m_failed; // the logs whose file couldn't be written, told once +}; + +// the physics logs of the drivers (physicslog yes), and those the black box writes of the user's vehicles +extern physicslog_writer PhysicsLogs; diff --git a/vehicle/Driver.cpp b/vehicle/Driver.cpp index 8255a93cc..cde6ed99f 100644 --- a/vehicle/Driver.cpp +++ b/vehicle/Driver.cpp @@ -18,6 +18,7 @@ http://mozilla.org/MPL/2.0/. #include "utilities/Globals.h" #include "utilities/translation.h" #include "utilities/Logs.h" +#include "utilities/physicslog.h" #include "vehicle/Train.h" #include "vehicle/brakesetting.h" #include "vehicle/handover.h" @@ -2336,20 +2337,13 @@ TController::TController(bool AI, TDynamicObject *NewControll, bool InitPsyche, TableClear(); if( WriteLogFlag ) { -#ifdef _WIN32 - CreateDirectory( "physicslog", nullptr); -#elif __unix__ - mkdir( "physicslog", 0740 ); -#endif - LogFile.open( std::string( "physicslog/" + VehicleName + ".dat" ), - std::ios::in | std::ios::out | std::ios::trunc ); + // physicslog/.dat, written every two seconds along with the logs of the other drivers #if LOGPRESS == 0 - LogFile << physicslog_header(); + PhysicsLogs.begin( VehicleName, physicslog_header() ); #endif #if LOGPRESS == 1 - LogFile << string( "t\tVel\tAcc\tPP\tVVP\tBP\tBVP\tCVP" ).c_str() << "\n"; + PhysicsLogs.begin( VehicleName, "t\tVel\tAcc\tPP\tVVP\tBP\tBVP\tCVP\n" ); #endif - LogFile.flush(); } // HACK: give the simulation a small window to potentially replace the AI with a human driver @@ -2358,7 +2352,6 @@ TController::TController(bool AI, TDynamicObject *NewControll, bool InitPsyche, TController::~TController() { // wykopanie mechanika z roboty - // LogFile is closed by its own destructor }; // zamiana kodu rozkazu na opis @@ -5407,21 +5400,19 @@ std::string TController::physicslog_line() const { void TController::PhysicsLog() { // zapis logu - na razie tylko wypisanie parametrów - if (LogFile.is_open()) - { #if LOGPRESS == 0 - LogFile << physicslog_line(); + PhysicsLogs.append( VehicleName, physicslog_line() ); #endif #if LOGPRESS == 1 - LogFile << ElapsedTime << "\t" << fabs(11.31 * mvOccupied->WheelDiameter * mvOccupied->nrot) - << "\t"; - LogFile << Controlling->AccS << "\t"; - LogFile << mvOccupied->PipePress << "\t" << mvOccupied->CntrlPipePress << "\t" - << mvOccupied->BrakePress << "\t"; - LogFile << mvOccupied->Volume << "\t" << mvOccupied->Hamulec->GetCRP() << "\n"; + std::ostringstream line; + line << ElapsedTime << "\t" << fabs(11.31 * mvOccupied->WheelDiameter * mvOccupied->nrot) + << "\t"; + line << mvControlling->AccS << "\t"; + line << mvOccupied->PipePress << "\t" << mvOccupied->CntrlPipePress << "\t" + << mvOccupied->BrakePress << "\t"; + line << mvOccupied->Volume << "\t" << mvOccupied->Hamulec->GetCRP() << "\n"; + PhysicsLogs.append( VehicleName, line.str() ); #endif - LogFile.flush(); - } }; void diff --git a/vehicle/Driver.h b/vehicle/Driver.h index 02b2c60e2..306287311 100644 --- a/vehicle/Driver.h +++ b/vehicle/Driver.h @@ -721,9 +721,9 @@ private: // logs // methods + // the line of the log, to PhysicsLogs (utilities/physicslog.h) void PhysicsLog(); // members - std::ofstream LogFile; // zapis parametrow fizycznych double LastUpdatedTime = 0.0; // czas od ostatniego logu double ElapsedTime = 0.0; // czas od poczatku logu From 7b7529e24e51628516b249baff54f38f97954e2b Mon Sep 17 00:00:00 2001 From: maj00r Date: Sat, 14 Nov 2026 00:48:18 +0100 Subject: [PATCH 04/14] feat(blackbox): the cab record holds the speed of the wheels, apart from the vehicle's when they slip The speed of the cab record is the vehicle's. The speedometer, and a speed recorder's tape with it, shows the speed of the wheels as they turn, which runs away from it while they slip and drops to 0 while they are locked in a skid. It is added at the end of the record (194 bytes), where the readers skip what they don't know; the .dat tapes have it already, in their second column. --- blackbox_doc.md | 6 ++++-- simulation/blackboxrecorder.cpp | 2 ++ tests/unit/test_blackbox.cpp | 3 +++ vehicle/blackbox.cpp | 1 + vehicle/blackbox.h | 5 ++++- 5 files changed, 14 insertions(+), 3 deletions(-) diff --git a/blackbox_doc.md b/blackbox_doc.md index ea7322ca8..d81b143bc 100644 --- a/blackbox_doc.md +++ b/blackbox_doc.md @@ -172,14 +172,15 @@ record. ### 0x20 cab -The vehicle the user is in, every 0.1 s, also while the user looks around outside it. 190 bytes. +The vehicle the user is in, every 0.1 s, also while the user looks around outside it. 194 bytes (190 in the +files written before the speed of the wheels was added at the end). | type | field | |---------|------------------------------------------------------------------------| | `f64` | t | | `u32` | number of the vehicle | | `dvec3` | position | -| `f32` | speed [km/h], positive along the front of the vehicle | +| `f32` | speed of the vehicle [km/h], positive along its front | | `f32` | acceleration along the track [m/s²] | | `f32` | odometer [km] | | `u8` | driver: 0 none, 1 the user, 2 the AI (the autopilot) | @@ -231,6 +232,7 @@ The vehicle the user is in, every 0.1 s, also while the user looks around outsid | `f32` | the clutch of a diesel's gearbox, 0 open .. 1 closed | | `f32` | the filling of a diesel's hydraulic torque converter, 0..1 | | `f32` | the highest brake cylinder pressure of the vehicles of the train [bar] | +| `f32` | speed of the wheels [km/h], as the speedometer shows it, signed as the speed of the vehicle: apart from it while the wheels slip, 0 while they are locked in a skid | The lights the train is to show follow from the movement and the two patterns after it: in a train run the head the pattern asked for, the usual one Pc 1 (both headlights and the upper one; Pc 2 is the left red marker with the right and the diff --git a/simulation/blackboxrecorder.cpp b/simulation/blackboxrecorder.cpp index 141ff070e..bc572afbd 100644 --- a/simulation/blackboxrecorder.cpp +++ b/simulation/blackboxrecorder.cpp @@ -418,6 +418,8 @@ void blackbox_recorder::write_cab() { sample.vehicle = vehicle_id(vehicle); sample.position = vehicle->GetPosition(); sample.speed_kmh = static_cast(occupied->V * 3.6); + // of the wheels as they turn, which the speedometer shows: apart from that of the vehicle when they slip or skid + sample.wheel_speed_kmh = static_cast(M_PI * occupied->WheelDiameter * occupied->nrot * 3.6); sample.acceleration = static_cast(controlling->AccS); sample.lateral_acceleration = static_cast(occupied->AccN); if (owner != nullptr && owner->is_train()) { diff --git a/tests/unit/test_blackbox.cpp b/tests/unit/test_blackbox.cpp index b11253b49..cad1920a1 100644 --- a/tests/unit/test_blackbox.cpp +++ b/tests/unit/test_blackbox.cpp @@ -127,6 +127,7 @@ blackbox::cab_sample sample() { s.clutch = 0.75f; s.converter_fill = 0.25f; s.consist_brake_cylinder = 3.5f; + s.wheel_speed_kmh = 71.5f; return s; } @@ -285,6 +286,8 @@ TEST_CASE("blackbox: a sample of the user's train has the documented size and or CHECK(cab.get() == 0.25f); // the highest brake cylinder pressure of the train CHECK(cab.get() == 3.5f); + // the speed of the wheels + CHECK(cab.get() == 71.5f); CHECK(cab.done()); } diff --git a/vehicle/blackbox.cpp b/vehicle/blackbox.cpp index 8612df1bd..fc05f7483 100644 --- a/vehicle/blackbox.cpp +++ b/vehicle/blackbox.cpp @@ -205,6 +205,7 @@ void writer::cab(cab_sample const &Sample) { out.f32(Sample.clutch); out.f32(Sample.converter_fill); out.f32(Sample.consist_brake_cylinder); + out.f32(Sample.wheel_speed_kmh); emit(record::cab); } diff --git a/vehicle/blackbox.h b/vehicle/blackbox.h index 473cf677c..62fc1df38 100644 --- a/vehicle/blackbox.h +++ b/vehicle/blackbox.h @@ -257,6 +257,9 @@ struct cab_sample { float clutch{0.0f}; // the clutch of a diesel's gearbox, 0 open .. 1 closed float converter_fill{0.0f}; // the filling of a diesel's hydraulic torque converter, 0..1 float consist_brake_cylinder{0.0f}; // the highest brake cylinder pressure of the vehicles of the train [bar] + // [km/h] of the wheels as they turn, which the speedometer shows, signed as speed_kmh: apart from it while the wheels + // slip, and 0 while they are locked in a skid + float wheel_speed_kmh{0.0f}; }; // a station of the timetable of the user's train; the times in minutes of the day, -1 none @@ -354,7 +357,7 @@ struct signal_passing { // sizes of the parts of fixed size, as the readers expect them inline constexpr std::size_t header_size{12}; inline constexpr std::size_t record_header_size{5}; -inline constexpr std::size_t cab_sample_size{190}; +inline constexpr std::size_t cab_sample_size{194}; inline constexpr std::size_t vehicle_state_size{25}; // writes the records to a stream. nothing is written until header() is From 7547e6bf07072eb74b842f06643b9da3a8ecdec1 Mon Sep 17 00:00:00 2001 From: maj00r Date: Sun, 15 Nov 2026 23:32:38 +0100 Subject: [PATCH 05/14] 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 }; From 5d0bf6ed004a3c0719fa5c1e98c328ef5f57f99c Mon Sep 17 00:00:00 2001 From: maj00r Date: Sun, 15 Nov 2026 23:32:39 +0100 Subject: [PATCH 06/14] fix(blackbox): the wheels of the powered vehicle, as the speedometer has them, and the logs described as the drivers' they are In a unit driven from an unpowered car, the speed of the wheels was of the car the user is in, whose wheels never slip; now of the motor car, signed as the occupied vehicle's speed. The doc tells of a log per driver, and of the two seconds of real time a crash loses. --- blackbox_doc.md | 15 +++++++++------ simulation/blackboxrecorder.cpp | 10 ++++++++-- simulation/blackboxrecorder.h | 4 ++-- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/blackbox_doc.md b/blackbox_doc.md index d81b143bc..33757788a 100644 --- a/blackbox_doc.md +++ b/blackbox_doc.md @@ -14,13 +14,16 @@ 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 begins when the user first is in the vehicle, and goes on while the -user looks around outside it, until the user takes another vehicle, whose log begins then. Back in a -vehicle, its log goes on after the gap in its time. A vehicle with no driver of its own takes the log of the -driver of its train. So the speed recorder's tape can be read from them as before, also while the +`physicslog yes` would write. 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 +the gap in its time. A vehicle with no driver of its own, or whose own driver is passive, takes the log of +the driver of its train. So the speed recorder's tape can be read from them as before, also while the simulator runs, without a log of every vehicle. Like those of `physicslog yes`, they are written every two seconds, each file opened only for the moment it is written to; a crash loses the last two seconds -of them. +of real time of them (at a higher time speed, more of the simulated time). A file another program holds +for a while doesn't lose its lines: they are written once it can be. The recording starts once the scenery is loaded and ends when the simulator closes. Going into the editor (F11) ends it too, since the editor may take away the tracks and the vehicles it refers to; no @@ -232,7 +235,7 @@ files written before the speed of the wheels was added at the end). | `f32` | the clutch of a diesel's gearbox, 0 open .. 1 closed | | `f32` | the filling of a diesel's hydraulic torque converter, 0..1 | | `f32` | the highest brake cylinder pressure of the vehicles of the train [bar] | -| `f32` | speed of the wheels [km/h], as the speedometer shows it, signed as the speed of the vehicle: apart from it while the wheels slip, 0 while they are locked in a skid | +| `f32` | speed of the wheels [km/h], as the speedometer shows it: those of the powered vehicle (in a unit driven from an unpowered car, its motor car), signed as the speed of the vehicle: apart from it while the wheels slip, 0 while they are locked in a skid | The lights the train is to show follow from the movement and the two patterns after it: in a train run the head the pattern asked for, the usual one Pc 1 (both headlights and the upper one; Pc 2 is the left red marker with the right and the diff --git a/simulation/blackboxrecorder.cpp b/simulation/blackboxrecorder.cpp index bc572afbd..d53180744 100644 --- a/simulation/blackboxrecorder.cpp +++ b/simulation/blackboxrecorder.cpp @@ -418,8 +418,14 @@ void blackbox_recorder::write_cab() { sample.vehicle = vehicle_id(vehicle); sample.position = vehicle->GetPosition(); sample.speed_kmh = static_cast(occupied->V * 3.6); - // of the wheels as they turn, which the speedometer shows: apart from that of the vehicle when they slip or skid - sample.wheel_speed_kmh = static_cast(M_PI * occupied->WheelDiameter * occupied->nrot * 3.6); + // of the wheels as they turn, which the speedometer shows: apart from that of the vehicle when they slip or skid. + // those of the powered vehicle, as the speedometer has it (TTrain::DynamicSet), which in a unit driven from an + // unpowered car is another one, maybe facing the other way: signed as the occupied one's speed + auto *powered{train->Dynamic()->FindPowered()}; + if (powered == nullptr) + powered = train->Dynamic(); + auto const facing{powered->DirectionGet() * vehicle->DirectionGet()}; + sample.wheel_speed_kmh = static_cast(facing * M_PI * powered->MoverParameters->WheelDiameter * powered->MoverParameters->nrot * 3.6); sample.acceleration = static_cast(controlling->AccS); sample.lateral_acceleration = static_cast(occupied->AccN); if (owner != nullptr && owner->is_train()) { diff --git a/simulation/blackboxrecorder.h b/simulation/blackboxrecorder.h index 093f22d1d..67742b24e 100644 --- a/simulation/blackboxrecorder.h +++ b/simulation/blackboxrecorder.h @@ -88,8 +88,8 @@ private: static std::vector> stop_point_headings(); void write_cab(); // with the physics logs off, the line of the log of the driver the user is with, to its log, physicslog/.dat, - // as the physics logs would have it. a log is begun at the first line of the recording, and goes on while the user - // looks around outside, until the user takes another vehicle; back in the vehicle, it goes on after the gap + // as the physics logs would have it. a driver's log is begun at the first line given for that driver, and goes on + // while the user looks around outside or is in another vehicle of that driver; back with it, after the gap void keep_tape_line(); // the clock, the user's vehicle, the vehicles, the switches and the signals void look_around(bool Keyframe); From 043b9fb2f0143cd2d0ccc6048f02e06423d8d3fb Mon Sep 17 00:00:00 2001 From: maj00r Date: Sun, 15 Nov 2026 23:38:24 +0100 Subject: [PATCH 07/14] fix(physicslog): failed logs kept within 64 MB in all, cut outside the lock, never written twice; the writer knows itself when stopped The lines kept while files can't be written are cut to their share of 64 MB before the lock the simulation waits on. A log written in part is cut back to its length, so the next try doesn't write its start twice. A stop from the writer itself is told by its id even after another thread took it to join, and the watchdog's flush waits at most its time, the writing on a thread of its own. --- blackbox_doc.md | 2 +- simulation/blackboxrecorder.cpp | 5 ++- simulation/simulationtest.cpp | 4 +-- utilities/physicslog.cpp | 62 ++++++++++++++++++++++++--------- utilities/physicslog.h | 8 +++-- 5 files changed, 55 insertions(+), 26 deletions(-) diff --git a/blackbox_doc.md b/blackbox_doc.md index 33757788a..704d1e954 100644 --- a/blackbox_doc.md +++ b/blackbox_doc.md @@ -23,7 +23,7 @@ the driver of its train. So the speed recorder's tape can be read from them as b simulator runs, without a log of every vehicle. Like those of `physicslog yes`, they are written every two seconds, each file opened only for the moment it is written to; a crash loses the last two seconds of real time of them (at a higher time speed, more of the simulated time). A file another program holds -for a while doesn't lose its lines: they are written once it can be. +for a while doesn't lose its lines: they are written once it can be, the last 64 MB of all such logs at most. The recording starts once the scenery is loaded and ends when the simulator closes. Going into the editor (F11) ends it too, since the editor may take away the tracks and the vehicles it refers to; no diff --git a/simulation/blackboxrecorder.cpp b/simulation/blackboxrecorder.cpp index d53180744..28e453926 100644 --- a/simulation/blackboxrecorder.cpp +++ b/simulation/blackboxrecorder.cpp @@ -421,9 +421,8 @@ void blackbox_recorder::write_cab() { // of the wheels as they turn, which the speedometer shows: apart from that of the vehicle when they slip or skid. // those of the powered vehicle, as the speedometer has it (TTrain::DynamicSet), which in a unit driven from an // unpowered car is another one, maybe facing the other way: signed as the occupied one's speed - auto *powered{train->Dynamic()->FindPowered()}; - if (powered == nullptr) - powered = train->Dynamic(); + // the vehicle itself, if none in the unit is powered + auto const *powered{train->Dynamic()->FindPowered()}; auto const facing{powered->DirectionGet() * vehicle->DirectionGet()}; sample.wheel_speed_kmh = static_cast(facing * M_PI * powered->MoverParameters->WheelDiameter * powered->MoverParameters->nrot * 3.6); sample.acceleration = static_cast(controlling->AccS); diff --git a/simulation/simulationtest.cpp b/simulation/simulationtest.cpp index 11aca23c4..d32fd0e72 100644 --- a/simulation/simulationtest.cpp +++ b/simulation/simulationtest.cpp @@ -262,8 +262,8 @@ 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, or the physics logs by a disk - // which doesn't answer: the run ends anyway + // what was logged last gets written, unless the log is held up by a hung thread, or the physics logs by a writer + // holding their files or a disk which doesn't answer: the run ends anyway, two seconds later at most PhysicsLogs.flush_for(std::chrono::seconds{2}); StopLogService(); std::this_thread::sleep_for(std::chrono::milliseconds(250)); diff --git a/utilities/physicslog.cpp b/utilities/physicslog.cpp index 8a9ec2434..299b74221 100644 --- a/utilities/physicslog.cpp +++ b/utilities/physicslog.cpp @@ -11,7 +11,10 @@ http://mozilla.org/MPL/2.0/. #include "utilities/physicslog.h" #include "utilities/Logs.h" +#include #include +#include +#include #include #include @@ -19,8 +22,18 @@ 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}; +// the most of all the logs kept while their files can't be written, shared by those, a log never less than the +// least: the oldest lines go beyond it +constexpr std::size_t keptwhilefailing{64 * 1024 * 1024}; +constexpr std::size_t keptwhilefailingleast{64 * 1024}; + +// the text cut to its last lines within Most +void keep_last(std::string &Text, std::size_t const Most) { + if (Text.size() <= Most) + return; + auto const cut{Text.find('\n', Text.size() - Most)}; + Text.erase(0, cut == std::string::npos ? Text.size() : cut + 1); +} } // namespace @@ -51,11 +64,15 @@ void physicslog_writer::flush() { } 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; + // on a thread of its own, left behind if it doesn't end in time: a disk which doesn't answer holds it up in the + // writing, not only the lock. for the end of the process, which doesn't wait for it + auto done{std::make_shared>()}; + auto ended{done->get_future()}; + std::thread([this, done] { + flush(); + done->set_value(); + }).detach(); + return ended.wait_for(Timeout) == std::future_status::ready; } void physicslog_writer::stop() { @@ -63,15 +80,19 @@ void physicslog_writer::stop() { { std::scoped_lock lock{m_mutex}; m_stopped = true; + // stopped from the writer itself, by the end of a run which failed in it: the files may be held by it, and + // another thread may be joining it already + if (m_writer == std::this_thread::get_id()) { + if (m_thread.joinable()) { + m_thread.request_stop(); + m_thread.detach(); + } + return; + } // taken by the first to stop, so it's joined once thread = std::move(m_thread); } 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(); } @@ -82,6 +103,7 @@ void physicslog_writer::start() { if (m_stopped || m_thread.joinable()) return; m_thread = std::jthread([this](std::stop_token const &Stop) { run(Stop); }); + m_writer = m_thread.get_id(); } void physicslog_writer::run(std::stop_token const &Stop) { @@ -111,6 +133,10 @@ void physicslog_writer::write() { if (lines.text.empty() && false == lines.fresh) continue; auto const path{m_folder / (name + ".dat")}; + // how long it was, to be cut back to if the lines are written in part only, a disk filling up say: else + // they'd be written twice, the next time + auto const before{lines.fresh ? 0 : std::filesystem::file_size(path, error)}; + auto const existed{false == lines.fresh && false == static_cast(error)}; // 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()) { @@ -124,12 +150,18 @@ void physicslog_writer::write() { WriteLog("Physics log: " + path.string() + " written again"); continue; } + if (existed) + std::filesystem::resize_file(path, before, error); if (m_failed.insert(name).second) - ErrorLog("Bad physics log: can't write " + path.string() + ", its lines are kept for the next try"); + ErrorLog("Bad physics log: can't write " + path.string() + ", its last lines are kept for the next try"); failed.emplace(name, std::move(lines)); } if (failed.empty()) return; + // cut before the lock, which the simulation waits on to give its lines: only what came since is added under it + auto const most{std::max(keptwhilefailingleast, keptwhilefailing / failed.size())}; + for (auto &[name, lines] : failed) + keep_last(lines.text, most); // 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}; @@ -138,10 +170,6 @@ void physicslog_writer::write() { 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 024ae8fba..44d04b4c3 100644 --- a/utilities/physicslog.h +++ b/utilities/physicslog.h @@ -23,7 +23,7 @@ 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 real time of them. lines whose file -// can't be written, held by another program say, are kept for the next try +// 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}); @@ -39,7 +39,8 @@ 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 + // as flush, waited for no longer than Timeout: then false, and the writing goes on by itself, on a thread left + // behind. for the end of the process, the files held up or the disk not answering 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(); @@ -64,7 +65,8 @@ private: bool m_stopped{false}; std::condition_variable_any m_wake; std::jthread m_thread; - std::timed_mutex m_filemutex; // the files are written one batch at a time, so the lines of a log stay in order + std::thread::id m_writer; // of the thread, kept after it is taken to be joined: the writer knows itself by it + std::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 }; From b7950af6b62794f94e3fb7ded4373e0c5849c5ef Mon Sep 17 00:00:00 2001 From: maj00r Date: Sun, 15 Nov 2026 23:48:23 +0100 Subject: [PATCH 08/14] feat(physicslog): the logs of the trains only, physicslog.trains "physicslog.trains yes" in eu07.ini writes the .dat log of each driver who drives rail vehicles, not of the passengers nor of the road vehicles that make "physicslog yes" hundreds of files on a big scenery: the tape of every train. A driver made primary later begins its log then, and isn't counted while it doesn't log. The black box goes with it when the ini says nothing of its own, and doesn't write a second log of a driver writing its own. The doc tells the doors' sides are the vehicle's, the driver's swapped in the rear cab. --- blackbox_doc.md | 10 +++++++--- simulation/blackboxrecorder.cpp | 5 ++++- utilities/Globals.cpp | 8 ++++++++ vehicle/Driver.cpp | 35 +++++++++++++++++++++++---------- vehicle/Driver.h | 10 ++++++++++ 5 files changed, 54 insertions(+), 14 deletions(-) diff --git a/blackbox_doc.md b/blackbox_doc.md index 704d1e954..3573b4271 100644 --- a/blackbox_doc.md +++ b/blackbox_doc.md @@ -8,7 +8,9 @@ the timetable and how the train kept to it at each station, and where it was tol starter, a viewer) can replay the session on the map, or set the user's driving against the AI's. `blackbox yes` in `eu07.ini` writes it, with the physics logs on or off; `blackbox no` never does. With no -`blackbox` entry it is written along with the physics logs, `physicslog yes`. It is never written in an +`blackbox` entry it is written along with the physics logs, `physicslog yes` or `physicslog.trains yes`. +The latter writes the `.dat` logs of the trains only: of each driver who drives rail vehicles, not of the +passengers nor of the vehicles of the roads, so a big scenery gives far fewer of them. It is never written in an editor session. The file goes to `physicslog/_.blackbox` (the local time the recording started at), next to the `.dat` logs of the vehicles when those are on; they stay as they are. @@ -20,7 +22,8 @@ moves to another cab of the unit or another vehicle of the same train. Taking a goes on in that driver's log, begun then if it is the first time. Back with a driver, the log goes on after the gap in its time. A vehicle with no driver of its own, or whose own driver is passive, takes the log of the driver of its train. So the speed recorder's tape can be read from them as before, also while the -simulator runs, without a log of every vehicle. Like those of `physicslog yes`, they are written every +simulator runs, without a log of every vehicle. A driver who writes a log of its own, with +`physicslog.trains yes`, isn't written twice. Like those of `physicslog yes`, they are written every two seconds, each file opened only for the moment it is written to; a crash loses the last two seconds of real time of them (at a higher time speed, more of the simulated time). A file another program holds for a while doesn't lose its lines: they are written once it can be, the last 64 MB of all such logs at most. @@ -300,7 +303,8 @@ whose motors the cab drives (the motor car of a unit). | 0x0800'0000 | the cruise control holds a speed | The doors, their permits and steps, the departure signal and the releaser are of the vehicle the user is -in; the pumps, the fans, the engine's heat and the cruise control of the one whose motors the cab drives. +in. Left and right are those of the vehicle, seen from its front: from the rear cab (the cab -1) the driver's +left doors are the vehicle's right, as the simulator works them from there. The pumps, the fans, the engine's heat and the cruise control of the one whose motors the cab drives. The lights: 0x001 left headlight, 0x002 left red marker, 0x004 upper headlight, 0x010 right headlight, 0x020 right red marker, 0x040 rear-end signals, 0x080 left and 0x100 right auxiliary diff --git a/simulation/blackboxrecorder.cpp b/simulation/blackboxrecorder.cpp index 28e453926..6d0f76502 100644 --- a/simulation/blackboxrecorder.cpp +++ b/simulation/blackboxrecorder.cpp @@ -236,7 +236,7 @@ void blackbox_recorder::update(double const Deltatime) { return; m_started = true; // "blackbox yes" or "no" of eu07.ini decides by itself, with no entry it goes with the physics logs - if (false == Global.BlackBox.value_or(WriteLogFlag) || Global.editor_session) + if (false == Global.BlackBox.value_or(WriteLogFlag || WriteTrainLogFlag) || Global.editor_session) return; open(); } @@ -503,6 +503,9 @@ void blackbox_recorder::keep_tape_line() { auto const *driver{vehicle->Mechanik != nullptr && vehicle->Mechanik->primary() ? vehicle->Mechanik : vehicle->ctOwner}; if (driver == nullptr) return; + // a driver writing its own, of a train with "physicslog.trains yes", isn't written twice + if (driver->writes_physicslog()) + return; auto const &name{driver->physicslog_name()}; if (m_tapes.insert(name).second) { PhysicsLogs.begin(name, TController::physicslog_header()); diff --git a/utilities/Globals.cpp b/utilities/Globals.cpp index 9c0c10279..db9e0ba79 100644 --- a/utilities/Globals.cpp +++ b/utilities/Globals.cpp @@ -538,6 +538,13 @@ bool global_settings::ConfigParseSimulation(cParser& Parser, const std::string& return true; } + // the physics logs of the trains only, of the drivers of rail vehicles; "physicslog yes" writes them all anyway + if (token == "physicslog.trains") + { + ParseOne(Parser, WriteTrainLogFlag); + return true; + } + if (token == "blackbox") { bool blackbox{false}; @@ -1737,6 +1744,7 @@ global_settings::export_as_text( std::ostream &Output ) const { export_as_text( Output, "sound.volume.ambient", EnvironmentAmbientVolume ); export_as_text( Output, "sound.asyncload", AudioAsyncLoad ); export_as_text( Output, "physicslog", WriteLogFlag ); + export_as_text( Output, "physicslog.trains", WriteTrainLogFlag ); if( BlackBox ) { export_as_text( Output, "blackbox", *BlackBox ); } diff --git a/vehicle/Driver.cpp b/vehicle/Driver.cpp index cde6ed99f..03c3f9d8f 100644 --- a/vehicle/Driver.cpp +++ b/vehicle/Driver.cpp @@ -169,6 +169,7 @@ const double FSOverchargeCRP = 0.05; const double FSMaxTime = 10.0; const double DepartBlockedFSDelay = 10.0; bool WriteLogFlag = false; +bool WriteTrainLogFlag = false; double const deltalog = 0.05; // przyrost czasu std::string StopReasonTable[] = { @@ -2336,14 +2337,8 @@ TController::TController(bool AI, TDynamicObject *NewControll, bool InitPsyche, SetDriverPsyche(); // na końcu, bo wymaga ustawienia zmiennych TableClear(); - if( WriteLogFlag ) { - // physicslog/.dat, written every two seconds along with the logs of the other drivers -#if LOGPRESS == 0 - PhysicsLogs.begin( VehicleName, physicslog_header() ); -#endif -#if LOGPRESS == 1 - PhysicsLogs.begin( VehicleName, "t\tVel\tAcc\tPP\tVVP\tBP\tBVP\tCVP\n" ); -#endif + if( writes_physicslog() ) { + begin_physicslog(); } // HACK: give the simulation a small window to potentially replace the AI with a human driver @@ -5398,6 +5393,18 @@ std::string TController::physicslog_line() const { return line.str(); } +void TController::begin_physicslog() +{ + // physicslog/.dat, written every two seconds along with the logs of the other drivers +#if LOGPRESS == 0 + PhysicsLogs.begin( VehicleName, physicslog_header() ); +#endif +#if LOGPRESS == 1 + PhysicsLogs.begin( VehicleName, "t\tVel\tAcc\tPP\tVVP\tBP\tBVP\tCVP\n" ); +#endif + PhysicsLogBegun = true; +} + void TController::PhysicsLog() { // zapis logu - na razie tylko wypisanie parametrów #if LOGPRESS == 0 @@ -6463,10 +6470,18 @@ TController::update_timers( double dt ) { void TController::update_logs( double const dt ) { // log vehicle data + if( false == writes_physicslog() ) { + // not counted while it isn't logged: a driver which begins to, made primary, writes a line every 0.05 s + // from then on, not one each update to make up for the time before + LastUpdatedTime = 0.0; + return; + } LastUpdatedTime += dt; - if( WriteLogFlag - && LastUpdatedTime > deltalog ) { + if( LastUpdatedTime > deltalog ) { // zapis do pliku DAT + if( false == PhysicsLogBegun ) { + begin_physicslog(); + } PhysicsLog(); LastUpdatedTime -= deltalog; } diff --git a/vehicle/Driver.h b/vehicle/Driver.h index 306287311..bfeeb1b16 100644 --- a/vehicle/Driver.h +++ b/vehicle/Driver.h @@ -198,6 +198,9 @@ static const bool Humandriver = false; static const int maxorders = 64; // ilość rozkazów w tabelce static const int maxdriverfails = 4; // ile błędów może zrobić AI zanim zmieni nastawienie extern bool WriteLogFlag; // logowanie parametrów fizycznych +// the physics logs of the trains only (physicslog.trains yes): of each driver of rail vehicles who drives them, not +// of the passengers nor of the vehicles of the roads +extern bool WriteTrainLogFlag; static const int BrakeAccTableSize = 20; static const int gbh_NP = -2; //odciecie w hamulcu ogolnym @@ -723,7 +726,10 @@ private: // methods // the line of the log, to PhysicsLogs (utilities/physicslog.h) void PhysicsLog(); + // its log begun anew, its file emptied and given the header + void begin_physicslog(); // members + bool PhysicsLogBegun = false; // its log was begun, by this driver double LastUpdatedTime = 0.0; // czas od ostatniego logu double ElapsedTime = 0.0; // czas od poczatku logu @@ -736,6 +742,10 @@ public: // user is in, to write its log at the end when the logs of all the vehicles are off std::string const &physicslog_name() const { return VehicleName; } + // whether it writes its physics log itself: all with "physicslog yes", with "physicslog.trains yes" one who drives + // rail vehicles. a driver made primary later begins its log then + bool writes_physicslog() const { + return WriteLogFlag || ( WriteTrainLogFlag && is_train() && primary() ); } static std::string_view physicslog_header(); std::string physicslog_line() const; TDynamicObject *Vehicle( end const Side ) const { From b421550cbedf4939fe8935787e6d7494c2034ef7 Mon Sep 17 00:00:00 2001 From: maj00r Date: Sun, 15 Nov 2026 23:56:30 +0100 Subject: [PATCH 09/14] fix(physicslog): a train's log begun at its first line, so passive crews get no file Every crew is primary in the constructor and the drivers but one of a train with crews in more vehicles are made passive only before it starts, so a log begun there left files with only the header. With physicslog on all logs begin at once as before; one of a train waits for update_logs. --- vehicle/Driver.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/vehicle/Driver.cpp b/vehicle/Driver.cpp index 03c3f9d8f..23d778bdb 100644 --- a/vehicle/Driver.cpp +++ b/vehicle/Driver.cpp @@ -2337,7 +2337,10 @@ TController::TController(bool AI, TDynamicObject *NewControll, bool InitPsyche, SetDriverPsyche(); // na końcu, bo wymaga ustawienia zmiennych TableClear(); - if( writes_physicslog() ) { + // the logs of all, begun at once as they always were. one of a train only at its first line (update_logs): every + // crew is made primary here, and the drivers but one of a train with crews in more vehicles are made passive + // before it starts, so they get no file with only the header + if( WriteLogFlag ) { begin_physicslog(); } @@ -6471,8 +6474,8 @@ void TController::update_logs( double const dt ) { // log vehicle data if( false == writes_physicslog() ) { - // not counted while it isn't logged: a driver which begins to, made primary, writes a line every 0.05 s - // from then on, not one each update to make up for the time before + // not counted while it isn't logged (the driver of a road vehicle, or no logs on): were the logs turned on, + // it would write a line every 0.05 s from then on, not one each update to make up for the time before LastUpdatedTime = 0.0; return; } From 94b7c7f01b6c45512aa9b28ffa7bd889459f7488 Mon Sep 17 00:00:00 2001 From: maj00r Date: Mon, 16 Nov 2026 02:08:04 +0100 Subject: [PATCH 10/14] fix(physicslog): the writer's destructor throws nothing, its wait a plain condition variable woken to stop; a driver of no vehicle controls nothing --- utilities/physicslog.cpp | 11 +++++++++-- utilities/physicslog.h | 2 +- vehicle/Driver.cpp | 4 ++++ 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/utilities/physicslog.cpp b/utilities/physicslog.cpp index 299b74221..19f18684b 100644 --- a/utilities/physicslog.cpp +++ b/utilities/physicslog.cpp @@ -40,7 +40,11 @@ void keep_last(std::string &Text, std::size_t const Most) { 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() { - stop(); + // at the end of the process: what the logs couldn't keep, memory or a file, isn't to end it worse + try { + stop(); + } catch (...) { + } } void physicslog_writer::begin(std::string const &Name, std::string_view const Header) { @@ -94,6 +98,9 @@ void physicslog_writer::stop() { } if (thread.joinable()) { thread.request_stop(); + // the writer waits under the lock: taken once, it's either about to wait, seeing the stop, or waiting already + { std::scoped_lock lock{m_mutex}; } + m_wake.notify_all(); thread.join(); } flush(); @@ -111,7 +118,7 @@ void physicslog_writer::run(std::stop_token const &Stop) { { std::unique_lock lock{m_mutex}; // woken early only to stop, which writes the rest itself - if (m_wake.wait_for(lock, Stop, m_interval, [] { return false; }) || Stop.stop_requested()) + if (m_wake.wait_for(lock, m_interval, [&Stop] { return Stop.stop_requested(); })) return; } flush(); diff --git a/utilities/physicslog.h b/utilities/physicslog.h index 44d04b4c3..9ecb88349 100644 --- a/utilities/physicslog.h +++ b/utilities/physicslog.h @@ -63,7 +63,7 @@ private: std::mutex m_mutex; // the lines kept and the thread std::map m_pending; bool m_stopped{false}; - std::condition_variable_any m_wake; + std::condition_variable m_wake; std::jthread m_thread; std::thread::id m_writer; // of the thread, kept after it is taken to be joined: the writer knows itself by it std::mutex m_filemutex; // the files are written one batch at a time, so the lines of a log stay in order diff --git a/vehicle/Driver.cpp b/vehicle/Driver.cpp index 23d778bdb..d2ebd1fd7 100644 --- a/vehicle/Driver.cpp +++ b/vehicle/Driver.cpp @@ -6372,6 +6372,10 @@ void TController::ControllingSet() // problematyczne jest sterowanie z członu biernego, dlatego damy AI silnikowy // dzięki temu będzie wirtualna kabina w silnikowym, działająca w rozrządczym // w plikach FIZ zostały zgubione ujemne maski sprzęgów, stąd problemy z EZT + if( pVehicle == nullptr ) { + // a driver of no vehicle has nothing to control + return; + } mvOccupied = pVehicle->MoverParameters; // domyślny skrót do obiektu parametrów BrakeSystem = mvOccupied->BrakeSystem; // domyślny sposób hamowania mvControlling = pVehicle->FindPowered()->MoverParameters; // poszukiwanie członu sterowanego From 423d49b42cf7c69170d6004ec4d4e3f5480840e0 Mon Sep 17 00:00:00 2001 From: maj00r Date: Mon, 16 Nov 2026 02:12:01 +0100 Subject: [PATCH 11/14] fix(physicslog): every stop asked wakes the writer, the one from itself too; no psyche for a driver of no vehicle --- utilities/physicslog.cpp | 33 ++++++++++++++++++--------------- vehicle/Driver.cpp | 4 +++- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/utilities/physicslog.cpp b/utilities/physicslog.cpp index 19f18684b..d51d119b4 100644 --- a/utilities/physicslog.cpp +++ b/utilities/physicslog.cpp @@ -40,10 +40,10 @@ void keep_last(std::string &Text, std::size_t const Most) { 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() { - // at the end of the process: what the logs couldn't keep, memory or a file, isn't to end it worse try { stop(); } catch (...) { + // at the end of the process: what the logs couldn't keep, memory or a file, isn't to end it worse } } @@ -81,28 +81,25 @@ bool physicslog_writer::flush_for(std::chrono::milliseconds const Timeout) { void physicslog_writer::stop() { std::jthread thread; + bool itself; { std::scoped_lock lock{m_mutex}; m_stopped = true; - // stopped from the writer itself, by the end of a run which failed in it: the files may be held by it, and - // another thread may be joining it already - if (m_writer == std::this_thread::get_id()) { - if (m_thread.joinable()) { - m_thread.request_stop(); - m_thread.detach(); - } - return; - } // taken by the first to stop, so it's joined once thread = std::move(m_thread); + itself = m_writer == std::this_thread::get_id(); } - if (thread.joinable()) { + // the stop asked outside the lock, which waking the writer takes + if (thread.joinable()) thread.request_stop(); - // the writer waits under the lock: taken once, it's either about to wait, seeing the stop, or waiting already - { std::scoped_lock lock{m_mutex}; } - m_wake.notify_all(); - thread.join(); + // stopped from the writer itself, by the end of a run which failed in it: the files may be held by it + if (itself) { + if (thread.joinable()) + thread.detach(); + return; } + if (thread.joinable()) + thread.join(); flush(); } @@ -114,6 +111,12 @@ void physicslog_writer::start() { } void physicslog_writer::run(std::stop_token const &Stop) { + // woken by every stop asked, by stop() or by the end of the thread itself. the lock taken once first: the writer is + // either about to wait, and sees the stop, or waiting already + std::stop_callback const wake{Stop, [this] { + { std::scoped_lock lock{m_mutex}; } + m_wake.notify_all(); + }}; while (false == Stop.stop_requested()) { { std::unique_lock lock{m_mutex}; diff --git a/vehicle/Driver.cpp b/vehicle/Driver.cpp index d2ebd1fd7..07eaf4575 100644 --- a/vehicle/Driver.cpp +++ b/vehicle/Driver.cpp @@ -2334,7 +2334,9 @@ TController::TController(bool AI, TDynamicObject *NewControll, bool InitPsyche, iDrivigFlags |= movePrimary; // aktywnie prowadzące pojazd } - SetDriverPsyche(); // na końcu, bo wymaga ustawienia zmiennych + if( mvOccupied != nullptr ) { + SetDriverPsyche(); // na końcu, bo wymaga ustawienia zmiennych + } TableClear(); // the logs of all, begun at once as they always were. one of a train only at its first line (update_logs): every From 99b93abd64ece61377dfdfbeda6ef30055144b75 Mon Sep 17 00:00:00 2001 From: maj00r Date: Mon, 16 Nov 2026 12:47:22 +0100 Subject: [PATCH 12/14] fix(physicslog): nothing that goes wrong in the writing ends the simulator; a name in 1250 makes a path on a Windows set to UTF-8 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The writer's thread called flush() with nothing around it, so an exception leaving write() ended the process with std::terminate, with no message. One that happens: the file of a log was made a std::filesystem::path of the vehicle's name, which MSVC converts in the code page of the system and throws for bytes not of it; on a Windows with "Use Unicode UTF-8 for worldwide language support" a vehicle named in 1250, as the sceneries have them (0xB3 for ł), isn't UTF-8. - the file of a log is made of the name with MultiByteToWideChar without MB_ERR_INVALID_CHARS: the same file as before for every name of the code page, U+FFFD for the bytes not of it instead of a throw. Elsewhere the bytes are the name of the file as they were - a log whose writing throws for any other reason (memory) is kept for the next try as one whose file can't be written, and the other logs are written still - the thread, flush_for() and stop() write through flush_safely(), which lets nothing out: what was taken for a writing failing as a whole is lost and told in the log, the next writing goes on - the messages name the file by the bytes of the name, not by path::string(), which converts back Tests: a log which can't be written doesn't keep the others from theirs; a vehicle named in 1250 has its log, read back by physicslog_writer::file(). Reported in the review of #385. Co-Authored-By: Claude Opus 5.5 --- tests/unit/test_physicslog.cpp | 32 +++++++++++- utilities/physicslog.cpp | 96 +++++++++++++++++++++++++++------- utilities/physicslog.h | 11 ++++ 3 files changed, 117 insertions(+), 22 deletions(-) diff --git a/tests/unit/test_physicslog.cpp b/tests/unit/test_physicslog.cpp index 3189c424f..b61755d4d 100644 --- a/tests/unit/test_physicslog.cpp +++ b/tests/unit/test_physicslog.cpp @@ -32,8 +32,9 @@ public: std::filesystem::path const &path() const { return m_path; } // the log as it stands, read in text mode as it is written: what was given to it - std::string read(std::string const &Name) const { - std::ifstream file{m_path / (Name + ".dat")}; + std::string read(std::string const &Name) const { return read_file(m_path / (Name + ".dat")); } + static std::string read_file(std::filesystem::path const &File) { + std::ifstream file{File}; if (false == file.is_open()) return {}; std::ostringstream text; @@ -155,6 +156,33 @@ TEST_CASE("physicslog: a log begun anew while its file can't be written drops wh CHECK(folder.read("SU45-001") == std::string{header} + "0.0500 2.0000\r\n"); } +TEST_CASE("physicslog: a log whose file can't be written doesn't keep the others from theirs") { + log_folder folder{"failedother"}; + physicslog_writer logs{folder.path(), std::chrono::hours{1}}; + + std::filesystem::create_directories(folder.path() / "EU07-006.dat"); + logs.begin("EU07-006", header); + logs.append("EU07-006", "0.0500 0.0000\r\n"); + logs.begin("EU07-007", header); + logs.append("EU07-007", "0.0500 1.0000\r\n"); + logs.flush(); + CHECK(std::filesystem::is_directory(folder.path() / "EU07-006.dat")); + CHECK(folder.read("EU07-007") == std::string{header} + "0.0500 1.0000\r\n"); +} + +TEST_CASE("physicslog: a vehicle named in the code page of its scenery has its log, whatever the code page of the system") { + log_folder folder{"codepage"}; + physicslog_writer logs{folder.path(), std::chrono::hours{1}}; + + // "SM42-Łódź" in 1250, as the sceneries have it: not UTF-8, which a Windows set to it would have thrown on + std::string const name{"SM42-\xA3\xF3" "d\x9F"}; // apart, or the d would be read as a digit of the \xF3 + logs.begin(name, header); + logs.append(name, "0.0500 0.0000\r\n"); + logs.flush(); + CHECK(logs.file(name).parent_path() == folder.path()); + CHECK(log_folder::read_file(logs.file(name)) == std::string{header} + "0.0500 0.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}}; diff --git a/utilities/physicslog.cpp b/utilities/physicslog.cpp index d51d119b4..25ab2556d 100644 --- a/utilities/physicslog.cpp +++ b/utilities/physicslog.cpp @@ -12,6 +12,7 @@ http://mozilla.org/MPL/2.0/. #include "utilities/Logs.h" #include +#include #include #include #include @@ -35,9 +36,19 @@ void keep_last(std::string &Text, std::size_t const Most) { Text.erase(0, cut == std::string::npos ? Text.size() : cut + 1); } +// told in the log of the simulator, if there's the memory for it +void tell(std::string const &Text) noexcept { + try { + ErrorLog(Text); + } catch (...) { + // out of memory for a line of the log too: nothing more to be done about it + } +} + } // 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(std::filesystem::path Folder, std::chrono::milliseconds const Interval) : + m_folder{std::move(Folder)}, m_shownfolder{m_folder.generic_string()}, m_interval{Interval} {} physicslog_writer::~physicslog_writer() { try { @@ -73,7 +84,7 @@ bool physicslog_writer::flush_for(std::chrono::milliseconds const Timeout) { auto done{std::make_shared>()}; auto ended{done->get_future()}; std::thread([this, done] { - flush(); + flush_safely(); done->set_value(); }).detach(); return ended.wait_for(Timeout) == std::future_status::ready; @@ -100,7 +111,26 @@ void physicslog_writer::stop() { } if (thread.joinable()) thread.join(); - flush(); + flush_safely(); +} + +std::filesystem::path physicslog_writer::file(std::string const &Name) const { + auto const name{Name + ".dat"}; +#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 + auto const size{static_cast(name.size())}; + std::wstring wide(static_cast(::MultiByteToWideChar(CP_ACP, 0, name.data(), size, nullptr, 0)), L'\0'); + ::MultiByteToWideChar(CP_ACP, 0, name.data(), size, wide.data(), static_cast(wide.size())); + return m_folder / wide; +#else + // the bytes are the name of the file as they are + return m_folder / name; +#endif +} + +std::string physicslog_writer::shown(std::string const &Name) const { + return m_shownfolder + '/' + Name + ".dat"; } void physicslog_writer::start() { @@ -124,7 +154,17 @@ void physicslog_writer::run(std::stop_token const &Stop) { if (m_wake.wait_for(lock, m_interval, [&Stop] { return Stop.stop_requested(); })) return; } + flush_safely(); + } +} + +void physicslog_writer::flush_safely() noexcept { + try { flush(); + } catch (std::exception const &Error) { + tell(std::string{"Bad physics log: the writing failed ("} + Error.what() + "), the lines taken for it are lost"); + } catch (...) { + tell("Bad physics log: the writing failed, the lines taken for it are lost"); } } @@ -142,28 +182,22 @@ void physicslog_writer::write() { for (auto &[name, lines] : taken) { if (lines.text.empty() && false == lines.fresh) continue; - auto const path{m_folder / (name + ".dat")}; - // how long it was, to be cut back to if the lines are written in part only, a disk filling up say: else - // they'd be written twice, the next time - auto const before{lines.fresh ? 0 : std::filesystem::file_size(path, error)}; - auto const existed{false == lines.fresh && false == static_cast(error)}; - // 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 (lines.fresh) - file << lines.header; - file << lines.text; + // a log going wrong otherwise, out of memory say, is kept for the next try as one whose file can't be written: + // the others are written still, and the writer goes on + auto written{false}; + std::string problem{"can't write"}; + try { + written = write_log(name, lines); + } catch (std::exception const &Error) { + problem = std::string{"failed ("} + Error.what() + ") to write"; } - file.close(); - if (false == file.fail()) { + if (written) { if (m_failed.erase(name) > 0) - WriteLog("Physics log: " + path.string() + " written again"); + WriteLog("Physics log: " + shown(name) + " written again"); continue; } - if (existed) - std::filesystem::resize_file(path, before, error); if (m_failed.insert(name).second) - ErrorLog("Bad physics log: can't write " + path.string() + ", its last lines are kept for the next try"); + ErrorLog("Bad physics log: " + problem + " " + shown(name) + ", its last lines are kept for the next try"); failed.emplace(name, std::move(lines)); } if (failed.empty()) @@ -183,3 +217,25 @@ void physicslog_writer::write() { now = std::move(lines); } } + +bool physicslog_writer::write_log(std::string const &Name, pending const &Lines) const { + auto const path{file(Name)}; + std::error_code error; + // how long it was, to be cut back to if the lines are written in part only, a disk filling up say: else they'd be + // written twice, the next time + auto const before{Lines.fresh ? 0 : std::filesystem::file_size(path, error)}; + auto const existed{false == Lines.fresh && false == static_cast(error)}; + // 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 stream{path, Lines.fresh ? std::ios::out | std::ios::trunc : std::ios::out | std::ios::app}; + if (stream.is_open()) { + if (Lines.fresh) + stream << Lines.header; + stream << Lines.text; + } + stream.close(); + if (false == stream.fail()) + return true; + if (existed) + std::filesystem::resize_file(path, before, error); + return false; +} diff --git a/utilities/physicslog.h b/utilities/physicslog.h index 9ecb88349..59ed34828 100644 --- a/utilities/physicslog.h +++ b/utilities/physicslog.h @@ -44,6 +44,9 @@ 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 + std::filesystem::path file(std::string const &Name) const; private: struct pending { @@ -55,10 +58,18 @@ private: // the thread, on the first line given, unless stopped. called with m_mutex held void start(); void run(std::stop_token const &Stop); + // as flush, letting nothing out: on the writer's thread, or at the end of the process, an exception would end the + // simulator. what was taken for a writing which failed as a whole is lost, the next one goes on with what came since + void flush_safely() noexcept; // called with m_filemutex held 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; std::chrono::milliseconds const m_interval; std::mutex m_mutex; // the lines kept and the thread std::map m_pending; From 549899567258f891f62241a55185d5639239833f Mon Sep 17 00:00:00 2001 From: maj00r Date: Mon, 16 Nov 2026 12:47:22 +0100 Subject: [PATCH 13/14] fix(blackbox): the tapes go on after the editor, with the physics logs off The tapes of the user's vehicles were written by update() after the check for the writer of the recording, which close() drops on entering the editor (F11). With "physicslog no", the speed recorder's tape ended at F11 while the drivers' own logs of "physicslog yes" went on. The tapes are files of their own: they are kept from the moment the recording was to be made, the file made or not, closed or not, until the simulator closes, after the gap of the time in the editor. Only the recording itself stops at F11, as before. Reported in the review of #385. Co-Authored-By: Claude Opus 5.5 --- blackbox_doc.md | 3 ++- simulation/blackboxrecorder.cpp | 16 ++++++++++------ simulation/blackboxrecorder.h | 6 ++++-- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/blackbox_doc.md b/blackbox_doc.md index 3573b4271..8b4e23e0f 100644 --- a/blackbox_doc.md +++ b/blackbox_doc.md @@ -30,7 +30,8 @@ for a while doesn't lose its lines: they are written once it can be, the last 64 The recording starts once the scenery is loaded and ends when the simulator closes. Going into the editor (F11) ends it too, since the editor may take away the tracks and the vehicles it refers to; no -new one starts when driving goes on after that. +new one starts when driving goes on after that. The `.dat` logs it gives with the physics logs off go on +then, after the gap of the time in the editor, until the simulator closes: they are files of their own. It is written as the session goes and flushed once a second, so the file can be read while the simulator still writes it, and after a crash all but the last second is there. A file without the `end` record diff --git a/simulation/blackboxrecorder.cpp b/simulation/blackboxrecorder.cpp index 6d0f76502..efd92c3fc 100644 --- a/simulation/blackboxrecorder.cpp +++ b/simulation/blackboxrecorder.cpp @@ -238,22 +238,26 @@ void blackbox_recorder::update(double const Deltatime) { // "blackbox yes" or "no" of eu07.ini decides by itself, with no entry it goes with the physics logs if (false == Global.BlackBox.value_or(WriteLogFlag || WriteTrainLogFlag) || Global.editor_session) return; + m_taping = true; open(); } - if (m_writer == nullptr) + if (false == m_taping) return; std::scoped_lock lock{m_mutex}; m_time += Deltatime; - if (m_time >= m_cabdue) { - write_cab(); - m_cabdue = std::max(m_cabdue + cabinterval, m_time); - } - // with the physics logs on, each driver writes its own all the time + // with the physics logs on, each driver writes its own all the time. the tapes are files of their own: they go on + // after the recording ends in the editor, and with a recording which couldn't be made if (false == WriteLogFlag && m_time >= m_tapedue) { keep_tape_line(); m_tapedue = std::max(m_tapedue + tapeinterval, m_time); } + if (m_writer == nullptr) + return; + if (m_time >= m_cabdue) { + write_cab(); + m_cabdue = std::max(m_cabdue + cabinterval, m_time); + } if (m_time >= m_cameradue) { write_camera(); m_cameradue = std::max(m_cameradue + camerainterval, m_time); diff --git a/simulation/blackboxrecorder.h b/simulation/blackboxrecorder.h index 67742b24e..a3aaf5d0f 100644 --- a/simulation/blackboxrecorder.h +++ b/simulation/blackboxrecorder.h @@ -32,13 +32,14 @@ enum class user_command; namespace simulation { // the black box of the session (see blackbox_doc.md): written along with the physics logs, in driver mode, -// from the moment the scenario is loaded until the simulator closes or the editor is entered +// from the moment the scenario is loaded until the simulator closes or the editor is entered. the tapes of the user's +// vehicles, with the physics logs off, go on after the editor until the simulator closes class blackbox_recorder { public: // starts the recording once the scenario is ready, if it is to be made, then records what is due. // Deltatime: simulation time passed since the last call void update(double Deltatime); - // writes the end of the recording and closes the file. a recording closed isn't started again + // writes the end of the recording and closes the file. a recording closed isn't started again; its tapes go on void close(); // what happens between the looks, from wherever it happens; the cabs may run on threads of their own. @@ -112,6 +113,7 @@ private: std::ofstream m_file; std::unique_ptr m_writer; bool m_started{false}; // tried to start, made or not + bool m_taping{false}; // the recording was to be made: its tapes are kept from then on, the file made or not, closed or not double m_time{0.0}; double m_cabdue{0.0}; double m_tapedue{0.0}; From 0a1c4fd42505a5b80b0a6e7fb2da7a87205fa14d Mon Sep 17 00:00:00 2001 From: maj00r Date: Mon, 16 Nov 2026 13:00:16 +0100 Subject: [PATCH 14/14] 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;