@@ -31,6 +31,14 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3131#ifndef COMMON_LOG_H_
3232#define COMMON_LOG_H_
3333
34+ #include " CPPStandard.h"
35+
36+ #include " engine/qcommon/q_shared.h"
37+
38+ #if defined( CPP_SOURCE_LOCATION )
39+ #include < source_location>
40+ #endif
41+
3442namespace Log {
3543
3644 /*
@@ -51,6 +59,12 @@ namespace Log {
5159 // The default filtering level
5260 const Level DEFAULT_FILTER_LEVEL = Level::WARNING;
5361
62+ extern Cvar::Cvar<bool > logExtendAll;
63+ extern Cvar::Cvar<bool > logExtendWarn;
64+ extern Cvar::Cvar<bool > logExtendNotice;
65+ extern Cvar::Cvar<bool > logExtendVerbose;
66+ extern Cvar::Cvar<bool > logExtendDebug;
67+
5468 /*
5569 * Loggers are used to group logs by subsystems and allow logs
5670 * to be filtered by log level by subsystem. They are used like so
@@ -83,17 +97,31 @@ namespace Log {
8397 public:
8498 Logger (Str::StringRef name, std::string prefix = " " , Level defaultLevel = DEFAULT_FILTER_LEVEL);
8599
100+ #if defined( CPP_SOURCE_LOCATION )
101+ template <typename ... Args>
102+ void WarnExt ( const std::source_location& srcLocation, Str::StringRef format, Args&& ... args );
103+
104+ template <typename ... Args>
105+ void NoticeExt ( const std::source_location& srcLocation, Str::StringRef format, Args&& ... args );
106+
107+ template <typename ... Args>
108+ void VerboseExt ( const std::source_location& srcLocation, Str::StringRef format, Args&& ... args );
109+
110+ template <typename ... Args>
111+ void DebugExt ( const std::source_location& srcLocation, Str::StringRef format, Args&& ... args );
112+ #else
86113 template <typename ... Args>
87- void Warn ( Str::StringRef format, Args&& ... args);
114+ void WarnExt ( Str::StringRef format, Args&& ... args );
88115
89116 template <typename ... Args>
90- void Notice ( Str::StringRef format, Args&& ... args);
117+ void NoticeExt ( Str::StringRef format, Args&& ... args );
91118
92119 template <typename ... Args>
93- void Verbose ( Str::StringRef format, Args&& ... args);
120+ void VerboseExt ( Str::StringRef format, Args&& ... args );
94121
95122 template <typename ... Args>
96- void Debug (Str::StringRef format, Args&& ... args);
123+ void DebugExt ( Str::StringRef format, Args&& ... args );
124+ #endif
97125
98126 template <typename F>
99127 void DoWarnCode (F&& code);
@@ -130,17 +158,31 @@ namespace Log {
130158 * cannot be filtered and will clutter the console.
131159 */
132160
161+ #if defined( CPP_SOURCE_LOCATION )
133162 template <typename ... Args>
134- void Warn (Str::StringRef format, Args&& ... args);
163+ void Warn ( const std::source_location& srcLocation, Str::StringRef format, Args&& ... args );
135164
136165 template <typename ... Args>
137- void Notice (Str::StringRef format, Args&& ... args);
166+ void Notice ( const std::source_location& srcLocation, Str::StringRef format, Args&& ... args );
138167
139168 template <typename ... Args>
140- void Verbose (Str::StringRef format, Args&& ... args);
169+ void Verbose ( const std::source_location& srcLocation, Str::StringRef format, Args&& ... args );
170+
171+ template <typename ... Args>
172+ void Debug ( const std::source_location& srcLocation, Str::StringRef format, Args&& ... args );
173+ #else
174+ template <typename ... Args>
175+ void Warn ( Str::StringRef format, Args&& ... args );
141176
142177 template <typename ... Args>
143- void Debug (Str::StringRef format, Args&& ... args);
178+ void Notice ( Str::StringRef format, Args&& ... args );
179+
180+ template <typename ... Args>
181+ void Verbose ( Str::StringRef format, Args&& ... args );
182+
183+ template <typename ... Args>
184+ void Debug ( Str::StringRef format, Args&& ... args );
185+ #endif
144186
145187 /*
146188 * For messages which are not true log messages, but rather are produced by
@@ -193,33 +235,80 @@ namespace Log {
193235
194236 // Logger
195237
238+ #if defined( CPP_SOURCE_LOCATION )
239+ inline std::string AddSrcLocation ( const std::string& message, const std::source_location& srcLocation, const bool extend ) {
240+ if ( logExtendAll.Get () || extend ) {
241+ return message + Str::Format ( " ^F(file: %s, line: %u:%u, func: %s)" ,
242+ start, srcLocation.file_name (), srcLocation.column (), srcLocation.function_name () );
243+ }
244+
245+ return message;
246+ }
247+
196248 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);
249+ void Logger::WarnExt ( const std::source_location& srcLocation, Str::StringRef format, Args&& ... args ) {
250+ if ( filterLevel->Get () <= Level::WARNING ) {
251+ this ->Dispatch (
252+ AddSrcLocation ( Prefix ( Str::Format ( format, std::forward<Args>( args ) ... ) ), srcLocation, logExtendWarn.Get () ),
253+ Level::WARNING, format );
200254 }
201255 }
202256
203257 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);
258+ void Logger::NoticeExt ( const std::source_location& srcLocation, Str::StringRef format, Args&& ... args ) {
259+ if ( filterLevel->Get () <= Level::NOTICE ) {
260+ this ->Dispatch (
261+ AddSrcLocation ( Prefix ( Str::Format ( format, std::forward<Args>( args ) ... ) ), srcLocation, logExtendNotice.Get () ),
262+ Level::NOTICE, format );
207263 }
208264 }
209265
210266 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);
267+ void Logger::VerboseExt ( const std::source_location& srcLocation, Str::StringRef format, Args&& ... args ) {
268+ if ( filterLevel->Get () <= Level::VERBOSE ) {
269+ this ->Dispatch (
270+ AddSrcLocation ( Prefix ( Str::Format ( format, std::forward<Args>( args ) ... ) ), srcLocation, logExtendVerbose.Get () ),
271+ Level::VERBOSE, format );
214272 }
215273 }
216274
217275 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);
276+ void Logger::DebugExt ( const std::source_location& srcLocation, Str::StringRef format, Args&& ... args ) {
277+ if ( filterLevel->Get () <= Level::DEBUG ) {
278+ this ->Dispatch (
279+ AddSrcLocation ( Prefix ( Str::Format ( format, std::forward<Args>( args ) ... ) ), srcLocation, logExtendDebug.Get () ),
280+ Level::DEBUG, format );
281+ }
282+ }
283+ #else
284+ template <typename ... Args>
285+ void Logger::WarnExt ( Str::StringRef format, Args&& ... args ) {
286+ if ( filterLevel->Get () <= Level::WARNING ) {
287+ this ->Dispatch ( Prefix ( Str::Format ( format, std::forward<Args>( args ) ... ) ), Level::WARNING, format );
288+ }
289+ }
290+
291+ template <typename ... Args>
292+ void Logger::NoticeExt ( Str::StringRef format, Args&& ... args ) {
293+ if ( filterLevel->Get () <= Level::NOTICE ) {
294+ this ->Dispatch ( Prefix ( Str::Format ( format, std::forward<Args>( args ) ... ) ), Level::NOTICE, format );
295+ }
296+ }
297+
298+ template <typename ... Args>
299+ void Logger::VerboseExt ( Str::StringRef format, Args&& ... args ) {
300+ if ( filterLevel->Get () <= Level::VERBOSE ) {
301+ this ->Dispatch ( Prefix ( Str::Format ( format, std::forward<Args>( args ) ... ) ), Level::VERBOSE, format );
302+ }
303+ }
304+
305+ template <typename ... Args>
306+ void Logger::DebugExt ( Str::StringRef format, Args&& ... args ) {
307+ if ( filterLevel->Get () <= Level::DEBUG ) {
308+ this ->Dispatch ( Prefix ( Str::Format ( format, std::forward<Args>( args ) ... ) ), Level::DEBUG, format );
221309 }
222310 }
311+ #endif
223312
224313 template <typename F>
225314 inline void Logger::DoWarnCode (F&& code) {
@@ -252,25 +341,58 @@ namespace Log {
252341 // Quick Logs
253342 extern Logger defaultLogger;
254343
344+ #if defined( CPP_SOURCE_LOCATION )
255345 template <typename ... Args>
256- void Warn ( Str::StringRef format, Args&& ... args) {
257- defaultLogger.Warn ( format, std::forward<Args>(args) ...);
346+ void WarnExt ( const std::source_location& srcLocation, Str::StringRef format, Args&& ... args ) {
347+ defaultLogger.WarnExt ( srcLocation, format, std::forward<Args>( args ) ... );
258348 }
259349
260350 template <typename ... Args>
261- void Notice ( Str::StringRef format, Args&& ... args) {
262- defaultLogger.Notice ( format, std::forward<Args>(args) ...);
351+ void NoticeExt ( const std::source_location& srcLocation, Str::StringRef format, Args&& ... args ) {
352+ defaultLogger.NoticeExt ( srcLocation, format, std::forward<Args>( args ) ... );
263353 }
264354
265355 template <typename ... Args>
266- void Verbose ( Str::StringRef format, Args&& ... args) {
267- defaultLogger.Verbose ( format, std::forward<Args>(args) ...);
356+ void VerboseExt ( const std::source_location& srcLocation, Str::StringRef format, Args&& ... args ) {
357+ defaultLogger.VerboseExt ( srcLocation, format, std::forward<Args>( args ) ... );
268358 }
269359
270360 template <typename ... Args>
271- void Debug ( Str::StringRef format, Args&& ... args) {
272- defaultLogger.Debug ( format, std::forward<Args>(args) ...);
361+ void DebugExt ( const std::source_location& srcLocation, Str::StringRef format, Args&& ... args ) {
362+ defaultLogger.DebugExt ( srcLocation, format, std::forward<Args>( args ) ... );
273363 }
364+
365+ // Use ##__VA_ARGS__ instead of __VA_ARGS__ because args may be empty. __VA_OPT__( , ) currently doesn't seem to work on MSVC
366+ #define Warn ( format, ... ) WarnExt( std::source_location::current(), format, ##__VA_ARGS__ )
367+ #define Notice ( format, ... ) NoticeExt( std::source_location::current(), format, ##__VA_ARGS__ )
368+ #define Verbose ( format, ... ) VerboseExt( std::source_location::current(), format, ##__VA_ARGS__ )
369+ #define Debug ( format, ... ) DebugExt( std::source_location::current(), format, ##__VA_ARGS__ )
370+ #else
371+ template <typename ... Args>
372+ void WarnExt ( Str::StringRef format, Args&& ... args ) {
373+ defaultLogger.WarnExt ( format, std::forward<Args>( args ) ... );
374+ }
375+
376+ template <typename ... Args>
377+ void NoticeExt ( Str::StringRef format, Args&& ... args ) {
378+ defaultLogger.NoticeExt ( format, std::forward<Args>( args ) ... );
379+ }
380+
381+ template <typename ... Args>
382+ void VerboseExt ( Str::StringRef format, Args&& ... args ) {
383+ defaultLogger.VerboseExt ( format, std::forward<Args>( args ) ... );
384+ }
385+
386+ template <typename ... Args>
387+ void DebugExt ( Str::StringRef format, Args&& ... args ) {
388+ defaultLogger.DebugExt ( format, std::forward<Args>( args ) ... );
389+ }
390+
391+ #define Warn WarnExt
392+ #define Notice NoticeExt
393+ #define Verbose VerboseExt
394+ #define Debug DebugExt
395+ #endif
274396}
275397
276398namespace Cvar {
0 commit comments