| 1 |
/* |
| 2 |
* SRT - Secure, Reliable, Transport |
| 3 |
* Copyright (c) 2018 Haivision Systems Inc. |
| 4 |
* |
| 5 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 6 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 |
* |
| 9 |
*/ |
| 10 |
|
| 11 |
/***************************************************************************** |
| 12 |
written by |
| 13 |
Haivision Systems Inc. |
| 14 |
*****************************************************************************/ |
| 15 |
|
| 16 |
#ifndef INC_SRT_LOGGING_H |
| 17 |
#define INC_SRT_LOGGING_H |
| 18 |
|
| 19 |
|
| 20 |
#include <iostream> |
| 21 |
#include <iomanip> |
| 22 |
#include <set> |
| 23 |
#include <sstream> |
| 24 |
#include <cstdarg> |
| 25 |
#ifdef _WIN32 |
| 26 |
#include "win/wintime.h" |
| 27 |
#include <sys/timeb.h> |
| 28 |
#else |
| 29 |
#include <sys/time.h> |
| 30 |
#endif |
| 31 |
|
| 32 |
#include "srt.h" |
| 33 |
#include "utilities.h" |
| 34 |
#include "threadname.h" |
| 35 |
#include "logging_api.h" |
| 36 |
#include "sync.h" |
| 37 |
|
| 38 |
#ifdef __GNUC__ |
| 39 |
#define PRINTF_LIKE __attribute__((format(printf,2,3))) |
| 40 |
#else |
| 41 |
#define PRINTF_LIKE |
| 42 |
#endif |
| 43 |
|
| 44 |
#if ENABLE_LOGGING |
| 45 |
|
| 46 |
// GENERAL NOTE: All logger functions ADD THEIR OWN \n (EOL). Don't add any your own EOL character. |
| 47 |
// The logging system may not add the EOL character, if appropriate flag was set in log settings. |
| 48 |
// Anyway, treat the whole contents of eventually formatted message as exactly one line. |
| 49 |
|
| 50 |
// LOGC uses an iostream-like syntax, using the special 'log' symbol. |
| 51 |
// This symbol isn't visible outside the log macro parameters. |
| 52 |
// Usage: LOGC(gglog.Debug, log << param1 << param2 << param3); |
| 53 |
#define LOGC(logdes, args) if (logdes.CheckEnabled()) \ |
| 54 |
{ \ |
| 55 |
srt_logging::LogDispatcher::Proxy log(logdes); \ |
| 56 |
log.setloc(__FILE__, __LINE__, __FUNCTION__); \ |
| 57 |
{ (void)(const srt_logging::LogDispatcher::Proxy&)(args); } \ |
| 58 |
} |
| 59 |
|
| 60 |
// LOGF uses printf-like style formatting. |
| 61 |
// Usage: LOGF(gglog.Debug, "%s: %d", param1.c_str(), int(param2)); |
| 62 |
// NOTE: LOGF is deprecated and should not be used |
| 63 |
#define LOGF(logdes, ...) if (logdes.CheckEnabled()) logdes().setloc(__FILE__, __LINE__, __FUNCTION__).form(__VA_ARGS__) |
| 64 |
|
| 65 |
// LOGP is C++11 only OR with only one string argument. |
| 66 |
// Usage: LOGP(gglog.Debug, param1, param2, param3); |
| 67 |
#define LOGP(logdes, ...) if (logdes.CheckEnabled()) logdes.printloc(__FILE__, __LINE__, __FUNCTION__,##__VA_ARGS__) |
| 68 |
|
| 69 |
#define IF_LOGGING(instr) instr |
| 70 |
|
| 71 |
#if ENABLE_HEAVY_LOGGING |
| 72 |
|
| 73 |
#define HLOGC LOGC |
| 74 |
#define HLOGP LOGP |
| 75 |
#define HLOGF LOGF |
| 76 |
|
| 77 |
#define IF_HEAVY_LOGGING(instr,...) instr,##__VA_ARGS__ |
| 78 |
|
| 79 |
#else |
| 80 |
|
| 81 |
#define HLOGC(...) |
| 82 |
#define HLOGF(...) |
| 83 |
#define HLOGP(...) |
| 84 |
|
| 85 |
#define IF_HEAVY_LOGGING(instr) (void)0 |
| 86 |
|
| 87 |
#endif |
| 88 |
|
| 89 |
#else |
| 90 |
|
| 91 |
#define LOGC(...) |
| 92 |
#define LOGF(...) |
| 93 |
#define LOGP(...) |
| 94 |
|
| 95 |
#define HLOGC(...) |
| 96 |
#define HLOGF(...) |
| 97 |
#define HLOGP(...) |
| 98 |
|
| 99 |
#define IF_HEAVY_LOGGING(instr) (void)0 |
| 100 |
#define IF_LOGGING(instr) (void)0 |
| 101 |
|
| 102 |
#endif |
| 103 |
|
| 104 |
namespace srt_logging |
| 105 |
{ |
| 106 |
|
| 107 |
struct LogConfig |
| 108 |
{ |
| 109 |
typedef std::bitset<SRT_LOGFA_LASTNONE+1> fa_bitset_t; |
| 110 |
fa_bitset_t enabled_fa; // NOTE: assumed atomic reading |
| 111 |
LogLevel::type max_level; // NOTE: assumed atomic reading |
| 112 |
std::ostream* log_stream; |
| 113 |
SRT_LOG_HANDLER_FN* loghandler_fn; |
| 114 |
void* loghandler_opaque; |
| 115 |
mutable srt::sync::Mutex mutex; |
| 116 |
int flags; |
| 117 |
|
| 118 |
LogConfig(const fa_bitset_t& efa, |
| 119 |
LogLevel::type l = LogLevel::warning, |
| 120 |
std::ostream* ls = &std::cerr) |
| 121 |
: enabled_fa(efa) |
| 122 |
, max_level(l) |
| 123 |
, log_stream(ls) |
| 124 |
, loghandler_fn() |
| 125 |
, loghandler_opaque() |
| 126 |
, flags() |
| 127 |
{ |
| 128 |
} |
| 129 |
|
| 130 |
~LogConfig() |
| 131 |
{ |
| 132 |
} |
| 133 |
|
| 134 |
SRT_ATTR_ACQUIRE(mutex) |
| 135 |
void lock() const { mutex.lock(); } |
| 136 |
|
| 137 |
SRT_ATTR_RELEASE(mutex) |
| 138 |
void unlock() const { mutex.unlock(); } |
| 139 |
}; |
| 140 |
|
| 141 |
// The LogDispatcher class represents the object that is responsible for |
| 142 |
// a decision whether to log something or not, and if so, print the log. |
| 143 |
struct SRT_API LogDispatcher |
| 144 |
{ |
| 145 |
private: |
| 146 |
int fa; |
| 147 |
LogLevel::type level; |
| 148 |
static const size_t MAX_PREFIX_SIZE = 32; |
| 149 |
char prefix[MAX_PREFIX_SIZE+1]; |
| 150 |
size_t prefix_len; |
| 151 |
LogConfig* src_config; |
| 152 |
|
| 153 |
bool isset(int flg) { return (src_config->flags & flg) != 0; } |
| 154 |
|
| 155 |
public: |
| 156 |
|
| 157 |
LogDispatcher(int functional_area, LogLevel::type log_level, const char* your_pfx, |
| 158 |
const char* logger_pfx /*[[nullable]]*/, LogConfig& config): |
| 159 |
fa(functional_area), |
| 160 |
level(log_level), |
| 161 |
src_config(&config) |
| 162 |
{ |
| 163 |
const size_t your_pfx_len = your_pfx ? strlen(your_pfx) : 0; |
| 164 |
const size_t logger_pfx_len = logger_pfx ? strlen(logger_pfx) : 0; |
| 165 |
|
| 166 |
if (logger_pfx && your_pfx_len + logger_pfx_len + 1 < MAX_PREFIX_SIZE) |
| 167 |
{ |
| 168 |
memcpy(prefix, your_pfx, your_pfx_len); |
| 169 |
prefix[your_pfx_len] = ':'; |
| 170 |
memcpy(prefix + your_pfx_len + 1, logger_pfx, logger_pfx_len); |
| 171 |
prefix[your_pfx_len + logger_pfx_len + 1] = '\0'; |
| 172 |
prefix_len = your_pfx_len + logger_pfx_len + 1; |
| 173 |
} |
| 174 |
else if (your_pfx) |
| 175 |
{ |
| 176 |
// Prefix too long, so copy only your_pfx and only |
| 177 |
// as much as it fits |
| 178 |
size_t copylen = std::min(+MAX_PREFIX_SIZE, your_pfx_len); |
| 179 |
memcpy(prefix, your_pfx, copylen); |
| 180 |
prefix[copylen] = '\0'; |
| 181 |
prefix_len = copylen; |
| 182 |
} |
| 183 |
else |
| 184 |
{ |
| 185 |
prefix[0] = '\0'; |
| 186 |
prefix_len = 0; |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
~LogDispatcher() |
| 191 |
{ |
| 192 |
} |
| 193 |
|
| 194 |
bool CheckEnabled(); |
| 195 |
|
| 196 |
void CreateLogLinePrefix(std::ostringstream&); |
| 197 |
void SendLogLine(const char* file, int line, const std::string& area, const std::string& sl); |
| 198 |
|
| 199 |
// log.Debug("This is the ", nth, " time"); <--- C++11 only. |
| 200 |
// log.Debug() << "This is the " << nth << " time"; <--- C++03 available. |
| 201 |
|
| 202 |
#if HAVE_CXX11 |
| 203 |
|
| 204 |
template <class... Args> |
| 205 |
void PrintLogLine(const char* file, int line, const std::string& area, Args&&... args); |
| 206 |
|
| 207 |
template<class... Args> |
| 208 |
void operator()(Args&&... args) |
| 209 |
{ |
| 210 |
PrintLogLine("UNKNOWN.c++", 0, "UNKNOWN", args...); |
| 211 |
} |
| 212 |
|
| 213 |
template<class... Args> |
| 214 |
void printloc(const char* file, int line, const std::string& area, Args&&... args) |
| 215 |
{ |
| 216 |
PrintLogLine(file, line, area, args...); |
| 217 |
} |
| 218 |
#else |
| 219 |
template <class Arg> |
| 220 |
void PrintLogLine(const char* file, int line, const std::string& area, const Arg& arg); |
| 221 |
|
| 222 |
// For C++03 (older) standard provide only with one argument. |
| 223 |
template <class Arg> |
| 224 |
void operator()(const Arg& arg) |
| 225 |
{ |
| 226 |
PrintLogLine("UNKNOWN.c++", 0, "UNKNOWN", arg); |
| 227 |
} |
| 228 |
|
| 229 |
void printloc(const char* file, int line, const std::string& area, const std::string& arg1) |
| 230 |
{ |
| 231 |
PrintLogLine(file, line, area, arg1); |
| 232 |
} |
| 233 |
#endif |
| 234 |
|
| 235 |
#if ENABLE_LOGGING |
| 236 |
|
| 237 |
struct Proxy; |
| 238 |
friend struct Proxy; |
| 239 |
|
| 240 |
Proxy operator()(); |
| 241 |
#else |
| 242 |
|
| 243 |
// Dummy proxy that does nothing |
| 244 |
struct DummyProxy |
| 245 |
{ |
| 246 |
DummyProxy(LogDispatcher&) |
| 247 |
{ |
| 248 |
} |
| 249 |
|
| 250 |
template <class T> |
| 251 |
DummyProxy& operator<<(const T& ) // predicted for temporary objects |
| 252 |
{ |
| 253 |
return *this; |
| 254 |
} |
| 255 |
|
| 256 |
// DEPRECATED: DO NOT use LOGF/HLOGF macros anymore. |
| 257 |
// Use iostream-style formatting with LOGC or a direct argument with LOGP. |
| 258 |
SRT_ATR_DEPRECATED_PX DummyProxy& form(const char*, ...) SRT_ATR_DEPRECATED |
| 259 |
{ |
| 260 |
return *this; |
| 261 |
} |
| 262 |
|
| 263 |
DummyProxy& vform(const char*, va_list) |
| 264 |
{ |
| 265 |
return *this; |
| 266 |
} |
| 267 |
|
| 268 |
DummyProxy& setloc(const char* , int , std::string) |
| 269 |
{ |
| 270 |
return *this; |
| 271 |
} |
| 272 |
}; |
| 273 |
|
| 274 |
DummyProxy operator()() |
| 275 |
{ |
| 276 |
return DummyProxy(*this); |
| 277 |
} |
| 278 |
|
| 279 |
#endif |
| 280 |
|
| 281 |
}; |
| 282 |
|
| 283 |
#if ENABLE_LOGGING |
| 284 |
|
| 285 |
struct LogDispatcher::Proxy |
| 286 |
{ |
| 287 |
LogDispatcher& that; |
| 288 |
|
| 289 |
std::ostringstream os; |
| 290 |
|
| 291 |
// Cache the 'enabled' state in the beginning. If the logging |
| 292 |
// becomes enabled or disabled in the middle of the log, we don't |
| 293 |
// want it to be partially printed anyway. |
| 294 |
bool that_enabled; |
| 295 |
int flags; |
| 296 |
|
| 297 |
// CACHE!!! |
| 298 |
const char* i_file; |
| 299 |
int i_line; |
| 300 |
std::string area; |
| 301 |
|
| 302 |
Proxy& setloc(const char* f, int l, std::string a) |
| 303 |
{ |
| 304 |
i_file = f; |
| 305 |
i_line = l; |
| 306 |
area = a; |
| 307 |
return *this; |
| 308 |
} |
| 309 |
|
| 310 |
// Left for future. Not sure if it's more convenient |
| 311 |
// to use this to translate __PRETTY_FUNCTION__ to |
| 312 |
// something short, or just let's leave __FUNCTION__ |
| 313 |
// or better __func__. |
| 314 |
std::string ExtractName(std::string pretty_function); |
| 315 |
|
| 316 |
Proxy(LogDispatcher& guy); |
| 317 |
|
| 318 |
// Copy constructor is needed due to noncopyable ostringstream. |
| 319 |
// This is used only in creation of the default object, so just |
| 320 |
// use the default values, just copy the location cache. |
| 321 |
Proxy(const Proxy& p): that(p.that), area(p.area) |
| 322 |
{ |
| 323 |
i_file = p.i_file; |
| 324 |
i_line = p.i_line; |
| 325 |
that_enabled = false; |
| 326 |
flags = p.flags; |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
template <class T> |
| 331 |
Proxy& operator<<(const T& arg) // predicted for temporary objects |
| 332 |
{ |
| 333 |
if ( that_enabled ) |
| 334 |
{ |
| 335 |
os << arg; |
| 336 |
} |
| 337 |
return *this; |
| 338 |
} |
| 339 |
|
| 340 |
~Proxy() |
| 341 |
{ |
| 342 |
if (that_enabled) |
| 343 |
{ |
| 344 |
if ((flags & SRT_LOGF_DISABLE_EOL) == 0) |
| 345 |
os << std::endl; |
| 346 |
that.SendLogLine(i_file, i_line, area, os.str()); |
| 347 |
} |
| 348 |
// Needed in destructor? |
| 349 |
//os.clear(); |
| 350 |
//os.str(""); |
| 351 |
} |
| 352 |
|
| 353 |
Proxy& form(const char* fmts, ...) PRINTF_LIKE |
| 354 |
{ |
| 355 |
if ( !that_enabled ) |
| 356 |
return *this; |
| 357 |
|
| 358 |
if ( !fmts || fmts[0] == '\0' ) |
| 359 |
return *this; |
| 360 |
|
| 361 |
va_list ap; |
| 362 |
va_start(ap, fmts); |
| 363 |
vform(fmts, ap); |
| 364 |
va_end(ap); |
| 365 |
return *this; |
| 366 |
} |
| 367 |
|
| 368 |
Proxy& vform(const char* fmts, va_list ap) |
| 369 |
{ |
| 370 |
char buf[512]; |
| 371 |
|
| 372 |
#if defined(_MSC_VER) && _MSC_VER < 1900 |
| 373 |
_vsnprintf(buf, sizeof(buf) - 1, fmts, ap); |
| 374 |
#else |
| 375 |
vsnprintf(buf, sizeof(buf), fmts, ap); |
| 376 |
#endif |
| 377 |
size_t len = strlen(buf); |
| 378 |
if ( buf[len-1] == '\n' ) |
| 379 |
{ |
| 380 |
// Remove EOL character, should it happen to be at the end. |
| 381 |
// The EOL will be added at the end anyway. |
| 382 |
buf[len-1] = '\0'; |
| 383 |
} |
| 384 |
|
| 385 |
os.write(buf, len); |
| 386 |
return *this; |
| 387 |
} |
| 388 |
}; |
| 389 |
|
| 390 |
|
| 391 |
#endif |
| 392 |
|
| 393 |
class Logger |
| 394 |
{ |
| 395 |
int m_fa; |
| 396 |
LogConfig& m_config; |
| 397 |
|
| 398 |
public: |
| 399 |
|
| 400 |
LogDispatcher Debug; |
| 401 |
LogDispatcher Note; |
| 402 |
LogDispatcher Warn; |
| 403 |
LogDispatcher Error; |
| 404 |
LogDispatcher Fatal; |
| 405 |
|
| 406 |
Logger(int functional_area, LogConfig& config, const char* logger_pfx = NULL): |
| 407 |
m_fa(functional_area), |
| 408 |
m_config(config), |
| 409 |
Debug ( m_fa, LogLevel::debug, " D", logger_pfx, m_config ), |
| 410 |
Note ( m_fa, LogLevel::note, ".N", logger_pfx, m_config ), |
| 411 |
Warn ( m_fa, LogLevel::warning, "!W", logger_pfx, m_config ), |
| 412 |
Error ( m_fa, LogLevel::error, "*E", logger_pfx, m_config ), |
| 413 |
Fatal ( m_fa, LogLevel::fatal, "!!FATAL!!", logger_pfx, m_config ) |
| 414 |
{ |
| 415 |
} |
| 416 |
}; |
| 417 |
|
| 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 |
config->lock(); |
| 429 |
int configured_enabled_fa = config->enabled_fa[fa]; |
| 430 |
int configured_maxlevel = config->max_level; |
| 431 |
config->unlock(); |
| 432 |
|
| 433 |
return configured_enabled_fa && level <= configured_maxlevel; |
| 434 |
} |
| 435 |
|
| 436 |
|
| 437 |
#if HAVE_CXX11 |
| 438 |
|
| 439 |
//extern std::mutex Debug_mutex; |
| 440 |
|
| 441 |
inline void PrintArgs(std::ostream&) {} |
| 442 |
|
| 443 |
template <class Arg1, class... Args> |
| 444 |
inline void PrintArgs(std::ostream& serr, Arg1&& arg1, Args&&... args) |
| 445 |
{ |
| 446 |
serr << std::forward<Arg1>(arg1); |
| 447 |
PrintArgs(serr, args...); |
| 448 |
} |
| 449 |
|
| 450 |
template <class... Args> |
| 451 |
inline void LogDispatcher::PrintLogLine(const char* file SRT_ATR_UNUSED, int line SRT_ATR_UNUSED, const std::string& area SRT_ATR_UNUSED, Args&&... args SRT_ATR_UNUSED) |
| 452 |
{ |
| 453 |
#ifdef ENABLE_LOGGING |
| 454 |
std::ostringstream serr; |
| 455 |
CreateLogLinePrefix(serr); |
| 456 |
PrintArgs(serr, args...); |
| 457 |
|
| 458 |
if ( !isset(SRT_LOGF_DISABLE_EOL) ) |
| 459 |
serr << std::endl; |
| 460 |
|
| 461 |
// Not sure, but it wasn't ever used. |
| 462 |
SendLogLine(file, line, area, serr.str()); |
| 463 |
#endif |
| 464 |
} |
| 465 |
|
| 466 |
#else // !HAVE_CXX11 |
| 467 |
|
| 468 |
template <class Arg> |
| 469 |
inline void LogDispatcher::PrintLogLine(const char* file SRT_ATR_UNUSED, int line SRT_ATR_UNUSED, const std::string& area SRT_ATR_UNUSED, const Arg& arg SRT_ATR_UNUSED) |
| 470 |
{ |
| 471 |
#ifdef ENABLE_LOGGING |
| 472 |
std::ostringstream serr; |
| 473 |
CreateLogLinePrefix(serr); |
| 474 |
serr << arg; |
| 475 |
|
| 476 |
if ( !isset(SRT_LOGF_DISABLE_EOL) ) |
| 477 |
serr << std::endl; |
| 478 |
|
| 479 |
// Not sure, but it wasn't ever used. |
| 480 |
SendLogLine(file, line, area, serr.str()); |
| 481 |
#endif |
| 482 |
} |
| 483 |
|
| 484 |
#endif // HAVE_CXX11 |
| 485 |
|
| 486 |
// SendLogLine can be compiled normally. It's intermediately used by: |
| 487 |
// - Proxy object, which is replaced by DummyProxy when !ENABLE_LOGGING |
| 488 |
// - PrintLogLine, which has empty body when !ENABLE_LOGGING |
| 489 |
inline void LogDispatcher::SendLogLine(const char* file, int line, const std::string& area, const std::string& msg) |
| 490 |
{ |
| 491 |
src_config->lock(); |
| 492 |
if ( src_config->loghandler_fn ) |
| 493 |
{ |
| 494 |
(*src_config->loghandler_fn)(src_config->loghandler_opaque, int(level), file, line, area.c_str(), msg.c_str()); |
| 495 |
} |
| 496 |
else if ( src_config->log_stream ) |
| 497 |
{ |
| 498 |
src_config->log_stream->write(msg.data(), msg.size()); |
| 499 |
(*src_config->log_stream).flush(); |
| 500 |
} |
| 501 |
src_config->unlock(); |
| 502 |
} |
| 503 |
|
| 504 |
} |
| 505 |
|
| 506 |
#endif // INC_SRT_LOGGING_H |