From 5afa22fd79d3213f5de8f860b659fc37dfecfa76 Mon Sep 17 00:00:00 2001 From: Miran Date: Sat, 5 Oct 2024 14:00:18 +0200 Subject: [PATCH] Do not output text formatting sequences to the log file. --- cleo_sdk/CLEO_Utils.h | 33 ++++++++++++++++++++++++ source/CDebug.cpp | 59 ++++++++++++++++++++++--------------------- 2 files changed, 63 insertions(+), 29 deletions(-) diff --git a/cleo_sdk/CLEO_Utils.h b/cleo_sdk/CLEO_Utils.h index 3555d8e6..884e1c14 100644 --- a/cleo_sdk/CLEO_Utils.h +++ b/cleo_sdk/CLEO_Utils.h @@ -117,6 +117,39 @@ namespace CLEO } } + // erase GTA's text formatting sequences like ~r~ + static void StringRemoveFormatting(std::string& str) + { + size_t pos = 0; + while (true) + { + pos = str.find('~', pos); + if (pos == std::string::npos || + (pos + 2) >= str.length()) // not enought characters left for formatting sequence + { + break; + } + + if (str[pos + 2] != '~') + { + pos++; + continue; + } + + switch (str[pos + 1]) + { + case 'n': + case 'N': + str.replace(pos, 3, "\n"); + break; + + default: + str.erase(pos, 3); + break; + } + } + } + static std::string ScriptInfoStr(CLEO::CRunningScript* thread) { std::string info(1024, '\0'); diff --git a/source/CDebug.cpp b/source/CDebug.cpp index 7afc4234..3866cb66 100644 --- a/source/CDebug.cpp +++ b/source/CDebug.cpp @@ -11,36 +11,9 @@ void CDebug::Trace(CLEO::eLogLevel level, const char* msg) { std::lock_guard guard(mutex); - static char szBuf[1024]; - - // time stamp - SYSTEMTIME t; - - //GetLocalTime(&t); - void (__stdcall * GTA_GetLocalTime)(LPSYSTEMTIME lpSystemTime) = memory_pointer(0x0081E514); // use ingame function instead as GetLocalTime seems to be considered suspicious by some AV software - GTA_GetLocalTime(&t); - - sprintf(szBuf, "%02d/%02d/%04d %02d:%02d:%02d.%03d ", t.wDay, t.wMonth, t.wYear, t.wHour, t.wMinute, t.wSecond, t.wMilliseconds); - char* stampEnd = szBuf + strlen(szBuf); - - // add separator line if frame rendered since last log entry - if (lastFrame != CTimer::m_FrameCounter) - { - if (m_hFile.good()) - m_hFile << szBuf << std::endl; - - lastFrame = CTimer::m_FrameCounter; - } - - strcpy(stampEnd, msg); - - // output to file - if(m_hFile.good()) - m_hFile << szBuf << std::endl; - // output to console #ifdef _DEBUG - OutputDebugString(szBuf); + OutputDebugString(msg); OutputDebugString("\n"); #endif @@ -51,9 +24,37 @@ void CDebug::Trace(CLEO::eLogLevel level, const char* msg) for (void* func : cleo.GetCallbacks(eCallbackId::Log)) { typedef void WINAPI callback(eLogLevel, const char*); - ((callback*)func)(level, stampEnd); + ((callback*)func)(level, msg); } } + + // output to log file + if (m_hFile.good()) + { + // time stamp + SYSTEMTIME t; + //GetLocalTime(&t); + void(__stdcall * GTA_GetLocalTime)(LPSYSTEMTIME lpSystemTime) = memory_pointer(0x0081E514); // use ingame function instead as GetLocalTime seems to be considered suspicious by some AV software + GTA_GetLocalTime(&t); + + char timestampStr[32]; + sprintf(timestampStr, "%02d/%02d/%04d %02d:%02d:%02d.%03d ", t.wDay, t.wMonth, t.wYear, t.wHour, t.wMinute, t.wSecond, t.wMilliseconds); + m_hFile << timestampStr; + + // add separator line if frame rendered since last log entry + if (lastFrame != CTimer::m_FrameCounter) + { + m_hFile << timestampStr << std::endl; + lastFrame = CTimer::m_FrameCounter; + } + + // message + auto message = std::string(msg); + StringRemoveFormatting(message); + m_hFile << message.c_str(); + + m_hFile << std::endl; + } } extern "C"