18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-28 23:13:23 +01:00

Keep the shared log.txt and errors.txt when the run writes its own logs (multiplelogs) (#341)

* fix: keep the shared log.txt and errors.txt when the run writes multiplelogs

Every start deleted log.txt and errors.txt in the game folder before the
settings were read, so a run with multiplelogs (each scenario test) wiped
the logs of the player's last ordinary game although it writes its own
files in logs/.

The shared files are now deleted after the settings, and only without
multiplelogs, where the run is about to write them (an errors.txt of an
earlier run must still not stay behind a run without errors). The log
service keeps what is logged until then, so an early message can't open
and empty log.txt before multiplelogs is known. The file names come from
one place (utilities/logfiles), covered by unit tests.

* refactor(log): the wait for the log file names without Sonar issues

The log service skips a pass with an early continue instead of wrapping
the writing in an if, so the writing keeps its nesting. No change in
behaviour.

* fix(log): the node bank preview tool opens its log files after the settings

The log service holds every message until OpenLogFiles() is called, and
only the game called it. The preview tool kept its whole log in memory
and wrote it only at the end, so a crash or a killed run lost it.
This commit is contained in:
jerrrrycho
2026-11-25 06:14:43 +01:00
committed by GitHub
parent cd60d0abc1
commit 8c8d5ff5cc
8 changed files with 129 additions and 9 deletions

View File

@@ -9,6 +9,7 @@ http://mozilla.org/MPL/2.0/.
#include "stdafx.h"
#include "utilities/Logs.h"
#include "utilities/logfiles.h"
#include "utilities/crashsignals.h"
#include <format>
@@ -82,6 +83,12 @@ std::atomic<bool> &log_stopped()
static std::atomic<bool> stopped{false};
return stopped;
}
// set once the settings are read: the files are named by them (multiplelogs, the scenery)
std::atomic<bool> &log_files_open()
{
static std::atomic<bool> open{false};
return open;
}
} // namespace
void StopLogService()
@@ -89,6 +96,11 @@ void StopLogService()
log_stopped() = true;
}
void OpenLogFiles()
{
log_files_open() = true;
}
void LogService()
{
@@ -96,6 +108,13 @@ void LogService()
for (bool last{false}; false == last;)
{
last = log_stopped();
// what is logged before the settings are read waits for them, or it would open (and empty) the shared log.txt
// of a run that writes its own files in logs/. a start that stops before that writes it anyway
if (false == log_files_open() && false == last)
{
std::this_thread::sleep_for(std::chrono::milliseconds(50));
continue;
}
{
// --- Obsługa InfoStack ---
while (!InfoStack.empty())
@@ -114,7 +133,7 @@ void LogService()
{
if (!output.is_open())
{
std::string filename = Global.MultipleLogs ? "logs/log (" + filename_scenery() + ") " + filename_date() + ".txt" : "log.txt";
std::string const filename { log_file_name(log_file::log, Global.MultipleLogs, filename_scenery(), filename_date()) };
output.open(filename, std::ios::trunc);
// the stack of a crash on Linux goes to the end of this file
crash_signals::set_log_path(filename);
@@ -153,8 +172,7 @@ void LogService()
if (!errors.is_open())
{
std::string filename = Global.MultipleLogs ? "logs/errors (" + filename_scenery() + ") " + filename_date() + ".txt" : "errors.txt";
errors.open(filename, std::ios::trunc);
errors.open(log_file_name(log_file::errors, Global.MultipleLogs, filename_scenery(), filename_date()), std::ios::trunc);
errors << "EU07.EXE " + Global.asVersion << "\n";
}