blob: 362aeae73c0d9e53cc03f5aa864db9e92a486a3e [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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 Bonadei8ed8e562017-10-27 09:43:53 +020011// RTC_LOG(...) an ostream target that can be used to send formatted
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000012// output to a variety of logging targets, such as debugger console, stderr,
Tommi0eefb4d2015-05-23 09:54:07 +020013// or any LogSink.
Mirko Bonadei8ed8e562017-10-27 09:43:53 +020014// The severity level passed as the first argument to the logging
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000015// functions is used as a filter, to limit the verbosity of the logging.
Mirko Bonadei8ed8e562017-10-27 09:43:53 +020016// Static members of LogMessage documented below are used to control the
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000017// verbosity and target of the output.
Mirko Bonadei8ed8e562017-10-27 09:43:53 +020018// There are several variations on the RTC_LOG macro which facilitate logging
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000019// of common error conditions, detailed below.
20
Mirko Bonadei8ed8e562017-10-27 09:43:53 +020021// RTC_LOG(sev) logs the given stream at severity "sev", which must be a
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000022// compile-time constant of the LoggingSeverity type, without the namespace
23// prefix.
Mirko Bonadei8ed8e562017-10-27 09:43:53 +020024// 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.
Tommie51a0a82018-02-27 15:30:29 +010029// RTC_LOG_GLE(sev [, mod]) attempt to add a string description of the
30// HRESULT returned by GetLastError.
Mirko Bonadei8ed8e562017-10-27 09:43:53 +020031// RTC_LOG_ERRNO(sev) attempts to add a string description of an errno-derived
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000032// error. errno and associated facilities exist on both Windows and POSIX,
33// but on Windows they only apply to the C/C++ runtime.
Mirko Bonadei8ed8e562017-10-27 09:43:53 +020034// 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.orgf0488722014-05-13 18:00:26 +000036// (The above three also all have _EX versions that let you specify the error
37// code, rather than using the last one.)
Mirko Bonadei8ed8e562017-10-27 09:43:53 +020038// RTC_LOG_E(sev, ctx, err, ...) logs a detailed error interpreted using the
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000039// specified context.
Mirko Bonadei8ed8e562017-10-27 09:43:53 +020040// 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.orgf0488722014-05-13 18:00:26 +000043
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020044#ifndef RTC_BASE_LOGGING_H_
45#define RTC_BASE_LOGGING_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000046
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020047#include <errno.h>
mostynbe38e4f62016-05-12 01:08:20 -070048
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020049#include <list>
Jonas Olsson941a07c2018-09-13 10:07:07 +020050#include <sstream> // no-presubmit-check TODO(webrtc:8982)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020051#include <string>
52#include <utility>
53
Jonas Olssonf2ce37c2018-09-12 15:32:47 +020054#include "absl/strings/string_view.h"
Steve Anton10542f22019-01-11 09:11:00 -080055#include "rtc_base/constructor_magic.h"
Tommie51a0a82018-02-27 15:30:29 +010056#include "rtc_base/deprecation.h"
Jonas Olssond8c50782018-09-07 11:21:28 +020057#include "rtc_base/strings/string_builder.h"
Karl Wibergcefc4652018-05-23 23:20:38 +020058#include "rtc_base/system/inline.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020059
Mirko Bonadei99a70a22018-10-09 16:45:14 +020060#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS)
61#include "rtc_base/logging_mac.h"
62#endif // WEBRTC_MAC
63
Fredrik Solenbergb3d7cac2017-11-17 15:22:37 +010064#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 Kjellanderec78f1c2017-06-29 07:52:50 +020070namespace rtc {
71
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020072//////////////////////////////////////////////////////////////////////
73
74// Note that the non-standard LoggingSeverity aliases exist because they are
75// still in broad use. The meanings of the levels are:
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020076// LS_VERBOSE: This level is for data which we do not want to appear in the
77// normal debug log, but should appear in diagnostic logs.
78// LS_INFO: Chatty level used in debugging for all sorts of things, the default
79// in debug builds.
80// LS_WARNING: Something that may warrant investigation.
81// LS_ERROR: Something that should not have occurred.
82// LS_NONE: Don't log.
83enum LoggingSeverity {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020084 LS_VERBOSE,
85 LS_INFO,
86 LS_WARNING,
87 LS_ERROR,
88 LS_NONE,
89 INFO = LS_INFO,
90 WARNING = LS_WARNING,
91 LERROR = LS_ERROR
92};
93
94// LogErrorContext assists in interpreting the meaning of an error value.
95enum LogErrorContext {
96 ERRCTX_NONE,
97 ERRCTX_ERRNO, // System-local errno
98 ERRCTX_HRESULT, // Windows HRESULT
99 ERRCTX_OSSTATUS, // MacOS OSStatus
100
101 // Abbreviations for LOG_E macro
102 ERRCTX_EN = ERRCTX_ERRNO, // LOG_E(sev, EN, x)
103 ERRCTX_HR = ERRCTX_HRESULT, // LOG_E(sev, HR, x)
104 ERRCTX_OS = ERRCTX_OSSTATUS, // LOG_E(sev, OS, x)
105};
106
107// Virtual sink interface that can receive log messages.
108class LogSink {
109 public:
110 LogSink() {}
111 virtual ~LogSink() {}
Paulina Hensmanf1e3cb42018-06-20 14:07:05 +0200112 virtual void OnLogMessage(const std::string& msg,
113 LoggingSeverity severity,
114 const char* tag);
Jiawei Ou3ea18782018-10-31 23:14:24 -0700115 virtual void OnLogMessage(const std::string& message,
116 LoggingSeverity severity);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200117 virtual void OnLogMessage(const std::string& message) = 0;
118};
119
Karl Wibergcefc4652018-05-23 23:20:38 +0200120namespace webrtc_logging_impl {
121
122class LogMetadata {
123 public:
124 LogMetadata(const char* file, int line, LoggingSeverity severity)
125 : file_(file),
126 line_and_sev_(static_cast<uint32_t>(line) << 3 | severity) {}
127 LogMetadata() = default;
128
129 const char* File() const { return file_; }
130 int Line() const { return line_and_sev_ >> 3; }
131 LoggingSeverity Severity() const {
132 return static_cast<LoggingSeverity>(line_and_sev_ & 0x7);
133 }
134
135 private:
136 const char* file_;
137
138 // Line number and severity, the former in the most significant 29 bits, the
139 // latter in the least significant 3 bits. (This is an optimization; since
140 // both numbers are usually compile-time constants, this way we can load them
141 // both with a single instruction.)
142 uint32_t line_and_sev_;
143};
144static_assert(std::is_trivial<LogMetadata>::value, "");
145
146struct LogMetadataErr {
147 LogMetadata meta;
148 LogErrorContext err_ctx;
149 int err;
150};
151
152#ifdef WEBRTC_ANDROID
153struct LogMetadataTag {
154 LoggingSeverity severity;
155 const char* tag;
156};
157#endif
158
159enum class LogArgType : int8_t {
160 kEnd = 0,
161 kInt,
162 kLong,
163 kLongLong,
164 kUInt,
165 kULong,
166 kULongLong,
167 kDouble,
168 kLongDouble,
169 kCharP,
170 kStdString,
Jonas Olssonf2ce37c2018-09-12 15:32:47 +0200171 kStringView,
Karl Wibergcefc4652018-05-23 23:20:38 +0200172 kVoidP,
173 kLogMetadata,
174 kLogMetadataErr,
175#ifdef WEBRTC_ANDROID
176 kLogMetadataTag,
177#endif
178};
179
180// Wrapper for log arguments. Only ever make values of this type with the
181// MakeVal() functions.
182template <LogArgType N, typename T>
183struct Val {
184 static constexpr LogArgType Type() { return N; }
185 T GetVal() const { return val; }
186 T val;
187};
188
Sebastian Janssonb1138622019-04-11 16:48:15 +0200189// Case for when we need to construct a temp string and then print that.
190// (We can't use Val<CheckArgType::kStdString, const std::string*>
191// because we need somewhere to store the temp string.)
192struct ToStringVal {
Karl Wibergcefc4652018-05-23 23:20:38 +0200193 static constexpr LogArgType Type() { return LogArgType::kStdString; }
194 const std::string* GetVal() const { return &val; }
195 std::string val;
196};
197
198inline Val<LogArgType::kInt, int> MakeVal(int x) {
199 return {x};
200}
201inline Val<LogArgType::kLong, long> MakeVal(long x) {
202 return {x};
203}
204inline Val<LogArgType::kLongLong, long long> MakeVal(long long x) {
205 return {x};
206}
207inline Val<LogArgType::kUInt, unsigned int> MakeVal(unsigned int x) {
208 return {x};
209}
210inline Val<LogArgType::kULong, unsigned long> MakeVal(unsigned long x) {
211 return {x};
212}
213inline Val<LogArgType::kULongLong, unsigned long long> MakeVal(
214 unsigned long long x) {
215 return {x};
216}
217
218inline Val<LogArgType::kDouble, double> MakeVal(double x) {
219 return {x};
220}
221inline Val<LogArgType::kLongDouble, long double> MakeVal(long double x) {
222 return {x};
223}
224
225inline Val<LogArgType::kCharP, const char*> MakeVal(const char* x) {
226 return {x};
227}
228inline Val<LogArgType::kStdString, const std::string*> MakeVal(
229 const std::string& x) {
230 return {&x};
231}
Jonas Olssonf2ce37c2018-09-12 15:32:47 +0200232inline Val<LogArgType::kStringView, const absl::string_view*> MakeVal(
233 const absl::string_view& x) {
234 return {&x};
235}
Karl Wibergcefc4652018-05-23 23:20:38 +0200236
237inline Val<LogArgType::kVoidP, const void*> MakeVal(const void* x) {
238 return {x};
239}
240
241inline Val<LogArgType::kLogMetadata, LogMetadata> MakeVal(
242 const LogMetadata& x) {
243 return {x};
244}
245inline Val<LogArgType::kLogMetadataErr, LogMetadataErr> MakeVal(
246 const LogMetadataErr& x) {
247 return {x};
248}
249
Amit Hilbuch10c5a932019-03-18 13:09:18 -0700250// The enum class types are not implicitly convertible to arithmetic types.
251template <
252 typename T,
253 typename std::enable_if<std::is_enum<T>::value &&
254 !std::is_arithmetic<T>::value>::type* = nullptr>
255inline decltype(MakeVal(std::declval<typename std::underlying_type<T>::type>()))
256MakeVal(T x) {
257 return {static_cast<typename std::underlying_type<T>::type>(x)};
258}
259
Karl Wibergcefc4652018-05-23 23:20:38 +0200260#ifdef WEBRTC_ANDROID
261inline Val<LogArgType::kLogMetadataTag, LogMetadataTag> MakeVal(
262 const LogMetadataTag& x) {
263 return {x};
264}
265#endif
266
Sebastian Janssonb1138622019-04-11 16:48:15 +0200267template <typename T, typename = void>
268struct has_to_log_string : std::false_type {};
269template <typename T>
270struct has_to_log_string<
271 T,
272 typename std::enable_if<
273 std::is_same<std::string,
274 decltype(ToLogString(std::declval<T>()))>::value>::type>
275 : std::true_type {};
276
Karl Wibergcefc4652018-05-23 23:20:38 +0200277// Handle arbitrary types other than the above by falling back to stringstream.
278// TODO(bugs.webrtc.org/9278): Get rid of this overload when callers don't need
279// it anymore. No in-tree caller does, but some external callers still do.
280template <
281 typename T,
Sebastian Janssonb1138622019-04-11 16:48:15 +0200282 typename T1 = typename std::decay<T>::type,
Karl Wibergcefc4652018-05-23 23:20:38 +0200283 typename std::enable_if<
284 std::is_class<T1>::value && !std::is_same<T1, std::string>::value &&
285 !std::is_same<T1, LogMetadata>::value &&
Sebastian Janssonb1138622019-04-11 16:48:15 +0200286 !has_to_log_string<T1>::value &&
Karl Wibergcefc4652018-05-23 23:20:38 +0200287#ifdef WEBRTC_ANDROID
288 !std::is_same<T1, LogMetadataTag>::value &&
289#endif
290 !std::is_same<T1, LogMetadataErr>::value>::type* = nullptr>
Sebastian Janssonb1138622019-04-11 16:48:15 +0200291ToStringVal MakeVal(const T& x) {
Karl Wibergcefc4652018-05-23 23:20:38 +0200292 std::ostringstream os; // no-presubmit-check TODO(webrtc:8982)
293 os << x;
294 return {os.str()};
295}
296
Sebastian Janssonb1138622019-04-11 16:48:15 +0200297template <
298 typename T,
299 typename T1 = typename std::decay<T>::type,
300 typename std::enable_if<std::is_class<T1>::value &&
301 has_to_log_string<T1>::value>::type* = nullptr>
302ToStringVal MakeVal(const T& x) {
303 return {ToLogString(x)};
304}
305
Karl Wibergcefc4652018-05-23 23:20:38 +0200306void Log(const LogArgType* fmt, ...);
307
308// Ephemeral type that represents the result of the logging << operator.
309template <typename... Ts>
310class LogStreamer;
311
312// Base case: Before the first << argument.
313template <>
314class LogStreamer<> final {
315 public:
Amit Hilbuch10c5a932019-03-18 13:09:18 -0700316 template <typename U,
317 typename std::enable_if<std::is_arithmetic<U>::value ||
318 std::is_enum<U>::value>::type* = nullptr>
Karl Wibergcefc4652018-05-23 23:20:38 +0200319 RTC_FORCE_INLINE LogStreamer<decltype(MakeVal(std::declval<U>()))> operator<<(
320 U arg) const {
321 return LogStreamer<decltype(MakeVal(std::declval<U>()))>(MakeVal(arg),
322 this);
323 }
324
Amit Hilbuch10c5a932019-03-18 13:09:18 -0700325 template <typename U,
326 typename std::enable_if<!std::is_arithmetic<U>::value &&
327 !std::is_enum<U>::value>::type* = nullptr>
Karl Wibergcefc4652018-05-23 23:20:38 +0200328 RTC_FORCE_INLINE LogStreamer<decltype(MakeVal(std::declval<U>()))> operator<<(
329 const U& arg) const {
330 return LogStreamer<decltype(MakeVal(std::declval<U>()))>(MakeVal(arg),
331 this);
332 }
333
334 template <typename... Us>
335 RTC_FORCE_INLINE static void Call(const Us&... args) {
336 static constexpr LogArgType t[] = {Us::Type()..., LogArgType::kEnd};
337 Log(t, args.GetVal()...);
338 }
339};
340
341// Inductive case: We've already seen at least one << argument. The most recent
342// one had type `T`, and the earlier ones had types `Ts`.
343template <typename T, typename... Ts>
344class LogStreamer<T, Ts...> final {
345 public:
346 RTC_FORCE_INLINE LogStreamer(T arg, const LogStreamer<Ts...>* prior)
347 : arg_(arg), prior_(prior) {}
348
Amit Hilbuch10c5a932019-03-18 13:09:18 -0700349 template <typename U,
350 typename std::enable_if<std::is_arithmetic<U>::value ||
351 std::is_enum<U>::value>::type* = nullptr>
Karl Wibergcefc4652018-05-23 23:20:38 +0200352 RTC_FORCE_INLINE LogStreamer<decltype(MakeVal(std::declval<U>())), T, Ts...>
353 operator<<(U arg) const {
354 return LogStreamer<decltype(MakeVal(std::declval<U>())), T, Ts...>(
355 MakeVal(arg), this);
356 }
357
Amit Hilbuch10c5a932019-03-18 13:09:18 -0700358 template <typename U,
359 typename std::enable_if<!std::is_arithmetic<U>::value &&
360 !std::is_enum<U>::value>::type* = nullptr>
Karl Wibergcefc4652018-05-23 23:20:38 +0200361 RTC_FORCE_INLINE LogStreamer<decltype(MakeVal(std::declval<U>())), T, Ts...>
362 operator<<(const U& arg) const {
363 return LogStreamer<decltype(MakeVal(std::declval<U>())), T, Ts...>(
364 MakeVal(arg), this);
365 }
366
367 template <typename... Us>
368 RTC_FORCE_INLINE void Call(const Us&... args) const {
369 prior_->Call(arg_, args...);
370 }
371
372 private:
373 // The most recent argument.
374 T arg_;
375
376 // Earlier arguments.
377 const LogStreamer<Ts...>* prior_;
378};
379
380class LogCall final {
381 public:
382 // This can be any binary operator with precedence lower than <<.
383 template <typename... Ts>
384 RTC_FORCE_INLINE void operator&(const LogStreamer<Ts...>& streamer) {
385 streamer.Call();
386 }
387};
388
Karl Wibergcefc4652018-05-23 23:20:38 +0200389} // namespace webrtc_logging_impl
390
391// Direct use of this class is deprecated; please use the logging macros
392// instead.
393// TODO(bugs.webrtc.org/9278): Move this class to an unnamed namespace in the
394// .cc file.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200395class LogMessage {
396 public:
Karl Wibergab4f1c12018-05-04 10:42:28 +0200397 LogMessage(const char* file, int line, LoggingSeverity sev);
Karl Wiberg1ffb3742018-05-04 15:04:48 +0200398
399 // Same as the above, but using a compile-time constant for the logging
400 // severity. This saves space at the call site, since passing an empty struct
401 // is generally the same as not passing an argument at all.
402 template <LoggingSeverity S>
403 RTC_NO_INLINE LogMessage(const char* file,
404 int line,
405 std::integral_constant<LoggingSeverity, S>)
406 : LogMessage(file, line, S) {}
407
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200408 LogMessage(const char* file,
409 int line,
410 LoggingSeverity sev,
Karl Wibergab4f1c12018-05-04 10:42:28 +0200411 LogErrorContext err_ctx,
412 int err);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200413
Tommie51a0a82018-02-27 15:30:29 +0100414#if defined(WEBRTC_ANDROID)
415 LogMessage(const char* file, int line, LoggingSeverity sev, const char* tag);
416#endif
417
418 // DEPRECATED - DO NOT USE - PLEASE USE THE MACROS INSTEAD OF THE CLASS.
419 // Android code should use the 'const char*' version since tags are static
420 // and we want to avoid allocating a std::string copy per log line.
421 RTC_DEPRECATED
Yves Gerey665174f2018-06-19 15:03:05 +0200422 LogMessage(const char* file,
423 int line,
424 LoggingSeverity sev,
Philip Eliasson278aa422018-02-26 14:54:45 +0000425 const std::string& tag);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200426
427 ~LogMessage();
428
Karl Wibergcefc4652018-05-23 23:20:38 +0200429 void AddTag(const char* tag);
430
Jonas Olssond8c50782018-09-07 11:21:28 +0200431 rtc::StringBuilder& stream();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200432
433 // Returns the time at which this function was called for the first time.
434 // The time will be used as the logging start time.
435 // If this is not called externally, the LogMessage ctor also calls it, in
436 // which case the logging start time will be the time of the first LogMessage
437 // instance is created.
438 static int64_t LogStartTime();
439
440 // Returns the wall clock equivalent of |LogStartTime|, in seconds from the
441 // epoch.
442 static uint32_t WallClockStartTime();
443
444 // LogThreads: Display the thread identifier of the current thread
445 static void LogThreads(bool on = true);
446
447 // LogTimestamps: Display the elapsed time of the program
448 static void LogTimestamps(bool on = true);
449
450 // These are the available logging channels
451 // Debug: Debug console on Windows, otherwise stderr
452 static void LogToDebug(LoggingSeverity min_sev);
Jonas Olsson2b6f1352018-02-15 11:57:03 +0100453 static LoggingSeverity GetLogToDebug();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200454
455 // Sets whether logs will be directed to stderr in debug mode.
456 static void SetLogToStderr(bool log_to_stderr);
457
458 // Stream: Any non-blocking stream interface. LogMessage takes ownership of
459 // the stream. Multiple streams may be specified by using AddLogToStream.
460 // LogToStream is retained for backwards compatibility; when invoked, it
461 // will discard any previously set streams and install the specified stream.
462 // GetLogToStream gets the severity for the specified stream, of if none
463 // is specified, the minimum stream severity.
464 // RemoveLogToStream removes the specified stream, without destroying it.
465 static int GetLogToStream(LogSink* stream = nullptr);
466 static void AddLogToStream(LogSink* stream, LoggingSeverity min_sev);
467 static void RemoveLogToStream(LogSink* stream);
468
469 // Testing against MinLogSeverity allows code to avoid potentially expensive
470 // logging operations by pre-checking the logging level.
Jonas Olsson2b6f1352018-02-15 11:57:03 +0100471 static int GetMinLogSeverity();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200472
473 // Parses the provided parameter stream to configure the options above.
474 // Useful for configuring logging from the command line.
475 static void ConfigureLogging(const char* params);
476
Jonas Olssond8c50782018-09-07 11:21:28 +0200477 // Checks the current global debug severity and if the |streams_| collection
478 // is empty. If |severity| is smaller than the global severity and if the
479 // |streams_| collection is empty, the LogMessage will be considered a noop
480 // LogMessage.
481 static bool IsNoop(LoggingSeverity severity);
482
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200483 private:
Tommifef05002018-02-27 13:51:08 +0100484 friend class LogMessageForTesting;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200485 typedef std::pair<LogSink*, LoggingSeverity> StreamAndSeverity;
486 typedef std::list<StreamAndSeverity> StreamList;
487
488 // Updates min_sev_ appropriately when debug sinks change.
489 static void UpdateMinLogSeverity();
490
Yves Gerey665174f2018-06-19 15:03:05 +0200491// These write out the actual log messages.
Tommie51a0a82018-02-27 15:30:29 +0100492#if defined(WEBRTC_ANDROID)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200493 static void OutputToDebug(const std::string& msg,
494 LoggingSeverity severity,
Tommie51a0a82018-02-27 15:30:29 +0100495 const char* tag);
496#else
497 static void OutputToDebug(const std::string& msg, LoggingSeverity severity);
498#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200499
Tommifef05002018-02-27 13:51:08 +0100500 // Called from the dtor (or from a test) to append optional extra error
501 // information to the log stream and a newline character.
502 void FinishPrintStream();
503
Jonas Olssond8c50782018-09-07 11:21:28 +0200504 // The stringbuilder that buffers the formatted message before output
505 rtc::StringBuilder print_stream_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200506
507 // The severity level of this message
508 LoggingSeverity severity_;
509
Tommie51a0a82018-02-27 15:30:29 +0100510#if defined(WEBRTC_ANDROID)
Paulina Hensmanf1e3cb42018-06-20 14:07:05 +0200511 // The default Android debug output tag.
Tommie51a0a82018-02-27 15:30:29 +0100512 const char* tag_ = "libjingle";
513#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200514
515 // String data generated in the constructor, that should be appended to
516 // the message before output.
517 std::string extra_;
518
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200519 // The output streams and their associated severities
520 static StreamList streams_;
521
522 // Flags for formatting options
523 static bool thread_, timestamp_;
524
525 // Determines if logs will be directed to stderr in debug mode.
526 static bool log_to_stderr_;
527
528 RTC_DISALLOW_COPY_AND_ASSIGN(LogMessage);
529};
530
531//////////////////////////////////////////////////////////////////////
532// Logging Helpers
533//////////////////////////////////////////////////////////////////////
534
Jonas Olsson388e4e92018-11-16 15:57:49 +0100535#define RTC_LOG_FILE_LINE(sev, file, line) \
536 rtc::webrtc_logging_impl::LogCall() & \
537 rtc::webrtc_logging_impl::LogStreamer<>() \
538 << rtc::webrtc_logging_impl::LogMetadata(file, line, sev)
Jonas Olsson8a7916b2018-09-06 09:50:23 +0200539
540#define RTC_LOG(sev) RTC_LOG_FILE_LINE(rtc::sev, __FILE__, __LINE__)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200541
Karl Wibergcefc4652018-05-23 23:20:38 +0200542// The _V version is for when a variable is passed in.
Jonas Olssond8c50782018-09-07 11:21:28 +0200543#define RTC_LOG_V(sev) RTC_LOG_FILE_LINE(sev, __FILE__, __LINE__)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200544
545// The _F version prefixes the message with the current function name.
546#if (defined(__GNUC__) && !defined(NDEBUG)) || defined(WANT_PRETTY_LOG_F)
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200547#define RTC_LOG_F(sev) RTC_LOG(sev) << __PRETTY_FUNCTION__ << ": "
Yves Gerey665174f2018-06-19 15:03:05 +0200548#define RTC_LOG_T_F(sev) \
549 RTC_LOG(sev) << this << ": " << __PRETTY_FUNCTION__ << ": "
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200550#else
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200551#define RTC_LOG_F(sev) RTC_LOG(sev) << __FUNCTION__ << ": "
Patrik Höglundc2962552017-11-17 13:40:22 +0000552#define RTC_LOG_T_F(sev) RTC_LOG(sev) << this << ": " << __FUNCTION__ << ": "
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200553#endif
554
Yves Gerey665174f2018-06-19 15:03:05 +0200555#define RTC_LOG_CHECK_LEVEL(sev) rtc::LogCheckLevel(rtc::sev)
556#define RTC_LOG_CHECK_LEVEL_V(sev) rtc::LogCheckLevel(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200557
558inline bool LogCheckLevel(LoggingSeverity sev) {
559 return (LogMessage::GetMinLogSeverity() <= sev);
560}
561
Karl Wibergcefc4652018-05-23 23:20:38 +0200562#define RTC_LOG_E(sev, ctx, err) \
Karl Wibergcefc4652018-05-23 23:20:38 +0200563 rtc::webrtc_logging_impl::LogCall() & \
564 rtc::webrtc_logging_impl::LogStreamer<>() \
565 << rtc::webrtc_logging_impl::LogMetadataErr { \
566 {__FILE__, __LINE__, rtc::sev}, rtc::ERRCTX_##ctx, (err) \
567 }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200568
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200569#define RTC_LOG_T(sev) RTC_LOG(sev) << this << ": "
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200570
Yves Gerey665174f2018-06-19 15:03:05 +0200571#define RTC_LOG_ERRNO_EX(sev, err) RTC_LOG_E(sev, ERRNO, err)
572#define RTC_LOG_ERRNO(sev) RTC_LOG_ERRNO_EX(sev, errno)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200573
574#if defined(WEBRTC_WIN)
Yves Gerey665174f2018-06-19 15:03:05 +0200575#define RTC_LOG_GLE_EX(sev, err) RTC_LOG_E(sev, HRESULT, err)
Karl Wibergcefc4652018-05-23 23:20:38 +0200576#define RTC_LOG_GLE(sev) RTC_LOG_GLE_EX(sev, static_cast<int>(GetLastError()))
Yves Gerey665174f2018-06-19 15:03:05 +0200577#define RTC_LOG_ERR_EX(sev, err) RTC_LOG_GLE_EX(sev, err)
578#define RTC_LOG_ERR(sev) RTC_LOG_GLE(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200579#elif defined(__native_client__) && __native_client__
Yves Gerey665174f2018-06-19 15:03:05 +0200580#define RTC_LOG_ERR_EX(sev, err) RTC_LOG(sev)
581#define RTC_LOG_ERR(sev) RTC_LOG(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200582#elif defined(WEBRTC_POSIX)
Yves Gerey665174f2018-06-19 15:03:05 +0200583#define RTC_LOG_ERR_EX(sev, err) RTC_LOG_ERRNO_EX(sev, err)
584#define RTC_LOG_ERR(sev) RTC_LOG_ERRNO(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200585#endif // WEBRTC_WIN
586
Karl Wibergcefc4652018-05-23 23:20:38 +0200587#ifdef WEBRTC_ANDROID
588
589namespace webrtc_logging_impl {
590// TODO(kwiberg): Replace these with absl::string_view.
Yves Gerey665174f2018-06-19 15:03:05 +0200591inline const char* AdaptString(const char* str) {
592 return str;
593}
594inline const char* AdaptString(const std::string& str) {
595 return str.c_str();
596}
Karl Wibergcefc4652018-05-23 23:20:38 +0200597} // namespace webrtc_logging_impl
598
599#define RTC_LOG_TAG(sev, tag) \
Karl Wibergcefc4652018-05-23 23:20:38 +0200600 rtc::webrtc_logging_impl::LogCall() & \
601 rtc::webrtc_logging_impl::LogStreamer<>() \
602 << rtc::webrtc_logging_impl::LogMetadataTag { \
603 sev, rtc::webrtc_logging_impl::AdaptString(tag) \
604 }
605
Tommie51a0a82018-02-27 15:30:29 +0100606#else
Karl Wibergcefc4652018-05-23 23:20:38 +0200607
Tommie51a0a82018-02-27 15:30:29 +0100608// DEPRECATED. This macro is only intended for Android.
Karl Wibergcefc4652018-05-23 23:20:38 +0200609#define RTC_LOG_TAG(sev, tag) RTC_LOG_V(sev)
610
Tommie51a0a82018-02-27 15:30:29 +0100611#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200612
Fredrik Solenbergb3d7cac2017-11-17 15:22:37 +0100613// The RTC_DLOG macros are equivalent to their RTC_LOG counterparts except that
614// they only generate code in debug builds.
615#if RTC_DLOG_IS_ON
616#define RTC_DLOG(sev) RTC_LOG(sev)
617#define RTC_DLOG_V(sev) RTC_LOG_V(sev)
618#define RTC_DLOG_F(sev) RTC_LOG_F(sev)
619#else
Karl Wibergcefc4652018-05-23 23:20:38 +0200620#define RTC_DLOG_EAT_STREAM_PARAMS() \
621 while (false) \
622 rtc::webrtc_logging_impl::LogStreamer<>()
623#define RTC_DLOG(sev) RTC_DLOG_EAT_STREAM_PARAMS()
624#define RTC_DLOG_V(sev) RTC_DLOG_EAT_STREAM_PARAMS()
625#define RTC_DLOG_F(sev) RTC_DLOG_EAT_STREAM_PARAMS()
Fredrik Solenbergb3d7cac2017-11-17 15:22:37 +0100626#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200627
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200628} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000629
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200630#endif // RTC_BASE_LOGGING_H_