Skip to content

Fix client side debugscript level filtering (#3498) #4323

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 22 additions & 14 deletions Client/mods/deathmatch/logic/CScriptDebugging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ CScriptDebugging::CScriptDebugging(CLuaManager* pLuaManager)
{
m_pLuaManager = pLuaManager;
m_uiLogFileLevel = 0;
m_pLogFile = NULL;
m_pLogFile = nullptr;
m_bTriggeringMessageEvent = false;
m_flushTimerHandle = NULL;
m_flushTimerHandle = nullptr;
}

CScriptDebugging::~CScriptDebugging()
Expand All @@ -33,13 +33,13 @@ CScriptDebugging::~CScriptDebugging()
fprintf(m_pLogFile, "INFO: Logging to this file ended\n");

// if we have a flush timer
if (m_flushTimerHandle != NULL)
if (m_flushTimerHandle != nullptr)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be reduced: if (m_flushTimerHandle)

{
// delete our flush timer
DeleteTimerQueueTimer(NULL, m_flushTimerHandle, INVALID_HANDLE_VALUE); // INVALID_HANDLE_VALUE = wait for running callbacks to finish
DeleteTimerQueueTimer(nullptr, m_flushTimerHandle, INVALID_HANDLE_VALUE); // INVALID_HANDLE_VALUE = wait for running callbacks to finish
}
fclose(m_pLogFile);
m_pLogFile = NULL;
m_pLogFile = nullptr;
}
}

Expand All @@ -52,7 +52,7 @@ void CScriptDebugging::LogBadLevel(lua_State* luaVM, unsigned int uiRequiredLeve
void CALLBACK TimerProc(void* lpParametar, BOOLEAN TimerOrWaitFired)
{
// Got a logfile?
if (CScriptDebugging::m_pLogFile != NULL)
if (CScriptDebugging::m_pLogFile != nullptr)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be reduced: if (CScriptDebugging::m_pLogFile)

{
// flush our log file
fflush((FILE*)CScriptDebugging::m_pLogFile);
Expand All @@ -68,13 +68,13 @@ bool CScriptDebugging::SetLogfile(const char* szFilename, unsigned int uiLevel)
{
fprintf(m_pLogFile, "INFO: Logging to this file ended\n");
// if we have a flush timer
if (m_flushTimerHandle != NULL)
if (m_flushTimerHandle != nullptr)
{
// delete our flush timer
DeleteTimerQueueTimer(NULL, m_flushTimerHandle, INVALID_HANDLE_VALUE); // INVALID_HANDLE_VALUE = wait for running callbacks to finish
DeleteTimerQueueTimer(nullptr, m_flushTimerHandle, INVALID_HANDLE_VALUE); // INVALID_HANDLE_VALUE = wait for running callbacks to finish
}
fclose(m_pLogFile);
m_pLogFile = NULL;
m_pLogFile = nullptr;
}

// Apply log size limit
Expand Down Expand Up @@ -102,38 +102,46 @@ bool CScriptDebugging::SetLogfile(const char* szFilename, unsigned int uiLevel)
// round 37.5 to 38 because we can't have half a message
// 8 * 256 bytes = 6004B
// round 6004 up to the nearest divisible by 1024 = 6144
// we have our buffer size.
setvbuf(pFile, NULL, _IOFBF, 6144);
setvbuf(pFile, nullptr, _IOFBF, 6144);

// Set the new pointer and level and return true
m_uiLogFileLevel = uiLevel;
m_pLogFile = pFile;

// Create a timer
::CreateTimerQueueTimer(&m_flushTimerHandle, NULL, TimerProc, NULL, 50, 50, WT_EXECUTEINTIMERTHREAD);
::CreateTimerQueueTimer(&m_flushTimerHandle, nullptr, TimerProc, nullptr, 50, 50, WT_EXECUTEINTIMERTHREAD);
return true;
}

return false;
}


void CScriptDebugging::UpdateLogOutput()
{
SLogLine line;
while (m_DuplicateLineFilter.PopOutputLine(line))
{
// Log it to the file if enough level
bool sufficientDebugLevel = CheckForSufficientDebugLevel(m_uiLogFileLevel, line.uiMinimumDebugLevel);

if (sufficientDebugLevel)
{
PrintLog(line.strText);
}

#ifdef MTA_DEBUG
if (!g_pCore->IsDebugVisible())
return;
#endif
g_pCore->DebugEchoColor(line.strText, line.ucRed, line.ucGreen, line.ucBlue);

std::uint8_t clientDebugLevel = 0;
auto* pLocalPlayer = g_pClientGame->GetPlayerManager()->GetLocalPlayer();
if (pLocalPlayer)
clientDebugLevel = pLocalPlayer->GetPlayerScriptDebugLevel();

bool shouldDisplayInConsole = CheckForSufficientDebugLevel(clientDebugLevel, line.uiMinimumDebugLevel);
if (shouldDisplayInConsole)
g_pCore->DebugEchoColor(line.strText, line.ucRed, line.ucGreen, line.ucBlue);
}
}

Expand Down
2 changes: 1 addition & 1 deletion Client/mods/deathmatch/logic/CScriptDebugging.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ class CScriptDebugging
SString ComposeErrorMessage(const char* szPrePend, const SLuaDebugInfo& luaDebugInfo, const char* szMessage);
void LogString(const char* szPrePend, const SLuaDebugInfo& luaDebugInfo, const char* szMessage, unsigned int uiMinimumDebugLevel, unsigned char ucRed = 255,
unsigned char ucGreen = 255, unsigned char ucBlue = 255);
bool CheckForSufficientDebugLevel(std::uint8_t playerDebugLevel, std::uint8_t messageDebugLevel) const noexcept;
void PrintLog(const char* szText);
bool CheckForSufficientDebugLevel(std::uint8_t playerDebugLevel, std::uint8_t messageDebugLevel) const noexcept;

public:
static FILE* m_pLogFile;
Expand Down
15 changes: 4 additions & 11 deletions Shared/mods/deathmatch/logic/CScriptDebugging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,24 +70,17 @@ void CScriptDebugging::LogPCallError(lua_State* luaVM, const SString& strRes, bo

bool CScriptDebugging::CheckForSufficientDebugLevel(std::uint8_t playerDebugLevel, std::uint8_t messageDebugLevel) const noexcept
{
bool sufficientDebugLevel = false;

switch (messageDebugLevel)
switch (messageDebugLevel)
{
case MESSAGE_TYPE_ERROR:
sufficientDebugLevel = (playerDebugLevel >= ERRORS_ONLY);
break;
return playerDebugLevel >= ERRORS_ONLY;
case MESSAGE_TYPE_WARNING:
sufficientDebugLevel = (playerDebugLevel >= ERRORS_AND_WARNINGS);
break;
return playerDebugLevel >= ERRORS_AND_WARNINGS;
case MESSAGE_TYPE_INFO:
case MESSAGE_TYPE_CUSTOM:
case MESSAGE_TYPE_DEBUG:
sufficientDebugLevel = (playerDebugLevel == ALL);
break;
return playerDebugLevel == ALL;
}

return sufficientDebugLevel;
}

void CScriptDebugging::LogCustom(lua_State* luaVM, unsigned char ucRed, unsigned char ucGreen, unsigned char ucBlue, const char* szFormat, ...)
Expand Down
Loading