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