mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-21 07:24:57 +01:00
* fix: a failed start says why in the log and ends with an error code When the window could not be made, the log only said "Bad init: failed to create glfw window", without the reason glfw knows (no OpenGL driver, no pixel format that fits, no EGL). Sometimes it did not even say that: the log thread writes every 50 ms, and main() ended the process with std::_Exit(0) without stopping it, so a start that failed within that time left a log that ends at "// startup". - glfw's errors go to the log and errors.txt through glfwSetErrorCallback, set before glfwInit(); each distinct message once. - after a failed start (or an exception caught in main) the log thread is stopped and waited for, as the test runs already do, so nothing logged last is lost. - a failed start ends with exit code 1 instead of 0, so the starter can tell it from a closing. A start that worked ends as before. * fix: glfw's errors told during the start only, a closed crash dialog is no failed start After review of the previous commit: - the glfw error callback is taken off at the end of init(). Once the simulator runs, glfw would only repeat what the platform can't do (glfwSetCursorPos on Wayland, on every mouse-look) or what a scenery asks for in vain (launcher key codes below GLFW_KEY_SPACE, checked on every update), which would put new lines in errors.txt of existing sceneries and cost a format and a lock per call. - closing the crash-report window ends the simulator as before, but init() now returns 1 for it (above 0: closed by the player), and only a result below 0 gives exit code 1. - a start that throws from init() (bad_alloc, or a runtime_error on Windows) ends with exit code 1 too; an exception after a start that worked ends with 0 as before. * feat: a small crash dump by default, the full one with "crashdump.full yes" The dump written by the crash handler (Windows, WITHDUMPGEN) held all the memory of the simulator (MiniDumpWithFullMemory), so it was as large as the memory the simulator took: gigabytes on a large scenery, written to the game folder at the moment of the crash, on machines with 8 GB of RAM too. By default it now holds the stacks with the memory they point to, the threads, the modules, the handles, the memory map and the modules' data segments (MiniDumpWithDataSegs in place of MiniDumpWithFullMemory, the rest of the flags as before). The globals, Global among them, stay readable. "crashdump.full yes" in eu07.ini writes the full dump as before. Measured on td.scn with a ghostview camera (1.2 GB committed): the small dump 44 MB in 2 s, the full one 1.6 GB in 19 s. The key is new and optional: eu07.ini files without it read as before, and the setting is listed in the log at start. * fix: the small crash dump keeps the process and thread blocks MiniDumpWithProcessThreadData adds the PEB and the TEBs, a few KB: the command line (the scenery and the vehicle), the environment and the last error of each thread, which the small dump otherwise lacks, since the heap behind Global's longer strings is not in it. * fix: an exception that ends the simulator says what it was in the log After review of #425: - the exceptions main() catches are logged ("Critical error: ..."): on Windows the runtime_error that only showed a message box, and on the other systems any std::exception, which used to end the simulator through std::terminate with no word in the log. On Windows the others are still left to CrashHandler, whose dump shows where they were thrown. - every exception caught there ends with exit code 1, in the start or later, as a crash; it used to be 0 when caught, the abort code when not. - closing the crash-report window logs "Crash report window closed, closing" instead of the error "Failed to run crash gui!", which stays for a result below 0. * feat: the stack of a crash on Linux in the log and on the standard error On Linux no dump is written (the crash handler is Windows only, crashpad is off), so a crash left a log that just stopped. A handler of SIGSEGV, SIGBUS, SIGFPE, SIGILL and SIGABRT (installed at the start of main) now writes, with write() only: Crash: signal 11 (SIGSEGV) at address 0x... Version: EU07 (cmake), <hash> (...) Stack (addr2line -f -C -e <the eu07 file of this version> <offset>): <backtrace_symbols_fd> to the end of the log file (its whole path, set when the log opens it) and to the standard error, which StarterNG shows first. The Linux builds carry their symbols (GENERATE_PDB adds -g, nothing strips them), so addr2line on the eu07 file of the same version gives the files and lines. Then the signal ends the process as before (SA_RESETHAND, raise): the same exit status, and a core dump where the system makes one. The handler runs on an alternate stack, so an overflow of the main thread's stack is reported too; backtrace() is warmed up at install so it doesn't allocate in the handler, and alarm(5) ends a handler that hangs (a crash in the allocator or the loader). WAMR is built without its hardware bound checks, so it installs no SIGSEGV handler of its own. Elsewhere crash_signals does nothing. A unit test (Linux only) crashes a child process and checks the report on its standard error and in the log, and that the child still ended by SIGSEGV. * fix: the crash report can't be cut by a second crash signal or end with SIGALRM After review: - the five crash signals are blocked while a report is written, so a second one waits rather than starting a second report in its middle (a fault that comes while blocked still ends the process). - a report that hangs past alarm(5) ends the process by the signal of the crash, not by SIGALRM: a SIGALRM handler, set only in the crash handler, raises the crash signal and unblocks it. - the child of the unit test calls alarm(10) first, so a child stuck before the crash can't hang the build. --------- Co-authored-by: maj00r <maj00r@host>
274 lines
6.6 KiB
C++
274 lines
6.6 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/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;
|
|
}
|
|
} // namespace
|
|
|
|
void StopLogService()
|
|
{
|
|
log_stopped() = 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();
|
|
{
|
|
// --- 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 filename = Global.MultipleLogs ? "logs/log (" + filename_scenery() + ") " + filename_date() + ".txt" : "log.txt";
|
|
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())
|
|
{
|
|
std::string filename = Global.MultipleLogs ? "logs/errors (" + filename_scenery() + ") " + filename_date() + ".txt" : "errors.txt";
|
|
errors.open(filename, 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);
|
|
};
|
|
|
|
//---------------------------------------------------------------------------
|