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