18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-27 21:45:16 +01:00
Files
maszyna/utilities/Logs.cpp
jerrrrycho 3eb2fdf462 Merge staging into feat/gl46-migration
origin/staging 7ac2472c3 into feat/gl46-migration db26a33ed, so that #468
merges into staging. Merge base 037158104. Staging carries master up to
571487612 (squashed in #514); this branch had merged master up to 7c12b83ba.

Conflicts, file by file:
- README.md: WITH_BETTER_RENDERER row dropped (gl46), USE_SYSTEM_JSON row kept (staging)
- CMakeLists.txt: gl46 lists, plus staging's new core sources (hardwareinfo, sn_utils, regionfile, inionlysettings, fieldshunting, gaugewrap, drivingaid, signalpassing, brakereleaser, crew, brakecharge); none listed twice
- tests/unit/CMakeLists.txt: both (texturealpha, freecameramotion; editorSwitchDrives, Camera)
- vehicle/brakingcurve.{h,cpp}, tests/unit/test_braking_curve.cpp (add/add): staging (#479 brake_acting on the same #409 code)
- editor/editorHttp.cpp: staging (it has gl46's #432 and certificate fixes, plus #439 libcurl loaded at run time)
- tests/unit/test_editor_http.cpp (add/add): staging (adds the available() case of #439)
- utilities/Globals.h: gl46 crashdump_full (staging's CrashDumpFull is #425, replaced here by utilities/crashdump.cpp); fmradio and staging's OpenAL options both; gfx_headlights_beamwidth (#481) not taken
- utilities/Globals.cpp: both (odometer.expiry, OpenAL keys, fmradio, gl46 gfx keys); gfx.headlights.beamwidth (#481) not taken
- utilities/Logs.cpp: staging's log_file_name() for both files, with gl46's crashdump_set_log_file() for both
- utilities/utilities.h: gl46 set_thread_random_engine(); Random(int, int) gone on both sides
- tests/unit/test_utilities.cpp: staging's includes
- simulation/simulationtest.cpp: gl46 (no NvRenderer, LegacyRenderer, BasicRenderer here)
- ci_shadervalidator/* (incl. add/add utilities/Logs.h): gl46's validator of shaders/ogl46 and shaders/vk14; staging's #437 fixed the GL 3.3 one
- gl/shader.cpp, gl/glsl_common.cpp: gl46 (shaders come from rendering/common/shadersource.cpp); HEADLIGHT_BEAM_WIDTH (#481) not taken
- shaders/mat_wire.frag: gl46. Rename detection paired staging's mat_colored.frag (#481) with it
- lang/editor_pl.po: union; the 12 camera speed / orthophoto entries staging adds are here already (master #312)
- audio/audiorenderer.{h,cpp}: both (fmradio; startsample and the low-pass filter); the destructor releases the radio, then the filter
- application/editormode.h, application/editormodeswitchdrive.cpp: staging (#509 on #330, which is all this side had)
- application/application.cpp: gl46's start sequence (no init_ogl, ANGLE or NvRenderer; crash window after init_ui) with staging's hardware::log_system() (#389)
- EU07.cpp: gl46 (crash reports of utilities/crashdump.cpp); staging's side was #425's handler, replaced here
- model/Texture.{h,cpp}: gl46 (texture_source); staging's side is master #399/#411/#362/#386, merged here before
- world/Track.h, world/TrackSleepers.h: both (TrackBlend, lay(Fastened)/fasten; TrackBuckle, pack_models)
- world/Segment.cpp: staging's track buckling on the split stray factors both sides have
- vehicle/Driver.h: staging's additions (#444, #494, #473, #346, timetable flags)
- vehicle/Driver.cpp: staging's file, with this branch's f4f8c75e4 (road orders measured along the lane) and 8fbf05674 (a holding road order blocks the ones past it) applied again; #409's SeenAheadTolerance gives way to #473
- vehicle/DynObj.h: friend opengl46_renderer and friend class vehicle_table
- vehicle/DynObj.cpp: gl46's vehicle_table::update() with staging's update_off_track_contacts() and update_brake_pipes()
- McZapkie/Mover.cpp: staging's function splits (#239) and hose exchange (#388); every line this branch added to Mover.cpp/MOVER.h is kept
- .github/workflows/build_bin.yml: staging's apt-install step (#497) and pinned download-artifact (#331)

Modify/delete, kept deleted:
- rendering/opengl33renderer.{cpp,h}, rendering/openglparticles.cpp, rendering/openglrenderer.{cpp,h}: staging's versions are master's, and every master change to them is in this branch's history already
- shaders/light_common.glsl, shaders/mat_sunlessnormalmap_specgloss.frag: see below

Carried into this branch's code:
- the hose exchange after each sub-step (#387/#388) also in update_steps_parallel(), per group of vehicles, as update() does it single-threaded
- physics.pipewake (pipe_wakes(), wake_cause::scndpipe) in staging's GetScndDVc(), where the main reservoir hose exchange went
- hardware::log_gl_context() (#389) in after_window() of the GL 4.6 backend, init_ogl() being gone

Taken back out of the auto-merge (renderers this branch removed):
- shaders/vk14/tonemapping.glsl: #481's switch to ACES, applied there by rename detection
- environment/skydome.cpp, simulation/simulationenvironment.cpp: #415, the sky of the legacy renderer
- application/application.cpp: #384's fallback of an unknown gfxrenderer, which this branch had dropped

Not carried from staging (renderer files removed here):
- master #481 (571487612): old assets drawn as in 26.05 (metal diffuse, sun highlight tint, ACES, SUN_DIFFUSE_SCALE 2.5, untinted Diffuse, mat_colored base) and gfx.headlights.beamwidth. Made for the GL 3.3 shaders; the PBR shaders of shaders/ogl46 and vk14 (and their SPIR-V binaries) are the author's call
- #437: the unique temporary file of the shader validator (Sonar S5443); gl46's validator still writes /tmp/shader*

Unit tests: 1133 of 1137 cases pass. Four staging tests fail (derailment: a
repaired vehicle is back on the rails; three pullee cases): the vehicle
stands, and with physics.sleepheld (on by default here) its physics sleeps
with the Fb / FStand of before. With physics.sleepheld off they pass. Left
to the author.
2026-11-27 05:26:13 +01:00

299 lines
7.5 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 "utilities/crashdump.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);
crashdump_set_log_file(crashdump_log_file::log, filename);
// 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 const filename { log_file_name(log_file::errors, Global.MultipleLogs, filename_scenery(), filename_date()) };
errors.open(filename, std::ios::trunc);
crashdump_set_log_file(crashdump_log_file::errors, filename);
errors << "EU07.EXE " + Global.asVersion << "\n";
}
errors << msg << "\n";
errors.flush();
}
}
if (!last)
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);
// at once rather than by the log service, which may not get to it before a crash
crashdump_note_log(message.data(), message.size());
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);
};
//---------------------------------------------------------------------------