mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-27 21:45:16 +01:00
* 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.
292 lines
7.1 KiB
C++
292 lines
7.1 KiB
C++
/*
|
|
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/Logs.h"
|
|
#include "utilities/logfiles.h"
|
|
#include "utilities/crashsignals.h"
|
|
|
|
#include <format>
|
|
|
|
#include "utilities/Globals.h"
|
|
#include "winheaders.h"
|
|
#include "utilities/utilities.h"
|
|
#include "application/uilayer.h"
|
|
#include <deque>
|
|
|
|
std::ofstream output; // standardowy "log.txt", można go wyłączyć
|
|
std::ofstream errors; // lista błędów "errors.txt", zawsze działa
|
|
std::ofstream comms; // lista komunikatow "comms.txt", można go wyłączyć
|
|
|
|
char const endstring[10] = "\n";
|
|
|
|
std::deque<std::string> log_scrollback;
|
|
|
|
std::string filename_date() {
|
|
::SYSTEMTIME st;
|
|
|
|
#ifdef __unix__
|
|
timespec ts;
|
|
clock_gettime(CLOCK_REALTIME, &ts);
|
|
std::tm const tm = local_tm(ts.tv_sec);
|
|
std::tm const *tms = &tm;
|
|
st.wYear = static_cast<uint16_t>(tms->tm_year);
|
|
st.wMonth = static_cast<uint16_t>(tms->tm_mon);
|
|
st.wDayOfWeek = static_cast<uint16_t>(tms->tm_wday);
|
|
st.wDay = static_cast<uint16_t>(tms->tm_mday);
|
|
st.wHour = static_cast<uint16_t>(tms->tm_hour);
|
|
st.wMinute = static_cast<uint16_t>(tms->tm_min);
|
|
st.wSecond = static_cast<uint16_t>(tms->tm_sec);
|
|
st.wMilliseconds = static_cast<uint16_t>(ts.tv_nsec / 1000000);
|
|
#elif _WIN32
|
|
::GetLocalTime( &st );
|
|
#endif
|
|
|
|
return std::format(
|
|
"{}{:02}{:02}_{:02}{:02}{:03}",
|
|
st.wYear,
|
|
st.wMonth,
|
|
st.wDay,
|
|
st.wHour,
|
|
st.wMinute,
|
|
st.wMilliseconds);
|
|
}
|
|
|
|
std::string filename_scenery() {
|
|
|
|
auto extension = Global.SceneryFile.rfind( '.' );
|
|
if( extension != std::string::npos ) {
|
|
return Global.SceneryFile.substr( 0, extension );
|
|
}
|
|
else {
|
|
return Global.SceneryFile;
|
|
}
|
|
}
|
|
|
|
// log service stacks
|
|
std::deque < std::pair<std::string, bool>> InfoStack;
|
|
std::deque<std::string> ErrorStack;
|
|
|
|
// lock for log stacks
|
|
std::mutex logMutex;
|
|
namespace
|
|
{
|
|
// the log is written through the closing of the simulator, to the end: set once the closing has nothing more to tell
|
|
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()
|
|
{
|
|
log_stopped() = true;
|
|
}
|
|
|
|
void OpenLogFiles()
|
|
{
|
|
log_files_open() = true;
|
|
}
|
|
|
|
|
|
void LogService()
|
|
{
|
|
// one more pass after the stop, so what came just before it gets written too
|
|
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())
|
|
{
|
|
std::string msg;
|
|
bool isError;
|
|
{
|
|
std::scoped_lock lock(logMutex);
|
|
msg = InfoStack.front().first;
|
|
isError = InfoStack.front().second;
|
|
InfoStack.pop_front();
|
|
}
|
|
|
|
// log to file
|
|
if (Global.iWriteLogEnabled & 1)
|
|
{
|
|
if (!output.is_open())
|
|
{
|
|
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);
|
|
}
|
|
output << msg << "\n";
|
|
output.flush();
|
|
}
|
|
|
|
// log to scrollback imgui
|
|
log_scrollback.emplace_back(msg);
|
|
if (log_scrollback.size() > 200)
|
|
log_scrollback.pop_front();
|
|
|
|
// log to console; not in test mode, where the standard output carries the result of the test only
|
|
if ((Global.iWriteLogEnabled & 2) && false == Global.test.enabled)
|
|
{
|
|
if (isError)
|
|
printf("\033[1;37;41m%s\033[0m\n", msg.c_str());
|
|
else
|
|
printf("\033[32m%s\033[0m\n", msg.c_str());
|
|
}
|
|
}
|
|
|
|
// --- Obsługa ErrorStack ---
|
|
while (!ErrorStack.empty())
|
|
{
|
|
std::string msg;
|
|
{
|
|
std::scoped_lock lock(logMutex);
|
|
msg = ErrorStack.front();
|
|
ErrorStack.pop_front();
|
|
}
|
|
|
|
if (!(Global.iWriteLogEnabled & 1))
|
|
continue;
|
|
|
|
if (!errors.is_open())
|
|
{
|
|
errors.open(log_file_name(log_file::errors, Global.MultipleLogs, filename_scenery(), filename_date()), std::ios::trunc);
|
|
errors << "EU07.EXE " + Global.asVersion << "\n";
|
|
}
|
|
|
|
errors << msg << "\n";
|
|
errors.flush();
|
|
}
|
|
}
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
|
}
|
|
}
|
|
|
|
|
|
bool ShouldSkipLog(std::string_view str, logtype type)
|
|
{
|
|
return str.empty() ||
|
|
TestFlag(Global.DisabledLogTypes, static_cast<unsigned int>(type));
|
|
}
|
|
|
|
std::string FormatLogMessage(std::string_view str)
|
|
{
|
|
const auto now = std::chrono::steady_clock::now();
|
|
const auto elapsed = now - Global.startTimestamp;
|
|
const double seconds = std::chrono::duration<double>(elapsed).count();
|
|
|
|
return std::format("[ {:8.3f} ]\t\t{}", seconds, str);
|
|
}
|
|
|
|
void WriteLog(std::string_view str, logtype type, bool isError)
|
|
{
|
|
if (ShouldSkipLog(str, type))
|
|
return;
|
|
|
|
const auto message = FormatLogMessage(str);
|
|
|
|
std::scoped_lock lock(logMutex);
|
|
InfoStack.emplace_back(message, isError);
|
|
}
|
|
|
|
void ErrorLog(std::string_view str, logtype type)
|
|
{
|
|
if (ShouldSkipLog(str, type))
|
|
return;
|
|
|
|
const auto message = FormatLogMessage(str);
|
|
|
|
std::scoped_lock lock(logMutex);
|
|
ErrorStack.push_back(message);
|
|
}
|
|
|
|
void WriteLog(const char* str, logtype type, bool isError)
|
|
{
|
|
if (str == nullptr || *str == '\0')
|
|
return;
|
|
|
|
WriteLog(std::string_view{str}, type, isError);
|
|
}
|
|
|
|
void ErrorLog(const char* str, logtype type)
|
|
{
|
|
if (str == nullptr || *str == '\0')
|
|
return;
|
|
|
|
ErrorLog(std::string_view{str}, type);
|
|
}
|
|
|
|
|
|
void Error(const std::string &asMessage, bool /*box*/)
|
|
{
|
|
// if (box)
|
|
// MessageBox(NULL, asMessage.c_str(), string("EU07 " + Global.asRelease).c_str(), MB_OK);
|
|
ErrorLog(asMessage);
|
|
}
|
|
|
|
void Error(const char *&asMessage, bool /*box*/)
|
|
{
|
|
// if (box)
|
|
// MessageBox(NULL, asMessage, string("EU07 " + Global.asRelease).c_str(), MB_OK);
|
|
ErrorLog(asMessage);
|
|
WriteLog(asMessage);
|
|
}
|
|
|
|
void ErrorLog(const std::string &str, logtype const Type )
|
|
{
|
|
ErrorLog( str.c_str(), Type );
|
|
WriteLog( str.c_str(), Type, true );
|
|
}
|
|
|
|
void WriteLog(const std::string &str, logtype const Type )
|
|
{ // Ra: wersja z AnsiString jest zamienna z Error()
|
|
WriteLog( str.c_str(), Type );
|
|
};
|
|
|
|
void CommLog(const char *str)
|
|
{ // Ra: warunkowa rejestracja komunikatów
|
|
WriteLog(str);
|
|
/* if (Global.iWriteLogEnabled & 4)
|
|
{
|
|
if (!comms.is_open())
|
|
{
|
|
comms.open("comms.txt", std::ios::trunc);
|
|
comms << AnsiString("EU07.EXE " + Global.asRelease).c_str() << "\n";
|
|
}
|
|
if (str)
|
|
comms << str;
|
|
comms << "\n";
|
|
comms.flush();
|
|
}*/
|
|
};
|
|
|
|
void CommLog(const std::string &str)
|
|
{ // Ra: wersja z AnsiString jest zamienna z Error()
|
|
WriteLog(str);
|
|
};
|
|
|
|
//---------------------------------------------------------------------------
|