You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

615 lines
20 KiB

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