From dc5774a69874734e9d32b0a2a94b6debd8f9579c Mon Sep 17 00:00:00 2001 From: nillerusr Date: Wed, 6 Jul 2022 18:17:09 +0300 Subject: [PATCH] add debug logger --- launcher/launcher.cpp | 3 ++ public/tier0/dbg.h | 8 ++++ tier0/dbg.cpp | 86 +++++++++++++++++++++++++++++++++++++++---- 3 files changed, 90 insertions(+), 7 deletions(-) diff --git a/launcher/launcher.cpp b/launcher/launcher.cpp index 217db87c..a814ec4c 100644 --- a/launcher/launcher.cpp +++ b/launcher/launcher.cpp @@ -765,6 +765,9 @@ bool CSourceAppSystemGroup::Create() bool CSourceAppSystemGroup::PreInit() { + if ( !CommandLine()->FindParm( "-nolog" ) ) + DebugLogger()->Init("engine.log"); + CreateInterfaceFn factory = GetFactory(); ConnectTier1Libraries( &factory, 1 ); ConVar_Register( ); diff --git a/public/tier0/dbg.h b/public/tier0/dbg.h index 1fff938c..9f2c734b 100644 --- a/public/tier0/dbg.h +++ b/public/tier0/dbg.h @@ -48,6 +48,14 @@ class Color; +class IDbgLogger +{ +public: + virtual void Init(const char *logfile) = 0; + virtual void Write(const char *data) = 0; +}; + +PLATFORM_INTERFACE IDbgLogger *DebugLogger(); //----------------------------------------------------------------------------- // Usage model for the Dbg library diff --git a/tier0/dbg.cpp b/tier0/dbg.cpp index 9d64da4a..b7a39383 100644 --- a/tier0/dbg.cpp +++ b/tier0/dbg.cpp @@ -49,11 +49,82 @@ // memdbgon must be the last include file in a .cpp file!!! #include "tier0/memdbgon.h" +class CDbgLogger : public IDbgLogger +{ +public: + CDbgLogger(); + ~CDbgLogger(); + + void Init(const char *logfile); + void Write(const char *data); + +private: + FILE *file; + float flStartTime; + bool bShouldLog; +}; + + +CDbgLogger::CDbgLogger() +{ + bShouldLog = false; + flStartTime = Plat_FloatTime(); +} + +void CDbgLogger::Init(const char *logfile) +{ + time_t timeCur; + struct tm tmStruct; + + char szTime[256]; + + bShouldLog = true; + + time( &timeCur ); + Plat_gmtime( &timeCur, &tmStruct ); + Plat_ctime( &timeCur, szTime, sizeof(szTime) ); + + file = fopen(logfile, "w+"); + fprintf(file, ">>> Engine started at %s\n", szTime); + fflush(file); +} + +CDbgLogger::~CDbgLogger() +{ + if( !bShouldLog ) + return; + + time_t timeCur; + struct tm tmStruct; + + char szTime[256]; + + time( &timeCur ); + Plat_gmtime( &timeCur, &tmStruct ); + Plat_ctime( &timeCur, szTime, sizeof(szTime) ); + + fprintf(file, "\n>>> Engine closed at %s\n", szTime); + fclose(file); +} + +void CDbgLogger::Write(const char *data) +{ + if( !bShouldLog ) + return; + + fprintf(file, "[%.4f] ", Plat_FloatTime() - flStartTime); + fprintf(file, "%s", data); + fflush(file); +} + +static CDbgLogger g_DbgLogger; +IDbgLogger *DebugLogger() { return &g_DbgLogger; } + //----------------------------------------------------------------------------- // internal structures //----------------------------------------------------------------------------- -enum -{ +enum +{ MAX_GROUP_NAME_LENGTH = 48 }; @@ -275,7 +346,7 @@ DBG_INTERFACE void _SpewInfo( SpewType_t type, const tchar* pFile, int line ) static SpewRetval_t _SpewMessage( SpewType_t spewType, const char *pGroupName, int nLevel, const Color *pColor, const tchar* pMsgFormat, va_list args ) { - tchar pTempBuffer[5020]; + tchar pTempBuffer[8192]; assert( _tcslen( pMsgFormat ) < sizeof( pTempBuffer) ); // check that we won't artifically truncate the string @@ -288,7 +359,7 @@ static SpewRetval_t _SpewMessage( SpewType_t spewType, const char *pGroupName, i if ( len == -1 ) return SPEW_ABORT; - + /* Create the message.... */ int val= _vsntprintf( &pTempBuffer[len], sizeof( pTempBuffer ) - len - 1, pMsgFormat, args ); if ( val == -1 ) @@ -302,10 +373,10 @@ static SpewRetval_t _SpewMessage( SpewType_t spewType, const char *pGroupName, i { len += _stprintf( &pTempBuffer[len], _T("\n") ); } - + assert( len < sizeof(pTempBuffer)/sizeof(pTempBuffer[0]) - 1 ); /* use normal assert here; to avoid recursion. */ assert( s_SpewOutputFunc ); - + /* direct it to the appropriate target(s) */ SpewRetval_t ret; assert( g_pSpewInfo == NULL ); @@ -317,8 +388,9 @@ static SpewRetval_t _SpewMessage( SpewType_t spewType, const char *pGroupName, i }; #ifdef ANDROID - __android_log_print( ANDROID_LOG_INFO, "SRCENG", "%s", pTempBuffer ); + __android_log_print( ANDROID_LOG_INFO, "SRCENG", "%s", pTempBuffer ); #endif + g_DbgLogger.Write( pTempBuffer ); g_pSpewInfo = &spewInfo; ret = s_SpewOutputFunc( spewType, pTempBuffer );