Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Withdrawn changes from #2858 as they break ABI compat #2899

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions srtcore/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4656,29 +4656,29 @@
{
ScopedLock gg(srt_logger_config.mutex);
srt_logger_config.max_level = ll;
srt_logger_config.updateLoggersState();
// srt_logger_config.updateLoggersState();
Fixed Show fixed Hide fixed
}

void addlogfa(LogFA fa)
{
ScopedLock gg(srt_logger_config.mutex);
srt_logger_config.enabled_fa.set(fa, true);
srt_logger_config.updateLoggersState();
// srt_logger_config.updateLoggersState();
Fixed Show fixed Hide fixed
}

void dellogfa(LogFA fa)
{
ScopedLock gg(srt_logger_config.mutex);
srt_logger_config.enabled_fa.set(fa, false);
srt_logger_config.updateLoggersState();
// srt_logger_config.updateLoggersState();
Fixed Show fixed Hide fixed
}

void resetlogfa(set<LogFA> fas)
{
ScopedLock gg(srt_logger_config.mutex);
for (int i = 0; i <= SRT_LOGFA_LASTNONE; ++i)
srt_logger_config.enabled_fa.set(i, fas.count(i));
srt_logger_config.updateLoggersState();
// srt_logger_config.updateLoggersState();
Fixed Show fixed Hide fixed
}

void resetlogfa(const int* fara, size_t fara_size)
Expand All @@ -4687,7 +4687,7 @@
srt_logger_config.enabled_fa.reset();
for (const int* i = fara; i != fara + fara_size; ++i)
srt_logger_config.enabled_fa.set(*i, true);
srt_logger_config.updateLoggersState();
// srt_logger_config.updateLoggersState();
Fixed Show fixed Hide fixed
}

void setlogstream(std::ostream& stream)
Expand Down
4 changes: 3 additions & 1 deletion srtcore/logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,69 +23,71 @@
namespace srt_logging
{

/* Blocked temporarily until 1.6.0. This causes ABI incompat with 1.5.0 line.

// Note: subscribe() and unsubscribe() functions are being called
// in the global constructor and destructor only, as the
// Logger objects (and inside them also their LogDispatcher)
// are being created. It's not predicted that LogDispatcher
// object are going to be created any other way than as
// global objects. Therefore the construction and destruction
// of them happens always in the main thread.

void LogConfig::subscribe(LogDispatcher* lg)
{
vector<LogDispatcher*>::iterator p = std::find(loggers.begin(), loggers.end(), lg);
if (p != loggers.end())
return; // Do not register twice

loggers.push_back(lg);
}

void LogConfig::unsubscribe(LogDispatcher* lg)
{
vector<LogDispatcher*>::iterator p = std::find(loggers.begin(), loggers.end(), lg);
if (p != loggers.end())
{
loggers.erase(p);
}
}

// This function doesn't have any protection on itself,
// however the API functions from which it is called, call
// it already under a mutex protection.
void LogConfig::updateLoggersState()
{
for (vector<LogDispatcher*>::iterator p = loggers.begin();
p != loggers.end(); ++p)
{
(*p)->Update();
}
}

void LogDispatcher::Update()
{
bool enabled_in_fa = src_config->enabled_fa[fa];
enabled = enabled_in_fa && level <= src_config->max_level;
}


// SendLogLine can be compiled normally. It's intermediately used by:
// - Proxy object, which is replaced by DummyProxy when !ENABLE_LOGGING
// - PrintLogLine, which has empty body when !ENABLE_LOGGING
void LogDispatcher::SendLogLine(const char* file, int line, const std::string& area, const std::string& msg)
{
src_config->lock();
if ( src_config->loghandler_fn )
{
(*src_config->loghandler_fn)(src_config->loghandler_opaque, int(level), file, line, area.c_str(), msg.c_str());
}
else if ( src_config->log_stream )
{
(*src_config->log_stream) << msg;
src_config->log_stream->flush();
}
src_config->unlock();
}

*/
Fixed Show fixed Hide fixed

#if ENABLE_LOGGING

Expand Down
57 changes: 45 additions & 12 deletions srtcore/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include <iostream>
#include <iomanip>
#include <set>
#include <vector>
//#include <vector>
Fixed Show fixed Hide fixed
ethouris marked this conversation as resolved.
Show resolved Hide resolved
#include <sstream>
#include <cstdarg>
#ifdef _WIN32
Expand Down Expand Up @@ -116,7 +116,7 @@
void* loghandler_opaque;
srt::sync::Mutex mutex;
int flags;
std::vector<struct LogDispatcher*> loggers;
// std::vector<struct LogDispatcher*> loggers;
Fixed Show fixed Hide fixed

LogConfig(const fa_bitset_t& efa,
LogLevel::type l = LogLevel::warning,
Expand All @@ -139,10 +139,11 @@

SRT_ATTR_RELEASE(mutex)
void unlock() { mutex.unlock(); }

/*
void subscribe(LogDispatcher*);
void unsubscribe(LogDispatcher*);
void updateLoggersState();
*/
Fixed Show fixed Hide fixed
};

// The LogDispatcher class represents the object that is responsible for
Expand All @@ -154,7 +155,7 @@
LogLevel::type level;
static const size_t MAX_PREFIX_SIZE = 32;
char prefix[MAX_PREFIX_SIZE+1];
srt::sync::atomic<bool> enabled;
// srt::sync::atomic<bool> enabled;
Fixed Show fixed Hide fixed
LogConfig* src_config;

bool isset(int flg) { return (src_config->flags & flg) != 0; }
Expand All @@ -165,7 +166,7 @@
const char* logger_pfx /*[[nullable]]*/, LogConfig& config):
fa(functional_area),
level(log_level),
enabled(false),
// enabled(false),
src_config(&config)
{
// XXX stpcpy desired, but not enough portable
Expand Down Expand Up @@ -193,18 +194,17 @@
prefix[MAX_PREFIX_SIZE] = '\0';
#endif
}
config.subscribe(this);
Update();
// config.subscribe(this);
// Update();
Fixed Show fixed Hide fixed
}

