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