blob: 98729eff9cef5475e94986c15d9856db97dce505 [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"
Steve Anton10542f22019-01-11 09:11:00 -080057#include "rtc_base/constructor_magic.h"
Jonas Olssond8c50782018-09-07 11:21:28 +020058#include "rtc_base/strings/string_builder.h"
Karl Wibergcefc4652018-05-23 23:20:38 +020059#include "rtc_base/system/inline.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020060
Fredrik Solenbergb3d7cac2017-11-17 15:22:37 +010061#if !defined(NDEBUG) || defined(DLOG_ALWAYS_ON)
62#define RTC_DLOG_IS_ON 1
63#else
64#define RTC_DLOG_IS_ON 0
65#endif
66
Artem Titov6a4a1462019-11-26 16:24:46 +010067#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 07:52:50 +020073namespace rtc {
74
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020075//////////////////////////////////////////////////////////////////////
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 07:52:50 +020079// 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 07:52:50 +020087 LS_VERBOSE,
88 LS_INFO,
89 LS_WARNING,
90 LS_ERROR,
91 LS_NONE,
Harald Alvestrand97597c02021-11-04 12:01:23 +000092 // Compatibility aliases, to be deleted.
93 // TODO(bugs.webrtc.org/13362): Remove usage and delete.
Harald Alvestranda18cad92021-11-23 13:53:29 +000094 INFO [[deprecated("Use LS_INFO")]] = LS_INFO,
95 // WARNING [[deprecated("Use LS_WARNING")]] = LS_WARNING,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020096 WARNING = LS_WARNING,
Harald Alvestrand5f341302021-11-24 10:01:32 +000097 LERROR [[deprecated("Use LS_ERROR")]] = LS_ERROR
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020098};
99
100// LogErrorContext assists in interpreting the meaning of an error value.
101enum LogErrorContext {
102 ERRCTX_NONE,
Jonas Olssona4d87372019-07-05 19:08:33 +0200103 ERRCTX_ERRNO, // System-local errno
104 ERRCTX_HRESULT, // Windows HRESULT
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200105
106 // Abbreviations for LOG_E macro
Jonas Olssona4d87372019-07-05 19:08:33 +0200107 ERRCTX_EN = ERRCTX_ERRNO, // LOG_E(sev, EN, x)
108 ERRCTX_HR = ERRCTX_HRESULT, // LOG_E(sev, HR, x)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200109};
110
Danil Chapovalovb9f69022019-10-21 09:19:10 +0200111class LogMessage;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200112// Virtual sink interface that can receive log messages.
113class LogSink {
114 public:
115 LogSink() {}
116 virtual ~LogSink() {}
Paulina Hensmanf1e3cb42018-06-20 14:07:05 +0200117 virtual void OnLogMessage(const std::string& msg,
118 LoggingSeverity severity,
119 const char* tag);
Jiawei Ou3ea18782018-10-31 23:14:24 -0700120 virtual void OnLogMessage(const std::string& message,
121 LoggingSeverity severity);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200122 virtual void OnLogMessage(const std::string& message) = 0;
Danil Chapovalovb9f69022019-10-21 09:19:10 +0200123
124 private:
125 friend class ::rtc::LogMessage;
Artem Titov6a4a1462019-11-26 16:24:46 +0100126#if RTC_LOG_ENABLED()
Danil Chapovalovb9f69022019-10-21 09:19:10 +0200127 // Members for LogMessage class to keep linked list of the registered sinks.
128 LogSink* next_ = nullptr;
129 LoggingSeverity min_severity_;
Artem Titov6a4a1462019-11-26 16:24:46 +0100130#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200131};
132
Karl Wibergcefc4652018-05-23 23:20:38 +0200133namespace webrtc_logging_impl {
134
135class LogMetadata {
136 public:
137 LogMetadata(const char* file, int line, LoggingSeverity severity)
138 : file_(file),
139 line_and_sev_(static_cast<uint32_t>(line) << 3 | severity) {}
140 LogMetadata() = default;
141
142 const char* File() const { return file_; }
143 int Line() const { return line_and_sev_ >> 3; }
144 LoggingSeverity Severity() const {
145 return static_cast<LoggingSeverity>(line_and_sev_ & 0x7);
146 }
147
148 private:
149 const char* file_;
150
151 // Line number and severity, the former in the most significant 29 bits, the
152 // latter in the least significant 3 bits. (This is an optimization; since
153 // both numbers are usually compile-time constants, this way we can load them
154 // both with a single instruction.)
155 uint32_t line_and_sev_;
156};
157static_assert(std::is_trivial<LogMetadata>::value, "");
158
159struct LogMetadataErr {
160 LogMetadata meta;
161 LogErrorContext err_ctx;
162 int err;
163};
164
165#ifdef WEBRTC_ANDROID
166struct LogMetadataTag {
167 LoggingSeverity severity;
168 const char* tag;
169};
170#endif
171
172enum class LogArgType : int8_t {
173 kEnd = 0,
174 kInt,
175 kLong,
176 kLongLong,
177 kUInt,
178 kULong,
179 kULongLong,
180 kDouble,
181 kLongDouble,
182 kCharP,
183 kStdString,
Jonas Olssonf2ce37c2018-09-12 15:32:47 +0200184 kStringView,
Karl Wibergcefc4652018-05-23 23:20:38 +0200185 kVoidP,
186 kLogMetadata,
187 kLogMetadataErr,
188#ifdef WEBRTC_ANDROID
189 kLogMetadataTag,
190#endif
191};
192
193// Wrapper for log arguments. Only ever make values of this type with the
194// MakeVal() functions.
195template <LogArgType N, typename T>
196struct Val {
197 static constexpr LogArgType Type() { return N; }
198 T GetVal() const { return val; }
199 T val;
200};
201
Sebastian Janssonb1138622019-04-11 16:48:15 +0200202// Case for when we need to construct a temp string and then print that.
203// (We can't use Val<CheckArgType::kStdString, const std::string*>
204// because we need somewhere to store the temp string.)
205struct ToStringVal {
Karl Wibergcefc4652018-05-23 23:20:38 +0200206 static constexpr LogArgType Type() { return LogArgType::kStdString; }
207 const std::string* GetVal() const { return &val; }
208 std::string val;
209};
210
211inline Val<LogArgType::kInt, int> MakeVal(int x) {
212 return {x};
213}
214inline Val<LogArgType::kLong, long> MakeVal(long x) {
215 return {x};
216}
217inline Val<LogArgType::kLongLong, long long> MakeVal(long long x) {
218 return {x};
219}
220inline Val<LogArgType::kUInt, unsigned int> MakeVal(unsigned int x) {
221 return {x};
222}
223inline Val<LogArgType::kULong, unsigned long> MakeVal(unsigned long x) {
224 return {x};
225}
226inline Val<LogArgType::kULongLong, unsigned long long> MakeVal(
227 unsigned long long x) {
228 return {x};
229}
230
231inline Val<LogArgType::kDouble, double> MakeVal(double x) {
232 return {x};
233}
234inline Val<LogArgType::kLongDouble, long double> MakeVal(long double x) {
235 return {x};
236}
237
238inline Val<LogArgType::kCharP, const char*> MakeVal(const char* x) {
239 return {x};
240}
241inline Val<LogArgType::kStdString, const std::string*> MakeVal(
242 const std::string& x) {
243 return {&x};
244}
Jonas Olssonf2ce37c2018-09-12 15:32:47 +0200245inline Val<LogArgType::kStringView, const absl::string_view*> MakeVal(
246 const absl::string_view& x) {
247 return {&x};
248}
Karl Wibergcefc4652018-05-23 23:20:38 +0200249
250inline Val<LogArgType::kVoidP, const void*> MakeVal(const void* x) {
251 return {x};
252}
253
254inline Val<LogArgType::kLogMetadata, LogMetadata> MakeVal(
255 const LogMetadata& x) {
256 return {x};
257}
258inline Val<LogArgType::kLogMetadataErr, LogMetadataErr> MakeVal(
259 const LogMetadataErr& x) {
260 return {x};
261}
262
Amit Hilbuch10c5a932019-03-18 13:09:18 -0700263// The enum class types are not implicitly convertible to arithmetic types.
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200264template <typename T,
265 absl::enable_if_t<std::is_enum<T>::value &&
266 !std::is_arithmetic<T>::value>* = nullptr>
267inline decltype(MakeVal(std::declval<absl::underlying_type_t<T>>())) MakeVal(
268 T x) {
269 return {static_cast<absl::underlying_type_t<T>>(x)};
Amit Hilbuch10c5a932019-03-18 13:09:18 -0700270}
271
Karl Wibergcefc4652018-05-23 23:20:38 +0200272#ifdef WEBRTC_ANDROID
273inline Val<LogArgType::kLogMetadataTag, LogMetadataTag> MakeVal(
274 const LogMetadataTag& x) {
275 return {x};
276}
277#endif
278
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200279template <typename T, class = void>
Sebastian Janssonb1138622019-04-11 16:48:15 +0200280struct has_to_log_string : std::false_type {};
281template <typename T>
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200282struct has_to_log_string<T, decltype(ToLogString(std::declval<T>()))>
Sebastian Janssonb1138622019-04-11 16:48:15 +0200283 : std::true_type {};
284
Karl Wibergcefc4652018-05-23 23:20:38 +0200285// Handle arbitrary types other than the above by falling back to stringstream.
286// TODO(bugs.webrtc.org/9278): Get rid of this overload when callers don't need
287// it anymore. No in-tree caller does, but some external callers still do.
288template <
289 typename T,
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200290 typename T1 = absl::decay_t<T>,
291 absl::enable_if_t<std::is_class<T1>::value &&
292 !std::is_same<T1, std::string>::value &&
293 !std::is_same<T1, LogMetadata>::value &&
294 !has_to_log_string<T1>::value &&
Karl Wibergcefc4652018-05-23 23:20:38 +0200295#ifdef WEBRTC_ANDROID
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200296 !std::is_same<T1, LogMetadataTag>::value &&
Karl Wibergcefc4652018-05-23 23:20:38 +0200297#endif
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200298 !std::is_same<T1, LogMetadataErr>::value>* = nullptr>
Sebastian Janssonb1138622019-04-11 16:48:15 +0200299ToStringVal MakeVal(const T& x) {
Karl Wibergcefc4652018-05-23 23:20:38 +0200300 std::ostringstream os; // no-presubmit-check TODO(webrtc:8982)
301 os << x;
302 return {os.str()};
303}
304
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200305template <typename T, absl::enable_if_t<has_to_log_string<T>::value>* = nullptr>
Sebastian Janssonb1138622019-04-11 16:48:15 +0200306ToStringVal MakeVal(const T& x) {
307 return {ToLogString(x)};
308}
309
Artem Titov6a4a1462019-11-26 16:24:46 +0100310#if RTC_LOG_ENABLED()
Karl Wibergcefc4652018-05-23 23:20:38 +0200311void Log(const LogArgType* fmt, ...);
Artem Titov6a4a1462019-11-26 16:24:46 +0100312#else
313inline void Log(const LogArgType* fmt, ...) {
314 // Do nothing, shouldn't be invoked
315}
316#endif
Karl Wibergcefc4652018-05-23 23:20:38 +0200317
318// Ephemeral type that represents the result of the logging << operator.
319template <typename... Ts>
320class LogStreamer;
321
322// Base case: Before the first << argument.
323template <>
324class LogStreamer<> final {
325 public:
Amit Hilbuch10c5a932019-03-18 13:09:18 -0700326 template <typename U,
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200327 typename V = decltype(MakeVal(std::declval<U>())),
328 absl::enable_if_t<std::is_arithmetic<U>::value ||
329 std::is_enum<U>::value>* = nullptr>
330 RTC_FORCE_INLINE LogStreamer<V> operator<<(U arg) const {
331 return LogStreamer<V>(MakeVal(arg), this);
Karl Wibergcefc4652018-05-23 23:20:38 +0200332 }
333
Amit Hilbuch10c5a932019-03-18 13:09:18 -0700334 template <typename U,
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200335 typename V = decltype(MakeVal(std::declval<U>())),
336 absl::enable_if_t<!std::is_arithmetic<U>::value &&
337 !std::is_enum<U>::value>* = nullptr>
338 RTC_FORCE_INLINE LogStreamer<V> operator<<(const U& arg) const {
339 return LogStreamer<V>(MakeVal(arg), this);
Karl Wibergcefc4652018-05-23 23:20:38 +0200340 }
341
342 template <typename... Us>
343 RTC_FORCE_INLINE static void Call(const Us&... args) {
344 static constexpr LogArgType t[] = {Us::Type()..., LogArgType::kEnd};
345 Log(t, args.GetVal()...);
346 }
347};
348
349// Inductive case: We've already seen at least one << argument. The most recent
350// one had type `T`, and the earlier ones had types `Ts`.
351template <typename T, typename... Ts>
352class LogStreamer<T, Ts...> final {
353 public:
354 RTC_FORCE_INLINE LogStreamer(T arg, const LogStreamer<Ts...>* prior)
355 : arg_(arg), prior_(prior) {}
356
Amit Hilbuch10c5a932019-03-18 13:09:18 -0700357 template <typename U,
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200358 typename V = decltype(MakeVal(std::declval<U>())),
359 absl::enable_if_t<std::is_arithmetic<U>::value ||
360 std::is_enum<U>::value>* = nullptr>
361 RTC_FORCE_INLINE LogStreamer<V, T, Ts...> operator<<(U arg) const {
362 return LogStreamer<V, T, Ts...>(MakeVal(arg), this);
Karl Wibergcefc4652018-05-23 23:20:38 +0200363 }
364
Amit Hilbuch10c5a932019-03-18 13:09:18 -0700365 template <typename U,
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200366 typename V = decltype(MakeVal(std::declval<U>())),
367 absl::enable_if_t<!std::is_arithmetic<U>::value &&
368 !std::is_enum<U>::value>* = nullptr>
369 RTC_FORCE_INLINE LogStreamer<V, T, Ts...> operator<<(const U& arg) const {
370 return LogStreamer<V, T, Ts...>(MakeVal(arg), this);
Karl Wibergcefc4652018-05-23 23:20:38 +0200371 }
372
373 template <typename... Us>
374 RTC_FORCE_INLINE void Call(const Us&... args) const {
375 prior_->Call(arg_, args...);
376 }
377
378 private:
379 // The most recent argument.
380 T arg_;
381
382 // Earlier arguments.
383 const LogStreamer<Ts...>* prior_;
384};
385
386class LogCall final {
387 public:
388 // This can be any binary operator with precedence lower than <<.
Artem Titov6a4a1462019-11-26 16:24:46 +0100389 // We return bool here to be able properly remove logging if
390 // RTC_DISABLE_LOGGING is defined.
Karl Wibergcefc4652018-05-23 23:20:38 +0200391 template <typename... Ts>
Artem Titov6a4a1462019-11-26 16:24:46 +0100392 RTC_FORCE_INLINE bool operator&(const LogStreamer<Ts...>& streamer) {
Karl Wibergcefc4652018-05-23 23:20:38 +0200393 streamer.Call();
Artem Titov6a4a1462019-11-26 16:24:46 +0100394 return true;
Karl Wibergcefc4652018-05-23 23:20:38 +0200395 }
396};
397
Mirko Bonadeif1df04b2020-03-23 19:53:23 +0100398// This class is used to explicitly ignore values in the conditional
399// logging macros. This avoids compiler warnings like "value computed
400// is not used" and "statement has no effect".
401class LogMessageVoidify {
402 public:
403 LogMessageVoidify() = default;
404 // This has to be an operator with a precedence lower than << but
405 // higher than ?:
406 template <typename... Ts>
407 void operator&(LogStreamer<Ts...>&& streamer) {}
408};
409
Karl Wibergcefc4652018-05-23 23:20:38 +0200410} // namespace webrtc_logging_impl
411
412// Direct use of this class is deprecated; please use the logging macros
413// instead.
414// TODO(bugs.webrtc.org/9278): Move this class to an unnamed namespace in the
415// .cc file.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200416class LogMessage {
417 public:
Karl Wiberg1ffb3742018-05-04 15:04:48 +0200418 // Same as the above, but using a compile-time constant for the logging
419 // severity. This saves space at the call site, since passing an empty struct
420 // is generally the same as not passing an argument at all.
421 template <LoggingSeverity S>
422 RTC_NO_INLINE LogMessage(const char* file,
423 int line,
424 std::integral_constant<LoggingSeverity, S>)
425 : LogMessage(file, line, S) {}
426
Artem Titov6a4a1462019-11-26 16:24:46 +0100427#if RTC_LOG_ENABLED()
428 LogMessage(const char* file, int line, LoggingSeverity sev);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200429 LogMessage(const char* file,
430 int line,
431 LoggingSeverity sev,
Karl Wibergab4f1c12018-05-04 10:42:28 +0200432 LogErrorContext err_ctx,
433 int err);
Tommie51a0a82018-02-27 15:30:29 +0100434#if defined(WEBRTC_ANDROID)
435 LogMessage(const char* file, int line, LoggingSeverity sev, const char* tag);
436#endif
Tommie51a0a82018-02-27 15:30:29 +0100437 // DEPRECATED - DO NOT USE - PLEASE USE THE MACROS INSTEAD OF THE CLASS.
438 // Android code should use the 'const char*' version since tags are static
439 // and we want to avoid allocating a std::string copy per log line.
Danil Chapovalove9041612021-02-22 12:43:39 +0100440 ABSL_DEPRECATED("Use RTC_LOG macros instead of accessing this class directly")
Yves Gerey665174f2018-06-19 15:03:05 +0200441 LogMessage(const char* file,
442 int line,
443 LoggingSeverity sev,
Philip Eliasson278aa422018-02-26 14:54:45 +0000444 const std::string& tag);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200445 ~LogMessage();
446
Karl Wibergcefc4652018-05-23 23:20:38 +0200447 void AddTag(const char* tag);
Jonas Olssond8c50782018-09-07 11:21:28 +0200448 rtc::StringBuilder& stream();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200449 // Returns the time at which this function was called for the first time.
450 // The time will be used as the logging start time.
451 // If this is not called externally, the LogMessage ctor also calls it, in
452 // which case the logging start time will be the time of the first LogMessage
453 // instance is created.
454 static int64_t LogStartTime();
Artem Titov96e3b992021-07-26 16:03:14 +0200455 // Returns the wall clock equivalent of `LogStartTime`, in seconds from the
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200456 // epoch.
457 static uint32_t WallClockStartTime();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200458 // LogThreads: Display the thread identifier of the current thread
459 static void LogThreads(bool on = true);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200460 // LogTimestamps: Display the elapsed time of the program
461 static void LogTimestamps(bool on = true);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200462 // These are the available logging channels
463 // Debug: Debug console on Windows, otherwise stderr
464 static void LogToDebug(LoggingSeverity min_sev);
Jonas Olsson2b6f1352018-02-15 11:57:03 +0100465 static LoggingSeverity GetLogToDebug();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200466 // Sets whether logs will be directed to stderr in debug mode.
467 static void SetLogToStderr(bool log_to_stderr);
Danil Chapovalovb9f69022019-10-21 09:19:10 +0200468 // Stream: Any non-blocking stream interface.
Artem Titov96e3b992021-07-26 16:03:14 +0200469 // Installs the `stream` to collect logs with severtiy `min_sev` or higher.
470 // `stream` must live until deinstalled by RemoveLogToStream.
471 // If `stream` is the first stream added to the system, we might miss some
Markus Handell531bd0f2020-07-08 10:58:19 +0200472 // early concurrent log statement happening from another thread happening near
473 // this instant.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200474 static void AddLogToStream(LogSink* stream, LoggingSeverity min_sev);
Markus Handell531bd0f2020-07-08 10:58:19 +0200475 // Removes the specified stream, without destroying it. When the method
Artem Titov96e3b992021-07-26 16:03:14 +0200476 // has completed, it's guaranteed that `stream` will receive no more logging
Markus Handell531bd0f2020-07-08 10:58:19 +0200477 // calls.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200478 static void RemoveLogToStream(LogSink* stream);
Danil Chapovalovb9f69022019-10-21 09:19:10 +0200479 // Returns the severity for the specified stream, of if none is specified,
480 // the minimum stream severity.
481 static int GetLogToStream(LogSink* stream = nullptr);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200482 // Testing against MinLogSeverity allows code to avoid potentially expensive
483 // logging operations by pre-checking the logging level.
Jonas Olsson2b6f1352018-02-15 11:57:03 +0100484 static int GetMinLogSeverity();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200485 // Parses the provided parameter stream to configure the options above.
486 // Useful for configuring logging from the command line.
487 static void ConfigureLogging(const char* params);
Artem Titov96e3b992021-07-26 16:03:14 +0200488 // Checks the current global debug severity and if the `streams_` collection
489 // is empty. If `severity` is smaller than the global severity and if the
490 // `streams_` collection is empty, the LogMessage will be considered a noop
Jonas Olssond8c50782018-09-07 11:21:28 +0200491 // LogMessage.
492 static bool IsNoop(LoggingSeverity severity);
Karl Wibergdd7df5c2020-09-22 11:07:53 +0200493 // Version of IsNoop that uses fewer instructions at the call site, since the
494 // caller doesn't have to pass an argument.
495 template <LoggingSeverity S>
496 RTC_NO_INLINE static bool IsNoop() {
497 return IsNoop(S);
498 }
Artem Titov6a4a1462019-11-26 16:24:46 +0100499#else
500 // Next methods do nothing; no one will call these functions.
501 LogMessage(const char* file, int line, LoggingSeverity sev) {}
502 LogMessage(const char* file,
503 int line,
504 LoggingSeverity sev,
505 LogErrorContext err_ctx,
506 int err) {}
507#if defined(WEBRTC_ANDROID)
508 LogMessage(const char* file, int line, LoggingSeverity sev, const char* tag) {
509 }
510#endif
511 // DEPRECATED - DO NOT USE - PLEASE USE THE MACROS INSTEAD OF THE CLASS.
512 // Android code should use the 'const char*' version since tags are static
513 // and we want to avoid allocating a std::string copy per log line.
Danil Chapovalove9041612021-02-22 12:43:39 +0100514 ABSL_DEPRECATED("Use RTC_LOG macros instead of accessing this class directly")
Artem Titov6a4a1462019-11-26 16:24:46 +0100515 LogMessage(const char* file,
516 int line,
517 LoggingSeverity sev,
518 const std::string& tag) {}
519 ~LogMessage() = default;
520
521 inline void AddTag(const char* tag) {}
522 inline rtc::StringBuilder& stream() { return print_stream_; }
523 inline static int64_t LogStartTime() { return 0; }
524 inline static uint32_t WallClockStartTime() { return 0; }
525 inline static void LogThreads(bool on = true) {}
526 inline static void LogTimestamps(bool on = true) {}
527 inline static void LogToDebug(LoggingSeverity min_sev) {}
528 inline static LoggingSeverity GetLogToDebug() {
529 return LoggingSeverity::LS_INFO;
530 }
531 inline static void SetLogToStderr(bool log_to_stderr) {}
532 inline static void AddLogToStream(LogSink* stream, LoggingSeverity min_sev) {}
533 inline static void RemoveLogToStream(LogSink* stream) {}
534 inline static int GetLogToStream(LogSink* stream = nullptr) { return 0; }
535 inline static int GetMinLogSeverity() { return 0; }
536 inline static void ConfigureLogging(const char* params) {}
Karl Wiberg0e884382020-09-22 09:34:45 +0200537 static constexpr bool IsNoop(LoggingSeverity severity) { return true; }
Karl Wibergdd7df5c2020-09-22 11:07:53 +0200538 template <LoggingSeverity S>
539 static constexpr bool IsNoop() {
540 return IsNoop(S);
541 }
Artem Titov6a4a1462019-11-26 16:24:46 +0100542#endif // RTC_LOG_ENABLED()
Jonas Olssond8c50782018-09-07 11:21:28 +0200543
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200544 private:
Tommifef05002018-02-27 13:51:08 +0100545 friend class LogMessageForTesting;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200546
Artem Titov6a4a1462019-11-26 16:24:46 +0100547#if RTC_LOG_ENABLED()
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200548 // Updates min_sev_ appropriately when debug sinks change.
549 static void UpdateMinLogSeverity();
550
Yves Gerey665174f2018-06-19 15:03:05 +0200551// These write out the actual log messages.
Tommie51a0a82018-02-27 15:30:29 +0100552#if defined(WEBRTC_ANDROID)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200553 static void OutputToDebug(const std::string& msg,
554 LoggingSeverity severity,
Tommie51a0a82018-02-27 15:30:29 +0100555 const char* tag);
556#else
557 static void OutputToDebug(const std::string& msg, LoggingSeverity severity);
Artem Titov6a4a1462019-11-26 16:24:46 +0100558#endif // defined(WEBRTC_ANDROID)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200559
Tommifef05002018-02-27 13:51:08 +0100560 // Called from the dtor (or from a test) to append optional extra error
561 // information to the log stream and a newline character.
562 void FinishPrintStream();
563
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200564 // The severity level of this message
565 LoggingSeverity severity_;
566
Tommie51a0a82018-02-27 15:30:29 +0100567#if defined(WEBRTC_ANDROID)
Paulina Hensmanf1e3cb42018-06-20 14:07:05 +0200568 // The default Android debug output tag.
Tommie51a0a82018-02-27 15:30:29 +0100569 const char* tag_ = "libjingle";
570#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200571
572 // String data generated in the constructor, that should be appended to
573 // the message before output.
574 std::string extra_;
575
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200576 // The output streams and their associated severities
Danil Chapovalovb9f69022019-10-21 09:19:10 +0200577 static LogSink* streams_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200578
Artem Titov96e3b992021-07-26 16:03:14 +0200579 // Holds true with high probability if `streams_` is empty, false with high
Markus Handellce1ff6f2020-07-08 08:52:48 +0200580 // probability otherwise. Operated on with std::memory_order_relaxed because
Markus Handell531bd0f2020-07-08 10:58:19 +0200581 // it's ok to lose or log some additional statements near the instant streams
Markus Handellce1ff6f2020-07-08 08:52:48 +0200582 // are added/removed.
583 static std::atomic<bool> streams_empty_;
584
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200585 // Flags for formatting options
586 static bool thread_, timestamp_;
587
588 // Determines if logs will be directed to stderr in debug mode.
589 static bool log_to_stderr_;
Artem Titov6a4a1462019-11-26 16:24:46 +0100590#else // RTC_LOG_ENABLED()
591 // Next methods do nothing; no one will call these functions.
592 inline static void UpdateMinLogSeverity() {}
593#if defined(WEBRTC_ANDROID)
594 inline static void OutputToDebug(const std::string& msg,
595 LoggingSeverity severity,
596 const char* tag) {}
597#else
598 inline static void OutputToDebug(const std::string& msg,
599 LoggingSeverity severity) {}
600#endif // defined(WEBRTC_ANDROID)
601 inline void FinishPrintStream() {}
602#endif // RTC_LOG_ENABLED()
603
604 // The stringbuilder that buffers the formatted message before output
605 rtc::StringBuilder print_stream_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200606
607 RTC_DISALLOW_COPY_AND_ASSIGN(LogMessage);
608};
609
610//////////////////////////////////////////////////////////////////////
611// Logging Helpers
612//////////////////////////////////////////////////////////////////////
613
Karl Wibergdd7df5c2020-09-22 11:07:53 +0200614#define RTC_LOG_FILE_LINE(sev, file, line) \
615 ::rtc::webrtc_logging_impl::LogCall() & \
616 ::rtc::webrtc_logging_impl::LogStreamer<>() \
617 << ::rtc::webrtc_logging_impl::LogMetadata(file, line, sev)
Jonas Olsson8a7916b2018-09-06 09:50:23 +0200618
Karl Wibergdd7df5c2020-09-22 11:07:53 +0200619#define RTC_LOG(sev) \
620 !rtc::LogMessage::IsNoop<::rtc::sev>() && \
621 RTC_LOG_FILE_LINE(::rtc::sev, __FILE__, __LINE__)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200622
Karl Wibergcefc4652018-05-23 23:20:38 +0200623// The _V version is for when a variable is passed in.
Karl Wibergdd7df5c2020-09-22 11:07:53 +0200624#define RTC_LOG_V(sev) \
625 !rtc::LogMessage::IsNoop(sev) && RTC_LOG_FILE_LINE(sev, __FILE__, __LINE__)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200626
627// The _F version prefixes the message with the current function name.
628#if (defined(__GNUC__) && !defined(NDEBUG)) || defined(WANT_PRETTY_LOG_F)
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200629#define RTC_LOG_F(sev) RTC_LOG(sev) << __PRETTY_FUNCTION__ << ": "
Yves Gerey665174f2018-06-19 15:03:05 +0200630#define RTC_LOG_T_F(sev) \
631 RTC_LOG(sev) << this << ": " << __PRETTY_FUNCTION__ << ": "
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200632#else
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200633#define RTC_LOG_F(sev) RTC_LOG(sev) << __FUNCTION__ << ": "
Patrik Höglundc2962552017-11-17 13:40:22 +0000634#define RTC_LOG_T_F(sev) RTC_LOG(sev) << this << ": " << __FUNCTION__ << ": "
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200635#endif
636
Jiawei Ou14e5f0b2020-03-04 13:38:02 -0800637#define RTC_LOG_CHECK_LEVEL(sev) ::rtc::LogCheckLevel(::rtc::sev)
638#define RTC_LOG_CHECK_LEVEL_V(sev) ::rtc::LogCheckLevel(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200639
640inline bool LogCheckLevel(LoggingSeverity sev) {
641 return (LogMessage::GetMinLogSeverity() <= sev);
642}
643
Karl Wiberg92e37962020-09-22 13:22:20 +0200644#define RTC_LOG_E(sev, ctx, err) \
645 !rtc::LogMessage::IsNoop<::rtc::sev>() && \
646 ::rtc::webrtc_logging_impl::LogCall() & \
647 ::rtc::webrtc_logging_impl::LogStreamer<>() \
648 << ::rtc::webrtc_logging_impl::LogMetadataErr { \
649 {__FILE__, __LINE__, ::rtc::sev}, ::rtc::ERRCTX_##ctx, (err) \
Jonas Olssona4d87372019-07-05 19:08:33 +0200650 }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200651
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200652#define RTC_LOG_T(sev) RTC_LOG(sev) << this << ": "
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200653
Yves Gerey665174f2018-06-19 15:03:05 +0200654#define RTC_LOG_ERRNO_EX(sev, err) RTC_LOG_E(sev, ERRNO, err)
655#define RTC_LOG_ERRNO(sev) RTC_LOG_ERRNO_EX(sev, errno)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200656
657#if defined(WEBRTC_WIN)
Yves Gerey665174f2018-06-19 15:03:05 +0200658#define RTC_LOG_GLE_EX(sev, err) RTC_LOG_E(sev, HRESULT, err)
Karl Wibergcefc4652018-05-23 23:20:38 +0200659#define RTC_LOG_GLE(sev) RTC_LOG_GLE_EX(sev, static_cast<int>(GetLastError()))
Yves Gerey665174f2018-06-19 15:03:05 +0200660#define RTC_LOG_ERR_EX(sev, err) RTC_LOG_GLE_EX(sev, err)
661#define RTC_LOG_ERR(sev) RTC_LOG_GLE(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200662#elif defined(__native_client__) && __native_client__
Yves Gerey665174f2018-06-19 15:03:05 +0200663#define RTC_LOG_ERR_EX(sev, err) RTC_LOG(sev)
664#define RTC_LOG_ERR(sev) RTC_LOG(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200665#elif defined(WEBRTC_POSIX)
Yves Gerey665174f2018-06-19 15:03:05 +0200666#define RTC_LOG_ERR_EX(sev, err) RTC_LOG_ERRNO_EX(sev, err)
667#define RTC_LOG_ERR(sev) RTC_LOG_ERRNO(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200668#endif // WEBRTC_WIN
669
Karl Wibergcefc4652018-05-23 23:20:38 +0200670#ifdef WEBRTC_ANDROID
671
672namespace webrtc_logging_impl {
673// TODO(kwiberg): Replace these with absl::string_view.
Yves Gerey665174f2018-06-19 15:03:05 +0200674inline const char* AdaptString(const char* str) {
675 return str;
676}
677inline const char* AdaptString(const std::string& str) {
678 return str.c_str();
679}
Karl Wibergcefc4652018-05-23 23:20:38 +0200680} // namespace webrtc_logging_impl
681
Karl Wiberg92e37962020-09-22 13:22:20 +0200682#define RTC_LOG_TAG(sev, tag) \
683 !rtc::LogMessage::IsNoop(sev) && \
684 ::rtc::webrtc_logging_impl::LogCall() & \
685 ::rtc::webrtc_logging_impl::LogStreamer<>() \
686 << ::rtc::webrtc_logging_impl::LogMetadataTag { \
687 sev, ::rtc::webrtc_logging_impl::AdaptString(tag) \
Jonas Olssona4d87372019-07-05 19:08:33 +0200688 }
Karl Wibergcefc4652018-05-23 23:20:38 +0200689
Tommie51a0a82018-02-27 15:30:29 +0100690#else
Karl Wibergcefc4652018-05-23 23:20:38 +0200691
Tommie51a0a82018-02-27 15:30:29 +0100692// DEPRECATED. This macro is only intended for Android.
Karl Wibergcefc4652018-05-23 23:20:38 +0200693#define RTC_LOG_TAG(sev, tag) RTC_LOG_V(sev)
694
Tommie51a0a82018-02-27 15:30:29 +0100695#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200696
Fredrik Solenbergb3d7cac2017-11-17 15:22:37 +0100697// The RTC_DLOG macros are equivalent to their RTC_LOG counterparts except that
698// they only generate code in debug builds.
699#if RTC_DLOG_IS_ON
700#define RTC_DLOG(sev) RTC_LOG(sev)
701#define RTC_DLOG_V(sev) RTC_LOG_V(sev)
702#define RTC_DLOG_F(sev) RTC_LOG_F(sev)
703#else
Mirko Bonadeif1df04b2020-03-23 19:53:23 +0100704#define RTC_DLOG_EAT_STREAM_PARAMS() \
705 while (false) \
706 ::rtc::webrtc_logging_impl::LogMessageVoidify() & \
707 (::rtc::webrtc_logging_impl::LogStreamer<>())
Karl Wibergcefc4652018-05-23 23:20:38 +0200708#define RTC_DLOG(sev) RTC_DLOG_EAT_STREAM_PARAMS()
709#define RTC_DLOG_V(sev) RTC_DLOG_EAT_STREAM_PARAMS()
710#define RTC_DLOG_F(sev) RTC_DLOG_EAT_STREAM_PARAMS()
Fredrik Solenbergb3d7cac2017-11-17 15:22:37 +0100711#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200712
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200713} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000714
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200715#endif // RTC_BASE_LOGGING_H_