diff --git a/CMakeLists.txt b/CMakeLists.txt index 9097a57c9..a6504301f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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" diff --git a/application/application.cpp b/application/application.cpp index bc4b7dc1d..25f620766 100644 --- a/application/application.cpp +++ b/application/application.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; diff --git a/nodebank_previews_standalone.cpp b/nodebank_previews_standalone.cpp index 65c5e07f3..93fc39732 100644 --- a/nodebank_previews_standalone.cpp +++ b/nodebank_previews_standalone.cpp @@ -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); diff --git a/tests/unit/test_log_files.cpp b/tests/unit/test_log_files.cpp new file mode 100644 index 000000000..8a0aac6f3 --- /dev/null +++ b/tests/unit/test_log_files.cpp @@ -0,0 +1,35 @@ +#include "stdafx.h" +#include "doctest.h" + +#include "utilities/logfiles.h" + +#include + +// 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()); +} diff --git a/utilities/Logs.cpp b/utilities/Logs.cpp index f4032d808..1b122535d 100644 --- a/utilities/Logs.cpp +++ b/utilities/Logs.cpp @@ -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 @@ -82,6 +83,12 @@ std::atomic &log_stopped() static std::atomic stopped{false}; return stopped; } +// set once the settings are read: the files are named by them (multiplelogs, the scenery) +std::atomic &log_files_open() +{ + static std::atomic 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"; } diff --git a/utilities/Logs.h b/utilities/Logs.h index 63727de9c..5fd5d4a56 100644 --- a/utilities/Logs.h +++ b/utilities/Logs.h @@ -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 ); diff --git a/utilities/logfiles.cpp b/utilities/logfiles.cpp new file mode 100644 index 000000000..7dd9a6745 --- /dev/null +++ b/utilities/logfiles.cpp @@ -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 + +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 stale_log_files(bool const MultipleLogs) +{ + if (MultipleLogs) + return {}; + return {log_file_name(log_file::log, false, {}, {}), log_file_name(log_file::errors, false, {}, {})}; +} diff --git a/utilities/logfiles.h b/utilities/logfiles.h new file mode 100644 index 000000000..7c88ea436 --- /dev/null +++ b/utilities/logfiles.h @@ -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 +#include +#include + +// 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 stale_log_files(bool MultipleLogs);