17
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-10-17 08:39:19 +02:00
Files
maszyna/utilities/Logs.cpp
jerrrrycho 1f256e9593 refactor(sonar): mark unused parameters (cpp:S1172)
Unused names are commented out; parameters used only on some platforms
are marked [[maybe_unused]].

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 15:18:45 +02:00

258 lines
6.0 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/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 logbuffer[ 256 ];
char 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
std::snprintf(
logbuffer,
sizeof(logbuffer),
"%d%02d%02d_%02d%02d%03d",
st.wYear,
st.wMonth,
st.wDay,
st.wHour,
st.wMinute,
st.wMilliseconds);
return std::string( logbuffer );
}
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;
void LogService()
{
while (!Global.applicationQuitOrder)
{
{
// --- 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);
}
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
if (Global.iWriteLogEnabled & 2)
{
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.c_str());
}
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);
};
//---------------------------------------------------------------------------