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