Skip to content

Commit 710c202

Browse files
authored
[core] Withdrawn changes from #2858 as they break ABI compat (#2899).
1 parent 12aad6e commit 710c202

File tree

3 files changed

+35
-83
lines changed

3 files changed

+35
-83
lines changed

srtcore/api.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4656,29 +4656,25 @@ void setloglevel(LogLevel::type ll)
46564656
{
46574657
ScopedLock gg(srt_logger_config.mutex);
46584658
srt_logger_config.max_level = ll;
4659-
srt_logger_config.updateLoggersState();
46604659
}
46614660

46624661
void addlogfa(LogFA fa)
46634662
{
46644663
ScopedLock gg(srt_logger_config.mutex);
46654664
srt_logger_config.enabled_fa.set(fa, true);
4666-
srt_logger_config.updateLoggersState();
46674665
}
46684666

46694667
void dellogfa(LogFA fa)
46704668
{
46714669
ScopedLock gg(srt_logger_config.mutex);
46724670
srt_logger_config.enabled_fa.set(fa, false);
4673-
srt_logger_config.updateLoggersState();
46744671
}
46754672

46764673
void resetlogfa(set<LogFA> fas)
46774674
{
46784675
ScopedLock gg(srt_logger_config.mutex);
46794676
for (int i = 0; i <= SRT_LOGFA_LASTNONE; ++i)
46804677
srt_logger_config.enabled_fa.set(i, fas.count(i));
4681-
srt_logger_config.updateLoggersState();
46824678
}
46834679

46844680
void resetlogfa(const int* fara, size_t fara_size)
@@ -4687,7 +4683,6 @@ void resetlogfa(const int* fara, size_t fara_size)
46874683
srt_logger_config.enabled_fa.reset();
46884684
for (const int* i = fara; i != fara + fara_size; ++i)
46894685
srt_logger_config.enabled_fa.set(*i, true);
4690-
srt_logger_config.updateLoggersState();
46914686
}
46924687

46934688
void setlogstream(std::ostream& stream)

srtcore/logging.cpp

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -23,69 +23,6 @@ using namespace std;
2323
namespace srt_logging
2424
{
2525

26-
// Note: subscribe() and unsubscribe() functions are being called
27-
// in the global constructor and destructor only, as the
28-
// Logger objects (and inside them also their LogDispatcher)
29-
// are being created. It's not predicted that LogDispatcher
30-
// object are going to be created any other way than as
31-
// global objects. Therefore the construction and destruction
32-
// of them happens always in the main thread.
33-
34-
void LogConfig::subscribe(LogDispatcher* lg)
35-
{
36-
vector<LogDispatcher*>::iterator p = std::find(loggers.begin(), loggers.end(), lg);
37-
if (p != loggers.end())
38-
return; // Do not register twice
39-
40-
loggers.push_back(lg);
41-
}
42-
43-
void LogConfig::unsubscribe(LogDispatcher* lg)
44-
{
45-
vector<LogDispatcher*>::iterator p = std::find(loggers.begin(), loggers.end(), lg);
46-
if (p != loggers.end())
47-
{
48-
loggers.erase(p);
49-
}
50-
}
51-
52-
// This function doesn't have any protection on itself,
53-
// however the API functions from which it is called, call
54-
// it already under a mutex protection.
55-
void LogConfig::updateLoggersState()
56-
{
57-
for (vector<LogDispatcher*>::iterator p = loggers.begin();
58-
p != loggers.end(); ++p)
59-
{
60-
(*p)->Update();
61-
}
62-
}
63-
64-
void LogDispatcher::Update()
65-
{
66-
bool enabled_in_fa = src_config->enabled_fa[fa];
67-
enabled = enabled_in_fa && level <= src_config->max_level;
68-
}
69-
70-
71-
// SendLogLine can be compiled normally. It's intermediately used by:
72-
// - Proxy object, which is replaced by DummyProxy when !ENABLE_LOGGING
73-
// - PrintLogLine, which has empty body when !ENABLE_LOGGING
74-
void LogDispatcher::SendLogLine(const char* file, int line, const std::string& area, const std::string& msg)
75-
{
76-
src_config->lock();
77-
if ( src_config->loghandler_fn )
78-
{
79-
(*src_config->loghandler_fn)(src_config->loghandler_opaque, int(level), file, line, area.c_str(), msg.c_str());
80-
}
81-
else if ( src_config->log_stream )
82-
{
83-
(*src_config->log_stream) << msg;
84-
src_config->log_stream->flush();
85-
}
86-
src_config->unlock();
87-
}
88-
8926

9027
#if ENABLE_LOGGING
9128

srtcore/logging.h

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)