mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-19 22:44: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:
@@ -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}};
|
||||
|
||||
@@ -12,6 +12,7 @@ http://mozilla.org/MPL/2.0/.
|
||||
#include "utilities/Logs.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <exception>
|
||||
#include <fstream>
|
||||
#include <future>
|
||||
#include <memory>
|
||||
@@ -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<std::promise<void>>()};
|
||||
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<int>(name.size())};
|
||||
std::wstring wide(static_cast<std::size_t>(::MultiByteToWideChar(CP_ACP, 0, name.data(), size, nullptr, 0)), L'\0');
|
||||
::MultiByteToWideChar(CP_ACP, 0, name.data(), size, wide.data(), static_cast<int>(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<bool>(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<bool>(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;
|
||||
}
|
||||
|
||||
@@ -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<std::string, pending> m_pending;
|
||||
|
||||
Reference in New Issue
Block a user