blob: 8991f6f2b411338569f2faf10fc0acf7774edfc9 [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//////////////////////////////////////////////////////////////////////
Harald Alvestrand57869da2022-03-09 10:52:14 +000075// The meanings of the levels are:
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020076// LS_VERBOSE: This level is for data which we do not want to appear in the
77// normal debug log, but should appear in diagnostic logs.
78// LS_INFO: Chatty level used in debugging for all sorts of things, the default
79// in debug builds.
80// LS_WARNING: Something that may warrant investigation.
81// LS_ERROR: Something that should not have occurred.
82// LS_NONE: Don't log.
83enum LoggingSeverity {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020084 LS_VERBOSE,
85 LS_INFO,
86 LS_WARNING,
87 LS_ERROR,
88 LS_NONE,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020089};
90
91// LogErrorContext assists in interpreting the meaning of an error value.
92enum LogErrorContext {
93 ERRCTX_NONE,
Jonas Olssona4d87372019-07-05 19:08:33 +020094 ERRCTX_ERRNO, // System-local errno
95 ERRCTX_HRESULT, // Windows HRESULT
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020096
97 // Abbreviations for LOG_E macro
Jonas Olssona4d87372019-07-05 19:08:33 +020098 ERRCTX_EN = ERRCTX_ERRNO, // LOG_E(sev, EN, x)
99 ERRCTX_HR = ERRCTX_HRESULT, // LOG_E(sev, HR, x)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200100};
101
Danil Chapovalovb9f69022019-10-21 09:19:10 +0200102class LogMessage;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200103// Virtual sink interface that can receive log messages.
104class LogSink {
105 public:
106 LogSink() {}
107 virtual ~LogSink() {}
Paulina Hensmanf1e3cb42018-06-20 14:07:05 +0200108 virtual void OnLogMessage(const std::string& msg,
109 LoggingSeverity severity,
110 const char* tag);
Jiawei Ou3ea18782018-10-31 23:14:24 -0700111 virtual void OnLogMessage(const std::string& message,
112 LoggingSeverity severity);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200113 virtual void OnLogMessage(const std::string& message) = 0;
Danil Chapovalovb9f69022019-10-21 09:19:10 +0200114
115 private:
116 friend class ::rtc::LogMessage;
Artem Titov6a4a1462019-11-26 16:24:46 +0100117#if RTC_LOG_ENABLED()
Danil Chapovalovb9f69022019-10-21 09:19:10 +0200118 // Members for LogMessage class to keep linked list of the registered sinks.
119 LogSink* next_ = nullptr;
120 LoggingSeverity min_severity_;
Artem Titov6a4a1462019-11-26 16:24:46 +0100121#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200122};
123
Karl Wibergcefc4652018-05-23 23:20:38 +0200124namespace webrtc_logging_impl {
125
126class LogMetadata {
127 public:
128 LogMetadata(const char* file, int line, LoggingSeverity severity)
129 : file_(file),
130 line_and_sev_(static_cast<uint32_t>(line) << 3 | severity) {}
131 LogMetadata() = default;
132
133 const char* File() const { return file_; }
134 int Line() const { return line_and_sev_ >> 3; }
135 LoggingSeverity Severity() const {
136 return static_cast<LoggingSeverity>(line_and_sev_ & 0x7);
137 }
138
139 private:
140 const char* file_;
141
142 // Line number and severity, the former in the most significant 29 bits, the
143 // latter in the least significant 3 bits. (This is an optimization; since
144 // both numbers are usually compile-time constants, this way we can load them
145 // both with a single instruction.)
146 uint32_t line_and_sev_;
147};
148static_assert(std::is_trivial<LogMetadata>::value, "");
149
150struct LogMetadataErr {
151 LogMetadata meta;
152 LogErrorContext err_ctx;
153 int err;
154};
155
156#ifdef WEBRTC_ANDROID
157struct LogMetadataTag {
158 LoggingSeverity severity;
159 const char* tag;
160};
161#endif
162
163enum class LogArgType : int8_t {
164 kEnd = 0,
165 kInt,
166 kLong,
167 kLongLong,
168 kUInt,
169 kULong,
170 kULongLong,
171 kDouble,
172 kLongDouble,
173 kCharP,
174 kStdString,
Jonas Olssonf2ce37c2018-09-12 15:32:47 +0200175 kStringView,
Karl Wibergcefc4652018-05-23 23:20:38 +0200176 kVoidP,
177 kLogMetadata,
178 kLogMetadataErr,
179#ifdef WEBRTC_ANDROID
180 kLogMetadataTag,
181#endif
182};
183
184// Wrapper for log arguments. Only ever make values of this type with the
185// MakeVal() functions.
186template <LogArgType N, typename T>
187struct Val {
188 static constexpr LogArgType Type() { return N; }
189 T GetVal() const { return val; }
190 T val;
191};
192
Sebastian Janssonb1138622019-04-11 16:48:15 +0200193// Case for when we need to construct a temp string and then print that.
194// (We can't use Val<CheckArgType::kStdString, const std::string*>
195// because we need somewhere to store the temp string.)
196struct ToStringVal {
Karl Wibergcefc4652018-05-23 23:20:38 +0200197 static constexpr LogArgType Type() { return LogArgType::kStdString; }
198 const std::string* GetVal() const { return &val; }
199 std::string val;
200};
201
202inline Val<LogArgType::kInt, int> MakeVal(int x) {
203 return {x};
204}
205inline Val<LogArgType::kLong, long> MakeVal(long x) {
206 return {x};
207}
208inline Val<LogArgType::kLongLong, long long> MakeVal(long long x) {
209 return {x};
210}
211inline Val<LogArgType::kUInt, unsigned int> MakeVal(unsigned int x) {
212 return {x};
213}
214inline Val<LogArgType::kULong, unsigned long> MakeVal(unsigned long x) {
215 return {x};
216}
217inline Val<LogArgType::kULongLong, unsigned long long> MakeVal(
218 unsigned long long x) {
219 return {x};
220}
221
222inline Val<LogArgType::kDouble, double> MakeVal(double x) {
223 return {x};
224}
225inline Val<LogArgType::kLongDouble, long double> MakeVal(long double x) {
226 return {x};
227}
228
229inline Val<LogArgType::kCharP, const char*> MakeVal(const char* x) {
230 return {x};
231}
232inline Val<LogArgType::kStdString, const std::string*> MakeVal(
233 const std::string& x) {
234 return {&x};
235}
Jonas Olssonf2ce37c2018-09-12 15:32:47 +0200236inline Val<LogArgType::kStringView, const absl::string_view*> MakeVal(
237 const absl::string_view& x) {
238 return {&x};
239}
Karl Wibergcefc4652018-05-23 23:20:38 +0200240
241inline Val<LogArgType::kVoidP, const void*> MakeVal(const void* x) {
242 return {x};
243}
244
245inline Val<LogArgType::kLogMetadata, LogMetadata> MakeVal(
246 const LogMetadata& x) {
247 return {x};
248}
249inline Val<LogArgType::kLogMetadataErr, LogMetadataErr> MakeVal(
250 const LogMetadataErr& x) {
251 return {x};
252}
253
Amit Hilbuch10c5a932019-03-18 13:09:18 -0700254// The enum class types are not implicitly convertible to arithmetic types.
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200255template <typename T,
256 absl::enable_if_t<std::is_enum<T>::value &&
257 !std::is_arithmetic<T>::value>* = nullptr>
258inline decltype(MakeVal(std::declval<absl::underlying_type_t<T>>())) MakeVal(
259 T x) {
260 return {static_cast<absl::underlying_type_t<T>>(x)};
Amit Hilbuch10c5a932019-03-18 13:09:18 -0700261}
262
Karl Wibergcefc4652018-05-23 23:20:38 +0200263#ifdef WEBRTC_ANDROID
264inline Val<LogArgType::kLogMetadataTag, LogMetadataTag> MakeVal(
265 const LogMetadataTag& x) {
266 return {x};
267}
268#endif
269
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200270template <typename T, class = void>
Sebastian Janssonb1138622019-04-11 16:48:15 +0200271struct has_to_log_string : std::false_type {};
272template <typename T>
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200273struct has_to_log_string<T, decltype(ToLogString(std::declval<T>()))>
Sebastian Janssonb1138622019-04-11 16:48:15 +0200274 : std::true_type {};
275
Karl Wibergcefc4652018-05-23 23:20:38 +0200276// Handle arbitrary types other than the above by falling back to stringstream.
277// TODO(bugs.webrtc.org/9278): Get rid of this overload when callers don't need
278// it anymore. No in-tree caller does, but some external callers still do.
279template <
280 typename T,
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200281 typename T1 = absl::decay_t<T>,
282 absl::enable_if_t<std::is_class<T1>::value &&
283 !std::is_same<T1, std::string>::value &&
284 !std::is_same<T1, LogMetadata>::value &&
285 !has_to_log_string<T1>::value &&
Karl Wibergcefc4652018-05-23 23:20:38 +0200286#ifdef WEBRTC_ANDROID
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200287 !std::is_same<T1, LogMetadataTag>::value &&
Karl Wibergcefc4652018-05-23 23:20:38 +0200288#endif
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200289 !std::is_same<T1, LogMetadataErr>::value>* = nullptr>
Sebastian Janssonb1138622019-04-11 16:48:15 +0200290ToStringVal MakeVal(const T& x) {
Karl Wibergcefc4652018-05-23 23:20:38 +0200291 std::ostringstream os; // no-presubmit-check TODO(webrtc:8982)
292 os << x;
293 return {os.str()};
294}
295
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200296template <typename T, absl::enable_if_t<has_to_log_string<T>::value>* = nullptr>
Sebastian Janssonb1138622019-04-11 16:48:15 +0200297ToStringVal MakeVal(const T& x) {
298 return {ToLogString(x)};
299}
300
Artem Titov6a4a1462019-11-26 16:24:46 +0100301#if RTC_LOG_ENABLED()
Karl Wibergcefc4652018-05-23 23:20:38 +0200302void Log(const LogArgType* fmt, ...);
Artem Titov6a4a1462019-11-26 16:24:46 +0100303#else
304inline void Log(const LogArgType* fmt, ...) {
305 // Do nothing, shouldn't be invoked
306}
307#endif
Karl Wibergcefc4652018-05-23 23:20:38 +0200308
309// Ephemeral type that represents the result of the logging << operator.
310template <typename... Ts>
311class LogStreamer;
312
313// Base case: Before the first << argument.
314template <>
315class LogStreamer<> final {
316 public:
Amit Hilbuch10c5a932019-03-18 13:09:18 -0700317 template <typename U,
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200318 typename V = decltype(MakeVal(std::declval<U>())),
319 absl::enable_if_t<std::is_arithmetic<U>::value ||
320 std::is_enum<U>::value>* = nullptr>
321 RTC_FORCE_INLINE LogStreamer<V> operator<<(U arg) const {
322 return LogStreamer<V>(MakeVal(arg), this);
Karl Wibergcefc4652018-05-23 23:20:38 +0200323 }
324
Amit Hilbuch10c5a932019-03-18 13:09:18 -0700325 template <typename U,
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200326 typename V = decltype(MakeVal(std::declval<U>())),
327 absl::enable_if_t<!std::is_arithmetic<U>::value &&
328 !std::is_enum<U>::value>* = nullptr>
329 RTC_FORCE_INLINE LogStreamer<V> operator<<(const U& arg) const {
330 return LogStreamer<V>(MakeVal(arg), this);
Karl Wibergcefc4652018-05-23 23:20:38 +0200331 }
332
333 template <typename... Us>
334 RTC_FORCE_INLINE static void Call(const Us&... args) {
335 static constexpr LogArgType t[] = {Us::Type()..., LogArgType::kEnd};
336 Log(t, args.GetVal()...);
337 }
338};
339
340// Inductive case: We've already seen at least one << argument. The most recent
341// one had type `T`, and the earlier ones had types `Ts`.
342template <typename T, typename... Ts>
343class LogStreamer<T, Ts...> final {
344 public:
345 RTC_FORCE_INLINE LogStreamer(T arg, const LogStreamer<Ts...>* prior)
346 : arg_(arg), prior_(prior) {}
347
Amit Hilbuch10c5a932019-03-18 13:09:18 -0700348 template <typename U,
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200349 typename V = decltype(MakeVal(std::declval<U>())),
350 absl::enable_if_t<std::is_arithmetic<U>::value ||
351 std::is_enum<U>::value>* = nullptr>
352 RTC_FORCE_INLINE LogStreamer<V, T, Ts...> operator<<(U arg) const {
353 return LogStreamer<V, T, Ts...>(MakeVal(arg), this);
Karl Wibergcefc4652018-05-23 23:20:38 +0200354 }
355
Amit Hilbuch10c5a932019-03-18 13:09:18 -0700356 template <typename U,
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200357 typename V = decltype(MakeVal(std::declval<U>())),
358 absl::enable_if_t<!std::is_arithmetic<U>::value &&
359 !std::is_enum<U>::value>* = nullptr>
360 RTC_FORCE_INLINE LogStreamer<V, T, Ts...> operator<<(const U& arg) const {
361 return LogStreamer<V, T, Ts...>(MakeVal(arg), this);
Karl Wibergcefc4652018-05-23 23:20:38 +0200362 }
363
364 template <typename... Us>
365 RTC_FORCE_INLINE void Call(const Us&... args) const {
366 prior_->Call(arg_, args...);
367 }
368
369 private:
370 // The most recent argument.
371 T arg_;
372
373 // Earlier arguments.
374 const LogStreamer<Ts...>* prior_;
375};
376
377class LogCall final {
378 public:
379 // This can be any binary operator with precedence lower than <<.
Artem Titov6a4a1462019-11-26 16:24:46 +0100380 // We return bool here to be able properly remove logging if
381 // RTC_DISABLE_LOGGING is defined.
Karl Wibergcefc4652018-05-23 23:20:38 +0200382 template <typename... Ts>
Artem Titov6a4a1462019-11-26 16:24:46 +0100383 RTC_FORCE_INLINE bool operator&(const LogStreamer<Ts...>& streamer) {
Karl Wibergcefc4652018-05-23 23:20:38 +0200384 streamer.Call();
Artem Titov6a4a1462019-11-26 16:24:46 +0100385 return true;
Karl Wibergcefc4652018-05-23 23:20:38 +0200386 }
387};
388
Mirko Bonadeif1df04b2020-03-23 19:53:23 +0100389// This class is used to explicitly ignore values in the conditional
390// logging macros. This avoids compiler warnings like "value computed
391// is not used" and "statement has no effect".
392class LogMessageVoidify {
393 public:
394 LogMessageVoidify() = default;
395 // This has to be an operator with a precedence lower than << but
396 // higher than ?:
397 template <typename... Ts>
398 void operator&(LogStreamer<Ts...>&& streamer) {}
399};
400
Karl Wibergcefc4652018-05-23 23:20:38 +0200401} // namespace webrtc_logging_impl
402
403// Direct use of this class is deprecated; please use the logging macros
404// instead.
405// TODO(bugs.webrtc.org/9278): Move this class to an unnamed namespace in the
406// .cc file.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200407class LogMessage {
408 public:
Karl Wiberg1ffb3742018-05-04 15:04:48 +0200409 // Same as the above, but using a compile-time constant for the logging
410 // severity. This saves space at the call site, since passing an empty struct
411 // is generally the same as not passing an argument at all.
412 template <LoggingSeverity S>
413 RTC_NO_INLINE LogMessage(const char* file,
414 int line,
415 std::integral_constant<LoggingSeverity, S>)
416 : LogMessage(file, line, S) {}
417
Artem Titov6a4a1462019-11-26 16:24:46 +0100418#if RTC_LOG_ENABLED()
419 LogMessage(const char* file, int line, LoggingSeverity sev);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200420 LogMessage(const char* file,
421 int line,
422 LoggingSeverity sev,
Karl Wibergab4f1c12018-05-04 10:42:28 +0200423 LogErrorContext err_ctx,
424 int err);
Tommie51a0a82018-02-27 15:30:29 +0100425#if defined(WEBRTC_ANDROID)
426 LogMessage(const char* file, int line, LoggingSeverity sev, const char* tag);
427#endif
Tommie51a0a82018-02-27 15:30:29 +0100428 // DEPRECATED - DO NOT USE - PLEASE USE THE MACROS INSTEAD OF THE CLASS.
429 // Android code should use the 'const char*' version since tags are static
430 // and we want to avoid allocating a std::string copy per log line.
Danil Chapovalove9041612021-02-22 12:43:39 +0100431 ABSL_DEPRECATED("Use RTC_LOG macros instead of accessing this class directly")
Yves Gerey665174f2018-06-19 15:03:05 +0200432 LogMessage(const char* file,
433 int line,
434 LoggingSeverity sev,
Philip Eliasson278aa422018-02-26 14:54:45 +0000435 const std::string& tag);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200436 ~LogMessage();
437
Byoungchan Lee14af7622022-01-12 05:24:58 +0900438 LogMessage(const LogMessage&) = delete;
439 LogMessage& operator=(const LogMessage&) = delete;
440
Karl Wibergcefc4652018-05-23 23:20:38 +0200441 void AddTag(const char* tag);
Jonas Olssond8c50782018-09-07 11:21:28 +0200442 rtc::StringBuilder& stream();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200443 // Returns the time at which this function was called for the first time.
444 // The time will be used as the logging start time.
445 // If this is not called externally, the LogMessage ctor also calls it, in
446 // which case the logging start time will be the time of the first LogMessage
447 // instance is created.
448 static int64_t LogStartTime();
Artem Titov96e3b992021-07-26 16:03:14 +0200449 // Returns the wall clock equivalent of `LogStartTime`, in seconds from the
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200450 // epoch.
451 static uint32_t WallClockStartTime();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200452 // LogThreads: Display the thread identifier of the current thread
453 static void LogThreads(bool on = true);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200454 // LogTimestamps: Display the elapsed time of the program
455 static void LogTimestamps(bool on = true);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200456 // These are the available logging channels
457 // Debug: Debug console on Windows, otherwise stderr
458 static void LogToDebug(LoggingSeverity min_sev);
Jonas Olsson2b6f1352018-02-15 11:57:03 +0100459 static LoggingSeverity GetLogToDebug();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200460 // Sets whether logs will be directed to stderr in debug mode.
461 static void SetLogToStderr(bool log_to_stderr);
Danil Chapovalovb9f69022019-10-21 09:19:10 +0200462 // Stream: Any non-blocking stream interface.
Artem Titov96e3b992021-07-26 16:03:14 +0200463 // Installs the `stream` to collect logs with severtiy `min_sev` or higher.
464 // `stream` must live until deinstalled by RemoveLogToStream.
465 // If `stream` is the first stream added to the system, we might miss some
Markus Handell531bd0f2020-07-08 10:58:19 +0200466 // early concurrent log statement happening from another thread happening near
467 // this instant.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200468 static void AddLogToStream(LogSink* stream, LoggingSeverity min_sev);
Markus Handell531bd0f2020-07-08 10:58:19 +0200469 // Removes the specified stream, without destroying it. When the method
Artem Titov96e3b992021-07-26 16:03:14 +0200470 // has completed, it's guaranteed that `stream` will receive no more logging
Markus Handell531bd0f2020-07-08 10:58:19 +0200471 // calls.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200472 static void RemoveLogToStream(LogSink* stream);
Danil Chapovalovb9f69022019-10-21 09:19:10 +0200473 // Returns the severity for the specified stream, of if none is specified,
474 // the minimum stream severity.
475 static int GetLogToStream(LogSink* stream = nullptr);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200476 // Testing against MinLogSeverity allows code to avoid potentially expensive
477 // logging operations by pre-checking the logging level.
Jonas Olsson2b6f1352018-02-15 11:57:03 +0100478 static int GetMinLogSeverity();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200479 // Parses the provided parameter stream to configure the options above.
480 // Useful for configuring logging from the command line.
481 static void ConfigureLogging(const char* params);
Artem Titov96e3b992021-07-26 16:03:14 +0200482 // Checks the current global debug severity and if the `streams_` collection
483 // is empty. If `severity` is smaller than the global severity and if the
484 // `streams_` collection is empty, the LogMessage will be considered a noop
Jonas Olssond8c50782018-09-07 11:21:28 +0200485 // LogMessage.
486 static bool IsNoop(LoggingSeverity severity);
Karl Wibergdd7df5c2020-09-22 11:07:53 +0200487 // Version of IsNoop that uses fewer instructions at the call site, since the
488 // caller doesn't have to pass an argument.
489 template <LoggingSeverity S>
490 RTC_NO_INLINE static bool IsNoop() {
491 return IsNoop(S);
492 }
Artem Titov6a4a1462019-11-26 16:24:46 +0100493#else
494 // Next methods do nothing; no one will call these functions.
495 LogMessage(const char* file, int line, LoggingSeverity sev) {}
496 LogMessage(const char* file,
497 int line,
498 LoggingSeverity sev,
499 LogErrorContext err_ctx,
500 int err) {}
501#if defined(WEBRTC_ANDROID)
502 LogMessage(const char* file, int line, LoggingSeverity sev, const char* tag) {
503 }
504#endif
505 // DEPRECATED - DO NOT USE - PLEASE USE THE MACROS INSTEAD OF THE CLASS.
506 // Android code should use the 'const char*' version since tags are static
507 // and we want to avoid allocating a std::string copy per log line.
Danil Chapovalove9041612021-02-22 12:43:39 +0100508 ABSL_DEPRECATED("Use RTC_LOG macros instead of accessing this class directly")
Artem Titov6a4a1462019-11-26 16:24:46 +0100509 LogMessage(const char* file,
510 int line,
511 LoggingSeverity sev,
512 const std::string& tag) {}
513 ~LogMessage() = default;
514
515 inline void AddTag(const char* tag) {}
516 inline rtc::StringBuilder& stream() { return print_stream_; }
517 inline static int64_t LogStartTime() { return 0; }
518 inline static uint32_t WallClockStartTime() { return 0; }
519 inline static void LogThreads(bool on = true) {}
520 inline static void LogTimestamps(bool on = true) {}
521 inline static void LogToDebug(LoggingSeverity min_sev) {}
522 inline static LoggingSeverity GetLogToDebug() {
523 return LoggingSeverity::LS_INFO;
524 }
525 inline static void SetLogToStderr(bool log_to_stderr) {}
526 inline static void AddLogToStream(LogSink* stream, LoggingSeverity min_sev) {}
527 inline static void RemoveLogToStream(LogSink* stream) {}
528 inline static int GetLogToStream(LogSink* stream = nullptr) { return 0; }
529 inline static int GetMinLogSeverity() { return 0; }
530 inline static void ConfigureLogging(const char* params) {}
Karl Wiberg0e884382020-09-22 09:34:45 +0200531 static constexpr bool IsNoop(LoggingSeverity severity) { return true; }
Karl Wibergdd7df5c2020-09-22 11:07:53 +0200532 template <LoggingSeverity S>
533 static constexpr bool IsNoop() {
534 return IsNoop(S);
535 }
Artem Titov6a4a1462019-11-26 16:24:46 +0100536#endif // RTC_LOG_ENABLED()
Jonas Olssond8c50782018-09-07 11:21:28 +0200537
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200538 private:
Tommifef05002018-02-27 13:51:08 +0100539 friend class LogMessageForTesting;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200540
Artem Titov6a4a1462019-11-26 16:24:46 +0100541#if RTC_LOG_ENABLED()
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200542 // Updates min_sev_ appropriately when debug sinks change.
543 static void UpdateMinLogSeverity();
544
Yves Gerey665174f2018-06-19 15:03:05 +0200545// These write out the actual log messages.
Tommie51a0a82018-02-27 15:30:29 +0100546#if defined(WEBRTC_ANDROID)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200547 static void OutputToDebug(const std::string& msg,
548 LoggingSeverity severity,
Tommie51a0a82018-02-27 15:30:29 +0100549 const char* tag);
550#else
551 static void OutputToDebug(const std::string& msg, LoggingSeverity severity);
Artem Titov6a4a1462019-11-26 16:24:46 +0100552#endif // defined(WEBRTC_ANDROID)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200553
Tommifef05002018-02-27 13:51:08 +0100554 // Called from the dtor (or from a test) to append optional extra error
555 // information to the log stream and a newline character.
556 void FinishPrintStream();
557
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200558 // The severity level of this message
559 LoggingSeverity severity_;
560
Tommie51a0a82018-02-27 15:30:29 +0100561#if defined(WEBRTC_ANDROID)
Paulina Hensmanf1e3cb42018-06-20 14:07:05 +0200562 // The default Android debug output tag.
Tommie51a0a82018-02-27 15:30:29 +0100563 const char* tag_ = "libjingle";
564#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200565
566 // String data generated in the constructor, that should be appended to
567 // the message before output.
568 std::string extra_;
569
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200570 // The output streams and their associated severities
Danil Chapovalovb9f69022019-10-21 09:19:10 +0200571 static LogSink* streams_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200572
Artem Titov96e3b992021-07-26 16:03:14 +0200573 // Holds true with high probability if `streams_` is empty, false with high
Markus Handellce1ff6f2020-07-08 08:52:48 +0200574 // probability otherwise. Operated on with std::memory_order_relaxed because
Markus Handell531bd0f2020-07-08 10:58:19 +0200575 // it's ok to lose or log some additional statements near the instant streams
Markus Handellce1ff6f2020-07-08 08:52:48 +0200576 // are added/removed.
577 static std::atomic<bool> streams_empty_;
578
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200579 // Flags for formatting options
580 static bool thread_, timestamp_;
581
582 // Determines if logs will be directed to stderr in debug mode.
583 static bool log_to_stderr_;
Artem Titov6a4a1462019-11-26 16:24:46 +0100584#else // RTC_LOG_ENABLED()
585 // Next methods do nothing; no one will call these functions.
586 inline static void UpdateMinLogSeverity() {}
587#if defined(WEBRTC_ANDROID)
588 inline static void OutputToDebug(const std::string& msg,
589 LoggingSeverity severity,
590 const char* tag) {}
591#else
592 inline static void OutputToDebug(const std::string& msg,
593 LoggingSeverity severity) {}
594#endif // defined(WEBRTC_ANDROID)
595 inline void FinishPrintStream() {}
596#endif // RTC_LOG_ENABLED()
597
598 // The stringbuilder that buffers the formatted message before output
599 rtc::StringBuilder print_stream_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200600};
601
602//////////////////////////////////////////////////////////////////////
603// Logging Helpers
604//////////////////////////////////////////////////////////////////////
605
Karl Wibergdd7df5c2020-09-22 11:07:53 +0200606#define RTC_LOG_FILE_LINE(sev, file, line) \
607 ::rtc::webrtc_logging_impl::LogCall() & \
608 ::rtc::webrtc_logging_impl::LogStreamer<>() \
609 << ::rtc::webrtc_logging_impl::LogMetadata(file, line, sev)
Jonas Olsson8a7916b2018-09-06 09:50:23 +0200610
Karl Wibergdd7df5c2020-09-22 11:07:53 +0200611#define RTC_LOG(sev) \
612 !rtc::LogMessage::IsNoop<::rtc::sev>() && \
613 RTC_LOG_FILE_LINE(::rtc::sev, __FILE__, __LINE__)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200614
Karl Wibergcefc4652018-05-23 23:20:38 +0200615// The _V version is for when a variable is passed in.
Karl Wibergdd7df5c2020-09-22 11:07:53 +0200616#define RTC_LOG_V(sev) \
617 !rtc::LogMessage::IsNoop(sev) && RTC_LOG_FILE_LINE(sev, __FILE__, __LINE__)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200618
619// The _F version prefixes the message with the current function name.
620#if (defined(__GNUC__) && !defined(NDEBUG)) || defined(WANT_PRETTY_LOG_F)
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200621#define RTC_LOG_F(sev) RTC_LOG(sev) << __PRETTY_FUNCTION__ << ": "
Yves Gerey665174f2018-06-19 15:03:05 +0200622#define RTC_LOG_T_F(sev) \
623 RTC_LOG(sev) << this << ": " << __PRETTY_FUNCTION__ << ": "
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200624#else
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200625#define RTC_LOG_F(sev) RTC_LOG(sev) << __FUNCTION__ << ": "
Patrik Höglundc2962552017-11-17 13:40:22 +0000626#define RTC_LOG_T_F(sev) RTC_LOG(sev) << this << ": " << __FUNCTION__ << ": "
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200627#endif
628
Jiawei Ou14e5f0b2020-03-04 13:38:02 -0800629#define RTC_LOG_CHECK_LEVEL(sev) ::rtc::LogCheckLevel(::rtc::sev)
630#define RTC_LOG_CHECK_LEVEL_V(sev) ::rtc::LogCheckLevel(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200631
632inline bool LogCheckLevel(LoggingSeverity sev) {
633 return (LogMessage::GetMinLogSeverity() <= sev);
634}
635
Karl Wiberg92e37962020-09-22 13:22:20 +0200636#define RTC_LOG_E(sev, ctx, err) \
637 !rtc::LogMessage::IsNoop<::rtc::sev>() && \
638 ::rtc::webrtc_logging_impl::LogCall() & \
639 ::rtc::webrtc_logging_impl::LogStreamer<>() \
640 << ::rtc::webrtc_logging_impl::LogMetadataErr { \
641 {__FILE__, __LINE__, ::rtc::sev}, ::rtc::ERRCTX_##ctx, (err) \
Jonas Olssona4d87372019-07-05 19:08:33 +0200642 }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200643
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200644#define RTC_LOG_T(sev) RTC_LOG(sev) << this << ": "
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200645
Yves Gerey665174f2018-06-19 15:03:05 +0200646#define RTC_LOG_ERRNO_EX(sev, err) RTC_LOG_E(sev, ERRNO, err)
647#define RTC_LOG_ERRNO(sev) RTC_LOG_ERRNO_EX(sev, errno)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200648
649#if defined(WEBRTC_WIN)
Yves Gerey665174f2018-06-19 15:03:05 +0200650#define RTC_LOG_GLE_EX(sev, err) RTC_LOG_E(sev, HRESULT, err)
Karl Wibergcefc4652018-05-23 23:20:38 +0200651#define RTC_LOG_GLE(sev) RTC_LOG_GLE_EX(sev, static_cast<int>(GetLastError()))
Yves Gerey665174f2018-06-19 15:03:05 +0200652#define RTC_LOG_ERR_EX(sev, err) RTC_LOG_GLE_EX(sev, err)
653#define RTC_LOG_ERR(sev) RTC_LOG_GLE(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200654#elif defined(__native_client__) && __native_client__
Yves Gerey665174f2018-06-19 15:03:05 +0200655#define RTC_LOG_ERR_EX(sev, err) RTC_LOG(sev)
656#define RTC_LOG_ERR(sev) RTC_LOG(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200657#elif defined(WEBRTC_POSIX)
Yves Gerey665174f2018-06-19 15:03:05 +0200658#define RTC_LOG_ERR_EX(sev, err) RTC_LOG_ERRNO_EX(sev, err)
659#define RTC_LOG_ERR(sev) RTC_LOG_ERRNO(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200660#endif // WEBRTC_WIN
661
Karl Wibergcefc4652018-05-23 23:20:38 +0200662#ifdef WEBRTC_ANDROID
663
664namespace webrtc_logging_impl {
665// TODO(kwiberg): Replace these with absl::string_view.
Yves Gerey665174f2018-06-19 15:03:05 +0200666inline const char* AdaptString(const char* str) {
667 return str;
668}
669inline const char* AdaptString(const std::string& str) {
670 return str.c_str();
671}
Karl Wibergcefc4652018-05-23 23:20:38 +0200672} // namespace webrtc_logging_impl
673
Karl Wiberg92e37962020-09-22 13:22:20 +0200674#define RTC_LOG_TAG(sev, tag) \
675 !rtc::LogMessage::IsNoop(sev) && \
676 ::rtc::webrtc_logging_impl::LogCall() & \
677 ::rtc::webrtc_logging_impl::LogStreamer<>() \
678 << ::rtc::webrtc_logging_impl::LogMetadataTag { \
679 sev, ::rtc::webrtc_logging_impl::AdaptString(tag) \
Jonas Olssona4d87372019-07-05 19:08:33 +0200680 }
Karl Wibergcefc4652018-05-23 23:20:38 +0200681
Tommie51a0a82018-02-27 15:30:29 +0100682#else
Karl Wibergcefc4652018-05-23 23:20:38 +0200683
Tommie51a0a82018-02-27 15:30:29 +0100684// DEPRECATED. This macro is only intended for Android.
Karl Wibergcefc4652018-05-23 23:20:38 +0200685#define RTC_LOG_TAG(sev, tag) RTC_LOG_V(sev)
686
Tommie51a0a82018-02-27 15:30:29 +0100687#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200688
Fredrik Solenbergb3d7cac2017-11-17 15:22:37 +0100689// The RTC_DLOG macros are equivalent to their RTC_LOG counterparts except that
690// they only generate code in debug builds.
691#if RTC_DLOG_IS_ON
692#define RTC_DLOG(sev) RTC_LOG(sev)
693#define RTC_DLOG_V(sev) RTC_LOG_V(sev)
694#define RTC_DLOG_F(sev) RTC_LOG_F(sev)
695#else
Mirko Bonadeif1df04b2020-03-23 19:53:23 +0100696#define RTC_DLOG_EAT_STREAM_PARAMS() \
697 while (false) \
698 ::rtc::webrtc_logging_impl::LogMessageVoidify() & \
699 (::rtc::webrtc_logging_impl::LogStreamer<>())
Karl Wibergcefc4652018-05-23 23:20:38 +0200700#define RTC_DLOG(sev) RTC_DLOG_EAT_STREAM_PARAMS()
701#define RTC_DLOG_V(sev) RTC_DLOG_EAT_STREAM_PARAMS()
702#define RTC_DLOG_F(sev) RTC_DLOG_EAT_STREAM_PARAMS()
Fredrik Solenbergb3d7cac2017-11-17 15:22:37 +0100703#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200704
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200705} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000706
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200707#endif // RTC_BASE_LOGGING_H_