Skip to content

Commit 73f7c76

Browse files
committed
Add extended log options
Adds `logs.writeSrcLocation.*` cvars. If `<source_location>` is supported, this will add the source of the log to the log line itself.
1 parent d68ed0c commit 73f7c76

File tree

2 files changed

+81
-28
lines changed

2 files changed

+81
-28
lines changed

src/common/Log.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3131
#include "Common.h"
3232

3333
namespace Log {
34+
Cvar::Cvar<bool> logExtendAll(
35+
"logs.writeSrcLocation.all", "Always print source code location for logs", Cvar::NONE, false );
36+
Cvar::Cvar<bool> logExtendWarn(
37+
"logs.writeSrcLocation.warn", "Print source code location for Warn logs", Cvar::NONE, false );
38+
Cvar::Cvar<bool> logExtendNotice(
39+
"logs.writeSrcLocation.notice", "Print source code location for Notice logs", Cvar::NONE, false );
40+
Cvar::Cvar<bool> logExtendVerbose(
41+
"logs.writeSrcLocation.verbose", "Print source code location for Verbose logs", Cvar::NONE, false );
42+
Cvar::Cvar<bool> logExtendDebug(
43+
"logs.writeSrcLocation.debug", "Print source code location for Debug logs", Cvar::NONE, false );
3444

3545
Logger::Logger(Str::StringRef name, std::string prefix, Level defaultLevel)
3646
: filterLevel(new Cvar::Cvar<Log::Level>(

src/common/Log.h

Lines changed: 71 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ namespace Log {
5151
// The default filtering level
5252
const Level DEFAULT_FILTER_LEVEL = Level::WARNING;
5353

54+
extern Cvar::Cvar<bool> logExtendAll;
55+
extern Cvar::Cvar<bool> logExtendWarn;
56+
extern Cvar::Cvar<bool> logExtendNotice;
57+
extern Cvar::Cvar<bool> logExtendVerbose;
58+
extern Cvar::Cvar<bool> logExtendDebug;
59+
5460
/*
5561
* Loggers are used to group logs by subsystems and allow logs
5662
* to be filtered by log level by subsystem. They are used like so
@@ -84,16 +90,16 @@ namespace Log {
8490
Logger(Str::StringRef name, std::string prefix = "", Level defaultLevel = DEFAULT_FILTER_LEVEL);
8591

8692
template<typename ... Args>
87-
void Warn(Str::StringRef format, Args&& ... args);
93+
void WarnExt( const char* file, const char* function, const int line, Str::StringRef format, Args&& ... args );
8894

8995
template<typename ... Args>
90-
void Notice(Str::StringRef format, Args&& ... args);
96+
void NoticeExt( const char* file, const char* function, const int line, Str::StringRef format, Args&& ... args );
9197

9298
template<typename ... Args>
93-
void Verbose(Str::StringRef format, Args&& ... args);
99+
void VerboseExt( const char* file, const char* function, const int line, Str::StringRef format, Args&& ... args );
94100

95101
template<typename ... Args>
96-
void Debug(Str::StringRef format, Args&& ... args);
102+
void DebugExt( const char* file, const char* function, const int line, Str::StringRef format, Args&& ... args );
97103

98104
template<typename F>
99105
void DoWarnCode(F&& code);
@@ -131,16 +137,16 @@ namespace Log {
131137
*/
132138

133139
template<typename ... Args>
134-
void Warn(Str::StringRef format, Args&& ... args);
140+
void Warn( const char* file, const char* function, const int line, Str::StringRef format, Args&& ... args );
135141

136142
template<typename ... Args>
137-
void Notice(Str::StringRef format, Args&& ... args);
143+
void Notice( const char* file, const char* function, const int line, Str::StringRef format, Args&& ... args );
138144

139145
template<typename ... Args>
140-
void Verbose(Str::StringRef format, Args&& ... args);
146+
void Verbose( const char* file, const char* function, const int line, Str::StringRef format, Args&& ... args );
141147

142148
template<typename ... Args>
143-
void Debug(Str::StringRef format, Args&& ... args);
149+
void Debug( const char* file, const char* function, const int line, Str::StringRef format, Args&& ... args );
144150

145151
/*
146152
* For messages which are not true log messages, but rather are produced by
@@ -193,31 +199,62 @@ namespace Log {
193199

194200
// Logger
195201

202+
inline std::string AddSrcLocation( const std::string& message,
203+
const char* file, const char* function, const int line,
204+
const bool extend ) {
205+
if ( logExtendAll.Get() || extend ) {
206+
return message + Str::Format( " ^F(%s:%u, %s)",
207+
file, line, function );
208+
}
209+
210+
return message;
211+
}
212+
196213
template<typename ... Args>
197-
void Logger::Warn(Str::StringRef format, Args&& ... args) {
198-
if (filterLevel->Get() <= Level::WARNING) {
199-
this->Dispatch(Prefix(Str::Format(format, std::forward<Args>(args) ...)), Level::WARNING, format);
214+
void Logger::WarnExt( const char* file, const char* function, const int line, Str::StringRef format, Args&& ... args ) {
215+
if ( filterLevel->Get() <= Level::WARNING ) {
216+
this->Dispatch(
217+
AddSrcLocation(
218+
Prefix( Str::Format( format, std::forward<Args>( args ) ... ) ),
219+
file, function, line, logExtendWarn.Get()
220+
),
221+
Level::WARNING, format );
200222
}
201223
}
202224

203225
template<typename ... Args>
204-
void Logger::Notice(Str::StringRef format, Args&& ... args) {
205-
if (filterLevel->Get() <= Level::NOTICE) {
206-
this->Dispatch(Prefix(Str::Format(format, std::forward<Args>(args) ...)), Level::NOTICE, format);
226+
void Logger::NoticeExt( const char* file, const char* function, const int line, Str::StringRef format, Args&& ... args ) {
227+
if ( filterLevel->Get() <= Level::NOTICE ) {
228+
this->Dispatch(
229+
AddSrcLocation(
230+
Prefix( Str::Format( format, std::forward<Args>( args ) ... ) ),
231+
file, function, line, logExtendNotice.Get()
232+
),
233+
Level::NOTICE, format );
207234
}
208235
}
209236

210237
template<typename ... Args>
211-
void Logger::Verbose(Str::StringRef format, Args&& ... args) {
212-
if (filterLevel->Get() <= Level::VERBOSE) {
213-
this->Dispatch(Prefix(Str::Format(format, std::forward<Args>(args) ...)), Level::VERBOSE, format);
238+
void Logger::VerboseExt( const char* file, const char* function, const int line, Str::StringRef format, Args&& ... args ) {
239+
if ( filterLevel->Get() <= Level::VERBOSE ) {
240+
this->Dispatch(
241+
AddSrcLocation(
242+
Prefix( Str::Format( format, std::forward<Args>( args ) ... ) ),
243+
file, function, line, logExtendVerbose.Get()
244+
),
245+
Level::VERBOSE, format );
214246
}
215247
}
216248

217249
template<typename ... Args>
218-
void Logger::Debug(Str::StringRef format, Args&& ... args) {
219-
if (filterLevel->Get() <= Level::DEBUG) {
220-
this->Dispatch(Prefix(Str::Format(format, std::forward<Args>(args) ...)), Level::DEBUG, format);
250+
void Logger::DebugExt( const char* file, const char* function, const int line, Str::StringRef format, Args&& ... args ) {
251+
if ( filterLevel->Get() <= Level::DEBUG ) {
252+
this->Dispatch(
253+
AddSrcLocation(
254+
Prefix( Str::Format( format, std::forward<Args>( args ) ... ) ),
255+
file, function, line, logExtendDebug.Get()
256+
),
257+
Level::DEBUG, format );
221258
}
222259
}
223260

@@ -253,24 +290,30 @@ namespace Log {
253290
extern Logger defaultLogger;
254291

255292
template<typename ... Args>
256-
void Warn(Str::StringRef format, Args&& ... args) {
257-
defaultLogger.Warn(format, std::forward<Args>(args) ...);
293+
void WarnExt( const char* file, const char* function, const int line, Str::StringRef format, Args&& ... args ) {
294+
defaultLogger.WarnExt( file, function, line, format, std::forward<Args>( args ) ... );
258295
}
259296

260297
template<typename ... Args>
261-
void Notice(Str::StringRef format, Args&& ... args) {
262-
defaultLogger.Notice(format, std::forward<Args>(args) ...);
298+
void NoticeExt( const char* file, const char* function, const int line, Str::StringRef format, Args&& ... args ) {
299+
defaultLogger.NoticeExt( file, function, line, format, std::forward<Args>( args ) ... );
263300
}
264301

265302
template<typename ... Args>
266-
void Verbose(Str::StringRef format, Args&& ... args) {
267-
defaultLogger.Verbose(format, std::forward<Args>(args) ...);
303+
void VerboseExt( const char* file, const char* function, const int line, Str::StringRef format, Args&& ... args ) {
304+
defaultLogger.VerboseExt( file, function, line, format, std::forward<Args>( args ) ... );
268305
}
269306

270307
template<typename ... Args>
271-
void Debug(Str::StringRef format, Args&& ... args) {
272-
defaultLogger.Debug(format, std::forward<Args>(args) ...);
308+
void DebugExt( const char* file, const char* function, const int line, Str::StringRef format, Args&& ... args ) {
309+
defaultLogger.DebugExt( file, function, line, format, std::forward<Args>( args ) ... );
273310
}
311+
312+
// Use ##__VA_ARGS__ instead of __VA_ARGS__ because args may be empty. __VA_OPT__( , ) currently doesn't seem to work on MSVC
313+
#define Warn( format, ... ) WarnExt( __FILE__, __func__, __LINE__, format, ##__VA_ARGS__ )
314+
#define Notice( format, ... ) NoticeExt( __FILE__, __func__, __LINE__, format, ##__VA_ARGS__ )
315+
#define Verbose( format, ... ) VerboseExt( __FILE__, __func__, __LINE__, format, ##__VA_ARGS__ )
316+
#define Debug( format, ... ) DebugExt( __FILE__, __func__, __LINE__, format, ##__VA_ARGS__ )
274317
}
275318

276319
namespace Cvar {

0 commit comments

Comments
 (0)