blob: e21c30e21af018e4eb3bc18e5298207ec2fa17cf [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:261/*
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 07:43:5311// RTC_LOG(...) an ostream target that can be used to send formatted
henrike@webrtc.orgf0488722014-05-13 18:00:2612// output to a variety of logging targets, such as debugger console, stderr,
Tommi0eefb4d2015-05-23 07:54:0713// or any LogSink.
Mirko Bonadei8ed8e562017-10-27 07:43:5314// The severity level passed as the first argument to the logging
henrike@webrtc.orgf0488722014-05-13 18:00:2615// functions is used as a filter, to limit the verbosity of the logging.
Mirko Bonadei8ed8e562017-10-27 07:43:5316// Static members of LogMessage documented below are used to control the
henrike@webrtc.orgf0488722014-05-13 18:00:2617// verbosity and target of the output.
Mirko Bonadei8ed8e562017-10-27 07:43:5318// There are several variations on the RTC_LOG macro which facilitate logging
henrike@webrtc.orgf0488722014-05-13 18:00:2619// of common error conditions, detailed below.
20
Mirko Bonadei8ed8e562017-10-27 07:43:5321// RTC_LOG(sev) logs the given stream at severity "sev", which must be a
henrike@webrtc.orgf0488722014-05-13 18:00:2622// compile-time constant of the LoggingSeverity type, without the namespace
23// prefix.
Mirko Bonadei8ed8e562017-10-27 07:43:5324// 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 14:30:2929// RTC_LOG_GLE(sev [, mod]) attempt to add a string description of the
30// HRESULT returned by GetLastError.
Mirko Bonadei8ed8e562017-10-27 07:43:5331// RTC_LOG_ERRNO(sev) attempts to add a string description of an errno-derived
henrike@webrtc.orgf0488722014-05-13 18:00:2632// 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 07:43:5334// 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:2636// (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 07:43:5338// RTC_LOG_E(sev, ctx, err, ...) logs a detailed error interpreted using the
henrike@webrtc.orgf0488722014-05-13 18:00:2639// specified context.
Mirko Bonadei8ed8e562017-10-27 07:43:5340// 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:2643
Mirko Bonadei92ea95e2017-09-15 04:47:3144#ifndef RTC_BASE_LOGGING_H_
45#define RTC_BASE_LOGGING_H_
henrike@webrtc.orgf0488722014-05-13 18:00:2646
Henrik Kjellanderec78f1c2017-06-29 05:52:5047#include <errno.h>
mostynbe38e4f62016-05-12 08:08:2048
Markus Handellce1ff6f2020-07-08 06:52:4849#include <atomic>
Jonas Olsson941a07c2018-09-13 08:07:0750#include <sstream> // no-presubmit-check TODO(webrtc:8982)
Henrik Kjellanderec78f1c2017-06-29 05:52:5051#include <string>
52#include <utility>
53
Danil Chapovalove9041612021-02-22 11:43:3954#include "absl/base/attributes.h"
Sebastian Janssonf4e085a2019-05-20 16:34:0755#include "absl/meta/type_traits.h"
Jonas Olssonf2ce37c2018-09-12 13:32:4756#include "absl/strings/string_view.h"
Steve Anton10542f22019-01-11 17:11:0057#include "rtc_base/constructor_magic.h"
Jonas Olssond8c50782018-09-07 09:21:2858#include "rtc_base/strings/string_builder.h"
Karl Wibergcefc4652018-05-23 21:20:3859#include "rtc_base/system/inline.h"
Henrik Kjellanderec78f1c2017-06-29 05:52:5060
Fredrik Solenbergb3d7cac2017-11-17 14:22:3761#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
Artem Titov6a4a1462019-11-26 15:24:4667#if defined(RTC_DISABLE_LOGGING)
68#define RTC_LOG_ENABLED() 0
69#else
70#define RTC_LOG_ENABLED() 1
71#endif
72
Henrik Kjellanderec78f1c2017-06-29 05:52:5073namespace rtc {
74
Henrik Kjellanderec78f1c2017-06-29 05:52:5075//////////////////////////////////////////////////////////////////////
76
77// Note that the non-standard LoggingSeverity aliases exist because they are
78// still in broad use. The meanings of the levels are:
Henrik Kjellanderec78f1c2017-06-29 05:52:5079// LS_VERBOSE: This level is for data which we do not want to appear in the
80// normal debug log, but should appear in diagnostic logs.
81// LS_INFO: Chatty level used in debugging for all sorts of things, the default
82// in debug builds.
83// LS_WARNING: Something that may warrant investigation.
84// LS_ERROR: Something that should not have occurred.
85// LS_NONE: Don't log.
86enum LoggingSeverity {
Henrik Kjellanderec78f1c2017-06-29 05:52:5087 LS_VERBOSE,
88 LS_INFO,
89 LS_WARNING,
90 LS_ERROR,
91 LS_NONE,
92 INFO = LS_INFO,
93 WARNING = LS_WARNING,
94 LERROR = LS_ERROR
95};
96
97// LogErrorContext assists in interpreting the meaning of an error value.
98enum LogErrorContext {
99 ERRCTX_NONE,
Jonas Olssona4d87372019-07-05 17:08:33100 ERRCTX_ERRNO, // System-local errno
101 ERRCTX_HRESULT, // Windows HRESULT
Henrik Kjellanderec78f1c2017-06-29 05:52:50102
103 // Abbreviations for LOG_E macro
Jonas Olssona4d87372019-07-05 17:08:33104 ERRCTX_EN = ERRCTX_ERRNO, // LOG_E(sev, EN, x)
105 ERRCTX_HR = ERRCTX_HRESULT, // LOG_E(sev, HR, x)
Henrik Kjellanderec78f1c2017-06-29 05:52:50106};
107
Danil Chapovalovb9f69022019-10-21 07:19:10108class LogMessage;
Henrik Kjellanderec78f1c2017-06-29 05:52:50109// Virtual sink interface that can receive log messages.
110class LogSink {
111 public:
112 LogSink() {}
113 virtual ~LogSink() {}
Paulina Hensmanf1e3cb42018-06-20 12:07:05114 virtual void OnLogMessage(const std::string& msg,
115 LoggingSeverity severity,
116 const char* tag);
Jiawei Ou3ea18782018-11-01 06:14:24117 virtual void OnLogMessage(const std::string& message,
118 LoggingSeverity severity);
Henrik Kjellanderec78f1c2017-06-29 05:52:50119 virtual void OnLogMessage(const std::string& message) = 0;
Danil Chapovalovb9f69022019-10-21 07:19:10120
121 private:
122 friend class ::rtc::LogMessage;
Artem Titov6a4a1462019-11-26 15:24:46123#if RTC_LOG_ENABLED()
Danil Chapovalovb9f69022019-10-21 07:19:10124 // Members for LogMessage class to keep linked list of the registered sinks.
125 LogSink* next_ = nullptr;
126 LoggingSeverity min_severity_;
Artem Titov6a4a1462019-11-26 15:24:46127#endif
Henrik Kjellanderec78f1c2017-06-29 05:52:50128};
129
Karl Wibergcefc4652018-05-23 21:20:38130namespace webrtc_logging_impl {
131
132class LogMetadata {
133 public:
134 LogMetadata(const char* file, int line, LoggingSeverity severity)
135 : file_(file),
136 line_and_sev_(static_cast<uint32_t>(line) << 3 | severity) {}
137 LogMetadata() = default;
138
139 const char* File() const { return file_; }
140 int Line() const { return line_and_sev_ >> 3; }
141 LoggingSeverity Severity() const {
142 return static_cast<LoggingSeverity>(line_and_sev_ & 0x7);
143 }
144
145 private:
146 const char* file_;
147
148 // Line number and severity, the former in the most significant 29 bits, the
149 // latter in the least significant 3 bits. (This is an optimization; since
150 // both numbers are usually compile-time constants, this way we can load them
151 // both with a single instruction.)
152 uint32_t line_and_sev_;
153};
154static_assert(std::is_trivial<LogMetadata>::value, "");
155
156struct LogMetadataErr {
157 LogMetadata meta;
158 LogErrorContext err_ctx;
159 int err;
160};
161
162#ifdef WEBRTC_ANDROID
163struct LogMetadataTag {
164 LoggingSeverity severity;
165 const char* tag;
166};
167#endif
168
169enum class LogArgType : int8_t {
170 kEnd = 0,
171 kInt,
172 kLong,
173 kLongLong,
174 kUInt,
175 kULong,
176 kULongLong,
177 kDouble,
178 kLongDouble,
179 kCharP,
180 kStdString,
Jonas Olssonf2ce37c2018-09-12 13:32:47181 kStringView,
Karl Wibergcefc4652018-05-23 21:20:38182 kVoidP,
183 kLogMetadata,
184 kLogMetadataErr,
185#ifdef WEBRTC_ANDROID
186 kLogMetadataTag,
187#endif
188};
189
190// Wrapper for log arguments. Only ever make values of this type with the
191// MakeVal() functions.
192template <LogArgType N, typename T>
193struct Val {
194 static constexpr LogArgType Type() { return N; }
195 T GetVal() const { return val; }
196 T val;
197};
198
Sebastian Janssonb1138622019-04-11 14:48:15199// Case for when we need to construct a temp string and then print that.
200// (We can't use Val<CheckArgType::kStdString, const std::string*>
201// because we need somewhere to store the temp string.)
202struct ToStringVal {
Karl Wibergcefc4652018-05-23 21:20:38203 static constexpr LogArgType Type() { return LogArgType::kStdString; }
204 const std::string* GetVal() const { return &val; }
205 std::string val;
206};
207
208inline Val<LogArgType::kInt, int> MakeVal(int x) {
209 return {x};
210}
211inline Val<LogArgType::kLong, long> MakeVal(long x) {
212 return {x};
213}
214inline Val<LogArgType::kLongLong, long long> MakeVal(long long x) {
215 return {x};
216}
217inline Val<LogArgType::kUInt, unsigned int> MakeVal(unsigned int x) {
218 return {x};
219}
220inline Val<LogArgType::kULong, unsigned long> MakeVal(unsigned long x) {
221 return {x};
222}
223inline Val<LogArgType::kULongLong, unsigned long long> MakeVal(
224 unsigned long long x) {
225 return {x};
226}
227
228inline Val<LogArgType::kDouble, double> MakeVal(double x) {
229 return {x};
230}
231inline Val<LogArgType::kLongDouble, long double> MakeVal(long double x) {
232 return {x};
233}
234
235inline Val<LogArgType::kCharP, const char*> MakeVal(const char* x) {
236 return {x};
237}
238inline Val<LogArgType::kStdString, const std::string*> MakeVal(
239 const std::string& x) {
240 return {&x};
241}
Jonas Olssonf2ce37c2018-09-12 13:32:47242inline Val<LogArgType::kStringView, const absl::string_view*> MakeVal(
243 const absl::string_view& x) {
244 return {&x};
245}
Karl Wibergcefc4652018-05-23 21:20:38246
247inline Val<LogArgType::kVoidP, const void*> MakeVal(const void* x) {
248 return {x};
249}
250
251inline Val<LogArgType::kLogMetadata, LogMetadata> MakeVal(
252 const LogMetadata& x) {
253 return {x};
254}
255inline Val<LogArgType::kLogMetadataErr, LogMetadataErr> MakeVal(
256 const LogMetadataErr& x) {
257 return {x};
258}
259
Amit Hilbuch10c5a932019-03-18 20:09:18260// The enum class types are not implicitly convertible to arithmetic types.
Sebastian Janssonf4e085a2019-05-20 16:34:07261template <typename T,
262 absl::enable_if_t<std::is_enum<T>::value &&
263 !std::is_arithmetic<T>::value>* = nullptr>
264inline decltype(MakeVal(std::declval<absl::underlying_type_t<T>>())) MakeVal(
265 T x) {
266 return {static_cast<absl::underlying_type_t<T>>(x)};
Amit Hilbuch10c5a932019-03-18 20:09:18267}
268
Karl Wibergcefc4652018-05-23 21:20:38269#ifdef WEBRTC_ANDROID
270inline Val<LogArgType::kLogMetadataTag, LogMetadataTag> MakeVal(
271 const LogMetadataTag& x) {
272 return {x};
273}
274#endif
275
Sebastian Janssonf4e085a2019-05-20 16:34:07276template <typename T, class = void>
Sebastian Janssonb1138622019-04-11 14:48:15277struct has_to_log_string : std::false_type {};
278template <typename T>
Sebastian Janssonf4e085a2019-05-20 16:34:07279struct has_to_log_string<T, decltype(ToLogString(std::declval<T>()))>
Sebastian Janssonb1138622019-04-11 14:48:15280 : std::true_type {};
281
Karl Wibergcefc4652018-05-23 21:20:38282// Handle arbitrary types other than the above by falling back to stringstream.
283// TODO(bugs.webrtc.org/9278): Get rid of this overload when callers don't need
284// it anymore. No in-tree caller does, but some external callers still do.
285template <
286 typename T,
Sebastian Janssonf4e085a2019-05-20 16:34:07287 typename T1 = absl::decay_t<T>,
288 absl::enable_if_t<std::is_class<T1>::value &&
289 !std::is_same<T1, std::string>::value &&
290 !std::is_same<T1, LogMetadata>::value &&
291 !has_to_log_string<T1>::value &&
Karl Wibergcefc4652018-05-23 21:20:38292#ifdef WEBRTC_ANDROID
Sebastian Janssonf4e085a2019-05-20 16:34:07293 !std::is_same<T1, LogMetadataTag>::value &&
Karl Wibergcefc4652018-05-23 21:20:38294#endif
Sebastian Janssonf4e085a2019-05-20 16:34:07295 !std::is_same<T1, LogMetadataErr>::value>* = nullptr>
Sebastian Janssonb1138622019-04-11 14:48:15296ToStringVal MakeVal(const T& x) {
Karl Wibergcefc4652018-05-23 21:20:38297 std::ostringstream os; // no-presubmit-check TODO(webrtc:8982)
298 os << x;
299 return {os.str()};
300}
301
Sebastian Janssonf4e085a2019-05-20 16:34:07302template <typename T, absl::enable_if_t<has_to_log_string<T>::value>* = nullptr>
Sebastian Janssonb1138622019-04-11 14:48:15303ToStringVal MakeVal(const T& x) {
304 return {ToLogString(x)};
305}
306
Artem Titov6a4a1462019-11-26 15:24:46307#if RTC_LOG_ENABLED()
Karl Wibergcefc4652018-05-23 21:20:38308void Log(const LogArgType* fmt, ...);
Artem Titov6a4a1462019-11-26 15:24:46309#else
310inline void Log(const LogArgType* fmt, ...) {
311 // Do nothing, shouldn't be invoked
312}
313#endif
Karl Wibergcefc4652018-05-23 21:20:38314
315// Ephemeral type that represents the result of the logging << operator.
316template <typename... Ts>
317class LogStreamer;
318
319// Base case: Before the first << argument.
320template <>
321class LogStreamer<> final {
322 public:
Amit Hilbuch10c5a932019-03-18 20:09:18323 template <typename U,
Sebastian Janssonf4e085a2019-05-20 16:34:07324 typename V = decltype(MakeVal(std::declval<U>())),
325 absl::enable_if_t<std::is_arithmetic<U>::value ||
326 std::is_enum<U>::value>* = nullptr>
327 RTC_FORCE_INLINE LogStreamer<V> operator<<(U arg) const {
328 return LogStreamer<V>(MakeVal(arg), this);
Karl Wibergcefc4652018-05-23 21:20:38329 }
330
Amit Hilbuch10c5a932019-03-18 20:09:18331 template <typename U,
Sebastian Janssonf4e085a2019-05-20 16:34:07332 typename V = decltype(MakeVal(std::declval<U>())),
333 absl::enable_if_t<!std::is_arithmetic<U>::value &&
334 !std::is_enum<U>::value>* = nullptr>
335 RTC_FORCE_INLINE LogStreamer<V> operator<<(const U& arg) const {
336 return LogStreamer<V>(MakeVal(arg), this);
Karl Wibergcefc4652018-05-23 21:20:38337 }
338
339 template <typename... Us>
340 RTC_FORCE_INLINE static void Call(const Us&... args) {
341 static constexpr LogArgType t[] = {Us::Type()..., LogArgType::kEnd};
342 Log(t, args.GetVal()...);
343 }
344};
345
346// Inductive case: We've already seen at least one << argument. The most recent
347// one had type `T`, and the earlier ones had types `Ts`.
348template <typename T, typename... Ts>
349class LogStreamer<T, Ts...> final {
350 public:
351 RTC_FORCE_INLINE LogStreamer(T arg, const LogStreamer<Ts...>* prior)
352 : arg_(arg), prior_(prior) {}
353
Amit Hilbuch10c5a932019-03-18 20:09:18354 template <typename U,
Sebastian Janssonf4e085a2019-05-20 16:34:07355 typename V = decltype(MakeVal(std::declval<U>())),
356 absl::enable_if_t<std::is_arithmetic<U>::value ||
357 std::is_enum<U>::value>* = nullptr>
358 RTC_FORCE_INLINE LogStreamer<V, T, Ts...> operator<<(U arg) const {
359 return LogStreamer<V, T, Ts...>(MakeVal(arg), this);
Karl Wibergcefc4652018-05-23 21:20:38360 }
361
Amit Hilbuch10c5a932019-03-18 20:09:18362 template <typename U,
Sebastian Janssonf4e085a2019-05-20 16:34:07363 typename V = decltype(MakeVal(std::declval<U>())),
364 absl::enable_if_t<!std::is_arithmetic<U>::value &&
365 !std::is_enum<U>::value>* = nullptr>
366 RTC_FORCE_INLINE LogStreamer<V, T, Ts...> operator<<(const U& arg) const {
367 return LogStreamer<V, T, Ts...>(MakeVal(arg), this);
Karl Wibergcefc4652018-05-23 21:20:38368 }
369
370 template <typename... Us>
371 RTC_FORCE_INLINE void Call(const Us&... args) const {
372 prior_->Call(arg_, args...);
373 }
374
375 private:
376 // The most recent argument.
377 T arg_;
378
379 // Earlier arguments.
380 const LogStreamer<Ts...>* prior_;
381};
382
383class LogCall final {
384 public:
385 // This can be any binary operator with precedence lower than <<.
Artem Titov6a4a1462019-11-26 15:24:46386 // We return bool here to be able properly remove logging if
387 // RTC_DISABLE_LOGGING is defined.
Karl Wibergcefc4652018-05-23 21:20:38388 template <typename... Ts>
Artem Titov6a4a1462019-11-26 15:24:46389 RTC_FORCE_INLINE bool operator&(const LogStreamer<Ts...>& streamer) {
Karl Wibergcefc4652018-05-23 21:20:38390 streamer.Call();
Artem Titov6a4a1462019-11-26 15:24:46391 return true;
Karl Wibergcefc4652018-05-23 21:20:38392 }
393};
394
Mirko Bonadeif1df04b2020-03-23 18:53:23395// This class is used to explicitly ignore values in the conditional
396// logging macros. This avoids compiler warnings like "value computed
397// is not used" and "statement has no effect".
398class LogMessageVoidify {
399 public:
400 LogMessageVoidify() = default;
401 // This has to be an operator with a precedence lower than << but
402 // higher than ?:
403 template <typename... Ts>
404 void operator&(LogStreamer<Ts...>&& streamer) {}
405};
406
Karl Wibergcefc4652018-05-23 21:20:38407} // namespace webrtc_logging_impl
408
409// Direct use of this class is deprecated; please use the logging macros
410// instead.
411// TODO(bugs.webrtc.org/9278): Move this class to an unnamed namespace in the
412// .cc file.
Henrik Kjellanderec78f1c2017-06-29 05:52:50413class LogMessage {
414 public:
Karl Wiberg1ffb3742018-05-04 13:04:48415 // Same as the above, but using a compile-time constant for the logging
416 // severity. This saves space at the call site, since passing an empty struct
417 // is generally the same as not passing an argument at all.
418 template <LoggingSeverity S>
419 RTC_NO_INLINE LogMessage(const char* file,
420 int line,
421 std::integral_constant<LoggingSeverity, S>)
422 : LogMessage(file, line, S) {}
423
Artem Titov6a4a1462019-11-26 15:24:46424#if RTC_LOG_ENABLED()
425 LogMessage(const char* file, int line, LoggingSeverity sev);
Henrik Kjellanderec78f1c2017-06-29 05:52:50426 LogMessage(const char* file,
427 int line,
428 LoggingSeverity sev,
Karl Wibergab4f1c12018-05-04 08:42:28429 LogErrorContext err_ctx,
430 int err);
Tommie51a0a82018-02-27 14:30:29431#if defined(WEBRTC_ANDROID)
432 LogMessage(const char* file, int line, LoggingSeverity sev, const char* tag);
433#endif
Tommie51a0a82018-02-27 14:30:29434 // DEPRECATED - DO NOT USE - PLEASE USE THE MACROS INSTEAD OF THE CLASS.
435 // Android code should use the 'const char*' version since tags are static
436 // and we want to avoid allocating a std::string copy per log line.
Danil Chapovalove9041612021-02-22 11:43:39437 ABSL_DEPRECATED("Use RTC_LOG macros instead of accessing this class directly")
Yves Gerey665174f2018-06-19 13:03:05438 LogMessage(const char* file,
439 int line,
440 LoggingSeverity sev,
Philip Eliasson278aa422018-02-26 14:54:45441 const std::string& tag);
Henrik Kjellanderec78f1c2017-06-29 05:52:50442 ~LogMessage();
443
Karl Wibergcefc4652018-05-23 21:20:38444 void AddTag(const char* tag);
Jonas Olssond8c50782018-09-07 09:21:28445 rtc::StringBuilder& stream();
Henrik Kjellanderec78f1c2017-06-29 05:52:50446 // Returns the time at which this function was called for the first time.
447 // The time will be used as the logging start time.
448 // If this is not called externally, the LogMessage ctor also calls it, in
449 // which case the logging start time will be the time of the first LogMessage
450 // instance is created.
451 static int64_t LogStartTime();
Henrik Kjellanderec78f1c2017-06-29 05:52:50452 // Returns the wall clock equivalent of |LogStartTime|, in seconds from the
453 // epoch.
454 static uint32_t WallClockStartTime();
Henrik Kjellanderec78f1c2017-06-29 05:52:50455 // LogThreads: Display the thread identifier of the current thread
456 static void LogThreads(bool on = true);
Henrik Kjellanderec78f1c2017-06-29 05:52:50457 // LogTimestamps: Display the elapsed time of the program
458 static void LogTimestamps(bool on = true);
Henrik Kjellanderec78f1c2017-06-29 05:52:50459 // These are the available logging channels
460 // Debug: Debug console on Windows, otherwise stderr
461 static void LogToDebug(LoggingSeverity min_sev);
Jonas Olsson2b6f1352018-02-15 10:57:03462 static LoggingSeverity GetLogToDebug();
Henrik Kjellanderec78f1c2017-06-29 05:52:50463 // Sets whether logs will be directed to stderr in debug mode.
464 static void SetLogToStderr(bool log_to_stderr);
Danil Chapovalovb9f69022019-10-21 07:19:10465 // Stream: Any non-blocking stream interface.
466 // Installs the |stream| to collect logs with severtiy |min_sev| or higher.
Markus Handell531bd0f2020-07-08 08:58:19467 // |stream| must live until deinstalled by RemoveLogToStream.
468 // If |stream| is the first stream added to the system, we might miss some
469 // early concurrent log statement happening from another thread happening near
470 // this instant.
Henrik Kjellanderec78f1c2017-06-29 05:52:50471 static void AddLogToStream(LogSink* stream, LoggingSeverity min_sev);
Markus Handell531bd0f2020-07-08 08:58:19472 // Removes the specified stream, without destroying it. When the method
473 // has completed, it's guaranteed that |stream| will receive no more logging
474 // calls.
Henrik Kjellanderec78f1c2017-06-29 05:52:50475 static void RemoveLogToStream(LogSink* stream);
Danil Chapovalovb9f69022019-10-21 07:19:10476 // Returns the severity for the specified stream, of if none is specified,
477 // the minimum stream severity.
478 static int GetLogToStream(LogSink* stream = nullptr);
Henrik Kjellanderec78f1c2017-06-29 05:52:50479 // Testing against MinLogSeverity allows code to avoid potentially expensive
480 // logging operations by pre-checking the logging level.
Jonas Olsson2b6f1352018-02-15 10:57:03481 static int GetMinLogSeverity();
Henrik Kjellanderec78f1c2017-06-29 05:52:50482 // Parses the provided parameter stream to configure the options above.
483 // Useful for configuring logging from the command line.
484 static void ConfigureLogging(const char* params);
Jonas Olssond8c50782018-09-07 09:21:28485 // Checks the current global debug severity and if the |streams_| collection
486 // is empty. If |severity| is smaller than the global severity and if the
487 // |streams_| collection is empty, the LogMessage will be considered a noop
488 // LogMessage.
489 static bool IsNoop(LoggingSeverity severity);
Karl Wibergdd7df5c2020-09-22 09:07:53490 // Version of IsNoop that uses fewer instructions at the call site, since the
491 // caller doesn't have to pass an argument.
492 template <LoggingSeverity S>
493 RTC_NO_INLINE static bool IsNoop() {
494 return IsNoop(S);
495 }
Artem Titov6a4a1462019-11-26 15:24:46496#else
497 // Next methods do nothing; no one will call these functions.
498 LogMessage(const char* file, int line, LoggingSeverity sev) {}
499 LogMessage(const char* file,
500 int line,
501 LoggingSeverity sev,
502 LogErrorContext err_ctx,
503 int err) {}
504#if defined(WEBRTC_ANDROID)
505 LogMessage(const char* file, int line, LoggingSeverity sev, const char* tag) {
506 }
507#endif
508 // DEPRECATED - DO NOT USE - PLEASE USE THE MACROS INSTEAD OF THE CLASS.
509 // Android code should use the 'const char*' version since tags are static
510 // and we want to avoid allocating a std::string copy per log line.
Danil Chapovalove9041612021-02-22 11:43:39511 ABSL_DEPRECATED("Use RTC_LOG macros instead of accessing this class directly")
Artem Titov6a4a1462019-11-26 15:24:46512 LogMessage(const char* file,
513 int line,
514 LoggingSeverity sev,
515 const std::string& tag) {}
516 ~LogMessage() = default;
517
518 inline void AddTag(const char* tag) {}
519 inline rtc::StringBuilder& stream() { return print_stream_; }
520 inline static int64_t LogStartTime() { return 0; }
521 inline static uint32_t WallClockStartTime() { return 0; }
522 inline static void LogThreads(bool on = true) {}
523 inline static void LogTimestamps(bool on = true) {}
524 inline static void LogToDebug(LoggingSeverity min_sev) {}
525 inline static LoggingSeverity GetLogToDebug() {
526 return LoggingSeverity::LS_INFO;
527 }
528 inline static void SetLogToStderr(bool log_to_stderr) {}
529 inline static void AddLogToStream(LogSink* stream, LoggingSeverity min_sev) {}
530 inline static void RemoveLogToStream(LogSink* stream) {}
531 inline static int GetLogToStream(LogSink* stream = nullptr) { return 0; }
532 inline static int GetMinLogSeverity() { return 0; }
533 inline static void ConfigureLogging(const char* params) {}
Karl Wiberg0e884382020-09-22 07:34:45534 static constexpr bool IsNoop(LoggingSeverity severity) { return true; }
Karl Wibergdd7df5c2020-09-22 09:07:53535 template <LoggingSeverity S>
536 static constexpr bool IsNoop() {
537 return IsNoop(S);
538 }
Artem Titov6a4a1462019-11-26 15:24:46539#endif // RTC_LOG_ENABLED()
Jonas Olssond8c50782018-09-07 09:21:28540
Henrik Kjellanderec78f1c2017-06-29 05:52:50541 private:
Tommifef05002018-02-27 12:51:08542 friend class LogMessageForTesting;
Henrik Kjellanderec78f1c2017-06-29 05:52:50543
Artem Titov6a4a1462019-11-26 15:24:46544#if RTC_LOG_ENABLED()
Henrik Kjellanderec78f1c2017-06-29 05:52:50545 // Updates min_sev_ appropriately when debug sinks change.
546 static void UpdateMinLogSeverity();
547
Yves Gerey665174f2018-06-19 13:03:05548// These write out the actual log messages.
Tommie51a0a82018-02-27 14:30:29549#if defined(WEBRTC_ANDROID)
Henrik Kjellanderec78f1c2017-06-29 05:52:50550 static void OutputToDebug(const std::string& msg,
551 LoggingSeverity severity,
Tommie51a0a82018-02-27 14:30:29552 const char* tag);
553#else
554 static void OutputToDebug(const std::string& msg, LoggingSeverity severity);
Artem Titov6a4a1462019-11-26 15:24:46555#endif // defined(WEBRTC_ANDROID)
Henrik Kjellanderec78f1c2017-06-29 05:52:50556
Tommifef05002018-02-27 12:51:08557 // Called from the dtor (or from a test) to append optional extra error
558 // information to the log stream and a newline character.
559 void FinishPrintStream();
560
Henrik Kjellanderec78f1c2017-06-29 05:52:50561 // The severity level of this message
562 LoggingSeverity severity_;
563
Tommie51a0a82018-02-27 14:30:29564#if defined(WEBRTC_ANDROID)
Paulina Hensmanf1e3cb42018-06-20 12:07:05565 // The default Android debug output tag.
Tommie51a0a82018-02-27 14:30:29566 const char* tag_ = "libjingle";
567#endif
Henrik Kjellanderec78f1c2017-06-29 05:52:50568
569 // String data generated in the constructor, that should be appended to
570 // the message before output.
571 std::string extra_;
572
Henrik Kjellanderec78f1c2017-06-29 05:52:50573 // The output streams and their associated severities
Danil Chapovalovb9f69022019-10-21 07:19:10574 static LogSink* streams_;
Henrik Kjellanderec78f1c2017-06-29 05:52:50575
Markus Handellce1ff6f2020-07-08 06:52:48576 // Holds true with high probability if |streams_| is empty, false with high
577 // probability otherwise. Operated on with std::memory_order_relaxed because
Markus Handell531bd0f2020-07-08 08:58:19578 // it's ok to lose or log some additional statements near the instant streams
Markus Handellce1ff6f2020-07-08 06:52:48579 // are added/removed.
580 static std::atomic<bool> streams_empty_;
581
Henrik Kjellanderec78f1c2017-06-29 05:52:50582 // Flags for formatting options
583 static bool thread_, timestamp_;
584
585 // Determines if logs will be directed to stderr in debug mode.
586 static bool log_to_stderr_;
Artem Titov6a4a1462019-11-26 15:24:46587#else // RTC_LOG_ENABLED()
588 // Next methods do nothing; no one will call these functions.
589 inline static void UpdateMinLogSeverity() {}
590#if defined(WEBRTC_ANDROID)
591 inline static void OutputToDebug(const std::string& msg,
592 LoggingSeverity severity,
593 const char* tag) {}
594#else
595 inline static void OutputToDebug(const std::string& msg,
596 LoggingSeverity severity) {}
597#endif // defined(WEBRTC_ANDROID)
598 inline void FinishPrintStream() {}
599#endif // RTC_LOG_ENABLED()
600
601 // The stringbuilder that buffers the formatted message before output
602 rtc::StringBuilder print_stream_;
Henrik Kjellanderec78f1c2017-06-29 05:52:50603
604 RTC_DISALLOW_COPY_AND_ASSIGN(LogMessage);
605};
606
607//////////////////////////////////////////////////////////////////////
608// Logging Helpers
609//////////////////////////////////////////////////////////////////////
610
Karl Wibergdd7df5c2020-09-22 09:07:53611#define RTC_LOG_FILE_LINE(sev, file, line) \
612 ::rtc::webrtc_logging_impl::LogCall() & \
613 ::rtc::webrtc_logging_impl::LogStreamer<>() \
614 << ::rtc::webrtc_logging_impl::LogMetadata(file, line, sev)
Jonas Olsson8a7916b2018-09-06 07:50:23615
Karl Wibergdd7df5c2020-09-22 09:07:53616#define RTC_LOG(sev) \
617 !rtc::LogMessage::IsNoop<::rtc::sev>() && \
618 RTC_LOG_FILE_LINE(::rtc::sev, __FILE__, __LINE__)
Henrik Kjellanderec78f1c2017-06-29 05:52:50619
Karl Wibergcefc4652018-05-23 21:20:38620// The _V version is for when a variable is passed in.
Karl Wibergdd7df5c2020-09-22 09:07:53621#define RTC_LOG_V(sev) \
622 !rtc::LogMessage::IsNoop(sev) && RTC_LOG_FILE_LINE(sev, __FILE__, __LINE__)
Henrik Kjellanderec78f1c2017-06-29 05:52:50623
624// The _F version prefixes the message with the current function name.
625#if (defined(__GNUC__) && !defined(NDEBUG)) || defined(WANT_PRETTY_LOG_F)
Mirko Bonadei8ed8e562017-10-27 07:43:53626#define RTC_LOG_F(sev) RTC_LOG(sev) << __PRETTY_FUNCTION__ << ": "
Yves Gerey665174f2018-06-19 13:03:05627#define RTC_LOG_T_F(sev) \
628 RTC_LOG(sev) << this << ": " << __PRETTY_FUNCTION__ << ": "
Henrik Kjellanderec78f1c2017-06-29 05:52:50629#else
Mirko Bonadei8ed8e562017-10-27 07:43:53630#define RTC_LOG_F(sev) RTC_LOG(sev) << __FUNCTION__ << ": "
Patrik Höglundc2962552017-11-17 13:40:22631#define RTC_LOG_T_F(sev) RTC_LOG(sev) << this << ": " << __FUNCTION__ << ": "
Henrik Kjellanderec78f1c2017-06-29 05:52:50632#endif
633
Jiawei Ou14e5f0b2020-03-04 21:38:02634#define RTC_LOG_CHECK_LEVEL(sev) ::rtc::LogCheckLevel(::rtc::sev)
635#define RTC_LOG_CHECK_LEVEL_V(sev) ::rtc::LogCheckLevel(sev)
Henrik Kjellanderec78f1c2017-06-29 05:52:50636
637inline bool LogCheckLevel(LoggingSeverity sev) {
638 return (LogMessage::GetMinLogSeverity() <= sev);
639}
640
Karl Wiberg92e37962020-09-22 11:22:20641#define RTC_LOG_E(sev, ctx, err) \
642 !rtc::LogMessage::IsNoop<::rtc::sev>() && \
643 ::rtc::webrtc_logging_impl::LogCall() & \
644 ::rtc::webrtc_logging_impl::LogStreamer<>() \
645 << ::rtc::webrtc_logging_impl::LogMetadataErr { \
646 {__FILE__, __LINE__, ::rtc::sev}, ::rtc::ERRCTX_##ctx, (err) \
Jonas Olssona4d87372019-07-05 17:08:33647 }
Henrik Kjellanderec78f1c2017-06-29 05:52:50648
Mirko Bonadei8ed8e562017-10-27 07:43:53649#define RTC_LOG_T(sev) RTC_LOG(sev) << this << ": "
Henrik Kjellanderec78f1c2017-06-29 05:52:50650
Yves Gerey665174f2018-06-19 13:03:05651#define RTC_LOG_ERRNO_EX(sev, err) RTC_LOG_E(sev, ERRNO, err)
652#define RTC_LOG_ERRNO(sev) RTC_LOG_ERRNO_EX(sev, errno)
Henrik Kjellanderec78f1c2017-06-29 05:52:50653
654#if defined(WEBRTC_WIN)
Yves Gerey665174f2018-06-19 13:03:05655#define RTC_LOG_GLE_EX(sev, err) RTC_LOG_E(sev, HRESULT, err)
Karl Wibergcefc4652018-05-23 21:20:38656#define RTC_LOG_GLE(sev) RTC_LOG_GLE_EX(sev, static_cast<int>(GetLastError()))
Yves Gerey665174f2018-06-19 13:03:05657#define RTC_LOG_ERR_EX(sev, err) RTC_LOG_GLE_EX(sev, err)
658#define RTC_LOG_ERR(sev) RTC_LOG_GLE(sev)
Henrik Kjellanderec78f1c2017-06-29 05:52:50659#elif defined(__native_client__) && __native_client__
Yves Gerey665174f2018-06-19 13:03:05660#define RTC_LOG_ERR_EX(sev, err) RTC_LOG(sev)
661#define RTC_LOG_ERR(sev) RTC_LOG(sev)
Henrik Kjellanderec78f1c2017-06-29 05:52:50662#elif defined(WEBRTC_POSIX)
Yves Gerey665174f2018-06-19 13:03:05663#define RTC_LOG_ERR_EX(sev, err) RTC_LOG_ERRNO_EX(sev, err)
664#define RTC_LOG_ERR(sev) RTC_LOG_ERRNO(sev)
Henrik Kjellanderec78f1c2017-06-29 05:52:50665#endif // WEBRTC_WIN
666
Karl Wibergcefc4652018-05-23 21:20:38667#ifdef WEBRTC_ANDROID
668
669namespace webrtc_logging_impl {
670// TODO(kwiberg): Replace these with absl::string_view.
Yves Gerey665174f2018-06-19 13:03:05671inline const char* AdaptString(const char* str) {
672 return str;
673}
674inline const char* AdaptString(const std::string& str) {
675 return str.c_str();
676}
Karl Wibergcefc4652018-05-23 21:20:38677} // namespace webrtc_logging_impl
678
Karl Wiberg92e37962020-09-22 11:22:20679#define RTC_LOG_TAG(sev, tag) \
680 !rtc::LogMessage::IsNoop(sev) && \
681 ::rtc::webrtc_logging_impl::LogCall() & \
682 ::rtc::webrtc_logging_impl::LogStreamer<>() \
683 << ::rtc::webrtc_logging_impl::LogMetadataTag { \
684 sev, ::rtc::webrtc_logging_impl::AdaptString(tag) \
Jonas Olssona4d87372019-07-05 17:08:33685 }
Karl Wibergcefc4652018-05-23 21:20:38686
Tommie51a0a82018-02-27 14:30:29687#else
Karl Wibergcefc4652018-05-23 21:20:38688
Tommie51a0a82018-02-27 14:30:29689// DEPRECATED. This macro is only intended for Android.
Karl Wibergcefc4652018-05-23 21:20:38690#define RTC_LOG_TAG(sev, tag) RTC_LOG_V(sev)
691
Tommie51a0a82018-02-27 14:30:29692#endif
Henrik Kjellanderec78f1c2017-06-29 05:52:50693
Fredrik Solenbergb3d7cac2017-11-17 14:22:37694// The RTC_DLOG macros are equivalent to their RTC_LOG counterparts except that
695// they only generate code in debug builds.
696#if RTC_DLOG_IS_ON
697#define RTC_DLOG(sev) RTC_LOG(sev)
698#define RTC_DLOG_V(sev) RTC_LOG_V(sev)
699#define RTC_DLOG_F(sev) RTC_LOG_F(sev)
700#else
Mirko Bonadeif1df04b2020-03-23 18:53:23701#define RTC_DLOG_EAT_STREAM_PARAMS() \
702 while (false) \
703 ::rtc::webrtc_logging_impl::LogMessageVoidify() & \
704 (::rtc::webrtc_logging_impl::LogStreamer<>())
Karl Wibergcefc4652018-05-23 21:20:38705#define RTC_DLOG(sev) RTC_DLOG_EAT_STREAM_PARAMS()
706#define RTC_DLOG_V(sev) RTC_DLOG_EAT_STREAM_PARAMS()
707#define RTC_DLOG_F(sev) RTC_DLOG_EAT_STREAM_PARAMS()
Fredrik Solenbergb3d7cac2017-11-17 14:22:37708#endif
Henrik Kjellanderec78f1c2017-06-29 05:52:50709
Henrik Kjellanderec78f1c2017-06-29 05:52:50710} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26711
Mirko Bonadei92ea95e2017-09-15 04:47:31712#endif // RTC_BASE_LOGGING_H_