From a328a228889f6bb7401a806c2847839480f57483 Mon Sep 17 00:00:00 2001 From: VReaperV Date: Mon, 11 Aug 2025 19:08:51 +0300 Subject: [PATCH] Add extended log options Adds `logs.writeSrcLocation.*` cvars. If `` is supported, this will add the source of the log to the log line itself. --- src/common/Log.cpp | 10 +++++ src/common/Log.h | 99 +++++++++++++++++++++++++++++++++------------- 2 files changed, 81 insertions(+), 28 deletions(-) diff --git a/src/common/Log.cpp b/src/common/Log.cpp index eb240a5d7d..c7271df2c6 100644 --- a/src/common/Log.cpp +++ b/src/common/Log.cpp @@ -31,6 +31,16 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "Common.h" namespace Log { + Cvar::Cvar logExtendAll( + "logs.writeSrcLocation.all", "Always print source code location for logs", Cvar::NONE, false ); + Cvar::Cvar logExtendWarn( + "logs.writeSrcLocation.warn", "Print source code location for Warn logs", Cvar::NONE, false ); + Cvar::Cvar logExtendNotice( + "logs.writeSrcLocation.notice", "Print source code location for Notice logs", Cvar::NONE, false ); + Cvar::Cvar logExtendVerbose( + "logs.writeSrcLocation.verbose", "Print source code location for Verbose logs", Cvar::NONE, false ); + Cvar::Cvar logExtendDebug( + "logs.writeSrcLocation.debug", "Print source code location for Debug logs", Cvar::NONE, false ); Logger::Logger(Str::StringRef name, std::string prefix, Level defaultLevel) : filterLevel(new Cvar::Cvar( diff --git a/src/common/Log.h b/src/common/Log.h index f09ad3bed7..5046d80923 100644 --- a/src/common/Log.h +++ b/src/common/Log.h @@ -51,6 +51,12 @@ namespace Log { // The default filtering level const Level DEFAULT_FILTER_LEVEL = Level::WARNING; + extern Cvar::Cvar logExtendAll; + extern Cvar::Cvar logExtendWarn; + extern Cvar::Cvar logExtendNotice; + extern Cvar::Cvar logExtendVerbose; + extern Cvar::Cvar logExtendDebug; + /* * Loggers are used to group logs by subsystems and allow logs * to be filtered by log level by subsystem. They are used like so @@ -84,16 +90,16 @@ namespace Log { Logger(Str::StringRef name, std::string prefix = "", Level defaultLevel = DEFAULT_FILTER_LEVEL); template - void Warn(Str::StringRef format, Args&& ... args); + void WarnExt( const char* file, const char* function, const int line, Str::StringRef format, Args&& ... args ); template - void Notice(Str::StringRef format, Args&& ... args); + void NoticeExt( const char* file, const char* function, const int line, Str::StringRef format, Args&& ... args ); template - void Verbose(Str::StringRef format, Args&& ... args); + void VerboseExt( const char* file, const char* function, const int line, Str::StringRef format, Args&& ... args ); template - void Debug(Str::StringRef format, Args&& ... args); + void DebugExt( const char* file, const char* function, const int line, Str::StringRef format, Args&& ... args ); template void DoWarnCode(F&& code); @@ -131,16 +137,16 @@ namespace Log { */ template - void Warn(Str::StringRef format, Args&& ... args); + void Warn( const char* file, const char* function, const int line, Str::StringRef format, Args&& ... args ); template - void Notice(Str::StringRef format, Args&& ... args); + void Notice( const char* file, const char* function, const int line, Str::StringRef format, Args&& ... args ); template - void Verbose(Str::StringRef format, Args&& ... args); + void Verbose( const char* file, const char* function, const int line, Str::StringRef format, Args&& ... args ); template - void Debug(Str::StringRef format, Args&& ... args); + void Debug( const char* file, const char* function, const int line, Str::StringRef format, Args&& ... args ); /* * For messages which are not true log messages, but rather are produced by @@ -193,31 +199,62 @@ namespace Log { // Logger + inline std::string AddSrcLocation( const std::string& message, + const char* file, const char* function, const int line, + const bool extend ) { + if ( logExtendAll.Get() || extend ) { + return message + Str::Format( " ^F(%s:%u, %s)", + file, line, function ); + } + + return message; + } + template - void Logger::Warn(Str::StringRef format, Args&& ... args) { - if (filterLevel->Get() <= Level::WARNING) { - this->Dispatch(Prefix(Str::Format(format, std::forward(args) ...)), Level::WARNING, format); + void Logger::WarnExt( const char* file, const char* function, const int line, Str::StringRef format, Args&& ... args ) { + if ( filterLevel->Get() <= Level::WARNING ) { + this->Dispatch( + AddSrcLocation( + Prefix( Str::Format( format, std::forward( args ) ... ) ), + file, function, line, logExtendWarn.Get() + ), + Level::WARNING, format ); } } template - void Logger::Notice(Str::StringRef format, Args&& ... args) { - if (filterLevel->Get() <= Level::NOTICE) { - this->Dispatch(Prefix(Str::Format(format, std::forward(args) ...)), Level::NOTICE, format); + void Logger::NoticeExt( const char* file, const char* function, const int line, Str::StringRef format, Args&& ... args ) { + if ( filterLevel->Get() <= Level::NOTICE ) { + this->Dispatch( + AddSrcLocation( + Prefix( Str::Format( format, std::forward( args ) ... ) ), + file, function, line, logExtendNotice.Get() + ), + Level::NOTICE, format ); } } template - void Logger::Verbose(Str::StringRef format, Args&& ... args) { - if (filterLevel->Get() <= Level::VERBOSE) { - this->Dispatch(Prefix(Str::Format(format, std::forward(args) ...)), Level::VERBOSE, format); + void Logger::VerboseExt( const char* file, const char* function, const int line, Str::StringRef format, Args&& ... args ) { + if ( filterLevel->Get() <= Level::VERBOSE ) { + this->Dispatch( + AddSrcLocation( + Prefix( Str::Format( format, std::forward( args ) ... ) ), + file, function, line, logExtendVerbose.Get() + ), + Level::VERBOSE, format ); } } template - void Logger::Debug(Str::StringRef format, Args&& ... args) { - if (filterLevel->Get() <= Level::DEBUG) { - this->Dispatch(Prefix(Str::Format(format, std::forward(args) ...)), Level::DEBUG, format); + void Logger::DebugExt( const char* file, const char* function, const int line, Str::StringRef format, Args&& ... args ) { + if ( filterLevel->Get() <= Level::DEBUG ) { + this->Dispatch( + AddSrcLocation( + Prefix( Str::Format( format, std::forward( args ) ... ) ), + file, function, line, logExtendDebug.Get() + ), + Level::DEBUG, format ); } } @@ -253,24 +290,30 @@ namespace Log { extern Logger defaultLogger; template - void Warn(Str::StringRef format, Args&& ... args) { - defaultLogger.Warn(format, std::forward(args) ...); + void WarnExt( const char* file, const char* function, const int line, Str::StringRef format, Args&& ... args ) { + defaultLogger.WarnExt( file, function, line, format, std::forward( args ) ... ); } template - void Notice(Str::StringRef format, Args&& ... args) { - defaultLogger.Notice(format, std::forward(args) ...); + void NoticeExt( const char* file, const char* function, const int line, Str::StringRef format, Args&& ... args ) { + defaultLogger.NoticeExt( file, function, line, format, std::forward( args ) ... ); } template - void Verbose(Str::StringRef format, Args&& ... args) { - defaultLogger.Verbose(format, std::forward(args) ...); + void VerboseExt( const char* file, const char* function, const int line, Str::StringRef format, Args&& ... args ) { + defaultLogger.VerboseExt( file, function, line, format, std::forward( args ) ... ); } template - void Debug(Str::StringRef format, Args&& ... args) { - defaultLogger.Debug(format, std::forward(args) ...); + void DebugExt( const char* file, const char* function, const int line, Str::StringRef format, Args&& ... args ) { + defaultLogger.DebugExt( file, function, line, format, std::forward( args ) ... ); } + + // Use ##__VA_ARGS__ instead of __VA_ARGS__ because args may be empty. __VA_OPT__( , ) currently doesn't seem to work on MSVC + #define Warn( format, ... ) WarnExt( __FILE__, __func__, __LINE__, format, ##__VA_ARGS__ ) + #define Notice( format, ... ) NoticeExt( __FILE__, __func__, __LINE__, format, ##__VA_ARGS__ ) + #define Verbose( format, ... ) VerboseExt( __FILE__, __func__, __LINE__, format, ##__VA_ARGS__ ) + #define Debug( format, ... ) DebugExt( __FILE__, __func__, __LINE__, format, ##__VA_ARGS__ ) } namespace Cvar {