logging.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. /*
  2. * Copyright 2004 The WebRTC Project Authors. All rights reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. // RTC_LOG(...) an ostream target that can be used to send formatted
  11. // output to a variety of logging targets, such as debugger console, stderr,
  12. // or any LogSink.
  13. // The severity level passed as the first argument to the logging
  14. // functions is used as a filter, to limit the verbosity of the logging.
  15. // Static members of LogMessage documented below are used to control the
  16. // verbosity and target of the output.
  17. // There are several variations on the RTC_LOG macro which facilitate logging
  18. // of common error conditions, detailed below.
  19. // RTC_LOG(sev) logs the given stream at severity "sev", which must be a
  20. // compile-time constant of the LoggingSeverity type, without the namespace
  21. // prefix.
  22. // RTC_LOG_V(sev) Like RTC_LOG(), but sev is a run-time variable of the
  23. // LoggingSeverity type (basically, it just doesn't prepend the namespace).
  24. // RTC_LOG_F(sev) Like RTC_LOG(), but includes the name of the current function.
  25. // RTC_LOG_T(sev) Like RTC_LOG(), but includes the this pointer.
  26. // RTC_LOG_T_F(sev) Like RTC_LOG_F(), but includes the this pointer.
  27. // RTC_LOG_GLE(sev [, mod]) attempt to add a string description of the
  28. // HRESULT returned by GetLastError.
  29. // RTC_LOG_ERRNO(sev) attempts to add a string description of an errno-derived
  30. // error. errno and associated facilities exist on both Windows and POSIX,
  31. // but on Windows they only apply to the C/C++ runtime.
  32. // RTC_LOG_ERR(sev) is an alias for the platform's normal error system, i.e.
  33. // _GLE on Windows and _ERRNO on POSIX.
  34. // (The above three also all have _EX versions that let you specify the error
  35. // code, rather than using the last one.)
  36. // RTC_LOG_E(sev, ctx, err, ...) logs a detailed error interpreted using the
  37. // specified context.
  38. // RTC_LOG_CHECK_LEVEL(sev) (and RTC_LOG_CHECK_LEVEL_V(sev)) can be used as a
  39. // test before performing expensive or sensitive operations whose sole
  40. // purpose is to output logging data at the desired level.
  41. #ifndef RTC_BASE_LOGGING_H_
  42. #define RTC_BASE_LOGGING_H_
  43. #include <errno.h>
  44. #include <atomic>
  45. #include <sstream> // no-presubmit-check TODO(webrtc:8982)
  46. #include <string>
  47. #include <utility>
  48. #include "absl/meta/type_traits.h"
  49. #include "absl/strings/string_view.h"
  50. #include "rtc_base/constructor_magic.h"
  51. #include "rtc_base/deprecation.h"
  52. #include "rtc_base/strings/string_builder.h"
  53. #include "rtc_base/system/inline.h"
  54. #if !defined(NDEBUG) || defined(DLOG_ALWAYS_ON)
  55. #define RTC_DLOG_IS_ON 1
  56. #else
  57. #define RTC_DLOG_IS_ON 0
  58. #endif
  59. #if defined(RTC_DISABLE_LOGGING)
  60. #define RTC_LOG_ENABLED() 0
  61. #else
  62. #define RTC_LOG_ENABLED() 1
  63. #endif
  64. namespace rtc {
  65. //////////////////////////////////////////////////////////////////////
  66. // Note that the non-standard LoggingSeverity aliases exist because they are
  67. // still in broad use. The meanings of the levels are:
  68. // LS_VERBOSE: This level is for data which we do not want to appear in the
  69. // normal debug log, but should appear in diagnostic logs.
  70. // LS_INFO: Chatty level used in debugging for all sorts of things, the default
  71. // in debug builds.
  72. // LS_WARNING: Something that may warrant investigation.
  73. // LS_ERROR: Something that should not have occurred.
  74. // LS_NONE: Don't log.
  75. enum LoggingSeverity {
  76. LS_VERBOSE,
  77. LS_INFO,
  78. LS_WARNING,
  79. LS_ERROR,
  80. LS_NONE,
  81. INFO = LS_INFO,
  82. WARNING = LS_WARNING,
  83. LERROR = LS_ERROR
  84. };
  85. // LogErrorContext assists in interpreting the meaning of an error value.
  86. enum LogErrorContext {
  87. ERRCTX_NONE,
  88. ERRCTX_ERRNO, // System-local errno
  89. ERRCTX_HRESULT, // Windows HRESULT
  90. // Abbreviations for LOG_E macro
  91. ERRCTX_EN = ERRCTX_ERRNO, // LOG_E(sev, EN, x)
  92. ERRCTX_HR = ERRCTX_HRESULT, // LOG_E(sev, HR, x)
  93. };
  94. class LogMessage;
  95. // Virtual sink interface that can receive log messages.
  96. class LogSink {
  97. public:
  98. LogSink() {}
  99. virtual ~LogSink() {}
  100. virtual void OnLogMessage(const std::string& msg,
  101. LoggingSeverity severity,
  102. const char* tag);
  103. virtual void OnLogMessage(const std::string& message,
  104. LoggingSeverity severity);
  105. virtual void OnLogMessage(const std::string& message) = 0;
  106. private:
  107. friend class ::rtc::LogMessage;
  108. #if RTC_LOG_ENABLED()
  109. // Members for LogMessage class to keep linked list of the registered sinks.
  110. LogSink* next_ = nullptr;
  111. LoggingSeverity min_severity_;
  112. #endif
  113. };
  114. namespace webrtc_logging_impl {
  115. class LogMetadata {
  116. public:
  117. LogMetadata(const char* file, int line, LoggingSeverity severity)
  118. : file_(file),
  119. line_and_sev_(static_cast<uint32_t>(line) << 3 | severity) {}
  120. LogMetadata() = default;
  121. const char* File() const { return file_; }
  122. int Line() const { return line_and_sev_ >> 3; }
  123. LoggingSeverity Severity() const {
  124. return static_cast<LoggingSeverity>(line_and_sev_ & 0x7);
  125. }
  126. private:
  127. const char* file_;
  128. // Line number and severity, the former in the most significant 29 bits, the
  129. // latter in the least significant 3 bits. (This is an optimization; since
  130. // both numbers are usually compile-time constants, this way we can load them
  131. // both with a single instruction.)
  132. uint32_t line_and_sev_;
  133. };
  134. static_assert(std::is_trivial<LogMetadata>::value, "");
  135. struct LogMetadataErr {
  136. LogMetadata meta;
  137. LogErrorContext err_ctx;
  138. int err;
  139. };
  140. #ifdef WEBRTC_ANDROID
  141. struct LogMetadataTag {
  142. LoggingSeverity severity;
  143. const char* tag;
  144. };
  145. #endif
  146. enum class LogArgType : int8_t {
  147. kEnd = 0,
  148. kInt,
  149. kLong,
  150. kLongLong,
  151. kUInt,
  152. kULong,
  153. kULongLong,
  154. kDouble,
  155. kLongDouble,
  156. kCharP,
  157. kStdString,
  158. kStringView,
  159. kVoidP,
  160. kLogMetadata,
  161. kLogMetadataErr,
  162. #ifdef WEBRTC_ANDROID
  163. kLogMetadataTag,
  164. #endif
  165. };
  166. // Wrapper for log arguments. Only ever make values of this type with the
  167. // MakeVal() functions.
  168. template <LogArgType N, typename T>
  169. struct Val {
  170. static constexpr LogArgType Type() { return N; }
  171. T GetVal() const { return val; }
  172. T val;
  173. };
  174. // Case for when we need to construct a temp string and then print that.
  175. // (We can't use Val<CheckArgType::kStdString, const std::string*>
  176. // because we need somewhere to store the temp string.)
  177. struct ToStringVal {
  178. static constexpr LogArgType Type() { return LogArgType::kStdString; }
  179. const std::string* GetVal() const { return &val; }
  180. std::string val;
  181. };
  182. inline Val<LogArgType::kInt, int> MakeVal(int x) {
  183. return {x};
  184. }
  185. inline Val<LogArgType::kLong, long> MakeVal(long x) {
  186. return {x};
  187. }
  188. inline Val<LogArgType::kLongLong, long long> MakeVal(long long x) {
  189. return {x};
  190. }
  191. inline Val<LogArgType::kUInt, unsigned int> MakeVal(unsigned int x) {
  192. return {x};
  193. }
  194. inline Val<LogArgType::kULong, unsigned long> MakeVal(unsigned long x) {
  195. return {x};
  196. }
  197. inline Val<LogArgType::kULongLong, unsigned long long> MakeVal(
  198. unsigned long long x) {
  199. return {x};
  200. }
  201. inline Val<LogArgType::kDouble, double> MakeVal(double x) {
  202. return {x};
  203. }
  204. inline Val<LogArgType::kLongDouble, long double> MakeVal(long double x) {
  205. return {x};
  206. }
  207. inline Val<LogArgType::kCharP, const char*> MakeVal(const char* x) {
  208. return {x};
  209. }
  210. inline Val<LogArgType::kStdString, const std::string*> MakeVal(
  211. const std::string& x) {
  212. return {&x};
  213. }
  214. inline Val<LogArgType::kStringView, const absl::string_view*> MakeVal(
  215. const absl::string_view& x) {
  216. return {&x};
  217. }
  218. inline Val<LogArgType::kVoidP, const void*> MakeVal(const void* x) {
  219. return {x};
  220. }
  221. inline Val<LogArgType::kLogMetadata, LogMetadata> MakeVal(
  222. const LogMetadata& x) {
  223. return {x};
  224. }
  225. inline Val<LogArgType::kLogMetadataErr, LogMetadataErr> MakeVal(
  226. const LogMetadataErr& x) {
  227. return {x};
  228. }
  229. // The enum class types are not implicitly convertible to arithmetic types.
  230. template <typename T,
  231. absl::enable_if_t<std::is_enum<T>::value &&
  232. !std::is_arithmetic<T>::value>* = nullptr>
  233. inline decltype(MakeVal(std::declval<absl::underlying_type_t<T>>())) MakeVal(
  234. T x) {
  235. return {static_cast<absl::underlying_type_t<T>>(x)};
  236. }
  237. #ifdef WEBRTC_ANDROID
  238. inline Val<LogArgType::kLogMetadataTag, LogMetadataTag> MakeVal(
  239. const LogMetadataTag& x) {
  240. return {x};
  241. }
  242. #endif
  243. template <typename T, class = void>
  244. struct has_to_log_string : std::false_type {};
  245. template <typename T>
  246. struct has_to_log_string<T, decltype(ToLogString(std::declval<T>()))>
  247. : std::true_type {};
  248. // Handle arbitrary types other than the above by falling back to stringstream.
  249. // TODO(bugs.webrtc.org/9278): Get rid of this overload when callers don't need
  250. // it anymore. No in-tree caller does, but some external callers still do.
  251. template <
  252. typename T,
  253. typename T1 = absl::decay_t<T>,
  254. absl::enable_if_t<std::is_class<T1>::value &&
  255. !std::is_same<T1, std::string>::value &&
  256. !std::is_same<T1, LogMetadata>::value &&
  257. !has_to_log_string<T1>::value &&
  258. #ifdef WEBRTC_ANDROID
  259. !std::is_same<T1, LogMetadataTag>::value &&
  260. #endif
  261. !std::is_same<T1, LogMetadataErr>::value>* = nullptr>
  262. ToStringVal MakeVal(const T& x) {
  263. std::ostringstream os; // no-presubmit-check TODO(webrtc:8982)
  264. os << x;
  265. return {os.str()};
  266. }
  267. template <typename T, absl::enable_if_t<has_to_log_string<T>::value>* = nullptr>
  268. ToStringVal MakeVal(const T& x) {
  269. return {ToLogString(x)};
  270. }
  271. #if RTC_LOG_ENABLED()
  272. void Log(const LogArgType* fmt, ...);
  273. #else
  274. inline void Log(const LogArgType* fmt, ...) {
  275. // Do nothing, shouldn't be invoked
  276. }
  277. #endif
  278. // Ephemeral type that represents the result of the logging << operator.
  279. template <typename... Ts>
  280. class LogStreamer;
  281. // Base case: Before the first << argument.
  282. template <>
  283. class LogStreamer<> final {
  284. public:
  285. template <typename U,
  286. typename V = decltype(MakeVal(std::declval<U>())),
  287. absl::enable_if_t<std::is_arithmetic<U>::value ||
  288. std::is_enum<U>::value>* = nullptr>
  289. RTC_FORCE_INLINE LogStreamer<V> operator<<(U arg) const {
  290. return LogStreamer<V>(MakeVal(arg), this);
  291. }
  292. template <typename U,
  293. typename V = decltype(MakeVal(std::declval<U>())),
  294. absl::enable_if_t<!std::is_arithmetic<U>::value &&
  295. !std::is_enum<U>::value>* = nullptr>
  296. RTC_FORCE_INLINE LogStreamer<V> operator<<(const U& arg) const {
  297. return LogStreamer<V>(MakeVal(arg), this);
  298. }
  299. template <typename... Us>
  300. RTC_FORCE_INLINE static void Call(const Us&... args) {
  301. static constexpr LogArgType t[] = {Us::Type()..., LogArgType::kEnd};
  302. Log(t, args.GetVal()...);
  303. }
  304. };
  305. // Inductive case: We've already seen at least one << argument. The most recent
  306. // one had type `T`, and the earlier ones had types `Ts`.
  307. template <typename T, typename... Ts>
  308. class LogStreamer<T, Ts...> final {
  309. public:
  310. RTC_FORCE_INLINE LogStreamer(T arg, const LogStreamer<Ts...>* prior)
  311. : arg_(arg), prior_(prior) {}
  312. template <typename U,
  313. typename V = decltype(MakeVal(std::declval<U>())),
  314. absl::enable_if_t<std::is_arithmetic<U>::value ||
  315. std::is_enum<U>::value>* = nullptr>
  316. RTC_FORCE_INLINE LogStreamer<V, T, Ts...> operator<<(U arg) const {
  317. return LogStreamer<V, T, Ts...>(MakeVal(arg), this);
  318. }
  319. template <typename U,
  320. typename V = decltype(MakeVal(std::declval<U>())),
  321. absl::enable_if_t<!std::is_arithmetic<U>::value &&
  322. !std::is_enum<U>::value>* = nullptr>
  323. RTC_FORCE_INLINE LogStreamer<V, T, Ts...> operator<<(const U& arg) const {
  324. return LogStreamer<V, T, Ts...>(MakeVal(arg), this);
  325. }
  326. template <typename... Us>
  327. RTC_FORCE_INLINE void Call(const Us&... args) const {
  328. prior_->Call(arg_, args...);
  329. }
  330. private:
  331. // The most recent argument.
  332. T arg_;
  333. // Earlier arguments.
  334. const LogStreamer<Ts...>* prior_;
  335. };
  336. class LogCall final {
  337. public:
  338. // This can be any binary operator with precedence lower than <<.
  339. // We return bool here to be able properly remove logging if
  340. // RTC_DISABLE_LOGGING is defined.
  341. template <typename... Ts>
  342. RTC_FORCE_INLINE bool operator&(const LogStreamer<Ts...>& streamer) {
  343. streamer.Call();
  344. return true;
  345. }
  346. };
  347. // This class is used to explicitly ignore values in the conditional
  348. // logging macros. This avoids compiler warnings like "value computed
  349. // is not used" and "statement has no effect".
  350. class LogMessageVoidify {
  351. public:
  352. LogMessageVoidify() = default;
  353. // This has to be an operator with a precedence lower than << but
  354. // higher than ?:
  355. template <typename... Ts>
  356. void operator&(LogStreamer<Ts...>&& streamer) {}
  357. };
  358. } // namespace webrtc_logging_impl
  359. // Direct use of this class is deprecated; please use the logging macros
  360. // instead.
  361. // TODO(bugs.webrtc.org/9278): Move this class to an unnamed namespace in the
  362. // .cc file.
  363. class LogMessage {
  364. public:
  365. // Same as the above, but using a compile-time constant for the logging
  366. // severity. This saves space at the call site, since passing an empty struct
  367. // is generally the same as not passing an argument at all.
  368. template <LoggingSeverity S>
  369. RTC_NO_INLINE LogMessage(const char* file,
  370. int line,
  371. std::integral_constant<LoggingSeverity, S>)
  372. : LogMessage(file, line, S) {}
  373. #if RTC_LOG_ENABLED()
  374. LogMessage(const char* file, int line, LoggingSeverity sev);
  375. LogMessage(const char* file,
  376. int line,
  377. LoggingSeverity sev,
  378. LogErrorContext err_ctx,
  379. int err);
  380. #if defined(WEBRTC_ANDROID)
  381. LogMessage(const char* file, int line, LoggingSeverity sev, const char* tag);
  382. #endif
  383. // DEPRECATED - DO NOT USE - PLEASE USE THE MACROS INSTEAD OF THE CLASS.
  384. // Android code should use the 'const char*' version since tags are static
  385. // and we want to avoid allocating a std::string copy per log line.
  386. RTC_DEPRECATED
  387. LogMessage(const char* file,
  388. int line,
  389. LoggingSeverity sev,
  390. const std::string& tag);
  391. ~LogMessage();
  392. void AddTag(const char* tag);
  393. rtc::StringBuilder& stream();
  394. // Returns the time at which this function was called for the first time.
  395. // The time will be used as the logging start time.
  396. // If this is not called externally, the LogMessage ctor also calls it, in
  397. // which case the logging start time will be the time of the first LogMessage
  398. // instance is created.
  399. static int64_t LogStartTime();
  400. // Returns the wall clock equivalent of |LogStartTime|, in seconds from the
  401. // epoch.
  402. static uint32_t WallClockStartTime();
  403. // LogThreads: Display the thread identifier of the current thread
  404. static void LogThreads(bool on = true);
  405. // LogTimestamps: Display the elapsed time of the program
  406. static void LogTimestamps(bool on = true);
  407. // These are the available logging channels
  408. // Debug: Debug console on Windows, otherwise stderr
  409. static void LogToDebug(LoggingSeverity min_sev);
  410. static LoggingSeverity GetLogToDebug();
  411. // Sets whether logs will be directed to stderr in debug mode.
  412. static void SetLogToStderr(bool log_to_stderr);
  413. // Stream: Any non-blocking stream interface.
  414. // Installs the |stream| to collect logs with severtiy |min_sev| or higher.
  415. // |stream| must live until deinstalled by RemoveLogToStream.
  416. // If |stream| is the first stream added to the system, we might miss some
  417. // early concurrent log statement happening from another thread happening near
  418. // this instant.
  419. static void AddLogToStream(LogSink* stream, LoggingSeverity min_sev);
  420. // Removes the specified stream, without destroying it. When the method
  421. // has completed, it's guaranteed that |stream| will receive no more logging
  422. // calls.
  423. static void RemoveLogToStream(LogSink* stream);
  424. // Returns the severity for the specified stream, of if none is specified,
  425. // the minimum stream severity.
  426. static int GetLogToStream(LogSink* stream = nullptr);
  427. // Testing against MinLogSeverity allows code to avoid potentially expensive
  428. // logging operations by pre-checking the logging level.
  429. static int GetMinLogSeverity();
  430. // Parses the provided parameter stream to configure the options above.
  431. // Useful for configuring logging from the command line.
  432. static void ConfigureLogging(const char* params);
  433. // Checks the current global debug severity and if the |streams_| collection
  434. // is empty. If |severity| is smaller than the global severity and if the
  435. // |streams_| collection is empty, the LogMessage will be considered a noop
  436. // LogMessage.
  437. static bool IsNoop(LoggingSeverity severity);
  438. // Version of IsNoop that uses fewer instructions at the call site, since the
  439. // caller doesn't have to pass an argument.
  440. template <LoggingSeverity S>
  441. RTC_NO_INLINE static bool IsNoop() {
  442. return IsNoop(S);
  443. }
  444. #else
  445. // Next methods do nothing; no one will call these functions.
  446. LogMessage(const char* file, int line, LoggingSeverity sev) {}
  447. LogMessage(const char* file,
  448. int line,
  449. LoggingSeverity sev,
  450. LogErrorContext err_ctx,
  451. int err) {}
  452. #if defined(WEBRTC_ANDROID)
  453. LogMessage(const char* file, int line, LoggingSeverity sev, const char* tag) {
  454. }
  455. #endif
  456. // DEPRECATED - DO NOT USE - PLEASE USE THE MACROS INSTEAD OF THE CLASS.
  457. // Android code should use the 'const char*' version since tags are static
  458. // and we want to avoid allocating a std::string copy per log line.
  459. RTC_DEPRECATED
  460. LogMessage(const char* file,
  461. int line,
  462. LoggingSeverity sev,
  463. const std::string& tag) {}
  464. ~LogMessage() = default;
  465. inline void AddTag(const char* tag) {}
  466. inline rtc::StringBuilder& stream() { return print_stream_; }
  467. inline static int64_t LogStartTime() { return 0; }
  468. inline static uint32_t WallClockStartTime() { return 0; }
  469. inline static void LogThreads(bool on = true) {}
  470. inline static void LogTimestamps(bool on = true) {}
  471. inline static void LogToDebug(LoggingSeverity min_sev) {}
  472. inline static LoggingSeverity GetLogToDebug() {
  473. return LoggingSeverity::LS_INFO;
  474. }
  475. inline static void SetLogToStderr(bool log_to_stderr) {}
  476. inline static void AddLogToStream(LogSink* stream, LoggingSeverity min_sev) {}
  477. inline static void RemoveLogToStream(LogSink* stream) {}
  478. inline static int GetLogToStream(LogSink* stream = nullptr) { return 0; }
  479. inline static int GetMinLogSeverity() { return 0; }
  480. inline static void ConfigureLogging(const char* params) {}
  481. static constexpr bool IsNoop(LoggingSeverity severity) { return true; }
  482. template <LoggingSeverity S>
  483. static constexpr bool IsNoop() {
  484. return IsNoop(S);
  485. }
  486. #endif // RTC_LOG_ENABLED()
  487. private:
  488. friend class LogMessageForTesting;
  489. #if RTC_LOG_ENABLED()
  490. // Updates min_sev_ appropriately when debug sinks change.
  491. static void UpdateMinLogSeverity();
  492. // These write out the actual log messages.
  493. #if defined(WEBRTC_ANDROID)
  494. static void OutputToDebug(const std::string& msg,
  495. LoggingSeverity severity,
  496. const char* tag);
  497. #else
  498. static void OutputToDebug(const std::string& msg, LoggingSeverity severity);
  499. #endif // defined(WEBRTC_ANDROID)
  500. // Called from the dtor (or from a test) to append optional extra error
  501. // information to the log stream and a newline character.
  502. void FinishPrintStream();
  503. // The severity level of this message
  504. LoggingSeverity severity_;
  505. #if defined(WEBRTC_ANDROID)
  506. // The default Android debug output tag.
  507. const char* tag_ = "libjingle";
  508. #endif
  509. // String data generated in the constructor, that should be appended to
  510. // the message before output.
  511. std::string extra_;
  512. // The output streams and their associated severities
  513. static LogSink* streams_;
  514. // Holds true with high probability if |streams_| is empty, false with high
  515. // probability otherwise. Operated on with std::memory_order_relaxed because
  516. // it's ok to lose or log some additional statements near the instant streams
  517. // are added/removed.
  518. static std::atomic<bool> streams_empty_;
  519. // Flags for formatting options
  520. static bool thread_, timestamp_;
  521. // Determines if logs will be directed to stderr in debug mode.
  522. static bool log_to_stderr_;
  523. #else // RTC_LOG_ENABLED()
  524. // Next methods do nothing; no one will call these functions.
  525. inline static void UpdateMinLogSeverity() {}
  526. #if defined(WEBRTC_ANDROID)
  527. inline static void OutputToDebug(const std::string& msg,
  528. LoggingSeverity severity,
  529. const char* tag) {}
  530. #else
  531. inline static void OutputToDebug(const std::string& msg,
  532. LoggingSeverity severity) {}
  533. #endif // defined(WEBRTC_ANDROID)
  534. inline void FinishPrintStream() {}
  535. #endif // RTC_LOG_ENABLED()
  536. // The stringbuilder that buffers the formatted message before output
  537. rtc::StringBuilder print_stream_;
  538. RTC_DISALLOW_COPY_AND_ASSIGN(LogMessage);
  539. };
  540. //////////////////////////////////////////////////////////////////////
  541. // Logging Helpers
  542. //////////////////////////////////////////////////////////////////////
  543. #define RTC_LOG_FILE_LINE(sev, file, line) \
  544. ::rtc::webrtc_logging_impl::LogCall() & \
  545. ::rtc::webrtc_logging_impl::LogStreamer<>() \
  546. << ::rtc::webrtc_logging_impl::LogMetadata(file, line, sev)
  547. #define RTC_LOG(sev) \
  548. !rtc::LogMessage::IsNoop<::rtc::sev>() && \
  549. RTC_LOG_FILE_LINE(::rtc::sev, __FILE__, __LINE__)
  550. // The _V version is for when a variable is passed in.
  551. #define RTC_LOG_V(sev) \
  552. !rtc::LogMessage::IsNoop(sev) && RTC_LOG_FILE_LINE(sev, __FILE__, __LINE__)
  553. // The _F version prefixes the message with the current function name.
  554. #if (defined(__GNUC__) && !defined(NDEBUG)) || defined(WANT_PRETTY_LOG_F)
  555. #define RTC_LOG_F(sev) RTC_LOG(sev) << __PRETTY_FUNCTION__ << ": "
  556. #define RTC_LOG_T_F(sev) \
  557. RTC_LOG(sev) << this << ": " << __PRETTY_FUNCTION__ << ": "
  558. #else
  559. #define RTC_LOG_F(sev) RTC_LOG(sev) << __FUNCTION__ << ": "
  560. #define RTC_LOG_T_F(sev) RTC_LOG(sev) << this << ": " << __FUNCTION__ << ": "
  561. #endif
  562. #define RTC_LOG_CHECK_LEVEL(sev) ::rtc::LogCheckLevel(::rtc::sev)
  563. #define RTC_LOG_CHECK_LEVEL_V(sev) ::rtc::LogCheckLevel(sev)
  564. inline bool LogCheckLevel(LoggingSeverity sev) {
  565. return (LogMessage::GetMinLogSeverity() <= sev);
  566. }
  567. #define RTC_LOG_E(sev, ctx, err) \
  568. !rtc::LogMessage::IsNoop<::rtc::sev>() && \
  569. ::rtc::webrtc_logging_impl::LogCall() & \
  570. ::rtc::webrtc_logging_impl::LogStreamer<>() \
  571. << ::rtc::webrtc_logging_impl::LogMetadataErr { \
  572. {__FILE__, __LINE__, ::rtc::sev}, ::rtc::ERRCTX_##ctx, (err) \
  573. }
  574. #define RTC_LOG_T(sev) RTC_LOG(sev) << this << ": "
  575. #define RTC_LOG_ERRNO_EX(sev, err) RTC_LOG_E(sev, ERRNO, err)
  576. #define RTC_LOG_ERRNO(sev) RTC_LOG_ERRNO_EX(sev, errno)
  577. #if defined(WEBRTC_WIN)
  578. #define RTC_LOG_GLE_EX(sev, err) RTC_LOG_E(sev, HRESULT, err)
  579. #define RTC_LOG_GLE(sev) RTC_LOG_GLE_EX(sev, static_cast<int>(GetLastError()))
  580. #define RTC_LOG_ERR_EX(sev, err) RTC_LOG_GLE_EX(sev, err)
  581. #define RTC_LOG_ERR(sev) RTC_LOG_GLE(sev)
  582. #elif defined(__native_client__) && __native_client__
  583. #define RTC_LOG_ERR_EX(sev, err) RTC_LOG(sev)
  584. #define RTC_LOG_ERR(sev) RTC_LOG(sev)
  585. #elif defined(WEBRTC_POSIX)
  586. #define RTC_LOG_ERR_EX(sev, err) RTC_LOG_ERRNO_EX(sev, err)
  587. #define RTC_LOG_ERR(sev) RTC_LOG_ERRNO(sev)
  588. #endif // WEBRTC_WIN
  589. #ifdef WEBRTC_ANDROID
  590. namespace webrtc_logging_impl {
  591. // TODO(kwiberg): Replace these with absl::string_view.
  592. inline const char* AdaptString(const char* str) {
  593. return str;
  594. }
  595. inline const char* AdaptString(const std::string& str) {
  596. return str.c_str();
  597. }
  598. } // namespace webrtc_logging_impl
  599. #define RTC_LOG_TAG(sev, tag) \
  600. !rtc::LogMessage::IsNoop(sev) && \
  601. ::rtc::webrtc_logging_impl::LogCall() & \
  602. ::rtc::webrtc_logging_impl::LogStreamer<>() \
  603. << ::rtc::webrtc_logging_impl::LogMetadataTag { \
  604. sev, ::rtc::webrtc_logging_impl::AdaptString(tag) \
  605. }
  606. #else
  607. // DEPRECATED. This macro is only intended for Android.
  608. #define RTC_LOG_TAG(sev, tag) RTC_LOG_V(sev)
  609. #endif
  610. // The RTC_DLOG macros are equivalent to their RTC_LOG counterparts except that
  611. // they only generate code in debug builds.
  612. #if RTC_DLOG_IS_ON
  613. #define RTC_DLOG(sev) RTC_LOG(sev)
  614. #define RTC_DLOG_V(sev) RTC_LOG_V(sev)
  615. #define RTC_DLOG_F(sev) RTC_LOG_F(sev)
  616. #else
  617. #define RTC_DLOG_EAT_STREAM_PARAMS() \
  618. while (false) \
  619. ::rtc::webrtc_logging_impl::LogMessageVoidify() & \
  620. (::rtc::webrtc_logging_impl::LogStreamer<>())
  621. #define RTC_DLOG(sev) RTC_DLOG_EAT_STREAM_PARAMS()
  622. #define RTC_DLOG_V(sev) RTC_DLOG_EAT_STREAM_PARAMS()
  623. #define RTC_DLOG_F(sev) RTC_DLOG_EAT_STREAM_PARAMS()
  624. #endif
  625. } // namespace rtc
  626. #endif // RTC_BASE_LOGGING_H_