@@ -30,33 +30,79 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3030
3131#include " Common.h"
3232
33+ #ifdef BUILD_ENGINE
34+ // TODO: when cvar multiple registration is available, just declare the cvars in each module.
35+ static Cvar::Cvar<bool > suppressionEnabled (
36+ " logs.suppression.enabled" , " Whether to suppress log messages that are printed too many times" , Cvar::NONE, true );
37+ static Cvar::Range<Cvar::Cvar<int >> suppressionInterval (
38+ " logs.suppression.interval" , " Interval in milliseconds for detecting log spam" , Cvar::NONE, 2000 , 1 , 1000000 );
39+ static Cvar::Range<Cvar::Cvar<int >> suppressionCount (
40+ " logs.suppression.count" , " Number of occurrences for a message to be considered log spam" , Cvar::NONE, 10 , 1 , 1000000 );
41+ static Cvar::Range<Cvar::Cvar<int >> suppressionBufSize (
42+ " 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 );
53+
54+ #define GET_LOG_CVAR (type, name, object ) (object.Get())
55+ #else
56+ #define GET_LOG_CVAR (type, name, object ) (GetCvarOrDie<type>(name))
57+ template <typename T>
58+ static T GetCvarOrDie (Str::StringRef cvar) {
59+ T value;
60+ std::string valueString = Cvar::GetValue (cvar);
61+ if (!Cvar::ParseCvarValue (valueString, value)) {
62+ Sys::Error (" Failed to deserialize cvar %s with value: %s" , cvar, valueString);
63+ }
64+ return value;
65+ }
66+ #endif
67+
3368namespace 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 );
4469
4570 Logger::Logger (Str::StringRef name, std::string prefix, Level defaultLevel)
4671 : filterLevel(new Cvar::Cvar<Log::Level>(
4772 " logs.level." + name, " Log::Level - logs from '" + name + " ' below the level specified are filtered" , 0 , defaultLevel)),
48- prefix (prefix), enableSuppression(true ) {
73+ enableSuppression (true )
74+ {
75+ if (!prefix.empty ()) {
76+ // TODO allow prefixes without a space, e.g. a color code
77+ this ->prefix = prefix + " " ;
78+ }
4979 }
5080
51- std::string Logger::Prefix (std::string message) const {
52- if (prefix.empty ()) {
53- return message;
54- } else {
55- return prefix + " " + message;
81+ std::string Logger::Prefix (Str::StringRef message) const {
82+ return prefix + message;
83+ }
84+
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);
5698 }
99+ ASSERT_UNREACHABLE ();
57100 }
58101
59- void Logger::Dispatch (std::string message, Log::Level level, Str::StringRef format) {
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+ }
60106 if (enableSuppression) {
61107 Log::DispatchWithSuppression (std::move (message), level, format);
62108 } else {
@@ -136,15 +182,6 @@ namespace Log {
136182 }
137183 }
138184
139- template <typename T>
140- static T GetCvarOrDie (Str::StringRef cvar) {
141- T value;
142- std::string valueString = Cvar::GetValue (cvar);
143- if (!Cvar::ParseCvarValue (valueString, value)) {
144- Sys::Error (" Failed to deserialize cvar %s with value: %s" , cvar, valueString);
145- }
146- return value;
147- }
148185 namespace {
149186 // Log-spam suppression: if more than MAX_OCCURRENCES log messages with the same format string
150187 // are sent in less than INTERVAL_MS milliseconds, they will stop being printed.
@@ -166,9 +203,9 @@ namespace Log {
166203 };
167204 Result UpdateAndEvaluate (Str::StringRef messageFormat) {
168205 std::lock_guard<std::mutex> lock (mutex);
169- int intervalMs = GetCvarOrDie< int >( " logs.suppression.interval" );
170- int maxOccurrences = GetCvarOrDie< int >( " logs.suppression.count" );
171- int bufferSize = GetCvarOrDie< int >( " logs.suppression.bufferSize" );
206+ int intervalMs = GET_LOG_CVAR ( int , " logs.suppression.interval" , suppressionInterval );
207+ int maxOccurrences = GET_LOG_CVAR ( int , " logs.suppression.count" , suppressionCount );
208+ int bufferSize = GET_LOG_CVAR ( int , " logs.suppression.bufferSize" , suppressionBufSize );
172209 buf.resize (bufferSize);
173210 MessageStatistics* oldest = &buf[0 ];
174211 int now = Sys::Milliseconds ();
@@ -199,7 +236,7 @@ namespace Log {
199236
200237 void DispatchWithSuppression (std::string message, Log::Level level, Str::StringRef format) {
201238 static LogSpamSuppressor suppressor;
202- if (level == Level::DEBUG || !GetCvarOrDie< bool >( " logs.suppression.enabled" )) {
239+ if (level == Level::DEBUG || !GET_LOG_CVAR ( bool , " logs.suppression.enabled" , suppressionEnabled )) {
203240 DispatchByLevel (std::move (message), level);
204241 return ;
205242 }
0 commit comments