Logging code cleanup

* Change qualifiers for some types (make const, static const, etc..).
* Renamed 'CNotifyText' to 'CTextNotify'.
* Renamed 's_LogMutex' to 'g_LogMutex' (mutex was not static).
This commit is contained in:
Kawe Mazidjatari 2022-10-30 10:08:14 +01:00
parent 3eadb1ed93
commit 196bf4c2b3
6 changed files with 54 additions and 56 deletions

View File

@ -84,7 +84,7 @@ SQRESULT SQVM_PrintFunc(HSQUIRRELVM v, SQChar* fmt, ...)
static std::shared_ptr<spdlog::logger> wconsole = spdlog::get("win_console");
static std::shared_ptr<spdlog::logger> sqlogger = spdlog::get("sqvm_info");
s_LogMutex.lock();
g_LogMutex.lock();
const char* pszUpTime = Plat_GetProcessUpTime();
{/////////////////////////////
@ -204,7 +204,7 @@ SQRESULT SQVM_PrintFunc(HSQUIRRELVM v, SQChar* fmt, ...)
g_spd_sys_w_oss.str("");
g_spd_sys_w_oss.clear();
s_LogMutex.unlock();
g_LogMutex.unlock();
return SQ_OK;
}
@ -228,7 +228,7 @@ SQRESULT SQVM_WarningFunc(HSQUIRRELVM v, SQInteger a2, SQInteger a3, SQInteger*
return result;
}
s_LogMutex.lock();
g_LogMutex.lock();
const char* pszUpTime = Plat_GetProcessUpTime();
#ifdef GAMEDLL_S3
context = v->GetContext();
@ -296,7 +296,7 @@ SQRESULT SQVM_WarningFunc(HSQUIRRELVM v, SQInteger a2, SQInteger a3, SQInteger*
g_spd_sys_w_oss.str("");
g_spd_sys_w_oss.clear();
s_LogMutex.unlock();
g_LogMutex.unlock();
return result;
}

View File

