blob: 1ca6ea8ade9fcdb9a7b5b10d330b554f6e0de2ee [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
Jonas Olsson941a07c2018-09-13 10:07:07 +020049#include <sstream> // no-presubmit-check TODO(webrtc:8982)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020050#include <string>
51#include <utility>
52
Sebastian Janssonf4e085a2019-05-20 18:34:07 +020053#include "absl/meta/type_traits.h"
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
Fredrik Solenbergb3d7cac2017-11-17 15:22:37 +010060#if !defined(NDEBUG) || defined(DLOG_ALWAYS_ON)
61#define RTC_DLOG_IS_ON 1
62#else
63#define RTC_DLOG_IS_ON 0
64#endif
65
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020066namespace rtc {
67
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020068//////////////////////////////////////////////////////////////////////
69
70// Note that the non-standard LoggingSeverity aliases exist because they are
71// still in broad use. The meanings of the levels are:
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020072// LS_VERBOSE: This level is for data which we do not want to appear in the
73// normal debug log, but should appear in diagnostic logs.
74// LS_INFO: Chatty level used in debugging for all sorts of things, the default
75// in debug builds.
76// LS_WARNING: Something that may warrant investigation.
77// LS_ERROR: Something that should not have occurred.
78// LS_NONE: Don't log.
79enum LoggingSeverity {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020080 LS_VERBOSE,
81 LS_INFO,
82 LS_WARNING,
83 LS_ERROR,
84 LS_NONE,
85 INFO = LS_INFO,
86 WARNING = LS_WARNING,
87 LERROR = LS_ERROR
88};
89
90// LogErrorContext assists in interpreting the meaning of an error value.
91enum LogErrorContext {
92 ERRCTX_NONE,
Jonas Olssona4d87372019-07-05 19:08:33 +020093 ERRCTX_ERRNO, // System-local errno
94 ERRCTX_HRESULT, // Windows HRESULT
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020095
96 // Abbreviations for LOG_E macro
Jonas Olssona4d87372019-07-05 19:08:33 +020097 ERRCTX_EN = ERRCTX_ERRNO, // LOG_E(sev, EN, x)
98 ERRCTX_HR = ERRCTX_HRESULT, // LOG_E(sev, HR, x)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020099};
100
Danil Chapovalovb9f69022019-10-21 09:19:10 +0200101class LogMessage;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200102// 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;
Danil Chapovalovb9f69022019-10-21 09:19:10 +0200113
114 private:
115 friend class ::rtc::LogMessage;
116 // Members for LogMessage class to keep linked list of the registered sinks.
117 LogSink* next_ = nullptr;
118 LoggingSeverity min_severity_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200119};
120
Karl Wibergcefc4652018-05-23 23:20:38 +0200121namespace webrtc_logging_impl {
122
123class LogMetadata {
124 public:
125 LogMetadata(const char* file, int line, LoggingSeverity severity)
126 : file_(file),
127 line_and_sev_(static_cast<uint32_t>(line) << 3 | severity) {}
128 LogMetadata() = default;
129
130 const char* File() const { return file_; }
131 int Line() const { return line_and_sev_ >> 3; }
132 LoggingSeverity Severity() const {
133 return static_cast<LoggingSeverity>(line_and_sev_ & 0x7);
134 }
135
136 private:
137 const char* file_;
138
139 // Line number and severity, the former in the most significant 29 bits, the
140 // latter in the least significant 3 bits. (This is an optimization; since
141 // both numbers are usually compile-time constants, this way we can load them
142 // both with a single instruction.)
143 uint32_t line_and_sev_;
144};
145static_assert(std::is_trivial<LogMetadata>::value, "");
146
147struct LogMetadataErr {
148 LogMetadata meta;
149 LogErrorContext err_ctx;
150 int err;
151};
152
153#ifdef WEBRTC_ANDROID
154struct LogMetadataTag {
155 LoggingSeverity severity;
156 const char* tag;
157};
158#endif
159
160enum class LogArgType : int8_t {
161 kEnd = 0,
162 kInt,
163 kLong,
164 kLongLong,
165 kUInt,
166 kULong,
167 kULongLong,
168 kDouble,
169 kLongDouble,
170 kCharP,
171 kStdString,
Jonas Olssonf2ce37c2018-09-12 15:32:47 +0200172 kStringView,
Karl Wibergcefc4652018-05-23 23:20:38 +0200173 kVoidP,
174 kLogMetadata,
175 kLogMetadataErr,
176#ifdef WEBRTC_ANDROID
177 kLogMetadataTag,
178#endif
179};
180
181// Wrapper for log arguments. Only ever make values of this type with the
182// MakeVal() functions.
183template <LogArgType N, typename T>
184struct Val {
185 static constexpr LogArgType Type() { return N; }
186 T GetVal() const { return val; }
187 T val;
188};
189
Sebastian Janssonb1138622019-04-11 16:48:15 +0200190// Case for when we need to construct a temp string and then print that.
191// (We can't use Val<CheckArgType::kStdString, const std::string*>
192// because we need somewhere to store the temp string.)
193struct ToStringVal {
Karl Wibergcefc4652018-05-23 23:20:38 +0200194 static constexpr LogArgType Type() { return LogArgType::kStdString; }
195 const std::string* GetVal() const { return &val; }
196 std::string val;
197};
198
199inline Val<LogArgType::kInt, int> MakeVal(int x) {
200 return {x};
201}
202inline Val<LogArgType::kLong, long> MakeVal(long x) {
203 return {x};
204}
205inline Val<LogArgType::kLongLong, long long> MakeVal(long long x) {
206 return {x};
207}
208inline Val<LogArgType::kUInt, unsigned int> MakeVal(unsigned int x) {
209 return {x};
210}
211inline Val<LogArgType::kULong, unsigned long> MakeVal(unsigned long x) {
212 return {x};
213}
214inline Val<LogArgType::kULongLong, unsigned long long> MakeVal(
215 unsigned long long x) {
216 return {x};
217}
218
219inline Val<LogArgType::kDouble, double> MakeVal(double x) {
220 return {x};
221}
222inline Val<LogArgType::kLongDouble, long double> MakeVal(long double x) {
223 return {x};
224}
225
226inline Val<LogArgType::kCharP, const char*> MakeVal(const char* x) {
227 return {x};
228}
229inline Val<LogArgType::kStdString, const std::string*> MakeVal(
230 const std::string& x) {
231 return {&x};
232}
Jonas Olssonf2ce37c2018-09-12 15:32:47 +0200233inline Val<LogArgType::kStringView, const absl::string_view*> MakeVal(
234 const absl::string_view& x) {
235 return {&x};
236}
Karl Wibergcefc4652018-05-23 23:20:38 +0200237
238inline Val<LogArgType::kVoidP, const void*> MakeVal(const void* x) {
239 return {x};
240}
241
242inline Val<LogArgType::kLogMetadata, LogMetadata> MakeVal(
243 const LogMetadata& x) {
244 return {x};
245}
246inline Val<LogArgType::kLogMetadataErr, LogMetadataErr> MakeVal(
247 const LogMetadataErr& x) {
248 return {x};
249}
250
Amit Hilbuch10c5a932019-03-18 13:09:18 -0700251// The enum class types are not implicitly convertible to arithmetic types.
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200252template <typename T,
253 absl::enable_if_t<std::is_enum<T>::value &&
254 !std::is_arithmetic<T>::value>* = nullptr>
255inline decltype(MakeVal(std::declval<absl::underlying_type_t<T>>())) MakeVal(
256 T x) {
257 return {static_cast<absl::underlying_type_t<T>>(x)};
Amit Hilbuch10c5a932019-03-18 13:09:18 -0700258}
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 Janssonf4e085a2019-05-20 18:34:07 +0200267template <typename T, class = void>
Sebastian Janssonb1138622019-04-11 16:48:15 +0200268struct has_to_log_string : std::false_type {};
269template <typename T>
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200270struct has_to_log_string<T, decltype(ToLogString(std::declval<T>()))>
Sebastian Janssonb1138622019-04-11 16:48:15 +0200271 : std::true_type {};
272
Karl Wibergcefc4652018-05-23 23:20:38 +0200273// Handle arbitrary types other than the above by falling back to stringstream.
274// TODO(bugs.webrtc.org/9278): Get rid of this overload when callers don't need
275// it anymore. No in-tree caller does, but some external callers still do.
276template <
277 typename T,
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200278 typename T1 = absl::decay_t<T>,
279 absl::enable_if_t<std::is_class<T1>::value &&
280 !std::is_same<T1, std::string>::value &&
281 !std::is_same<T1, LogMetadata>::value &&
282 !has_to_log_string<T1>::value &&
Karl Wibergcefc4652018-05-23 23:20:38 +0200283#ifdef WEBRTC_ANDROID
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200284 !std::is_same<T1, LogMetadataTag>::value &&
Karl Wibergcefc4652018-05-23 23:20:38 +0200285#endif
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200286 !std::is_same<T1, LogMetadataErr>::value>* = nullptr>
Sebastian Janssonb1138622019-04-11 16:48:15 +0200287ToStringVal MakeVal(const T& x) {
Karl Wibergcefc4652018-05-23 23:20:38 +0200288 std::ostringstream os; // no-presubmit-check TODO(webrtc:8982)
289 os << x;
290 return {os.str()};
291}
292
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200293template <typename T, absl::enable_if_t<has_to_log_string<T>::value>* = nullptr>
Sebastian Janssonb1138622019-04-11 16:48:15 +0200294ToStringVal MakeVal(const T& x) {
295 return {ToLogString(x)};
296}
297
Karl Wibergcefc4652018-05-23 23:20:38 +0200298void Log(const LogArgType* fmt, ...);
299
300// Ephemeral type that represents the result of the logging << operator.
301template <typename... Ts>
302class LogStreamer;
303
304// Base case: Before the first << argument.
305template <>
306class LogStreamer<> final {
307 public:
Amit Hilbuch10c5a932019-03-18 13:09:18 -0700308 template <typename U,
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200309 typename V = decltype(MakeVal(std::declval<U>())),
310 absl::enable_if_t<std::is_arithmetic<U>::value ||
311 std::is_enum<U>::value>* = nullptr>
312 RTC_FORCE_INLINE LogStreamer<V> operator<<(U arg) const {
313 return LogStreamer<V>(MakeVal(arg), this);
Karl Wibergcefc4652018-05-23 23:20:38 +0200314 }
315
Amit Hilbuch10c5a932019-03-18 13:09:18 -0700316 template <typename U,
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200317 typename V = decltype(MakeVal(std::declval<U>())),
318 absl::enable_if_t<!std::is_arithmetic<U>::value &&
319 !std::is_enum<U>::value>* = nullptr>
320 RTC_FORCE_INLINE LogStreamer<V> operator<<(const U& arg) const {
321 return LogStreamer<V>(MakeVal(arg), this);
Karl Wibergcefc4652018-05-23 23:20:38 +0200322 }
323
324 template <typename... Us>
325 RTC_FORCE_INLINE static void Call(const Us&... args) {
326 static constexpr LogArgType t[] = {Us::Type()..., LogArgType::kEnd};
327 Log(t, args.GetVal()...);
328 }
329};
330
331// Inductive case: We've already seen at least one << argument. The most recent
332// one had type `T`, and the earlier ones had types `Ts`.
333template <typename T, typename... Ts>
334class LogStreamer<T, Ts...> final {
335 public:
336 RTC_FORCE_INLINE LogStreamer(T arg, const LogStreamer<Ts...>* prior)
337 : arg_(arg), prior_(prior) {}
338
Amit Hilbuch10c5a932019-03-18 13:09:18 -0700339 template <typename U,
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200340 typename V = decltype(MakeVal(std::declval<U>())),
341 absl::enable_if_t<std::is_arithmetic<U>::value ||
342 std::is_enum<U>::value>* = nullptr>
343 RTC_FORCE_INLINE LogStreamer<V, T, Ts...> operator<<(U arg) const {
344 return LogStreamer<V, T, Ts...>(MakeVal(arg), this);
Karl Wibergcefc4652018-05-23 23:20:38 +0200345 }
346
Amit Hilbuch10c5a932019-03-18 13:09:18 -0700347 template <typename U,
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200348 typename V = decltype(MakeVal(std::declval<U>())),
349 absl::enable_if_t<!std::is_arithmetic<U>::value &&
350 !std::is_enum<U>::value>* = nullptr>
351 RTC_FORCE_INLINE LogStreamer<V, T, Ts...> operator<<(const U& arg) const {
352 return LogStreamer<V, T, Ts...>(MakeVal(arg), this);
Karl Wibergcefc4652018-05-23 23:20:38 +0200353 }
354
355 template <typename... Us>
356 RTC_FORCE_INLINE void Call(const Us&... args) const {
357 prior_->Call(arg_, args...);
358 }
359
360 private:
361 // The most recent argument.
362 T arg_;
363
364 // Earlier arguments.
365 const LogStreamer<Ts...>* prior_;
366};
367
368class LogCall final {
369 public:
370 // This can be any binary operator with precedence lower than <<.
371 template <typename... Ts>
372 RTC_FORCE_INLINE void operator&(const LogStreamer<Ts...>& streamer) {
373 streamer.Call();
374 }
375};
376
Karl Wibergcefc4652018-05-23 23:20:38 +0200377} // namespace webrtc_logging_impl
378
379// Direct use of this class is deprecated; please use the logging macros
380// instead.
381// TODO(bugs.webrtc.org/9278): Move this class to an unnamed namespace in the
382// .cc file.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200383class LogMessage {
384 public:
Karl Wibergab4f1c12018-05-04 10:42:28 +0200385 LogMessage(const char* file, int line, LoggingSeverity sev);
Karl Wiberg1ffb3742018-05-04 15:04:48 +0200386
387 // Same as the above, but using a compile-time constant for the logging
388 // severity. This saves space at the call site, since passing an empty struct
389 // is generally the same as not passing an argument at all.
390 template <LoggingSeverity S>
391 RTC_NO_INLINE LogMessage(const char* file,
392 int line,
393 std::integral_constant<LoggingSeverity, S>)
394 : LogMessage(file, line, S) {}
395
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200396 LogMessage(const char* file,
397 int line,
398 LoggingSeverity sev,
Karl Wibergab4f1c12018-05-04 10:42:28 +0200399 LogErrorContext err_ctx,
400 int err);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200401
Tommie51a0a82018-02-27 15:30:29 +0100402#if defined(WEBRTC_ANDROID)
403 LogMessage(const char* file, int line, LoggingSeverity sev, const char* tag);
404#endif
405
406 // DEPRECATED - DO NOT USE - PLEASE USE THE MACROS INSTEAD OF THE CLASS.
407 // Android code should use the 'const char*' version since tags are static
408 // and we want to avoid allocating a std::string copy per log line.
409 RTC_DEPRECATED
Yves Gerey665174f2018-06-19 15:03:05 +0200410 LogMessage(const char* file,
411 int line,
412 LoggingSeverity sev,
Philip Eliasson278aa422018-02-26 14:54:45 +0000413 const std::string& tag);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200414
415 ~LogMessage();
416
Karl Wibergcefc4652018-05-23 23:20:38 +0200417 void AddTag(const char* tag);
418
Jonas Olssond8c50782018-09-07 11:21:28 +0200419 rtc::StringBuilder& stream();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200420
421 // Returns the time at which this function was called for the first time.
422 // The time will be used as the logging start time.
423 // If this is not called externally, the LogMessage ctor also calls it, in
424 // which case the logging start time will be the time of the first LogMessage
425 // instance is created.
426 static int64_t LogStartTime();
427
428 // Returns the wall clock equivalent of |LogStartTime|, in seconds from the
429 // epoch.
430 static uint32_t WallClockStartTime();
431
432 // LogThreads: Display the thread identifier of the current thread
433 static void LogThreads(bool on = true);
434
435 // LogTimestamps: Display the elapsed time of the program
436 static void LogTimestamps(bool on = true);
437
438 // These are the available logging channels
439 // Debug: Debug console on Windows, otherwise stderr
440 static void LogToDebug(LoggingSeverity min_sev);
Jonas Olsson2b6f1352018-02-15 11:57:03 +0100441 static LoggingSeverity GetLogToDebug();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200442
443 // Sets whether logs will be directed to stderr in debug mode.
444 static void SetLogToStderr(bool log_to_stderr);
445
Danil Chapovalovb9f69022019-10-21 09:19:10 +0200446 // Stream: Any non-blocking stream interface.
447 // Installs the |stream| to collect logs with severtiy |min_sev| or higher.
448 // |stream| must live until deinstalled by RemoveLogToStream
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200449 static void AddLogToStream(LogSink* stream, LoggingSeverity min_sev);
Danil Chapovalovb9f69022019-10-21 09:19:10 +0200450 // Removes the specified stream, without destroying it.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200451 static void RemoveLogToStream(LogSink* stream);
Danil Chapovalovb9f69022019-10-21 09:19:10 +0200452 // Returns the severity for the specified stream, of if none is specified,
453 // the minimum stream severity.
454 static int GetLogToStream(LogSink* stream = nullptr);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200455
456 // Testing against MinLogSeverity allows code to avoid potentially expensive
457 // logging operations by pre-checking the logging level.
Jonas Olsson2b6f1352018-02-15 11:57:03 +0100458 static int GetMinLogSeverity();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200459
460 // Parses the provided parameter stream to configure the options above.
461 // Useful for configuring logging from the command line.
462 static void ConfigureLogging(const char* params);
463
Jonas Olssond8c50782018-09-07 11:21:28 +0200464 // Checks the current global debug severity and if the |streams_| collection
465 // is empty. If |severity| is smaller than the global severity and if the
466 // |streams_| collection is empty, the LogMessage will be considered a noop
467 // LogMessage.
468 static bool IsNoop(LoggingSeverity severity);
469
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200470 private:
Tommifef05002018-02-27 13:51:08 +0100471 friend class LogMessageForTesting;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200472
473 // Updates min_sev_ appropriately when debug sinks change.
474 static void UpdateMinLogSeverity();
475
Yves Gerey665174f2018-06-19 15:03:05 +0200476// These write out the actual log messages.
Tommie51a0a82018-02-27 15:30:29 +0100477#if defined(WEBRTC_ANDROID)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200478 static void OutputToDebug(const std::string& msg,
479 LoggingSeverity severity,
Tommie51a0a82018-02-27 15:30:29 +0100480 const char* tag);
481#else
482 static void OutputToDebug(const std::string& msg, LoggingSeverity severity);
483#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200484
Tommifef05002018-02-27 13:51:08 +0100485 // Called from the dtor (or from a test) to append optional extra error
486 // information to the log stream and a newline character.
487 void FinishPrintStream();
488
Jonas Olssond8c50782018-09-07 11:21:28 +0200489 // The stringbuilder that buffers the formatted message before output
490 rtc::StringBuilder print_stream_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200491
492 // The severity level of this message
493 LoggingSeverity severity_;
494
Tommie51a0a82018-02-27 15:30:29 +0100495#if defined(WEBRTC_ANDROID)
Paulina Hensmanf1e3cb42018-06-20 14:07:05 +0200496 // The default Android debug output tag.
Tommie51a0a82018-02-27 15:30:29 +0100497 const char* tag_ = "libjingle";
498#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200499
500 // String data generated in the constructor, that should be appended to
501 // the message before output.
502 std::string extra_;
503
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200504 // The output streams and their associated severities
Danil Chapovalovb9f69022019-10-21 09:19:10 +0200505 static LogSink* streams_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200506
507 // Flags for formatting options
508 static bool thread_, timestamp_;
509
510 // Determines if logs will be directed to stderr in debug mode.
511 static bool log_to_stderr_;
512
513 RTC_DISALLOW_COPY_AND_ASSIGN(LogMessage);
514};
515
516//////////////////////////////////////////////////////////////////////
517// Logging Helpers
518//////////////////////////////////////////////////////////////////////
519
Jonas Olsson388e4e92018-11-16 15:57:49 +0100520#define RTC_LOG_FILE_LINE(sev, file, line) \
521 rtc::webrtc_logging_impl::LogCall() & \
522 rtc::webrtc_logging_impl::LogStreamer<>() \
523 << rtc::webrtc_logging_impl::LogMetadata(file, line, sev)
Jonas Olsson8a7916b2018-09-06 09:50:23 +0200524
525#define RTC_LOG(sev) RTC_LOG_FILE_LINE(rtc::sev, __FILE__, __LINE__)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200526
Karl Wibergcefc4652018-05-23 23:20:38 +0200527// The _V version is for when a variable is passed in.
Jonas Olssond8c50782018-09-07 11:21:28 +0200528#define RTC_LOG_V(sev) RTC_LOG_FILE_LINE(sev, __FILE__, __LINE__)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200529
530// The _F version prefixes the message with the current function name.
531#if (defined(__GNUC__) && !defined(NDEBUG)) || defined(WANT_PRETTY_LOG_F)
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200532#define RTC_LOG_F(sev) RTC_LOG(sev) << __PRETTY_FUNCTION__ << ": "
Yves Gerey665174f2018-06-19 15:03:05 +0200533#define RTC_LOG_T_F(sev) \
534 RTC_LOG(sev) << this << ": " << __PRETTY_FUNCTION__ << ": "
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200535#else
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200536#define RTC_LOG_F(sev) RTC_LOG(sev) << __FUNCTION__ << ": "
Patrik Höglundc2962552017-11-17 13:40:22 +0000537#define RTC_LOG_T_F(sev) RTC_LOG(sev) << this << ": " << __FUNCTION__ << ": "
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200538#endif
539
Yves Gerey665174f2018-06-19 15:03:05 +0200540#define RTC_LOG_CHECK_LEVEL(sev) rtc::LogCheckLevel(rtc::sev)
541#define RTC_LOG_CHECK_LEVEL_V(sev) rtc::LogCheckLevel(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200542
543inline bool LogCheckLevel(LoggingSeverity sev) {
544 return (LogMessage::GetMinLogSeverity() <= sev);
545}
546
Jonas Olssona4d87372019-07-05 19:08:33 +0200547#define RTC_LOG_E(sev, ctx, err) \
548 rtc::webrtc_logging_impl::LogCall() & \
549 rtc::webrtc_logging_impl::LogStreamer<>() \
550 << rtc::webrtc_logging_impl::LogMetadataErr { \
551 {__FILE__, __LINE__, rtc::sev}, rtc::ERRCTX_##ctx, (err) \
552 }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200553
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200554#define RTC_LOG_T(sev) RTC_LOG(sev) << this << ": "
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200555
Yves Gerey665174f2018-06-19 15:03:05 +0200556#define RTC_LOG_ERRNO_EX(sev, err) RTC_LOG_E(sev, ERRNO, err)
557#define RTC_LOG_ERRNO(sev) RTC_LOG_ERRNO_EX(sev, errno)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200558
559#if defined(WEBRTC_WIN)
Yves Gerey665174f2018-06-19 15:03:05 +0200560#define RTC_LOG_GLE_EX(sev, err) RTC_LOG_E(sev, HRESULT, err)
Karl Wibergcefc4652018-05-23 23:20:38 +0200561#define RTC_LOG_GLE(sev) RTC_LOG_GLE_EX(sev, static_cast<int>(GetLastError()))
Yves Gerey665174f2018-06-19 15:03:05 +0200562#define RTC_LOG_ERR_EX(sev, err) RTC_LOG_GLE_EX(sev, err)
563#define RTC_LOG_ERR(sev) RTC_LOG_GLE(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200564#elif defined(__native_client__) && __native_client__
Yves Gerey665174f2018-06-19 15:03:05 +0200565#define RTC_LOG_ERR_EX(sev, err) RTC_LOG(sev)
566#define RTC_LOG_ERR(sev) RTC_LOG(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200567#elif defined(WEBRTC_POSIX)
Yves Gerey665174f2018-06-19 15:03:05 +0200568#define RTC_LOG_ERR_EX(sev, err) RTC_LOG_ERRNO_EX(sev, err)
569#define RTC_LOG_ERR(sev) RTC_LOG_ERRNO(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200570#endif // WEBRTC_WIN
571
Karl Wibergcefc4652018-05-23 23:20:38 +0200572#ifdef WEBRTC_ANDROID
573
574namespace webrtc_logging_impl {
575// TODO(kwiberg): Replace these with absl::string_view.
Yves Gerey665174f2018-06-19 15:03:05 +0200576inline const char* AdaptString(const char* str) {
577 return str;
578}
579inline const char* AdaptString(const std::string& str) {
580 return str.c_str();
581}
Karl Wibergcefc4652018-05-23 23:20:38 +0200582} // namespace webrtc_logging_impl
583
Jonas Olssona4d87372019-07-05 19:08:33 +0200584#define RTC_LOG_TAG(sev, tag) \
585 rtc::webrtc_logging_impl::LogCall() & \
586 rtc::webrtc_logging_impl::LogStreamer<>() \
587 << rtc::webrtc_logging_impl::LogMetadataTag { \
588 sev, rtc::webrtc_logging_impl::AdaptString(tag) \
589 }
Karl Wibergcefc4652018-05-23 23:20:38 +0200590
Tommie51a0a82018-02-27 15:30:29 +0100591#else
Karl Wibergcefc4652018-05-23 23:20:38 +0200592
Tommie51a0a82018-02-27 15:30:29 +0100593// DEPRECATED. This macro is only intended for Android.
Karl Wibergcefc4652018-05-23 23:20:38 +0200594#define RTC_LOG_TAG(sev, tag) RTC_LOG_V(sev)
595
Tommie51a0a82018-02-27 15:30:29 +0100596#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200597
Fredrik Solenbergb3d7cac2017-11-17 15:22:37 +0100598// The RTC_DLOG macros are equivalent to their RTC_LOG counterparts except that
599// they only generate code in debug builds.
600#if RTC_DLOG_IS_ON
601#define RTC_DLOG(sev) RTC_LOG(sev)
602#define RTC_DLOG_V(sev) RTC_LOG_V(sev)
603#define RTC_DLOG_F(sev) RTC_LOG_F(sev)
604#else
Karl Wibergcefc4652018-05-23 23:20:38 +0200605#define RTC_DLOG_EAT_STREAM_PARAMS() \
606 while (false) \
607 rtc::webrtc_logging_impl::LogStreamer<>()
608#define RTC_DLOG(sev) RTC_DLOG_EAT_STREAM_PARAMS()
609#define RTC_DLOG_V(sev) RTC_DLOG_EAT_STREAM_PARAMS()
610#define RTC_DLOG_F(sev) RTC_DLOG_EAT_STREAM_PARAMS()
Fredrik Solenbergb3d7cac2017-11-17 15:22:37 +0100611#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200612
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200613} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000614
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200615#endif // RTC_BASE_LOGGING_H_