Skip to content

Commit 18f5c57

Browse files
committed
Fix file:line logging in gamelogic
logs.writeSrcLocation.* cvars no longer emit warnings due to duplicate registration, and are obeyed by the gamelogic.
1 parent 8183091 commit 18f5c57

File tree

2 files changed

+41
-53
lines changed

2 files changed

+41
-53
lines changed

src/common/Log.cpp

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@ static Cvar::Range<Cvar::Cvar<int>> suppressionCount(
4040
"logs.suppression.count", "Number of occurrences for a message to be considered log spam", Cvar::NONE, 10, 1, 1000000);
4141
static Cvar::Range<Cvar::Cvar<int>> suppressionBufSize(
4242
"logs.suppression.bufferSize", "How many distinct messages to track for log suppression", Cvar::NONE, 50, 1, 1000000);
43+
static Cvar::Cvar<bool> logExtendAll(
44+
"logs.writeSrcLocation.all", "Always print source code location for logs", Cvar::NONE, false);
45+
static Cvar::Cvar<bool> logExtendWarn(
46+
"logs.writeSrcLocation.warn", "Print source code location for Warn logs", Cvar::NONE, false);
47+
static Cvar::Cvar<bool> logExtendNotice(
48+
"logs.writeSrcLocation.notice", "Print source code location for Notice logs", Cvar::NONE, false);
49+
static Cvar::Cvar<bool> logExtendVerbose(
50+
"logs.writeSrcLocation.verbose", "Print source code location for Verbose logs", Cvar::NONE, false);
51+
static Cvar::Cvar<bool> logExtendDebug(
52+
"logs.writeSrcLocation.debug", "Print source code location for Debug logs", Cvar::NONE, false);
4353

