logging.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright 2015 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. // This file overrides the logging macros in WebRTC (webrtc/rtc_base/logging.h).
  5. // Instead of using WebRTC's logging implementation, the WebRTC macros are
  6. // mapped to DIAGNOSTIC_LOGGING. In its implementation (DiagnosticLogMessage in
  7. // third_party/webrtc_overrides/rtc_base/diagnostic_logging.h), the
  8. // corresponding base/logging.h macros (e.g. Chromium's VLOG) are used.
  9. // If this file is included outside of WebRTC/libjingle it should be included
  10. // after base/logging.h (if any) or compiler error or unexpected behavior may
  11. // occur (macros that have the same name in WebRTC as in Chromium will use
  12. // the WebRTC definition if this file is included first).
  13. // Setting the LoggingSeverity (and lower) that should be written to file should
  14. // be done via command line by specifying the flags:
  15. // --vmodule or --v please see base/logging.h for details on how to use them.
  16. // Specifying what file to write to is done using InitLogging also in
  17. // base/logging.h.
  18. // The macros and classes declared in here are not described as they are
  19. // NOT TO BE USED outside of WebRTC/libjingle.
  20. #ifndef THIRD_PARTY_WEBRTC_OVERRIDES_WEBRTC_RTC_BASE_LOGGING_H_
  21. #define THIRD_PARTY_WEBRTC_OVERRIDES_WEBRTC_RTC_BASE_LOGGING_H_
  22. #include "third_party/webrtc_overrides/rtc_base/diagnostic_logging.h"
  23. //////////////////////////////////////////////////////////////////////
  24. // WebRTC macros which in DiagnosticLogMessage are mapped over to
  25. // their VLOG equivalent in base/logging.h.
  26. //////////////////////////////////////////////////////////////////////
  27. #if defined(LOGGING_INSIDE_WEBRTC)
  28. #include <errno.h>
  29. namespace rtc {
  30. // Note that |N| is the size *with* the null terminator.
  31. bool CheckVlogIsOnHelper(LoggingSeverity severity, const char* file, size_t N);
  32. template <size_t N>
  33. bool CheckVlogIsOn(LoggingSeverity severity, const char (&file)[N]) {
  34. return CheckVlogIsOnHelper(severity, file, N);
  35. }
  36. } // namespace rtc
  37. // The LogMessageVoidify() call suppresses -Wunreachable-code diagnostics
  38. // in certain conditions (see https://crbug.com/1065568)
  39. // TODO(thakis): Make the warning smarter and then remove this again.
  40. #define DIAGNOSTIC_LOG(sev, ctx, err, ...) \
  41. rtc::LogMessageVoidify() & rtc::DiagnosticLogMessage(__FILE__, __LINE__, \
  42. sev, rtc::ERRCTX_##ctx, \
  43. err, ##__VA_ARGS__) \
  44. .stream()
  45. #define RTC_LOG_CHECK_LEVEL(sev) CheckVlogIsOn(rtc::sev, __FILE__)
  46. #define RTC_LOG_CHECK_LEVEL_V(sev) CheckVlogIsOn(sev, __FILE__)
  47. #define RTC_LOG_V(sev) DIAGNOSTIC_LOG(sev, NONE, 0)
  48. #undef RTC_LOG
  49. #define RTC_LOG(sev) DIAGNOSTIC_LOG(rtc::sev, NONE, 0)
  50. // The _F version prefixes the message with the current function name.
  51. #if defined(__GNUC__) && defined(_DEBUG)
  52. #define RTC_LOG_F(sev) RTC_LOG(sev) << __PRETTY_FUNCTION__ << ": "
  53. #else
  54. #define RTC_LOG_F(sev) RTC_LOG(sev) << __FUNCTION__ << ": "
  55. #endif
  56. #define RTC_LOG_E(sev, ctx, err, ...) \
  57. DIAGNOSTIC_LOG(rtc::sev, ctx, err, ##__VA_ARGS__)
  58. #undef RTC_LOG_ERRNO_EX
  59. #define RTC_LOG_ERRNO_EX(sev, err) RTC_LOG_E(sev, ERRNO, err)
  60. #undef RTC_LOG_ERRNO
  61. #define RTC_LOG_ERRNO(sev) RTC_LOG_ERRNO_EX(sev, errno)
  62. #if defined(WEBRTC_WIN)
  63. #define RTC_LOG_GLE_EX(sev, err) RTC_LOG_E(sev, HRESULT, err)
  64. #define RTC_LOG_GLE(sev) RTC_LOG_GLE_EX(sev, GetLastError())
  65. #define RTC_LOG_GLEM(sev, mod) RTC_LOG_E(sev, HRESULT, GetLastError(), mod)
  66. #define RTC_LOG_ERR_EX(sev, err) RTC_LOG_GLE_EX(sev, err)
  67. #define RTC_LOG_ERR(sev) RTC_LOG_GLE(sev)
  68. #define RTC_LAST_SYSTEM_ERROR (::GetLastError())
  69. #else
  70. #define RTC_LOG_ERR_EX(sev, err) RTC_LOG_ERRNO_EX(sev, err)
  71. #define RTC_LOG_ERR(sev) RTC_LOG_ERRNO(sev)
  72. #define RTC_LAST_SYSTEM_ERROR (errno)
  73. #endif // OS_WIN
  74. #undef RTC_PLOG
  75. #define RTC_PLOG(sev, err) RTC_LOG_ERR_EX(sev, err)
  76. // The RTC_DLOG macros are equivalent to their RTC_LOG counterparts except that
  77. // they only generate code in debug builds.
  78. #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
  79. #define RTC_DLOG_IS_ON 0
  80. #else
  81. #define RTC_DLOG_IS_ON 1
  82. #endif
  83. #if RTC_DLOG_IS_ON
  84. #define RTC_DLOG(sev) RTC_LOG(sev)
  85. #define RTC_DLOG_V(sev) RTC_LOG_V(sev)
  86. #define RTC_DLOG_F(sev) RTC_LOG_F(sev)
  87. #else
  88. #define RTC_DLOG_EAT_STREAM_PARAMS(sev) \
  89. (true ? true : ((void)(rtc::sev), true)) \
  90. ? static_cast<void>(0) \
  91. : rtc::LogMessageVoidify() & \
  92. rtc::DiagnosticLogMessage(__FILE__, __LINE__, rtc::sev, \
  93. rtc::ERRCTX_NONE, 0).stream()
  94. #define RTC_DLOG(sev) RTC_DLOG_EAT_STREAM_PARAMS(sev)
  95. #define RTC_DLOG_V(sev) RTC_DLOG_EAT_STREAM_PARAMS(sev)
  96. #define RTC_DLOG_F(sev) RTC_DLOG_EAT_STREAM_PARAMS(sev)
  97. #endif
  98. #endif // LOGGING_INSIDE_WEBRTC
  99. #endif // THIRD_PARTY_WEBRTC_OVERRIDES_WEBRTC_RTC_BASE_LOGGING_H_