blob: eaa84e893aa1bed4e7e30b3665ffe7977d60cccb [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
Markus Handellce1ff6f2020-07-08 08:52:48 +020049#include <atomic>
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
Danil Chapovalove9041612021-02-22 12:43:39 +010054#include "absl/base/attributes.h"
Sebastian Janssonf4e085a2019-05-20 18:34:07 +020055#include "absl/meta/type_traits.h"
Jonas Olssonf2ce37c2018-09-12 15:32:47 +020056#include "absl/strings/string_view.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
Artem Titov6a4a1462019-11-26 16:24:46 +010066#if defined(RTC_DISABLE_LOGGING)
67#define RTC_LOG_ENABLED() 0
68#else
69#define RTC_LOG_ENABLED() 1
70#endif
71
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020072namespace rtc {
73
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020074//////////////////////////////////////////////////////////////////////
75
76// Note that the non-standard LoggingSeverity aliases exist because they are
77// still in broad use. The meanings of the levels are:
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020078// LS_VERBOSE: This level is for data which we do not want to appear in the
79// normal debug log, but should appear in diagnostic logs.
80// LS_INFO: Chatty level used in debugging for all sorts of things, the default
81// in debug builds.
82// LS_WARNING: Something that may warrant investigation.
83// LS_ERROR: Something that should not have occurred.
84// LS_NONE: Don't log.
85enum LoggingSeverity {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020086 LS_VERBOSE,
87 LS_INFO,
88 LS_WARNING,
89 LS_ERROR,
90 LS_NONE,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020091};
92
93// LogErrorContext assists in interpreting the meaning of an error value.
94enum LogErrorContext {
95 ERRCTX_NONE,
Jonas Olssona4d87372019-07-05 19:08:33 +020096 ERRCTX_ERRNO, // System-local errno
97 ERRCTX_HRESULT, // Windows HRESULT
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020098
99 // Abbreviations for LOG_E macro
Jonas Olssona4d87372019-07-05 19:08:33 +0200100 ERRCTX_EN = ERRCTX_ERRNO, // LOG_E(sev, EN, x)
101 ERRCTX_HR = ERRCTX_HRESULT, // LOG_E(sev, HR, x)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200102};
103
Danil Chapovalovb9f69022019-10-21 09:19:10 +0200104class LogMessage;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200105// Virtual sink interface that can receive log messages.
106class LogSink {
107 public:
108 LogSink() {}
109 virtual ~LogSink() {}
Paulina Hensmanf1e3cb42018-06-20 14:07:05 +0200110 virtual void OnLogMessage(const std::string& msg,
111 LoggingSeverity severity,
112 const char* tag);
Jiawei Ou3ea18782018-10-31 23:14:24 -0700113 virtual void OnLogMessage(const std::string& message,
114 LoggingSeverity severity);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200115 virtual void OnLogMessage(const std::string& message) = 0;
Danil Chapovalovb9f69022019-10-21 09:19:10 +0200116
117 private:
118 friend class ::rtc::LogMessage;
Artem Titov6a4a1462019-11-26 16:24:46 +0100119#if RTC_LOG_ENABLED()
Danil Chapovalovb9f69022019-10-21 09:19:10 +0200120 // Members for LogMessage class to keep linked list of the registered sinks.
121 LogSink* next_ = nullptr;
122 LoggingSeverity min_severity_;
Artem Titov6a4a1462019-11-26 16:24:46 +0100123#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200124};
125
Karl Wibergcefc4652018-05-23 23:20:38 +0200126namespace webrtc_logging_impl {
127
128class LogMetadata {
129 public:
130 LogMetadata(const char* file, int line, LoggingSeverity severity)
131 : file_(file),
132 line_and_sev_(static_cast<uint32_t>(line) << 3 | severity) {}
133 LogMetadata() = default;
134
135 const char* File() const { return file_; }
136 int Line() const { return line_and_sev_ >> 3; }
137 LoggingSeverity Severity() const {
138 return static_cast<LoggingSeverity>(line_and_sev_ & 0x7);
139 }
140
141 private:
142 const char* file_;
143
144 // Line number and severity, the former in the most significant 29 bits, the
145 // latter in the least significant 3 bits. (This is an optimization; since
146 // both numbers are usually compile-time constants, this way we can load them
147 // both with a single instruction.)
148 uint32_t line_and_sev_;
149};
150static_assert(std::is_trivial<LogMetadata>::value, "");
151
152struct LogMetadataErr {
153 LogMetadata meta;
154 LogErrorContext err_ctx;
155 int err;
156};
157
158#ifdef WEBRTC_ANDROID
159struct LogMetadataTag {
160 LoggingSeverity severity;
161 const char* tag;
162};
163#endif
164
165enum class LogArgType : int8_t {
166 kEnd = 0,
167 kInt,
168 kLong,
169 kLongLong,
170 kUInt,
171 kULong,
172 kULongLong,
173 kDouble,
174 kLongDouble,
175 kCharP,
176 kStdString,
Jonas Olssonf2ce37c2018-09-12 15:32:47 +0200177 kStringView,
Karl Wibergcefc4652018-05-23 23:20:38 +0200178 kVoidP,
179 kLogMetadata,
180 kLogMetadataErr,
181#ifdef WEBRTC_ANDROID
182 kLogMetadataTag,
183#endif
184};
185
186// Wrapper for log arguments. Only ever make values of this type with the
187// MakeVal() functions.
188template <LogArgType N, typename T>
189struct Val {
190 static constexpr LogArgType Type() { return N; }
191 T GetVal() const { return val; }
192 T val;
193};
194
Sebastian Janssonb1138622019-04-11 16:48:15 +0200195// Case for when we need to construct a temp string and then print that.
196// (We can't use Val<CheckArgType::kStdString, const std::string*>
197// because we need somewhere to store the temp string.)
198struct ToStringVal {
Karl Wibergcefc4652018-05-23 23:20:38 +0200199 static constexpr LogArgType Type() { return LogArgType::kStdString; }
200 const std::string* GetVal() const { return &val; }
201 std::string val;
202};
203
204inline Val<LogArgType::kInt, int> MakeVal(int x) {
205 return {x};
206}
207inline Val<LogArgType::kLong, long> MakeVal(long x) {
208 return {x};
209}
210inline Val<LogArgType::kLongLong, long long> MakeVal(long long x) {
211 return {x};
212}
213inline Val<LogArgType::kUInt, unsigned int> MakeVal(unsigned int x) {
214 return {x};
215}
216inline Val<LogArgType::kULong, unsigned long> MakeVal(unsigned long x) {
217 return {x};
218}
219inline Val<LogArgType::kULongLong, unsigned long long> MakeVal(
220 unsigned long long x) {
221 return {x};
222}
223
224inline Val<LogArgType::kDouble, double> MakeVal(double x) {
225 return {x};
226}
227inline Val<LogArgType::kLongDouble, long double> MakeVal(long double x) {
228 return {x};
229}
230
231inline Val<LogArgType::kCharP, const char*> MakeVal(const char* x) {
232 return {x};
233}
234inline Val<LogArgType::kStdString, const std::string*> MakeVal(
235 const std::string& x) {
236 return {&x};
237}
Jonas Olssonf2ce37c2018-09-12 15:32:47 +0200238inline Val<LogArgType::kStringView, const absl::string_view*> MakeVal(
239 const absl::string_view& x) {
240 return {&x};
241}
Karl Wibergcefc4652018-05-23 23:20:38 +0200242
243inline Val<LogArgType::kVoidP, const void*> MakeVal(const void* x) {
244 return {x};
245}
246
247inline Val<LogArgType::kLogMetadata, LogMetadata> MakeVal(
248 const LogMetadata& x) {
249 return {x};
250}
251inline Val<LogArgType::kLogMetadataErr, LogMetadataErr> MakeVal(
252 const LogMetadataErr& x) {
253 return {x};
254}
255
Amit Hilbuch10c5a932019-03-18 13:09:18 -0700256// The enum class types are not implicitly convertible to arithmetic types.
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200257template <typename T,
258 absl::enable_if_t<std::is_enum<T>::value &&
259 !std::is_arithmetic<T>::value>* = nullptr>
260inline decltype(MakeVal(std::declval<absl::underlying_type_t<T>>())) MakeVal(
261 T x) {
262 return {static_cast<absl::underlying_type_t<T>>(x)};
Amit Hilbuch10c5a932019-03-18 13:09:18 -0700263}
264
Karl Wibergcefc4652018-05-23 23:20:38 +0200265#ifdef WEBRTC_ANDROID
266inline Val<LogArgType::kLogMetadataTag, LogMetadataTag> MakeVal(
267 const LogMetadataTag& x) {
268 return {x};
269}
270#endif
271
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200272template <typename T, class = void>
Sebastian Janssonb1138622019-04-11 16:48:15 +0200273struct has_to_log_string : std::false_type {};
274template <typename T>
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200275struct has_to_log_string<T, decltype(ToLogString(std::declval<T>()))>
Sebastian Janssonb1138622019-04-11 16:48:15 +0200276 : std::true_type {};
277
Karl Wibergcefc4652018-05-23 23:20:38 +0200278// Handle arbitrary types other than the above by falling back to stringstream.
279// TODO(bugs.webrtc.org/9278): Get rid of this overload when callers don't need
280// it anymore. No in-tree caller does, but some external callers still do.
281template <
282 typename T,
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200283 typename T1 = absl::decay_t<T>,
284 absl::enable_if_t<std::is_class<T1>::value &&
285 !std::is_same<T1, std::string>::value &&
286 !std::is_same<T1, LogMetadata>::value &&
287 !has_to_log_string<T1>::value &&
Karl Wibergcefc4652018-05-23 23:20:38 +0200288#ifdef WEBRTC_ANDROID
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200289 !std::is_same<T1, LogMetadataTag>::value &&
Karl Wibergcefc4652018-05-23 23:20:38 +0200290#endif
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200291 !std::is_same<T1, LogMetadataErr>::value>* = nullptr>
Sebastian Janssonb1138622019-04-11 16:48:15 +0200292ToStringVal MakeVal(const T& x) {
Karl Wibergcefc4652018-05-23 23:20:38 +0200293 std::ostringstream os; // no-presubmit-check TODO(webrtc:8982)
294 os << x;
295 return {os.str()};
296}
297
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200298template <typename T, absl::enable_if_t<has_to_log_string<T>::value>* = nullptr>
Sebastian Janssonb1138622019-04-11 16:48:15 +0200299ToStringVal MakeVal(const T& x) {
300 return {ToLogString(x)};
301}
302
Artem Titov6a4a1462019-11-26 16:24:46 +0100303#if RTC_LOG_ENABLED()
Karl Wibergcefc4652018-05-23 23:20:38 +0200304void Log(const LogArgType* fmt, ...);
Artem Titov6a4a1462019-11-26 16:24:46 +0100305#else
306inline void Log(const LogArgType* fmt, ...) {
307 // Do nothing, shouldn't be invoked
308}
309#endif
Karl Wibergcefc4652018-05-23 23:20:38 +0200310
311// Ephemeral type that represents the result of the logging << operator.
312template <typename... Ts>
313class LogStreamer;
314
315// Base case: Before the first << argument.
316template <>
317class LogStreamer<> final {
318 public:
Amit Hilbuch10c5a932019-03-18 13:09:18 -0700319 template <typename U,
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200320 typename V = decltype(MakeVal(std::declval<U>())),
321 absl::enable_if_t<std::is_arithmetic<U>::value ||
322 std::is_enum<U>::value>* = nullptr>
323 RTC_FORCE_INLINE LogStreamer<V> operator<<(U arg) const {
324 return LogStreamer<V>(MakeVal(arg), this);
Karl Wibergcefc4652018-05-23 23:20:38 +0200325 }
326
Amit Hilbuch10c5a932019-03-18 13:09:18 -0700327 template <typename U,
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200328 typename V = decltype(MakeVal(std::declval<U>())),
329 absl::enable_if_t<!std::is_arithmetic<U>::value &&
330 !std::is_enum<U>::value>* = nullptr>
331 RTC_FORCE_INLINE LogStreamer<V> operator<<(const U& arg) const {
332 return LogStreamer<V>(MakeVal(arg), this);
Karl Wibergcefc4652018-05-23 23:20:38 +0200333 }
334
335 template <typename... Us>
336 RTC_FORCE_INLINE static void Call(const Us&... args) {
337 static constexpr LogArgType t[] = {Us::Type()..., LogArgType::kEnd};
338 Log(t, args.GetVal()...);
339 }
340};
341
342// Inductive case: We've already seen at least one << argument. The most recent
343// one had type `T`, and the earlier ones had types `Ts`.
344template <typename T, typename... Ts>
345class LogStreamer<T, Ts...> final {
346 public:
347 RTC_FORCE_INLINE LogStreamer(T arg, const LogStreamer<Ts...>* prior)
348 : arg_(arg), prior_(prior) {}
349
Amit Hilbuch10c5a932019-03-18 13:09:18 -0700350 template <typename U,
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200351 typename V = decltype(MakeVal(std::declval<U>())),
352 absl::enable_if_t<std::is_arithmetic<U>::value ||
353 std::is_enum<U>::value>* = nullptr>
354 RTC_FORCE_INLINE LogStreamer<V, T, Ts...> operator<<(U arg) const {
355 return LogStreamer<V, T, Ts...>(MakeVal(arg), this);
Karl Wibergcefc4652018-05-23 23:20:38 +0200356 }
357
Amit Hilbuch10c5a932019-03-18 13:09:18 -0700358 template <typename U,
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200359 typename V = decltype(MakeVal(std::declval<U>())),
360 absl::enable_if_t<!std::is_arithmetic<U>::value &&
361 !std::is_enum<U>::value>* = nullptr>
362 RTC_FORCE_INLINE LogStreamer<V, T, Ts...> operator<<(const U& arg) const {
363 return LogStreamer<V, T, Ts...>(MakeVal(arg), this);
Karl Wibergcefc4652018-05-23 23:20:38 +0200364 }
365
366 template <typename... Us>
367 RTC_FORCE_INLINE void Call(const Us&... args) const {
368 prior_->Call(arg_, args...);
369 }
370
371 private:
372 // The most recent argument.
373 T arg_;
374
375 // Earlier arguments.
376 const LogStreamer<Ts...>* prior_;
377};
378
379class LogCall final {
380 public:
381 // This can be any binary operator with precedence lower than <<.
Artem Titov6a4a1462019-11-26 16:24:46 +0100382 // We return bool here to be able properly remove logging if
383 // RTC_DISABLE_LOGGING is defined.
Karl Wibergcefc4652018-05-23 23:20:38 +0200384 template <typename... Ts>
Artem Titov6a4a1462019-11-26 16:24:46 +0100385 RTC_FORCE_INLINE bool operator&(const LogStreamer<Ts...>& streamer) {
Karl Wibergcefc4652018-05-23 23:20:38 +0200386 streamer.Call();
Artem Titov6a4a1462019-11-26 16:24:46 +0100387 return true;
Karl Wibergcefc4652018-05-23 23:20:38 +0200388 }
389};
390
Mirko Bonadeif1df04b2020-03-23 19:53:23 +0100391// This class is used to explicitly ignore values in the conditional
392// logging macros. This avoids compiler warnings like "value computed
393// is not used" and "statement has no effect".
394class LogMessageVoidify {
395 public:
396 LogMessageVoidify() = default;
397 // This has to be an operator with a precedence lower than << but
398 // higher than ?:
399 template <typename... Ts>
400 void operator&(LogStreamer<Ts...>&& streamer) {}
401};
402
Karl Wibergcefc4652018-05-23 23:20:38 +0200403} // namespace webrtc_logging_impl
404
405// Direct use of this class is deprecated; please use the logging macros
406// instead.
407// TODO(bugs.webrtc.org/9278): Move this class to an unnamed namespace in the
408// .cc file.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200409class LogMessage {
410 public:
Karl Wiberg1ffb3742018-05-04 15:04:48 +0200411 // Same as the above, but using a compile-time constant for the logging
412 // severity. This saves space at the call site, since passing an empty struct
413 // is generally the same as not passing an argument at all.
414 template <LoggingSeverity S>
415 RTC_NO_INLINE LogMessage(const char* file,
416 int line,
417 std::integral_constant<LoggingSeverity, S>)
418 : LogMessage(file, line, S) {}
419
Artem Titov6a4a1462019-11-26 16:24:46 +0100420#if RTC_LOG_ENABLED()
421 LogMessage(const char* file, int line, LoggingSeverity sev);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200422 LogMessage(const char* file,
423 int line,
424 LoggingSeverity sev,
Karl Wibergab4f1c12018-05-04 10:42:28 +0200425 LogErrorContext err_ctx,
426 int err);
Tommie51a0a82018-02-27 15:30:29 +0100427#if defined(WEBRTC_ANDROID)
428 LogMessage(const char* file, int line, LoggingSeverity sev, const char* tag);
429#endif
Tommie51a0a82018-02-27 15:30:29 +0100430 // DEPRECATED - DO NOT USE - PLEASE USE THE MACROS INSTEAD OF THE CLASS.
431 // Android code should use the 'const char*' version since tags are static
432 // and we want to avoid allocating a std::string copy per log line.
Danil Chapovalove9041612021-02-22 12:43:39 +0100433 ABSL_DEPRECATED("Use RTC_LOG macros instead of accessing this class directly")
Yves Gerey665174f2018-06-19 15:03:05 +0200434 LogMessage(const char* file,
435 int line,
436 LoggingSeverity sev,
Philip Eliasson278aa422018-02-26 14:54:45 +0000437 const std::string& tag);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200438 ~LogMessage();
439
Byoungchan Lee14af7622022-01-12 05:24:58 +0900440 LogMessage(const LogMessage&) = delete;
441 LogMessage& operator=(const LogMessage&) = delete;
442
Karl Wibergcefc4652018-05-23 23:20:38 +0200443 void AddTag(const char* tag);
Jonas Olssond8c50782018-09-07 11:21:28 +0200444 rtc::StringBuilder& stream();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200445 // Returns the time at which this function was called for the first time.
446 // The time will be used as the logging start time.
447 // If this is not called externally, the LogMessage ctor also calls it, in
448 // which case the logging start time will be the time of the first LogMessage
449 // instance is created.
450 static int64_t LogStartTime();
Artem Titov96e3b992021-07-26 16:03:14 +0200451 // Returns the wall clock equivalent of `LogStartTime`, in seconds from the
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200452 // epoch.
453 static uint32_t WallClockStartTime();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200454 // LogThreads: Display the thread identifier of the current thread
455 static void LogThreads(bool on = true);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200456 // LogTimestamps: Display the elapsed time of the program
457 static void LogTimestamps(bool on = true);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200458 // These are the available logging channels
459 // Debug: Debug console on Windows, otherwise stderr
460 static void LogToDebug(LoggingSeverity min_sev);
Jonas Olsson2b6f1352018-02-15 11:57:03 +0100461 static LoggingSeverity GetLogToDebug();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200462 // Sets whether logs will be directed to stderr in debug mode.
463 static void SetLogToStderr(bool log_to_stderr);
Danil Chapovalovb9f69022019-10-21 09:19:10 +0200464 // Stream: Any non-blocking stream interface.
Artem Titov96e3b992021-07-26 16:03:14 +0200465 // Installs the `stream` to collect logs with severtiy `min_sev` or higher.
466 // `stream` must live until deinstalled by RemoveLogToStream.
467 // If `stream` is the first stream added to the system, we might miss some
Markus Handell531bd0f2020-07-08 10:58:19 +0200468 // early concurrent log statement happening from another thread happening near
469 // this instant.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200470 static void AddLogToStream(LogSink* stream, LoggingSeverity min_sev);
Markus Handell531bd0f2020-07-08 10:58:19 +0200471 // Removes the specified stream, without destroying it. When the method
Artem Titov96e3b992021-07-26 16:03:14 +0200472 // has completed, it's guaranteed that `stream` will receive no more logging
Markus Handell531bd0f2020-07-08 10:58:19 +0200473 // calls.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200474 static void RemoveLogToStream(LogSink* stream);
Danil Chapovalovb9f69022019-10-21 09:19:10 +0200475 // Returns the severity for the specified stream, of if none is specified,
476 // the minimum stream severity.
477 static int GetLogToStream(LogSink* stream = nullptr);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200478 // Testing against MinLogSeverity allows code to avoid potentially expensive
479 // logging operations by pre-checking the logging level.
Jonas Olsson2b6f1352018-02-15 11:57:03 +0100480 static int GetMinLogSeverity();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200481 // Parses the provided parameter stream to configure the options above.
482 // Useful for configuring logging from the command line.
483 static void ConfigureLogging(const char* params);
Artem Titov96e3b992021-07-26 16:03:14 +0200484 // Checks the current global debug severity and if the `streams_` collection
485 // is empty. If `severity` is smaller than the global severity and if the
486 // `streams_` collection is empty, the LogMessage will be considered a noop
Jonas Olssond8c50782018-09-07 11:21:28 +0200487 // LogMessage.
488 static bool IsNoop(LoggingSeverity severity);
Karl Wibergdd7df5c2020-09-22 11:07:53 +0200489 // Version of IsNoop that uses fewer instructions at the call site, since the
490 // caller doesn't have to pass an argument.
491 template <LoggingSeverity S>
492 RTC_NO_INLINE static bool IsNoop() {
493 return IsNoop(S);
494 }
Artem Titov6a4a1462019-11-26 16:24:46 +0100495#else
496 // Next methods do nothing; no one will call these functions.
497 LogMessage(const char* file, int line, LoggingSeverity sev) {}
498 LogMessage(const char* file,
499 int line,
500 LoggingSeverity sev,
501 LogErrorContext err_ctx,
502 int err) {}
503#if defined(WEBRTC_ANDROID)
504 LogMessage(const char* file, int line, LoggingSeverity sev, const char* tag) {
505 }
506#endif
507 // DEPRECATED - DO NOT USE - PLEASE USE THE MACROS INSTEAD OF THE CLASS.
508 // Android code should use the 'const char*' version since tags are static
509 // and we want to avoid allocating a std::string copy per log line.
Danil Chapovalove9041612021-02-22 12:43:39 +0100510 ABSL_DEPRECATED("Use RTC_LOG macros instead of accessing this class directly")
Artem Titov6a4a1462019-11-26 16:24:46 +0100511 LogMessage(const char* file,
512 int line,
513 LoggingSeverity sev,
514 const std::string& tag) {}
515 ~LogMessage() = default;
516
517 inline void AddTag(const char* tag) {}
518 inline rtc::StringBuilder& stream() { return print_stream_; }
519 inline static int64_t LogStartTime() { return 0; }
520 inline static uint32_t WallClockStartTime() { return 0; }
521 inline static void LogThreads(bool on = true) {}
522 inline static void LogTimestamps(bool on = true) {}
523 inline static void LogToDebug(LoggingSeverity min_sev) {}
524 inline static LoggingSeverity GetLogToDebug() {
525 return LoggingSeverity::LS_INFO;
526 }
527 inline static void SetLogToStderr(bool log_to_stderr) {}
528 inline static void AddLogToStream(LogSink* stream, LoggingSeverity min_sev) {}
529 inline static void RemoveLogToStream(LogSink* stream) {}
530 inline static int GetLogToStream(LogSink* stream = nullptr) { return 0; }
531 inline static int GetMinLogSeverity() { return 0; }
532 inline static void ConfigureLogging(const char* params) {}
Karl Wiberg0e884382020-09-22 09:34:45 +0200533 static constexpr bool IsNoop(LoggingSeverity severity) { return true; }
Karl Wibergdd7df5c2020-09-22 11:07:53 +0200534 template <LoggingSeverity S>
535 static constexpr bool IsNoop() {
536 return IsNoop(S);
537 }
Artem Titov6a4a1462019-11-26 16:24:46 +0100538#endif // RTC_LOG_ENABLED()
Jonas Olssond8c50782018-09-07 11:21:28 +0200539
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200540 private:
Tommifef05002018-02-27 13:51:08 +0100541 friend class LogMessageForTesting;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200542
Artem Titov6a4a1462019-11-26 16:24:46 +0100543#if RTC_LOG_ENABLED()
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200544 // Updates min_sev_ appropriately when debug sinks change.
545 static void UpdateMinLogSeverity();
546
Yves Gerey665174f2018-06-19 15:03:05 +0200547// These write out the actual log messages.
Tommie51a0a82018-02-27 15:30:29 +0100548#if defined(WEBRTC_ANDROID)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200549 static void OutputToDebug(const std::string& msg,
550 LoggingSeverity severity,
Tommie51a0a82018-02-27 15:30:29 +0100551 const char* tag);
552#else
553 static void OutputToDebug(const std::string& msg, LoggingSeverity severity);
Artem Titov6a4a1462019-11-26 16:24:46 +0100554#endif // defined(WEBRTC_ANDROID)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200555
Tommifef05002018-02-27 13:51:08 +0100556 // Called from the dtor (or from a test) to append optional extra error
557 // information to the log stream and a newline character.
558 void FinishPrintStream();
559
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200560 // The severity level of this message
561 LoggingSeverity severity_;
562
Tommie51a0a82018-02-27 15:30:29 +0100563#if defined(WEBRTC_ANDROID)
Paulina Hensmanf1e3cb42018-06-20 14:07:05 +0200564 // The default Android debug output tag.
Tommie51a0a82018-02-27 15:30:29 +0100565 const char* tag_ = "libjingle";
566#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200567
568 // String data generated in the constructor, that should be appended to
569 // the message before output.
570 std::string extra_;
571
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200572 // The output streams and their associated severities
Danil Chapovalovb9f69022019-10-21 09:19:10 +0200573 static LogSink* streams_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200574
Artem Titov96e3b992021-07-26 16:03:14 +0200575 // Holds true with high probability if `streams_` is empty, false with high
Markus Handellce1ff6f2020-07-08 08:52:48 +0200576 // probability otherwise. Operated on with std::memory_order_relaxed because
Markus Handell531bd0f2020-07-08 10:58:19 +0200577 // it's ok to lose or log some additional statements near the instant streams
Markus Handellce1ff6f2020-07-08 08:52:48 +0200578 // are added/removed.
579 static std::atomic<bool> streams_empty_;
580
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200581 // Flags for formatting options
582 static bool thread_, timestamp_;
583
584 // Determines if logs will be directed to stderr in debug mode.
585 static bool log_to_stderr_;
Artem Titov6a4a1462019-11-26 16:24:46 +0100586#else // RTC_LOG_ENABLED()
587 // Next methods do nothing; no one will call these functions.
588 inline static void UpdateMinLogSeverity() {}
589#if defined(WEBRTC_ANDROID)
590 inline static void OutputToDebug(const std::string& msg,
591 LoggingSeverity severity,
592 const char* tag) {}
593#else
594 inline static void OutputToDebug(const std::string& msg,
595 LoggingSeverity severity) {}
596#endif // defined(WEBRTC_ANDROID)
597 inline void FinishPrintStream() {}
598#endif // RTC_LOG_ENABLED()
599
600 // The stringbuilder that buffers the formatted message before output
601 rtc::StringBuilder print_stream_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200602};
603
604//////////////////////////////////////////////////////////////////////
605// Logging Helpers
606//////////////////////////////////////////////////////////////////////
607
Karl Wibergdd7df5c2020-09-22 11:07:53 +0200608#define RTC_LOG_FILE_LINE(sev, file, line) \
609 ::rtc::webrtc_logging_impl::LogCall() & \
610 ::rtc::webrtc_logging_impl::LogStreamer<>() \
611 << ::rtc::webrtc_logging_impl::LogMetadata(file, line, sev)
Jonas Olsson8a7916b2018-09-06 09:50:23 +0200612
Karl Wibergdd7df5c2020-09-22 11:07:53 +0200613#define RTC_LOG(sev) \
614 !rtc::LogMessage::IsNoop<::rtc::sev>() && \
615 RTC_LOG_FILE_LINE(::rtc::sev, __FILE__, __LINE__)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200616
Karl Wibergcefc4652018-05-23 23:20:38 +0200617// The _V version is for when a variable is passed in.
Karl Wibergdd7df5c2020-09-22 11:07:53 +0200618#define RTC_LOG_V(sev) \
619 !rtc::LogMessage::IsNoop(sev) && RTC_LOG_FILE_LINE(sev, __FILE__, __LINE__)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200620
621// The _F version prefixes the message with the current function name.
622#if (defined(__GNUC__) && !defined(NDEBUG)) || defined(WANT_PRETTY_LOG_F)
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200623#define RTC_LOG_F(sev) RTC_LOG(sev) << __PRETTY_FUNCTION__ << ": "
Yves Gerey665174f2018-06-19 15:03:05 +0200624#define RTC_LOG_T_F(sev) \
625 RTC_LOG(sev) << this << ": " << __PRETTY_FUNCTION__ << ": "
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200626#else
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200627#define RTC_LOG_F(sev) RTC_LOG(sev) << __FUNCTION__ << ": "
Patrik Höglundc2962552017-11-17 13:40:22 +0000628#define RTC_LOG_T_F(sev) RTC_LOG(sev) << this << ": " << __FUNCTION__ << ": "
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200629#endif
630
Jiawei Ou14e5f0b2020-03-04 13:38:02 -0800631#define RTC_LOG_CHECK_LEVEL(sev) ::rtc::LogCheckLevel(::rtc::sev)
632#define RTC_LOG_CHECK_LEVEL_V(sev) ::rtc::LogCheckLevel(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200633
634inline bool LogCheckLevel(LoggingSeverity sev) {
635 return (LogMessage::GetMinLogSeverity() <= sev);
636}
637
Karl Wiberg92e37962020-09-22 13:22:20 +0200638#define RTC_LOG_E(sev, ctx, err) \
639 !rtc::LogMessage::IsNoop<::rtc::sev>() && \
640 ::rtc::webrtc_logging_impl::LogCall() & \
641 ::rtc::webrtc_logging_impl::LogStreamer<>() \
642 << ::rtc::webrtc_logging_impl::LogMetadataErr { \
643 {__FILE__, __LINE__, ::rtc::sev}, ::rtc::ERRCTX_##ctx, (err) \
Jonas Olssona4d87372019-07-05 19:08:33 +0200644 }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200645
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200646#define RTC_LOG_T(sev) RTC_LOG(sev) << this << ": "
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200647
Yves Gerey665174f2018-06-19 15:03:05 +0200648#define RTC_LOG_ERRNO_EX(sev, err) RTC_LOG_E(sev, ERRNO, err)
649#define RTC_LOG_ERRNO(sev) RTC_LOG_ERRNO_EX(sev, errno)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200650
651#if defined(WEBRTC_WIN)
Yves Gerey665174f2018-06-19 15:03:05 +0200652#define RTC_LOG_GLE_EX(sev, err) RTC_LOG_E(sev, HRESULT, err)
Karl Wibergcefc4652018-05-23 23:20:38 +0200653#define RTC_LOG_GLE(sev) RTC_LOG_GLE_EX(sev, static_cast<int>(GetLastError()))
Yves Gerey665174f2018-06-19 15:03:05 +0200654#define RTC_LOG_ERR_EX(sev, err) RTC_LOG_GLE_EX(sev, err)
655#define RTC_LOG_ERR(sev) RTC_LOG_GLE(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200656#elif defined(__native_client__) && __native_client__
Yves Gerey665174f2018-06-19 15:03:05 +0200657#define RTC_LOG_ERR_EX(sev, err) RTC_LOG(sev)
658#define RTC_LOG_ERR(sev) RTC_LOG(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200659#elif defined(WEBRTC_POSIX)
Yves Gerey665174f2018-06-19 15:03:05 +0200660#define RTC_LOG_ERR_EX(sev, err) RTC_LOG_ERRNO_EX(sev, err)
661#define RTC_LOG_ERR(sev) RTC_LOG_ERRNO(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200662#endif // WEBRTC_WIN
663
Karl Wibergcefc4652018-05-23 23:20:38 +0200664#ifdef WEBRTC_ANDROID
665
666namespace webrtc_logging_impl {
667// TODO(kwiberg): Replace these with absl::string_view.
Yves Gerey665174f2018-06-19 15:03:05 +0200668inline const char* AdaptString(const char* str) {
669 return str;
670}
671inline const char* AdaptString(const std::string& str) {
672 return str.c_str();
673}
Karl Wibergcefc4652018-05-23 23:20:38 +0200674} // namespace webrtc_logging_impl
675
Karl Wiberg92e37962020-09-22 13:22:20 +0200676#define RTC_LOG_TAG(sev, tag) \
677 !rtc::LogMessage::IsNoop(sev) && \
678 ::rtc::webrtc_logging_impl::LogCall() & \
679 ::rtc::webrtc_logging_impl::LogStreamer<>() \
680 << ::rtc::webrtc_logging_impl::LogMetadataTag { \
681 sev, ::rtc::webrtc_logging_impl::AdaptString(tag) \
Jonas Olssona4d87372019-07-05 19:08:33 +0200682 }
Karl Wibergcefc4652018-05-23 23:20:38 +0200683
Tommie51a0a82018-02-27 15:30:29 +0100684#else
Karl Wibergcefc4652018-05-23 23:20:38 +0200685
Tommie51a0a82018-02-27 15:30:29 +0100686// DEPRECATED. This macro is only intended for Android.
Karl Wibergcefc4652018-05-23 23:20:38 +0200687#define RTC_LOG_TAG(sev, tag) RTC_LOG_V(sev)
688
Tommie51a0a82018-02-27 15:30:29 +0100689#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200690
Fredrik Solenbergb3d7cac2017-11-17 15:22:37 +0100691// The RTC_DLOG macros are equivalent to their RTC_LOG counterparts except that
692// they only generate code in debug builds.
693#if RTC_DLOG_IS_ON
694#define RTC_DLOG(sev) RTC_LOG(sev)
695#define RTC_DLOG_V(sev) RTC_LOG_V(sev)
696#define RTC_DLOG_F(sev) RTC_LOG_F(sev)
697#else
Mirko Bonadeif1df04b2020-03-23 19:53:23 +0100698#define RTC_DLOG_EAT_STREAM_PARAMS() \
699 while (false) \
700 ::rtc::webrtc_logging_impl::LogMessageVoidify() & \
701 (::rtc::webrtc_logging_impl::LogStreamer<>())
Karl Wibergcefc4652018-05-23 23:20:38 +0200702#define RTC_DLOG(sev) RTC_DLOG_EAT_STREAM_PARAMS()
703#define RTC_DLOG_V(sev) RTC_DLOG_EAT_STREAM_PARAMS()
704#define RTC_DLOG_F(sev) RTC_DLOG_EAT_STREAM_PARAMS()
Fredrik Solenbergb3d7cac2017-11-17 15:22:37 +0100705#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200706
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200707} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000708
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200709#endif // RTC_BASE_LOGGING_H_