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