#ifndef INC_SRT_LOGGING_H
#define INC_SRT_LOGGING_H
#include <iostream>
#include <iomanip>
#include <set>
#include <sstream>
#include <cstdarg>
#ifdef _WIN32
#include "win/wintime.h"
#include <sys/timeb.h>
#else
#include <sys/time.h>
#endif
#include "srt.h"
#include "utilities.h"
#include "threadname.h"
#include "logging_api.h"
#include "srt_compat.h"
#include "sync.h"
#ifdef __GNUC__
#define PRINTF_LIKE __attribute__((format(printf,2,3)))
#else
#define PRINTF_LIKE
#endif
#if ENABLE_LOGGING
#define LOGC(logdes, args) if (logdes.CheckEnabled()) \
{ \
srt_logging::LogDispatcher::Proxy log(logdes); \
log.setloc(__FILE__, __LINE__, __FUNCTION__); \
const srt_logging::LogDispatcher::Proxy& log_prox SRT_ATR_UNUSED = args; \
}
#define LOGF(logdes, ...) if (logdes.CheckEnabled()) logdes().setloc(__FILE__, __LINE__, __FUNCTION__).form(__VA_ARGS__)
#define LOGP(logdes, ...) if (logdes.CheckEnabled()) logdes.printloc(__FILE__, __LINE__, __FUNCTION__,##__VA_ARGS__)
#if ENABLE_HEAVY_LOGGING
#define HLOGC LOGC
#define HLOGP LOGP
#define HLOGF LOGF
#define IF_HEAVY_LOGGING(instr) instr
#else
#define HLOGC(...)
#define HLOGF(...)
#define HLOGP(...)
#define IF_HEAVY_LOGGING(instr) (void)0
#endif
#else
#define LOGC(...)
#define LOGF(...)
#define LOGP(...)
#define HLOGC(...)
#define HLOGF(...)
#define HLOGP(...)
#define IF_HEAVY_LOGGING(instr) (void)0
#endif
namespace srt_logging
{
struct LogConfig
{
typedef std::bitset<SRT_LOGFA_LASTNONE+1> fa_bitset_t;
fa_bitset_t enabled_fa; LogLevel::type max_level; std::ostream* log_stream;
SRT_LOG_HANDLER_FN* loghandler_fn;
void* loghandler_opaque;
srt::sync::Mutex mutex;
int flags;
LogConfig(const fa_bitset_t& efa,
LogLevel::type l = LogLevel::warning,
std::ostream* ls = &std::cerr)
: enabled_fa(efa)
, max_level(l)
, log_stream(ls)
, loghandler_fn()
, loghandler_opaque()
, flags()
{
}
~LogConfig()
{
}
void lock() { mutex.lock(); }
void unlock() { mutex.unlock(); }
};
struct SRT_API LogDispatcher
{
private:
int fa;
LogLevel::type level;
static const size_t MAX_PREFIX_SIZE = 32;
char prefix[MAX_PREFIX_SIZE+1];
LogConfig* src_config;
bool isset(int flg) { return (src_config->flags & flg) != 0; }
public:
LogDispatcher(int functional_area, LogLevel::type log_level, const char* your_pfx,
const char* logger_pfx , LogConfig& config):
fa(functional_area),
level(log_level),
src_config(&config)
{
strcpy(prefix, your_pfx);
if (logger_pfx && strlen(prefix) + strlen(logger_pfx) + 1 < MAX_PREFIX_SIZE)
{
strcat(prefix, ":");
strcat(prefix, logger_pfx);
}
}
~LogDispatcher()
{
}
bool CheckEnabled();
void CreateLogLinePrefix(std::ostringstream&);
void SendLogLine(const char* file, int line, const std::string& area, const std::string& sl);
#if HAVE_CXX11
template <class... Args>
void PrintLogLine(const char* file, int line, const std::string& area, Args&&... args);
template<class Arg1, class... Args>
void operator()(Arg1&& arg1, Args&&... args)
{
PrintLogLine("UNKNOWN.c++", 0, "UNKNOWN", arg1, args...);
}
template<class Arg1, class... Args>
void printloc(const char* file, int line, const std::string& area, Arg1&& arg1, Args&&... args)
{
PrintLogLine(file, line, area, arg1, args...);
}
#else
template <class Arg>
void PrintLogLine(const char* file, int line, const std::string& area, const Arg& arg);
template <class Arg>
void operator()(const Arg& arg)
{
PrintLogLine("UNKNOWN.c++", 0, "UNKNOWN", arg);
}
void printloc(const char* file, int line, const std::string& area, const std::string& arg1)
{
PrintLogLine(file, line, area, arg1);
}
#endif
#if ENABLE_LOGGING
struct Proxy;
friend struct Proxy;
Proxy operator()();
#else
struct DummyProxy
{
DummyProxy(LogDispatcher&)
{
}
template <class T>
DummyProxy& operator<<(const T& ) {
return *this;
}
DummyProxy& form(const char*, ...)
{
return *this;
}
DummyProxy& setloc(const char* , int , std::string)
{
return *this;
}
};
DummyProxy operator()()
{
return DummyProxy(*this);
}
#endif
};
#if ENABLE_LOGGING
struct LogDispatcher::Proxy
{
LogDispatcher& that;
std::ostringstream os;
bool that_enabled;
int flags;
const char* i_file;
int i_line;
std::string area;
Proxy& setloc(const char* f, int l, std::string a)
{
i_file = f;
i_line = l;
area = a;
return *this;
}
std::string ExtractName(std::string pretty_function);
Proxy(LogDispatcher& guy);
Proxy(const Proxy& p): that(p.that), area(p.area)
{
i_file = p.i_file;
i_line = p.i_line;
that_enabled = false;
flags = p.flags;
}
template <class T>
Proxy& operator<<(const T& arg) {
if ( that_enabled )
{
os << arg;
}
return *this;
}
~Proxy()
{
if ( that_enabled )
{
if ( (flags & SRT_LOGF_DISABLE_EOL) == 0 )
os << std::endl;
that.SendLogLine(i_file, i_line, area, os.str());
}
}
Proxy& form(const char* fmts, ...) PRINTF_LIKE
{
if ( !that_enabled )
return *this;
if ( !fmts || fmts[0] == '\0' )
return *this;
va_list ap;
va_start(ap, fmts);
vform(fmts, ap);
va_end(ap);
return *this;
}
Proxy& vform(const char* fmts, va_list ap)
{
char buf[512];
vsprintf(buf, fmts, ap);
size_t len = strlen(buf);
if ( buf[len-1] == '\n' )
{
buf[len-1] = '\0';
}
os << buf;
return *this;
}
};
#endif
class Logger
{
int m_fa;
LogConfig& m_config;
public:
LogDispatcher Debug;
LogDispatcher Note;
LogDispatcher Warn;
LogDispatcher Error;
LogDispatcher Fatal;
Logger(int functional_area, LogConfig& config, const char* logger_pfx = NULL):
m_fa(functional_area),
m_config(config),
Debug ( m_fa, LogLevel::debug, " D", logger_pfx, m_config ),
Note ( m_fa, LogLevel::note, ".N", logger_pfx, m_config ),
Warn ( m_fa, LogLevel::warning, "!W", logger_pfx, m_config ),
Error ( m_fa, LogLevel::error, "*E", logger_pfx, m_config ),
Fatal ( m_fa, LogLevel::fatal, "!!FATAL!!", logger_pfx, m_config )
{
}
};
inline bool LogDispatcher::CheckEnabled()
{
const LogConfig* config = src_config; int configured_enabled_fa = config->enabled_fa[fa];
int configured_maxlevel = config->max_level;
return configured_enabled_fa && level <= configured_maxlevel;
}
#if HAVE_CXX11
inline void PrintArgs(std::ostream&) {}
template <class Arg1, class... Args>
inline void PrintArgs(std::ostream& serr, Arg1&& arg1, Args&&... args)
{
serr << arg1;
PrintArgs(serr, args...);
}
template <class... Args>
inline void LogDispatcher::PrintLogLine(const char* file ATR_UNUSED, int line ATR_UNUSED, const std::string& area ATR_UNUSED, Args&&... args ATR_UNUSED)
{
#ifdef ENABLE_LOGGING
std::ostringstream serr;
CreateLogLinePrefix(serr);
PrintArgs(serr, args...);
if ( !isset(SRT_LOGF_DISABLE_EOL) )
serr << std::endl;
SendLogLine(file, line, area, serr.str());
#endif
}
#else
template <class Arg>
inline void LogDispatcher::PrintLogLine(const char* file ATR_UNUSED, int line ATR_UNUSED, const std::string& area ATR_UNUSED, const Arg& arg ATR_UNUSED)
{
#ifdef ENABLE_LOGGING
std::ostringstream serr;
CreateLogLinePrefix(serr);
serr << arg;
if ( !isset(SRT_LOGF_DISABLE_EOL) )
serr << std::endl;
SendLogLine(file, line, area, serr.str());
#endif
}
#endif
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