4454
#define GET_LOG_CVAR(type, name, object) (object.Get())
4555
#else
@@ -56,16 +66,6 @@ static T GetCvarOrDie(Str::StringRef cvar) {
5666
#endif
5767

5868
namespace Log {
59-
Cvar::Cvar<bool> logExtendAll(
60-
"logs.writeSrcLocation.all", "Always print source code location for logs", Cvar::NONE, false );
61-
Cvar::Cvar<bool> logExtendWarn(
62-
"logs.writeSrcLocation.warn", "Print source code location for Warn logs", Cvar::NONE, false );
63-
Cvar::Cvar<bool> logExtendNotice(
64-
"logs.writeSrcLocation.notice", "Print source code location for Notice logs", Cvar::NONE, false );
65-
Cvar::Cvar<bool> logExtendVerbose(
66-
"logs.writeSrcLocation.verbose", "Print source code location for Verbose logs", Cvar::NONE, false );
67-
Cvar::Cvar<bool> logExtendDebug(
68-
"logs.writeSrcLocation.debug", "Print source code location for Debug logs", Cvar::NONE, false );
6969

7070
Logger::Logger(Str::StringRef name, std::string prefix, Level defaultLevel)
7171
: filterLevel(new Cvar::Cvar<Log::Level>(
@@ -82,7 +82,27 @@ namespace Log {
8282
return prefix + message;
8383
}
8484

85-
void Logger::Dispatch(std::string message, Log::Level level, Str::StringRef format) {
85+
static bool WantLocationInfo(Log::Level level) {
86+
if (GET_LOG_CVAR(bool, "logs.writeSrcLocation.all", logExtendAll)) {
87+
return true;
88+
}
89+
switch (level) {
90+
case Log::Level::DEBUG:
91+
return GET_LOG_CVAR(bool, "logs.writeSrcLocation.debug", logExtendDebug);
92+
case Log::Level::VERBOSE:
93+
return GET_LOG_CVAR(bool, "logs.writeSrcLocation.verbose", logExtendVerbose);
94+
case Log::Level::NOTICE:
95+
return GET_LOG_CVAR(bool, "logs.writeSrcLocation.notice", logExtendNotice);
96+
case Log::Level::WARNING:
97+
return GET_LOG_CVAR(bool, "logs.writeSrcLocation.warn", logExtendWarn);
98+
}
99+
ASSERT_UNREACHABLE();
100+
}
101+
102+
void Logger::Dispatch(std::string message, Log::Level level, Str::StringRef format, const char* file, const char* function, int line) {
103+
if (WantLocationInfo(level)) {
104+
message = Str::Format("%s ^F(%s:%u, %s)", message, file, line, function);
105+
}
86106
if (enableSuppression) {
87107
Log::DispatchWithSuppression(std::move(message), level, format);
88108
} else {

src/common/Log.h

Lines changed: 10 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,6 @@ 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-
6054
/*
6155
* Loggers are used to group logs by subsystems and allow logs
6256
* to be filtered by log level by subsystem. They are used like so
@@ -116,7 +110,8 @@ namespace Log {
116110
Logger WithoutSuppression();
117111

118112
private:
119-
void Dispatch(std::string message, Log::Level level, Str::StringRef format);
113+
void Dispatch(std::string message, Log::Level level, Str::StringRef format,
114+
const char* file, const char* function, int line);
120115

121116
std::string Prefix(Str::StringRef message) const;
122117

@@ -203,62 +198,35 @@ namespace Log {
203198

204199
// Logger
205200

206-
inline std::string AddSrcLocation( const std::string& message,
207-
const char* file, const char* function, const int line,
208-
const bool extend ) {
209-
if ( logExtendAll.Get() || extend ) {
210-
return message + Str::Format( " ^F(%s:%u, %s)",
211-
file, line, function );
212-
}
213-
214-
return message;
215-
}
216-
217201
template<typename ... Args>
218202
void Logger::WarnExt( const char* file, const char* function, const int line, Str::StringRef format, Args&& ... args ) {
219203
if ( filterLevel->Get() <= Level::WARNING ) {
220-
this->Dispatch(
221-
AddSrcLocation(
222-
Prefix( Str::Format( format, std::forward<Args>( args ) ... ) ),
223-
file, function, line, logExtendWarn.Get()
224-
),
225-
Level::WARNING, format );
204+
this->Dispatch(Prefix(Str::Format(format, std::forward<Args>(args)...)),
205+
Level::WARNING, format, file, function, line);
226206
}
227207
}
228208

229209
template<typename ... Args>
230210
void Logger::NoticeExt( const char* file, const char* function, const int line, Str::StringRef format, Args&& ... args ) {
231211
if ( filterLevel->Get() <= Level::NOTICE ) {
232-
this->Dispatch(
233-
AddSrcLocation(
234-
Prefix( Str::Format( format, std::forward<Args>( args ) ... ) ),
235-
file, function, line, logExtendNotice.Get()
236-
),
237-
Level::NOTICE, format );
212+
this->Dispatch(Prefix(Str::Format(format, std::forward<Args>(args)...)),
213+
Level::NOTICE, format, file, function, line);
238214
}
239215
}
240216

241217
template<typename ... Args>
242218
void Logger::VerboseExt( const char* file, const char* function, const int line, Str::StringRef format, Args&& ... args ) {
243219
if ( filterLevel->Get() <= Level::VERBOSE ) {
244-
this->Dispatch(
245-
AddSrcLocation(
246-
Prefix( Str::Format( format, std::forward<Args>( args ) ... ) ),
247-
file, function, line, logExtendVerbose.Get()
248-
),
249-
Level::VERBOSE, format );
220+
this->Dispatch(Prefix(Str::Format(format, std::forward<Args>(args)...)),
221+
Level::VERBOSE, format, file, function, line);
250222
}
251223
}
252224

253225
template<typename ... Args>
254226
void Logger::DebugExt( const char* file, const char* function, const int line, Str::StringRef format, Args&& ... args ) {
255227
if ( filterLevel->Get() <= Level::DEBUG ) {
256-
this->Dispatch(
257-
AddSrcLocation(
258-
Prefix( Str::Format( format, std::forward<Args>( args ) ... ) ),
259-
file, function, line, logExtendDebug.Get()
260-
),
261-
Level::DEBUG, format );
228+
this->Dispatch(Prefix(Str::Format(format, std::forward<Args>(args)...)),
229+
Level::DEBUG, format, file, function, line);
262230
}
263231
}
264232

0 commit comments

Comments
 (0)