18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-20 01:04:59 +01:00

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

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 <noreply@anthropic.com>
This commit is contained in:
maj00r
2026-11-16 12:47:22 +01:00
parent 423d49b42c
commit 99b93abd64
3 changed files with 117 additions and 22 deletions

View File

@@ -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}};