mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-27 21:45:16 +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:
@@ -552,6 +552,7 @@ set(CORE_SOURCES
|
||||
"world/TrackCheck.cpp"
|
||||
"world/SleeperPack.cpp"
|
||||
"McZapkie/Oerlikon_ESt.cpp"
|
||||
"utilities/logfiles.cpp"
|
||||
"utilities/parser.cpp"
|
||||
"utilities/utilities.cpp"
|
||||
"scene/sn_utils.cpp"
|
||||
|
||||
@@ -25,6 +25,7 @@ http://mozilla.org/MPL/2.0/.
|
||||
#include "rendering/renderer.h"
|
||||
#include "application/uilayer.h"
|
||||
#include "utilities/Logs.h"
|
||||
#include "utilities/logfiles.h"
|
||||
#include "world/SleeperWorker.h"
|
||||
#include "rendering/screenshot.h"
|
||||
#include "utilities/translation.h"
|
||||
@@ -403,8 +404,10 @@ int eu07_application::init(int Argc, char *Argv[])
|
||||
Global.threads.emplace("LogService", std::move(sLoggingService));
|
||||
|
||||
init_debug();
|
||||
result = init_settings(Argc, Argv);
|
||||
// the log files are named by the settings (multiplelogs), so they are cleared and opened once those are read
|
||||
init_files();
|
||||
if ((result = init_settings(Argc, Argv)) != 0)
|
||||
if (result != 0)
|
||||
{
|
||||
ErrorLog("Failed to initialize settings! Maybe you're missing eu07.ini file?");
|
||||
return result;
|
||||
@@ -1262,16 +1265,19 @@ void eu07_application::init_console()
|
||||
|
||||
void eu07_application::init_files()
|
||||
{
|
||||
|
||||
// without multiplelogs the run writes the shared log.txt / errors.txt, and those of the run before go. with it the
|
||||
// run writes files of its own in logs/, and the shared ones are left to whoever ran the game last without it
|
||||
for (auto const &file : stale_log_files(Global.MultipleLogs))
|
||||
{
|
||||
std::error_code error; // not there is fine
|
||||
std::filesystem::remove(file, error);
|
||||
}
|
||||
#ifdef _WIN32
|
||||
DeleteFile("log.txt");
|
||||
DeleteFile("errors.txt");
|
||||
CreateDirectory("logs", nullptr);
|
||||
#elif __unix__
|
||||
unlink("log.txt");
|
||||
unlink("errors.txt");
|
||||
mkdir("logs", 0750);
|
||||
#endif
|
||||
OpenLogFiles();
|
||||
}
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
|
||||
@@ -287,6 +287,8 @@ int generate_nodebank_previews(int Argc, char *Argv[])
|
||||
{
|
||||
Global.LoadIniFile("eu07.ini");
|
||||
}
|
||||
// the settings are read, so the log service writes as it goes instead of holding everything until it stops
|
||||
OpenLogFiles();
|
||||
// the progress goes to the console, the log keeps the details
|
||||
Global.iWriteLogEnabled &= ~2;
|
||||
std::thread logservice(LogService);
|
||||
|
||||
35
tests/unit/test_log_files.cpp
Normal file
35
tests/unit/test_log_files.cpp
Normal file
@@ -0,0 +1,35 @@
|
||||
#include "stdafx.h"
|
||||
#include "doctest.h"
|
||||
|
||||
#include "utilities/logfiles.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
// the files the log is written to, and which of them a run deletes when it starts
|
||||
|
||||
TEST_CASE("log files: without multiplelogs the run writes the shared log.txt and errors.txt")
|
||||
{
|
||||
CHECK(log_file_name(log_file::log, false, "td", "20261112_1200123") == "log.txt");
|
||||
CHECK(log_file_name(log_file::errors, false, "td", "20261112_1200123") == "errors.txt");
|
||||
}
|
||||
|
||||
TEST_CASE("log files: with multiplelogs the run writes its own in logs/, named after the scenery and the time")
|
||||
{
|
||||
CHECK(log_file_name(log_file::log, true, "autotest_case", "20261112_1200123") == "logs/log (autotest_case) 20261112_1200123.txt");
|
||||
CHECK(log_file_name(log_file::errors, true, "autotest_case", "20261112_1200123") == "logs/errors (autotest_case) 20261112_1200123.txt");
|
||||
}
|
||||
|
||||
TEST_CASE("log files: without multiplelogs the start deletes the shared files it is going to write")
|
||||
{
|
||||
// errors.txt is made at the first error only: one of the run before must not stay behind a run without errors
|
||||
auto const stale{stale_log_files(false)};
|
||||
CHECK(stale.size() == 2);
|
||||
for (auto const kind : {log_file::log, log_file::errors})
|
||||
CHECK(std::ranges::count(stale, log_file_name(kind, false, "td", "20261112_1200123")) == 1);
|
||||
}
|
||||
|
||||
TEST_CASE("log files: with multiplelogs the start keeps the shared log.txt and errors.txt of the last ordinary run")
|
||||
{
|
||||
// the scenario tests run with multiplelogs; the logs of the player's last game in the game folder stay
|
||||
CHECK(stale_log_files(true).empty());
|
||||
}
|
||||
@@ -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";
|
||||
}
|
||||
|
||||
|
||||
@@ -26,6 +26,9 @@ enum class logtype : unsigned int {
|
||||
// writes the log until StopLogService(), and all of it that came before
|
||||
void LogService();
|
||||
void StopLogService();
|
||||
// the settings are read: the log files can be named and written from now on; until then the log service keeps what
|
||||
// is logged
|
||||
void OpenLogFiles();
|
||||
void WriteLog( const char *str, logtype const Type = logtype::generic, bool isError = false );
|
||||
void Error( const std::string &asMessage, bool box = false );
|
||||
void Error( const char* &asMessage, bool box = false );
|
||||
|
||||
26
utilities/logfiles.cpp
Normal file
26
utilities/logfiles.cpp
Normal file
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
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/logfiles.h"
|
||||
|
||||
#include <format>
|
||||
|
||||
std::string log_file_name(log_file const Kind, bool const MultipleLogs, std::string_view const Scenery, std::string_view const Date)
|
||||
{
|
||||
std::string_view const name{Kind == log_file::errors ? "errors" : "log"};
|
||||
return MultipleLogs ? std::format("logs/{} ({}) {}.txt", name, Scenery, Date) : std::format("{}.txt", name);
|
||||
}
|
||||
|
||||
std::vector<std::string> stale_log_files(bool const MultipleLogs)
|
||||
{
|
||||
if (MultipleLogs)
|
||||
return {};
|
||||
return {log_file_name(log_file::log, false, {}, {}), log_file_name(log_file::errors, false, {}, {})};
|
||||
}
|
||||
29
utilities/logfiles.h
Normal file
29
utilities/logfiles.h
Normal file
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
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 <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
// the files the log is written to, and which of them a run clears when it starts. kept apart from the log service so
|
||||
// the unit tests can check them
|
||||
|
||||
// the log of everything, and the list of the errors (made at the first error)
|
||||
enum class log_file { log, errors };
|
||||
|
||||
// the name of a log file: the shared one in the game folder, or with multiplelogs the run's own in logs/, named after
|
||||
// the scenery and the time
|
||||
std::string log_file_name(log_file Kind, bool MultipleLogs, std::string_view Scenery, std::string_view Date);
|
||||
|
||||
// the shared log files a run deletes when it starts: the ones it writes itself, that is without multiplelogs, so an
|
||||
// errors.txt of an earlier run doesn't stay behind when this one logs no error. with multiplelogs the run writes files
|
||||
// of its own in logs/, and the log.txt / errors.txt of the last ordinary run are kept
|
||||
std::vector<std::string> stale_log_files(bool MultipleLogs);
|
||||
Reference in New Issue
Block a user