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