blob: 17c6c24bf726170ed2a3d6eb6b459dada13c95b5 [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
Sebastian Janssonf4e085a2019-05-20 18:34:07 +020054#include "absl/meta/type_traits.h"
Jonas Olssonf2ce37c2018-09-12 15:32:47 +020055#include "absl/strings/string_view.h"
Steve Anton10542f22019-01-11 09:11:00 -080056#include "rtc_base/constructor_magic.h"
Tommie51a0a82018-02-27 15:30:29 +010057#include "rtc_base/deprecation.h"
Jonas Olssond8c50782018-09-07 11:21:28 +020058#include "rtc_base/strings/string_builder.h"
Karl Wibergcefc4652018-05-23 23:20:38 +020059#include "rtc_base/system/inline.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020060
Fredrik Solenbergb3d7cac2017-11-17 15:22:37 +010061#if !defined(NDEBUG) || defined(DLOG_ALWAYS_ON)
62#define RTC_DLOG_IS_ON 1
63#else
64#define RTC_DLOG_IS_ON 0
65#endif
66
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020067namespace rtc {
68
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020069//////////////////////////////////////////////////////////////////////
70
71// Note that the non-standard LoggingSeverity aliases exist because they are
72// still in broad use. The meanings of the levels are:
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020073// LS_VERBOSE: This level is for data which we do not want to appear in the
74// normal debug log, but should appear in diagnostic logs.
75// LS_INFO: Chatty level used in debugging for all sorts of things, the default
76// in debug builds.
77// LS_WARNING: Something that may warrant investigation.
78// LS_ERROR: Something that should not have occurred.
79// LS_NONE: Don't log.
80enum LoggingSeverity {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020081 LS_VERBOSE,
82 LS_INFO,
83 LS_WARNING,
84 LS_ERROR,
85 LS_NONE,
86 INFO = LS_INFO,
87 WARNING = LS_WARNING,
88 LERROR = LS_ERROR
89};
90
91// LogErrorContext assists in interpreting the meaning of an error value.
92enum LogErrorContext {
93 ERRCTX_NONE,
Jonas Olssona4d87372019-07-05 19:08:33 +020094 ERRCTX_ERRNO, // System-local errno
95 ERRCTX_HRESULT, // Windows HRESULT
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020096
97 // Abbreviations for LOG_E macro
Jonas Olssona4d87372019-07-05 19:08:33 +020098 ERRCTX_EN = ERRCTX_ERRNO, // LOG_E(sev, EN, x)
99 ERRCTX_HR = ERRCTX_HRESULT, // LOG_E(sev, HR, x)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200100};
101
102// Virtual sink interface that can receive log messages.
103class LogSink {
104 public:
105 LogSink() {}
106 virtual ~LogSink() {}
Paulina Hensmanf1e3cb42018-06-20 14:07:05 +0200107 virtual void OnLogMessage(const std::string& msg,
108 LoggingSeverity severity,
109 const char* tag);
Jiawei Ou3ea18782018-10-31 23:14:24 -0700110 virtual void OnLogMessage(const std::string& message,
111 LoggingSeverity severity);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200112 virtual void OnLogMessage(const std::string& message) = 0;
113};
114
Karl Wibergcefc4652018-05-23 23:20:38 +0200115namespace webrtc_logging_impl {
116
117class LogMetadata {
118 public:
119 LogMetadata(const char* file, int line, LoggingSeverity severity)
120 : file_(file),
121 line_and_sev_(static_cast<uint32_t>(line) << 3 | severity) {}
122 LogMetadata() = default;
123
124 const char* File() const { return file_; }
125 int Line() const { return line_and_sev_ >> 3; }
126 LoggingSeverity Severity() const {
127 return static_cast<LoggingSeverity>(line_and_sev_ & 0x7);
128 }
129
130 private:
131 const char* file_;
132
133 // Line number and severity, the former in the most significant 29 bits, the
134 // latter in the least significant 3 bits. (This is an optimization; since
135 // both numbers are usually compile-time constants, this way we can load them
136 // both with a single instruction.)
137 uint32_t line_and_sev_;
138};
139static_assert(std::is_trivial<LogMetadata>::value, "");
140
141struct LogMetadataErr {
142 LogMetadata meta;
143 LogErrorContext err_ctx;
144 int err;
145};
146
147#ifdef WEBRTC_ANDROID
148struct LogMetadataTag {
149 LoggingSeverity severity;
150 const char* tag;
151};
152#endif
153
154enum class LogArgType : int8_t {
155 kEnd = 0,
156 kInt,
157 kLong,
158 kLongLong,
159 kUInt,
160 kULong,
161 kULongLong,
162 kDouble,
163 kLongDouble,
164 kCharP,
165 kStdString,
Jonas Olssonf2ce37c2018-09-12 15:32:47 +0200166 kStringView,
Karl Wibergcefc4652018-05-23 23:20:38 +0200167 kVoidP,
168 kLogMetadata,
169 kLogMetadataErr,
170#ifdef WEBRTC_ANDROID
171 kLogMetadataTag,
172#endif
173};
174
175// Wrapper for log arguments. Only ever make values of this type with the
176// MakeVal() functions.
177template <LogArgType N, typename T>
178struct Val {
179 static constexpr LogArgType Type() { return N; }
180 T GetVal() const { return val; }
181 T val;
182};
183
Sebastian Janssonb1138622019-04-11 16:48:15 +0200184// Case for when we need to construct a temp string and then print that.
185// (We can't use Val<CheckArgType::kStdString, const std::string*>
186// because we need somewhere to store the temp string.)
187struct ToStringVal {
Karl Wibergcefc4652018-05-23 23:20:38 +0200188 static constexpr LogArgType Type() { return LogArgType::kStdString; }
189 const std::string* GetVal() const { return &val; }
190 std::string val;
191};
192
193inline Val<LogArgType::kInt, int> MakeVal(int x) {
194 return {x};
195}
196inline Val<LogArgType::kLong, long> MakeVal(long x) {
197 return {x};
198}
199inline Val<LogArgType::kLongLong, long long> MakeVal(long long x) {
200 return {x};
201}
202inline Val<LogArgType::kUInt, unsigned int> MakeVal(unsigned int x) {
203 return {x};
204}
205inline Val<LogArgType::kULong, unsigned long> MakeVal(unsigned long x) {
206 return {x};
207}
208inline Val<LogArgType::kULongLong, unsigned long long> MakeVal(
209 unsigned long long x) {
210 return {x};
211}
212
213inline Val<LogArgType::kDouble, double> MakeVal(double x) {
214 return {x};
215}
216inline Val<LogArgType::kLongDouble, long double> MakeVal(long double x) {
217 return {x};
218}
219
220inline Val<LogArgType::kCharP, const char*> MakeVal(const char* x) {
221 return {x};
222}
223inline Val<LogArgType::kStdString, const std::string*> MakeVal(
224 const std::string& x) {
225 return {&x};
226}
Jonas Olssonf2ce37c2018-09-12 15:32:47 +0200227inline Val<LogArgType::kStringView, const absl::string_view*> MakeVal(
228 const absl::string_view& x) {
229 return {&x};
230}
Karl Wibergcefc4652018-05-23 23:20:38 +0200231
232inline Val<LogArgType::kVoidP, const void*> MakeVal(const void* x) {
233 return {x};
234}
235
236inline Val<LogArgType::kLogMetadata, LogMetadata> MakeVal(
237 const LogMetadata& x) {
238 return {x};
239}
240inline Val<LogArgType::kLogMetadataErr, LogMetadataErr> MakeVal(
241 const LogMetadataErr& x) {
242 return {x};
243}
244
Amit Hilbuch10c5a932019-03-18 13:09:18 -0700245// The enum class types are not implicitly convertible to arithmetic types.
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200246template <typename T,
247 absl::enable_if_t<std::is_enum<T>::value &&
248 !std::is_arithmetic<T>::value>* = nullptr>
249inline decltype(MakeVal(std::declval<absl::underlying_type_t<T>>())) MakeVal(
250 T x) {
251 return {static_cast<absl::underlying_type_t<T>>(x)};
Amit Hilbuch10c5a932019-03-18 13:09:18 -0700252}
253
Karl Wibergcefc4652018-05-23 23:20:38 +0200254#ifdef WEBRTC_ANDROID
255inline Val<LogArgType::kLogMetadataTag, LogMetadataTag> MakeVal(
256 const LogMetadataTag& x) {
257 return {x};
258}
259#endif
260
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200261template <typename T, class = void>
Sebastian Janssonb1138622019-04-11 16:48:15 +0200262struct has_to_log_string : std::false_type {};
263template <typename T>
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200264struct has_to_log_string<T, decltype(ToLogString(std::declval<T>()))>
Sebastian Janssonb1138622019-04-11 16:48:15 +0200265 : std::true_type {};
266
Karl Wibergcefc4652018-05-23 23:20:38 +0200267// Handle arbitrary types other than the above by falling back to stringstream.
268// TODO(bugs.webrtc.org/9278): Get rid of this overload when callers don't need
269// it anymore. No in-tree caller does, but some external callers still do.
270template <
271 typename T,
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200272 typename T1 = absl::decay_t<T>,
273 absl::enable_if_t<std::is_class<T1>::value &&
274 !std::is_same<T1, std::string>::value &&
275 !std::is_same<T1, LogMetadata>::value &&
276 !has_to_log_string<T1>::value &&
Karl Wibergcefc4652018-05-23 23:20:38 +0200277#ifdef WEBRTC_ANDROID
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200278 !std::is_same<T1, LogMetadataTag>::value &&
Karl Wibergcefc4652018-05-23 23:20:38 +0200279#endif
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200280 !std::is_same<T1, LogMetadataErr>::value>* = nullptr>
Sebastian Janssonb1138622019-04-11 16:48:15 +0200281ToStringVal MakeVal(const T& x) {
Karl Wibergcefc4652018-05-23 23:20:38 +0200282 std::ostringstream os; // no-presubmit-check TODO(webrtc:8982)
283 os << x;
284 return {os.str()};
285}
286
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200287template <typename T, absl::enable_if_t<has_to_log_string<T>::value>* = nullptr>
Sebastian Janssonb1138622019-04-11 16:48:15 +0200288ToStringVal MakeVal(const T& x) {
289 return {ToLogString(x)};
290}
291
Karl Wibergcefc4652018-05-23 23:20:38 +0200292void Log(const LogArgType* fmt, ...);
293
294// Ephemeral type that represents the result of the logging << operator.
295template <typename... Ts>
296class LogStreamer;
297
298// Base case: Before the first << argument.
299template <>
300class LogStreamer<> final {
301 public:
Amit Hilbuch10c5a932019-03-18 13:09:18 -0700302 template <typename U,
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200303 typename V = decltype(MakeVal(std::declval<U>())),
304 absl::enable_if_t<std::is_arithmetic<U>::value ||
305 std::is_enum<U>::value>* = nullptr>
306 RTC_FORCE_INLINE LogStreamer<V> operator<<(U arg) const {
307 return LogStreamer<V>(MakeVal(arg), this);
Karl Wibergcefc4652018-05-23 23:20:38 +0200308 }
309
Amit Hilbuch10c5a932019-03-18 13:09:18 -0700310 template <typename U,
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200311 typename V = decltype(MakeVal(std::declval<U>())),
312 absl::enable_if_t<!std::is_arithmetic<U>::value &&
313 !std::is_enum<U>::value>* = nullptr>
314 RTC_FORCE_INLINE LogStreamer<V> operator<<(const U& arg) const {
315 return LogStreamer<V>(MakeVal(arg), this);
Karl Wibergcefc4652018-05-23 23:20:38 +0200316 }
317
318 template <typename... Us>
319 RTC_FORCE_INLINE static void Call(const Us&... args) {
320 static constexpr LogArgType t[] = {Us::Type()..., LogArgType::kEnd};
321 Log(t, args.GetVal()...);
322 }
323};
324
325// Inductive case: We've already seen at least one << argument. The most recent
326// one had type `T`, and the earlier ones had types `Ts`.
327template <typename T, typename... Ts>
328class LogStreamer<T, Ts...> final {
329 public:
330 RTC_FORCE_INLINE LogStreamer(T arg, const LogStreamer<Ts...>* prior)
331 : arg_(arg), prior_(prior) {}
332
Amit Hilbuch10c5a932019-03-18 13:09:18 -0700333 template <typename U,
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200334 typename V = decltype(MakeVal(std::declval<U>())),
335 absl::enable_if_t<std::is_arithmetic<U>::value ||
336 std::is_enum<U>::value>* = nullptr>
337 RTC_FORCE_INLINE LogStreamer<V, T, Ts...> operator<<(U arg) const {
338 return LogStreamer<V, T, Ts...>(MakeVal(arg), this);
Karl Wibergcefc4652018-05-23 23:20:38 +0200339 }
340
Amit Hilbuch10c5a932019-03-18 13:09:18 -0700341 template <typename U,
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200342 typename V = decltype(MakeVal(std::declval<U>())),
343 absl::enable_if_t<!std::is_arithmetic<U>::value &&
344 !std::is_enum<U>::value>* = nullptr>
345 RTC_FORCE_INLINE LogStreamer<V, T, Ts...> operator<<(const U& arg) const {
346 return LogStreamer<V, T, Ts...>(MakeVal(arg), this);
Karl Wibergcefc4652018-05-23 23:20:38 +0200347 }
348
349 template <typename... Us>
350 RTC_FORCE_INLINE void Call(const Us&... args) const {
351 prior_->Call(arg_, args...);
352 }
353
354 private:
355 // The most recent argument.
356 T arg_;
357
358 // Earlier arguments.
359 const LogStreamer<Ts...>* prior_;
360};
361
362class LogCall final {
363 public:
364 // This can be any binary operator with precedence lower than <<.
365 template <typename... Ts>
366 RTC_FORCE_INLINE void operator&(const LogStreamer<Ts...>& streamer) {
367 streamer.Call();
368 }
369};
370
Karl Wibergcefc4652018-05-23 23:20:38 +0200371} // namespace webrtc_logging_impl
372
373// Direct use of this class is deprecated; please use the logging macros
374// instead.
375// TODO(bugs.webrtc.org/9278): Move this class to an unnamed namespace in the
376// .cc file.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200377class LogMessage {
378 public:
Karl Wibergab4f1c12018-05-04 10:42:28 +0200379 LogMessage(const char* file, int line, LoggingSeverity sev);
Karl Wiberg1ffb3742018-05-04 15:04:48 +0200380
381 // Same as the above, but using a compile-time constant for the logging
382 // severity. This saves space at the call site, since passing an empty struct
383 // is generally the same as not passing an argument at all.
384 template <LoggingSeverity S>
385 RTC_NO_INLINE LogMessage(const char* file,
386 int line,
387 std::integral_constant<LoggingSeverity, S>)
388 : LogMessage(file, line, S) {}
389
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200390 LogMessage(const char* file,
391 int line,
392 LoggingSeverity sev,
Karl Wibergab4f1c12018-05-04 10:42:28 +0200393 LogErrorContext err_ctx,
394 int err);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200395
Tommie51a0a82018-02-27 15:30:29 +0100396#if defined(WEBRTC_ANDROID)
397 LogMessage(const char* file, int line, LoggingSeverity sev, const char* tag);
398#endif
399
400 // DEPRECATED - DO NOT USE - PLEASE USE THE MACROS INSTEAD OF THE CLASS.
401 // Android code should use the 'const char*' version since tags are static
402 // and we want to avoid allocating a std::string copy per log line.
403 RTC_DEPRECATED
Yves Gerey665174f2018-06-19 15:03:05 +0200404 LogMessage(const char* file,
405 int line,
406 LoggingSeverity sev,
Philip Eliasson278aa422018-02-26 14:54:45 +0000407 const std::string& tag);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200408
409 ~LogMessage();
410
Karl Wibergcefc4652018-05-23 23:20:38 +0200411 void AddTag(const char* tag);
412
Jonas Olssond8c50782018-09-07 11:21:28 +0200413 rtc::StringBuilder& stream();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200414
415 // Returns the time at which this function was called for the first time.
416 // The time will be used as the logging start time.
417 // If this is not called externally, the LogMessage ctor also calls it, in
418 // which case the logging start time will be the time of the first LogMessage
419 // instance is created.
420 static int64_t LogStartTime();
421
422 // Returns the wall clock equivalent of |LogStartTime|, in seconds from the
423 // epoch.
424 static uint32_t WallClockStartTime();
425
426 // LogThreads: Display the thread identifier of the current thread
427 static void LogThreads(bool on = true);
428
429 // LogTimestamps: Display the elapsed time of the program
430 static void LogTimestamps(bool on = true);
431
432 // These are the available logging channels
433 // Debug: Debug console on Windows, otherwise stderr
434 static void LogToDebug(LoggingSeverity min_sev);
Jonas Olsson2b6f1352018-02-15 11:57:03 +0100435 static LoggingSeverity GetLogToDebug();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200436
437 // Sets whether logs will be directed to stderr in debug mode.
438 static void SetLogToStderr(bool log_to_stderr);
439
440 // Stream: Any non-blocking stream interface. LogMessage takes ownership of
441 // the stream. Multiple streams may be specified by using AddLogToStream.
442 // LogToStream is retained for backwards compatibility; when invoked, it
443 // will discard any previously set streams and install the specified stream.
444 // GetLogToStream gets the severity for the specified stream, of if none
445 // is specified, the minimum stream severity.
446 // RemoveLogToStream removes the specified stream, without destroying it.
447 static int GetLogToStream(LogSink* stream = nullptr);
448 static void AddLogToStream(LogSink* stream, LoggingSeverity min_sev);
449 static void RemoveLogToStream(LogSink* stream);
450
451 // Testing against MinLogSeverity allows code to avoid potentially expensive
452 // logging operations by pre-checking the logging level.
Jonas Olsson2b6f1352018-02-15 11:57:03 +0100453 static int GetMinLogSeverity();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200454
455 // Parses the provided parameter stream to configure the options above.
456 // Useful for configuring logging from the command line.
457 static void ConfigureLogging(const char* params);
458
Jonas Olssond8c50782018-09-07 11:21:28 +0200459 // Checks the current global debug severity and if the |streams_| collection
460 // is empty. If |severity| is smaller than the global severity and if the
461 // |streams_| collection is empty, the LogMessage will be considered a noop
462 // LogMessage.
463 static bool IsNoop(LoggingSeverity severity);
464
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200465 private:
Tommifef05002018-02-27 13:51:08 +0100466 friend class LogMessageForTesting;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200467 typedef std::pair<LogSink*, LoggingSeverity> StreamAndSeverity;
468 typedef std::list<StreamAndSeverity> StreamList;
469
470 // Updates min_sev_ appropriately when debug sinks change.
471 static void UpdateMinLogSeverity();
472
Yves Gerey665174f2018-06-19 15:03:05 +0200473// These write out the actual log messages.
Tommie51a0a82018-02-27 15:30:29 +0100474#if defined(WEBRTC_ANDROID)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200475 static void OutputToDebug(const std::string& msg,
476 LoggingSeverity severity,
Tommie51a0a82018-02-27 15:30:29 +0100477 const char* tag);
478#else
479 static void OutputToDebug(const std::string& msg, LoggingSeverity severity);
480#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200481
Tommifef05002018-02-27 13:51:08 +0100482 // Called from the dtor (or from a test) to append optional extra error
483 // information to the log stream and a newline character.
484 void FinishPrintStream();
485
Jonas Olssond8c50782018-09-07 11:21:28 +0200486 // The stringbuilder that buffers the formatted message before output
487 rtc::StringBuilder print_stream_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200488
489 // The severity level of this message
490 LoggingSeverity severity_;
491
Tommie51a0a82018-02-27 15:30:29 +0100492#if defined(WEBRTC_ANDROID)
Paulina Hensmanf1e3cb42018-06-20 14:07:05 +0200493 // The default Android debug output tag.
Tommie51a0a82018-02-27 15:30:29 +0100494 const char* tag_ = "libjingle";
495#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200496
497 // String data generated in the constructor, that should be appended to
498 // the message before output.
499 std::string extra_;
500
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200501 // The output streams and their associated severities
502 static StreamList streams_;
503
504 // Flags for formatting options
505 static bool thread_, timestamp_;
506
507 // Determines if logs will be directed to stderr in debug mode.
508 static bool log_to_stderr_;
509
510 RTC_DISALLOW_COPY_AND_ASSIGN(LogMessage);
511};
512
513//////////////////////////////////////////////////////////////////////
514// Logging Helpers
515//////////////////////////////////////////////////////////////////////
516
Jonas Olsson388e4e92018-11-16 15:57:49 +0100517#define RTC_LOG_FILE_LINE(sev, file, line) \
518 rtc::webrtc_logging_impl::LogCall() & \
519 rtc::webrtc_logging_impl::LogStreamer<>() \
520 << rtc::webrtc_logging_impl::LogMetadata(file, line, sev)
Jonas Olsson8a7916b2018-09-06 09:50:23 +0200521
522#define RTC_LOG(sev) RTC_LOG_FILE_LINE(rtc::sev, __FILE__, __LINE__)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200523
Karl Wibergcefc4652018-05-23 23:20:38 +0200524// The _V version is for when a variable is passed in.
Jonas Olssond8c50782018-09-07 11:21:28 +0200525#define RTC_LOG_V(sev) RTC_LOG_FILE_LINE(sev, __FILE__, __LINE__)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200526
527// The _F version prefixes the message with the current function name.
528#if (defined(__GNUC__) && !defined(NDEBUG)) || defined(WANT_PRETTY_LOG_F)
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200529#define RTC_LOG_F(sev) RTC_LOG(sev) << __PRETTY_FUNCTION__ << ": "
Yves Gerey665174f2018-06-19 15:03:05 +0200530#define RTC_LOG_T_F(sev) \
531 RTC_LOG(sev) << this << ": " << __PRETTY_FUNCTION__ << ": "
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200532#else
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200533#define RTC_LOG_F(sev) RTC_LOG(sev) << __FUNCTION__ << ": "
Patrik Höglundc2962552017-11-17 13:40:22 +0000534#define RTC_LOG_T_F(sev) RTC_LOG(sev) << this << ": " << __FUNCTION__ << ": "
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200535#endif
536
Yves Gerey665174f2018-06-19 15:03:05 +0200537#define RTC_LOG_CHECK_LEVEL(sev) rtc::LogCheckLevel(rtc::sev)
538#define RTC_LOG_CHECK_LEVEL_V(sev) rtc::LogCheckLevel(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200539
540inline bool LogCheckLevel(LoggingSeverity sev) {
541 return (LogMessage::GetMinLogSeverity() <= sev);
542}
543
Jonas Olssona4d87372019-07-05 19:08:33 +0200544#define RTC_LOG_E(sev, ctx, err) \
545 rtc::webrtc_logging_impl::LogCall() & \
546 rtc::webrtc_logging_impl::LogStreamer<>() \
547 << rtc::webrtc_logging_impl::LogMetadataErr { \
548 {__FILE__, __LINE__, rtc::sev}, rtc::ERRCTX_##ctx, (err) \
549 }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200550
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200551#define RTC_LOG_T(sev) RTC_LOG(sev) << this << ": "
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200552
Yves Gerey665174f2018-06-19 15:03:05 +0200553#define RTC_LOG_ERRNO_EX(sev, err) RTC_LOG_E(sev, ERRNO, err)
554#define RTC_LOG_ERRNO(sev) RTC_LOG_ERRNO_EX(sev, errno)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200555
556#if defined(WEBRTC_WIN)
Yves Gerey665174f2018-06-19 15:03:05 +0200557#define RTC_LOG_GLE_EX(sev, err) RTC_LOG_E(sev, HRESULT, err)
Karl Wibergcefc4652018-05-23 23:20:38 +0200558#define RTC_LOG_GLE(sev) RTC_LOG_GLE_EX(sev, static_cast<int>(GetLastError()))
Yves Gerey665174f2018-06-19 15:03:05 +0200559#define RTC_LOG_ERR_EX(sev, err) RTC_LOG_GLE_EX(sev, err)
560#define RTC_LOG_ERR(sev) RTC_LOG_GLE(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200561#elif defined(__native_client__) && __native_client__
Yves Gerey665174f2018-06-19 15:03:05 +0200562#define RTC_LOG_ERR_EX(sev, err) RTC_LOG(sev)
563#define RTC_LOG_ERR(sev) RTC_LOG(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200564#elif defined(WEBRTC_POSIX)
Yves Gerey665174f2018-06-19 15:03:05 +0200565#define RTC_LOG_ERR_EX(sev, err) RTC_LOG_ERRNO_EX(sev, err)
566#define RTC_LOG_ERR(sev) RTC_LOG_ERRNO(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200567#endif // WEBRTC_WIN
568
Karl Wibergcefc4652018-05-23 23:20:38 +0200569#ifdef WEBRTC_ANDROID
570
571namespace webrtc_logging_impl {
572// TODO(kwiberg): Replace these with absl::string_view.
Yves Gerey665174f2018-06-19 15:03:05 +0200573inline const char* AdaptString(const char* str) {
574 return str;
575}
576inline const char* AdaptString(const std::string& str) {
577 return str.c_str();
578}
Karl Wibergcefc4652018-05-23 23:20:38 +0200579} // namespace webrtc_logging_impl
580
Jonas Olssona4d87372019-07-05 19:08:33 +0200581#define RTC_LOG_TAG(sev, tag) \
582 rtc::webrtc_logging_impl::LogCall() & \
583 rtc::webrtc_logging_impl::LogStreamer<>() \
584 << rtc::webrtc_logging_impl::LogMetadataTag { \
585 sev, rtc::webrtc_logging_impl::AdaptString(tag) \
586 }
Karl Wibergcefc4652018-05-23 23:20:38 +0200587
Tommie51a0a82018-02-27 15:30:29 +0100588#else
Karl Wibergcefc4652018-05-23 23:20:38 +0200589
Tommie51a0a82018-02-27 15:30:29 +0100590// DEPRECATED. This macro is only intended for Android.
Karl Wibergcefc4652018-05-23 23:20:38 +0200591#define RTC_LOG_TAG(sev, tag) RTC_LOG_V(sev)
592
Tommie51a0a82018-02-27 15:30:29 +0100593#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200594
Fredrik Solenbergb3d7cac2017-11-17 15:22:37 +0100595// The RTC_DLOG macros are equivalent to their RTC_LOG counterparts except that
596// they only generate code in debug builds.
597#if RTC_DLOG_IS_ON
598#define RTC_DLOG(sev) RTC_LOG(sev)
599#define RTC_DLOG_V(sev) RTC_LOG_V(sev)
600#define RTC_DLOG_F(sev) RTC_LOG_F(sev)
601#else
Karl Wibergcefc4652018-05-23 23:20:38 +0200602#define RTC_DLOG_EAT_STREAM_PARAMS() \
603 while (false) \
604 rtc::webrtc_logging_impl::LogStreamer<>()
605#define RTC_DLOG(sev) RTC_DLOG_EAT_STREAM_PARAMS()
606#define RTC_DLOG_V(sev) RTC_DLOG_EAT_STREAM_PARAMS()
607#define RTC_DLOG_F(sev) RTC_DLOG_EAT_STREAM_PARAMS()
Fredrik Solenbergb3d7cac2017-11-17 15:22:37 +0100608#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200609
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200610} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000611
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200612#endif // RTC_BASE_LOGGING_H_