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