~LogDispatcher()
{
src_config->unsubscribe(this);
// src_config->unsubscribe(this);
Fixed Show fixed Hide fixed
}

void Update();

bool CheckEnabled() { return enabled; }
// void Update();
bool CheckEnabled(); // { return enabled; }

void CreateLogLinePrefix(std::ostringstream&);
void SendLogLine(const char* file, int line, const std::string& area, const std::string& sl);
Expand Down Expand Up @@ -428,6 +428,22 @@
}
};

inline bool LogDispatcher::CheckEnabled()
{
// Don't use enabler caching. Check enabled state every time.

// These assume to be atomically read, so the lock is not needed
// (note that writing to this field is still mutex-protected).
// It's also no problem if the level was changed at the moment
// when the enabler check is tested here. Worst case, the log
// will be printed just a moment after it was turned off.
const LogConfig* config = src_config; // to enforce using const operator[]
int configured_enabled_fa = config->enabled_fa[fa];
int configured_maxlevel = config->max_level;

return configured_enabled_fa && level <= configured_maxlevel;
}


#if HAVE_CXX11

Expand Down Expand Up @@ -478,7 +494,24 @@

#endif // HAVE_CXX11

// SendLogLine can be compiled normally. It's intermediately used by:
// - Proxy object, which is replaced by DummyProxy when !ENABLE_LOGGING
// - PrintLogLine, which has empty body when !ENABLE_LOGGING
inline void LogDispatcher::SendLogLine(const char* file, int line, const std::string& area, const std::string& msg)
{
src_config->lock();
if ( src_config->loghandler_fn )
{
(*src_config->loghandler_fn)(src_config->loghandler_opaque, int(level), file, line, area.c_str(), msg.c_str());
}
else if ( src_config->log_stream )
{
(*src_config->log_stream) << msg;
(*src_config->log_stream).flush();
}
src_config->unlock();
}

}

#endif // INC_SRT_LOGGING_H