@@ -20,7 +20,6 @@ written by
2020#include < iostream>
2121#include < iomanip>
2222#include < set>
23- #include < vector>
2423#include < sstream>
2524#include < cstdarg>
2625#ifdef _WIN32
@@ -116,7 +115,6 @@ struct LogConfig
116115 void * loghandler_opaque;
117116 srt::sync::Mutex mutex;
118117 int flags;
119- std::vector<struct LogDispatcher *> loggers;
120118
121119 LogConfig (const fa_bitset_t & efa,
122120 LogLevel::type l = LogLevel::warning,
@@ -139,10 +137,6 @@ struct LogConfig
139137
140138 SRT_ATTR_RELEASE (mutex)
141139 void unlock () { mutex.unlock (); }
142-
143- void subscribe (LogDispatcher*);
144- void unsubscribe (LogDispatcher*);
145- void updateLoggersState ();
146140};
147141
148142// The LogDispatcher class represents the object that is responsible for
@@ -154,7 +148,6 @@ struct SRT_API LogDispatcher
154148 LogLevel::type level;
155149 static const size_t MAX_PREFIX_SIZE = 32 ;
156150 char prefix[MAX_PREFIX_SIZE+1 ];
157- srt::sync::atomic<bool > enabled;
158151 LogConfig* src_config;
159152
160153 bool isset (int flg) { return (src_config->flags & flg) != 0 ; }
@@ -165,7 +158,6 @@ struct SRT_API LogDispatcher
165158 const char * logger_pfx /* [[nullable]]*/ , LogConfig& config):
166159 fa (functional_area),
167160 level (log_level),
168- enabled (false ),
169161 src_config (&config)
170162 {
171163 // XXX stpcpy desired, but not enough portable
@@ -193,18 +185,13 @@ struct SRT_API LogDispatcher
193185 prefix[MAX_PREFIX_SIZE] = ' \0 ' ;
194186#endif
195187 }
196- config.subscribe (this );
197- Update ();
198188 }
199189
200190 ~LogDispatcher ()
201191 {
202- src_config->unsubscribe (this );
203192 }
204193
205- void Update ();
206-
207- bool CheckEnabled () { return enabled; }
194+ bool CheckEnabled ();
208195
209196 void CreateLogLinePrefix (std::ostringstream&);
210197 void SendLogLine (const char * file, int line, const std::string& area, const std::string& sl);
@@ -428,6 +415,22 @@ class Logger
428415 }
429416};
430417
418+ inline bool LogDispatcher::CheckEnabled ()
419+ {
420+ // Don't use enabler caching. Check enabled state every time.
421+
422+ // These assume to be atomically read, so the lock is not needed
423+ // (note that writing to this field is still mutex-protected).
424+ // It's also no problem if the level was changed at the moment
425+ // when the enabler check is tested here. Worst case, the log
426+ // will be printed just a moment after it was turned off.
427+ const LogConfig* config = src_config; // to enforce using const operator[]
428+ int configured_enabled_fa = config->enabled_fa [fa];
429+ int configured_maxlevel = config->max_level ;
430+
431+ return configured_enabled_fa && level <= configured_maxlevel;
432+ }
433+
431434
432435#if HAVE_CXX11
433436
@@ -478,7 +481,24 @@ inline void LogDispatcher::PrintLogLine(const char* file SRT_ATR_UNUSED, int lin
478481
479482#endif // HAVE_CXX11
480483
484+ // SendLogLine can be compiled normally. It's intermediately used by:
485+ // - Proxy object, which is replaced by DummyProxy when !ENABLE_LOGGING
486+ // - PrintLogLine, which has empty body when !ENABLE_LOGGING
487+ inline void LogDispatcher::SendLogLine (const char * file, int line, const std::string& area, const std::string& msg)
488+ {
489+ src_config->lock ();
490+ if ( src_config->loghandler_fn )
491+ {
492+ (*src_config->loghandler_fn )(src_config->loghandler_opaque , int (level), file, line, area.c_str (), msg.c_str ());
493+ }
494+ else if ( src_config->log_stream )
495+ {
496+ (*src_config->log_stream ) << msg;
497+ (*src_config->log_stream ).flush ();
498+ }
499+ src_config->unlock ();
481500}
482501
483- # endif // INC_SRT_LOGGING_H
502+ }
484503
504+ #endif // INC_SRT_LOGGING_H
0 commit comments