logging.h 24 KB

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