Skip to content

Commit

Permalink
Do not output text formatting sequences to the log file.
Browse files Browse the repository at this point in the history
  • Loading branch information
MiranDMC committed Oct 5, 2024
1 parent f037ea9 commit 5afa22f
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 29 deletions.
33 changes: 33 additions & 0 deletions cleo_sdk/CLEO_Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
59 changes: 30 additions & 29 deletions source/CDebug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,9 @@ void CDebug::Trace(CLEO::eLogLevel level, const char* msg)
{
std::lock_guard<std::mutex> 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

Expand All @@ -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"
Expand Down

0 comments on commit 5afa22f

Please sign in to comment.