@ -22,7 +22,7 @@
#if defined( _X360 )
#include "xbox/xbox_console.h"
#endif
std::mutex s_LogMutex;
std::mutex g_LogMutex;
//-----------------------------------------------------------------------------
// True if -hushasserts was passed on command line.
@ -99,10 +99,10 @@ void NetMsg(EGlobalContext_t context, const char* fmt, ...)
#ifndef DEDICATED
static char szBuf[4096] = {};
static std::string svOut;
static std::regex rxAnsiExp("\\\033\\[.*?m");
static std::shared_ptr<spdlog::logger> iconsole = spdlog::get("game_console");
static std::shared_ptr<spdlog::logger> wconsole = spdlog::get("win_console");
static std::shared_ptr<spdlog::logger> ntlogger = spdlog::get("net_con");
static const std::shared_ptr<spdlog::logger> iconsole = spdlog::get("game_console");
static const std::shared_ptr<spdlog::logger> wconsole = spdlog::get("win_console");
static const std::shared_ptr<spdlog::logger> ntlogger = spdlog::get("net_con");
switch (context)
{
case EGlobalContext_t::GLOBAL_NONE:
@ -110,7 +110,7 @@ void NetMsg(EGlobalContext_t context, const char* fmt, ...)
case EGlobalContext_t::SCRIPT_CLIENT:
case EGlobalContext_t::SCRIPT_UI:
{
s_LogMutex.lock();
g_LogMutex.lock();
{/////////////////////////////
va_list args{};
va_start(args, fmt);
@ -160,11 +160,11 @@ void NetMsg(EGlobalContext_t context, const char* fmt, ...)
if (g_bSpdLog_UseAnsiClr)
{
wconsole->debug(svOut);
svOut = std::regex_replace(svOut, rxAnsiExp, "");
svOut = std::regex_replace(svOut, rANSI_EXP, "");
}
else
{
svOut = std::regex_replace(svOut, rxAnsiExp, "");
svOut = std::regex_replace(svOut, rANSI_EXP, "");
wconsole->debug(svOut);
}
@ -177,7 +177,7 @@ void NetMsg(EGlobalContext_t context, const char* fmt, ...)
g_spd_sys_w_oss.str("");
g_spd_sys_w_oss.clear();
s_LogMutex.unlock();
g_LogMutex.unlock();
break;
}
@ -191,7 +191,7 @@ void NetMsg(EGlobalContext_t context, const char* fmt, ...)
case EGlobalContext_t::NETCON_S:
case EGlobalContext_t::COMMON_C:
{
s_LogMutex.lock();
g_LogMutex.lock();
{/////////////////////////////
va_list args{};
va_start(args, fmt);
@ -259,11 +259,11 @@ void NetMsg(EGlobalContext_t context, const char* fmt, ...)
if (g_bSpdLog_UseAnsiClr)
{
wconsole->debug(svOut);
svOut = std::regex_replace(svOut, rxAnsiExp, "");
svOut = std::regex_replace(svOut, rANSI_EXP, "");
}
else
{
svOut = std::regex_replace(svOut, rxAnsiExp, "");
svOut = std::regex_replace(svOut, rANSI_EXP, "");
wconsole->debug(svOut);
}
@ -275,7 +275,7 @@ void NetMsg(EGlobalContext_t context, const char* fmt, ...)
g_spd_sys_w_oss.str("");
g_spd_sys_w_oss.clear();
s_LogMutex.unlock();
g_LogMutex.unlock();
break;
}
}
@ -294,13 +294,11 @@ void DevMsg(eDLL_T context, const char* fmt, ...)
static std::string svOut;
static std::string svAnsiOut;
static std::regex rxAnsiExp("\\\033\\[.*?m");
static std::shared_ptr<spdlog::logger> iconsole = spdlog::get("game_console");
static std::shared_ptr<spdlog::logger> wconsole = spdlog::get("win_console");
static std::shared_ptr<spdlog::logger> sqlogger = spdlog::get("sdk_info");
s_LogMutex.lock();
g_LogMutex.lock();
const char* pszUpTime = Plat_GetProcessUpTime();
{/////////////////////////////
@ -316,7 +314,7 @@ void DevMsg(eDLL_T context, const char* fmt, ...)
svOut = g_bSpdLog_PostInit ? pszUpTime : "";
svOut.append(sDLL_T[static_cast<int>(context)]);
svOut.append(szBuf);
svOut = std::regex_replace(svOut, rxAnsiExp, "");
svOut = std::regex_replace(svOut, rANSI_EXP, "");
if (svOut.back() != '\n')
{
@ -397,7 +395,7 @@ void DevMsg(eDLL_T context, const char* fmt, ...)
g_spd_sys_w_oss.str("");
g_spd_sys_w_oss.clear();
#endif // !DEDICATED
s_LogMutex.unlock();
g_LogMutex.unlock();
}
//-----------------------------------------------------------------------------
@ -412,13 +410,11 @@ void Warning(eDLL_T context, const char* fmt, ...)
static std::string svOut;
static std::string svAnsiOut;
static std::regex rxAnsiExp("\\\033\\[.*?m");
static std::shared_ptr<spdlog::logger> iconsole = spdlog::get("game_console");
static std::shared_ptr<spdlog::logger> wconsole = spdlog::get("win_console");
static std::shared_ptr<spdlog::logger> sqlogger = spdlog::get("sdk_warn");
s_LogMutex.lock();
g_LogMutex.lock();
const char* pszUpTime = Plat_GetProcessUpTime();
{/////////////////////////////
@ -434,7 +430,7 @@ void Warning(eDLL_T context, const char* fmt, ...)
svOut = g_bSpdLog_PostInit ? pszUpTime : "";
svOut.append(sDLL_T[static_cast<int>(context)]);
svOut.append(szBuf);
svOut = std::regex_replace(svOut, rxAnsiExp, "");
svOut = std::regex_replace(svOut, rANSI_EXP, "");
if (svOut.back() != '\n')
{
@ -478,7 +474,7 @@ void Warning(eDLL_T context, const char* fmt, ...)
g_spd_sys_w_oss.str("");
g_spd_sys_w_oss.clear();
#endif // !DEDICATED
s_LogMutex.unlock();
g_LogMutex.unlock();
}
//-----------------------------------------------------------------------------
@ -487,20 +483,18 @@ void Warning(eDLL_T context, const char* fmt, ...)
// code -
// *fmt - ... -
//-----------------------------------------------------------------------------
void Error(eDLL_T context, UINT code, const char* fmt, ...)
void Error(eDLL_T context, const UINT code, const char* fmt, ...)
{
static char szBuf[4096] = {};
static std::string svOut;
static std::string svAnsiOut;
static std::regex rxAnsiExp("\\\033\\[.*?m");
static std::shared_ptr<spdlog::logger> iconsole = spdlog::get("game_console");
static std::shared_ptr<spdlog::logger> wconsole = spdlog::get("win_console");
static std::shared_ptr<spdlog::logger> sqlogger = spdlog::get("sdk_error");
s_LogMutex.lock();
g_LogMutex.lock();
const char* pszUpTime = Plat_GetProcessUpTime();
{/////////////////////////////
@ -516,7 +510,7 @@ void Error(eDLL_T context, UINT code, const char* fmt, ...)
svOut = g_bSpdLog_PostInit ? pszUpTime : "";
svOut.append(sDLL_T[static_cast<int>(context)]);
svOut.append(szBuf);
svOut = std::regex_replace(svOut, rxAnsiExp, "");
svOut = std::regex_replace(svOut, rANSI_EXP, "");
if (svOut.back() != '\n')
{
@ -568,5 +562,5 @@ void Error(eDLL_T context, UINT code, const char* fmt, ...)
TerminateProcess(GetCurrentProcess(), code);
}
}
s_LogMutex.unlock();
g_LogMutex.unlock();
}

View File

@ -46,7 +46,7 @@ enum class eDLL_T : int
COMMON = 8
};
const string sDLL_T[9] =
static const string sDLL_T[9] =
{
"Native(S):",
"Native(C):",
@ -59,7 +59,7 @@ const string sDLL_T[9] =
""
};
const static string sANSI_DLL_T[9] =
static const string sANSI_DLL_T[9] =
{
"\033[38;2;059;120;218mNative(S):",
"\033[38;2;118;118;118mNative(C):",
@ -71,7 +71,11 @@ const static string sANSI_DLL_T[9] =
"\033[38;2;204;204;204mNetcon(X):",
"\033[38;2;255;204;153m"
};
extern std::mutex s_LogMutex;
static const std::regex rANSI_EXP("\\\033\\[.*?m");
extern std::mutex g_LogMutex;
//////////////////////////////////////////////////////////////////////////
// Legacy Logging System
@ -80,8 +84,8 @@ extern std::mutex s_LogMutex;
// These functions do not return.
PLATFORM_INTERFACE void NetMsg(EGlobalContext_t context, const char* fmt, ...) FMTFUNCTION(2, 3);
PLATFORM_INTERFACE void DevMsg(eDLL_T context, const char* fmt, ...) FMTFUNCTION(2, 3);
PLATFORM_INTERFACE void Warning(eDLL_T context, const char* fmt, ...) FMTFUNCTION(1, 2);
PLATFORM_INTERFACE void Error(eDLL_T context, UINT code, const char* fmt, ...) FMTFUNCTION(1, 2);
PLATFORM_INTERFACE void Warning(eDLL_T context, const char* fmt, ...) FMTFUNCTION(2, 3);
PLATFORM_INTERFACE void Error(eDLL_T context, const UINT code, const char* fmt, ...) FMTFUNCTION(3, 4);
// You can use this macro like a runtime assert macro.
// If the condition fails, then Error is called with the message. This macro is called

View File

@ -69,7 +69,7 @@ void CTextOverlay::AddLog(const EGlobalContext_t context, const string& svText)
}
std::lock_guard<std::mutex> l(m_Mutex);
m_vNotifyText.push_back(CNotifyText{ context, con_notifytime->GetFloat() , svText });
m_vNotifyText.push_back(CTextNotify{ context, con_notifytime->GetFloat() , svText });
while (m_vNotifyText.size() > 0 &&
(m_vNotifyText.size() > con_notifylines->GetInt()))
@ -89,7 +89,7 @@ void CTextOverlay::DrawNotify(void)
std::lock_guard<std::mutex> l(m_Mutex);
for (size_t i = 0, j = m_vNotifyText.size(); i < j; i++)
{
CNotifyText* pNotify = &m_vNotifyText[i];
CTextNotify* pNotify = &m_vNotifyText[i];
Color c = GetLogColorForType(m_vNotifyText[i].m_type);
float flTimeleft = pNotify->m_flLifeRemaining;
@ -159,7 +159,7 @@ void CTextOverlay::ShouldDraw(const float flFrameTime)
int c = m_vNotifyText.size();
for (i = c - 1; i >= 0; i--)
{
CNotifyText* pNotify = &m_vNotifyText[i];
CTextNotify* pNotify = &m_vNotifyText[i];
pNotify->m_flLifeRemaining -= flFrameTime;
if (pNotify->m_flLifeRemaining <= 0.0f)
@ -181,9 +181,9 @@ void CTextOverlay::ShouldDraw(const float flFrameTime)
//-----------------------------------------------------------------------------
void CTextOverlay::DrawHostStats(void) const
{
const static Color c = { 255, 255, 255, 255 };
int nWidth = cl_hoststats_invert_x->GetBool() ? g_nWindowWidth - cl_hoststats_offset_x->GetInt() : cl_hoststats_offset_x->GetInt();
int nHeight = cl_hoststats_invert_y->GetBool() ? g_nWindowHeight - cl_hoststats_offset_y->GetInt() : cl_hoststats_offset_y->GetInt();
static const Color c = { 255, 255, 255, 255 };
const int nWidth = cl_hoststats_invert_x->GetBool() ? g_nWindowWidth - cl_hoststats_offset_x->GetInt() : cl_hoststats_offset_x->GetInt();
const int nHeight = cl_hoststats_invert_y->GetBool() ? g_nWindowHeight - cl_hoststats_offset_y->GetInt() : cl_hoststats_offset_y->GetInt();
CMatSystemSurface_DrawColoredText(g_pMatSystemSurface, v_Rui_GetFontFace(), m_nFontHeight, nWidth, nHeight, c.r(), c.g(), c.b(), c.a(), m_pszCon_NPrintf_Buf);
}
@ -193,9 +193,9 @@ void CTextOverlay::DrawHostStats(void) const
//-----------------------------------------------------------------------------
void CTextOverlay::DrawSimStats(void) const
{
static Color c = { 255, 255, 255, 255 };
int nWidth = cl_simstats_invert_x->GetBool() ? g_nWindowWidth - cl_simstats_offset_x->GetInt() : cl_simstats_offset_x->GetInt();
int nHeight = cl_simstats_invert_y->GetBool() ? g_nWindowHeight - cl_simstats_offset_y->GetInt() : cl_simstats_offset_y->GetInt();
static const Color c = { 255, 255, 255, 255 };
const int nWidth = cl_simstats_invert_x->GetBool() ? g_nWindowWidth - cl_simstats_offset_x->GetInt() : cl_simstats_offset_x->GetInt();
const int nHeight = cl_simstats_invert_y->GetBool() ? g_nWindowHeight - cl_simstats_offset_y->GetInt() : cl_simstats_offset_y->GetInt();
DrawFormat(nWidth, nHeight, c, "Server Frame: (%d) Client Frame: (%d) Render Frame: (%d)\n",
g_pClientState->GetServerTickCount(), g_pClientState->GetClientTickCount(), *g_nRenderTickCount);
@ -206,9 +206,9 @@ void CTextOverlay::DrawSimStats(void) const
//-----------------------------------------------------------------------------
void CTextOverlay::DrawGPUStats(void) const
{
static Color c = { 255, 255, 255, 255 };
int nWidth = cl_gpustats_invert_x->GetBool() ? g_nWindowWidth - cl_gpustats_offset_x->GetInt() : cl_gpustats_offset_x->GetInt();
int nHeight = cl_gpustats_invert_y->GetBool() ? g_nWindowHeight - cl_gpustats_offset_y->GetInt() : cl_gpustats_offset_y->GetInt();
static const Color c = { 255, 255, 255, 255 };
const int nWidth = cl_gpustats_invert_x->GetBool() ? g_nWindowWidth - cl_gpustats_offset_x->GetInt() : cl_gpustats_offset_x->GetInt();
const int nHeight = cl_gpustats_invert_y->GetBool() ? g_nWindowHeight - cl_gpustats_offset_y->GetInt() : cl_gpustats_offset_y->GetInt();
DrawFormat(nWidth, nHeight, c, "%8d/%8d/%8dkiB unusable/unfree/total GPU Streaming Texture memory\n",
*g_nUnusableStreamingTextureMemory / 1024, *g_nUnfreeStreamingTextureMemory / 1024, *g_nUnusableStreamingTextureMemory / 1024);
@ -239,7 +239,7 @@ void CTextOverlay::DrawCrosshairMaterial(void) const
void CTextOverlay::DrawStreamOverlay(void) const
{
static char szLogbuf[4096];
static Color c = { 255, 255, 255, 255 };
static const Color c = { 255, 255, 255, 255 };
GetStreamOverlay(stream_overlay_mode->GetString(), szLogbuf, sizeof(szLogbuf));
CMatSystemSurface_DrawColoredText(g_pMatSystemSurface, v_Rui_GetFontFace(), m_nFontHeight, 20, 300, c.r(), c.g(), c.b(), c.a(), szLogbuf);

View File

@ -2,9 +2,9 @@
#include "core/stdafx.h"
#include "mathlib/color.h"
struct CNotifyText
struct CTextNotify
{
CNotifyText(const EGlobalContext_t type, const float nTime, const string& svMessage)
CTextNotify(const EGlobalContext_t type, const float nTime, const string& svMessage)
{
this->m_svMessage = svMessage;
this->m_flLifeRemaining = nTime;
@ -37,7 +37,7 @@ public:
private:
Color GetLogColorForType(const EGlobalContext_t type) const;
vector<CNotifyText> m_vNotifyText;
vector<CTextNotify> m_vNotifyText;
int m_nFontHeight; // Hardcoded to 16 in this engine.
mutable std::mutex m_Mutex;

View File

@ -16,7 +16,7 @@ int HQHull_PrintFunc(const char* fmt, ...)
static std::shared_ptr<spdlog::logger> wconsole = spdlog::get("win_console");
static std::shared_ptr<spdlog::logger> qhlogger = spdlog::get("qhull_info");
s_LogMutex.lock();
g_LogMutex.lock();
{/////////////////////////////
va_list args{};
va_start(args, fmt);
@ -38,7 +38,7 @@ int HQHull_PrintFunc(const char* fmt, ...)
g_spd_sys_w_oss.clear();
#endif // !DEDICATED
s_LogMutex.unlock();
g_LogMutex.unlock();
return NULL;
}