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