blob: b6fdf9c3a979b2c90ecece2024d1600a8dd985d9 [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 Alvestranda18cad92021-11-23 13:53:29 +000097 // LERROR [[deprecated("Use LS_ERROR")]] = LS_ERROR
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020098 LERROR = LS_ERROR
99};
100
101// LogErrorContext assists in interpreting the meaning of an error value.
102enum LogErrorContext {
103 ERRCTX_NONE,
Jonas Olssona4d87372019-07-05 19:08:33 +0200104 ERRCTX_ERRNO, // System-local errno
105 ERRCTX_HRESULT, // Windows HRESULT
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200106
107 // Abbreviations for LOG_E macro
Jonas Olssona4d87372019-07-05 19:08:33 +0200108 ERRCTX_EN = ERRCTX_ERRNO, // LOG_E(sev, EN, x)
109 ERRCTX_HR = ERRCTX_HRESULT, // LOG_E(sev, HR, x)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200110};
111
Danil Chapovalovb9f69022019-10-21 09:19:10 +0200112class LogMessage;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200113// Virtual sink interface that can receive log messages.
114class LogSink {
115 public:
116 LogSink() {}
117 virtual ~LogSink() {}
Paulina Hensmanf1e3cb42018-06-20 14:07:05 +0200118 virtual void OnLogMessage(const std::string& msg,
119 LoggingSeverity severity,
120 const char* tag);
Jiawei Ou3ea18782018-10-31 23:14:24 -0700121 virtual void OnLogMessage(const std::string& message,
122 LoggingSeverity severity);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200123 virtual void OnLogMessage(const std::string& message) = 0;
Danil Chapovalovb9f69022019-10-21 09:19:10 +0200124
125 private:
126 friend class ::rtc::LogMessage;
Artem Titov6a4a1462019-11-26 16:24:46 +0100127#if RTC_LOG_ENABLED()
Danil Chapovalovb9f69022019-10-21 09:19:10 +0200128 // Members for LogMessage class to keep linked list of the registered sinks.
129 LogSink* next_ = nullptr;
130 LoggingSeverity min_severity_;
Artem Titov6a4a1462019-11-26 16:24:46 +0100131#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200132};
133
Karl Wibergcefc4652018-05-23 23:20:38 +0200134namespace webrtc_logging_impl {
135
136class LogMetadata {
137 public:
138 LogMetadata(const char* file, int line, LoggingSeverity severity)
139 : file_(file),
140 line_and_sev_(static_cast<uint32_t>(line) << 3 | severity) {}
141 LogMetadata() = default;
142
143 const char* File() const { return file_; }
144 int Line() const { return line_and_sev_ >> 3; }
145 LoggingSeverity Severity() const {
146 return static_cast<LoggingSeverity>(line_and_sev_ & 0x7);
147 }
148
149 private:
150 const char* file_;
151
152 // Line number and severity, the former in the most significant 29 bits, the
153 // latter in the least significant 3 bits. (This is an optimization; since
154 // both numbers are usually compile-time constants, this way we can load them
155 // both with a single instruction.)
156 uint32_t line_and_sev_;
157};
158static_assert(std::is_trivial<LogMetadata>::value, "");
159
160struct LogMetadataErr {
161 LogMetadata meta;
162 LogErrorContext err_ctx;
163 int err;
164};
165
166#ifdef WEBRTC_ANDROID
167struct LogMetadataTag {
168 LoggingSeverity severity;
169 const char* tag;
170};
171#endif
172
173enum class LogArgType : int8_t {
174 kEnd = 0,
175 kInt,
176 kLong,
177 kLongLong,
178 kUInt,
179 kULong,
180 kULongLong,
181 kDouble,
182 kLongDouble,
183 kCharP,
184 kStdString,
Jonas Olssonf2ce37c2018-09-12 15:32:47 +0200185 kStringView,
Karl Wibergcefc4652018-05-23 23:20:38 +0200186 kVoidP,
187 kLogMetadata,
188 kLogMetadataErr,
189#ifdef WEBRTC_ANDROID
190 kLogMetadataTag,
191#endif
192};
193
194// Wrapper for log arguments. Only ever make values of this type with the
195// MakeVal() functions.
196template <LogArgType N, typename T>
197struct Val {
198 static constexpr LogArgType Type() { return N; }
199 T GetVal() const { return val; }
200 T val;
201};
202
Sebastian Janssonb1138622019-04-11 16:48:15 +0200203// Case for when we need to construct a temp string and then print that.
204// (We can't use Val<CheckArgType::kStdString, const std::string*>
205// because we need somewhere to store the temp string.)
206struct ToStringVal {
Karl Wibergcefc4652018-05-23 23:20:38 +0200207 static constexpr LogArgType Type() { return LogArgType::kStdString; }
208 const std::string* GetVal() const { return &val; }
209 std::string val;
210};
211
212inline Val<LogArgType::kInt, int> MakeVal(int x) {
213 return {x};
214}
215inline Val<LogArgType::kLong, long> MakeVal(long x) {
216 return {x};
217}
218inline Val<LogArgType::kLongLong, long long> MakeVal(long long x) {
219 return {x};
220}
221inline Val<LogArgType::kUInt, unsigned int> MakeVal(unsigned int x) {
222 return {x};
223}
224inline Val<LogArgType::kULong, unsigned long> MakeVal(unsigned long x) {
225 return {x};
226}
227inline Val<LogArgType::kULongLong, unsigned long long> MakeVal(
228 unsigned long long x) {
229 return {x};
230}
231
232inline Val<LogArgType::kDouble, double> MakeVal(double x) {
233 return {x};
234}
235inline Val<LogArgType::kLongDouble, long double> MakeVal(long double x) {
236 return {x};
237}
238
239inline Val<LogArgType::kCharP, const char*> MakeVal(const char* x) {
240 return {x};
241}
242inline Val<LogArgType::kStdString, const std::string*> MakeVal(
243 const std::string& x) {
244 return {&x};
245}
Jonas Olssonf2ce37c2018-09-12 15:32:47 +0200246inline Val<LogArgType::kStringView, const absl::string_view*> MakeVal(
247 const absl::string_view& x) {
248 return {&x};
249}
Karl Wibergcefc4652018-05-23 23:20:38 +0200250
251inline Val<LogArgType::kVoidP, const void*> MakeVal(const void* x) {
252 return {x};
253}
254
255inline Val<LogArgType::kLogMetadata, LogMetadata> MakeVal(
256 const LogMetadata& x) {
257 return {x};
258}
259inline Val<LogArgType::kLogMetadataErr, LogMetadataErr> MakeVal(
260 const LogMetadataErr& x) {
261 return {x};
262}
263
Amit Hilbuch10c5a932019-03-18 13:09:18 -0700264// The enum class types are not implicitly convertible to arithmetic types.
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200265template <typename T,
266 absl::enable_if_t<std::is_enum<T>::value &&
267 !std::is_arithmetic<T>::value>* = nullptr>
268inline decltype(MakeVal(std::declval<absl::underlying_type_t<T>>())) MakeVal(
269 T x) {
270 return {static_cast<absl::underlying_type_t<T>>(x)};
Amit Hilbuch10c5a932019-03-18 13:09:18 -0700271}
272
Karl Wibergcefc4652018-05-23 23:20:38 +0200273#ifdef WEBRTC_ANDROID
274inline Val<LogArgType::kLogMetadataTag, LogMetadataTag> MakeVal(
275 const LogMetadataTag& x) {
276 return {x};
277}
278#endif
279
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200280template <typename T, class = void>
Sebastian Janssonb1138622019-04-11 16:48:15 +0200281struct has_to_log_string : std::false_type {};
282template <typename T>
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200283struct has_to_log_string<T, decltype(ToLogString(std::declval<T>()))>
Sebastian Janssonb1138622019-04-11 16:48:15 +0200284 : std::true_type {};
285
Karl Wibergcefc4652018-05-23 23:20:38 +0200286// Handle arbitrary types other than the above by falling back to stringstream.
287// TODO(bugs.webrtc.org/9278): Get rid of this overload when callers don't need
288// it anymore. No in-tree caller does, but some external callers still do.
289template <
290 typename T,
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200291 typename T1 = absl::decay_t<T>,
292 absl::enable_if_t<std::is_class<T1>::value &&
293 !std::is_same<T1, std::string>::value &&
294 !std::is_same<T1, LogMetadata>::value &&
295 !has_to_log_string<T1>::value &&
Karl Wibergcefc4652018-05-23 23:20:38 +0200296#ifdef WEBRTC_ANDROID
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200297 !std::is_same<T1, LogMetadataTag>::value &&
Karl Wibergcefc4652018-05-23 23:20:38 +0200298#endif
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200299 !std::is_same<T1, LogMetadataErr>::value>* = nullptr>
Sebastian Janssonb1138622019-04-11 16:48:15 +0200300ToStringVal MakeVal(const T& x) {
Karl Wibergcefc4652018-05-23 23:20:38 +0200301 std::ostringstream os; // no-presubmit-check TODO(webrtc:8982)
302 os << x;
303 return {os.str()};
304}
305
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200306template <typename T, absl::enable_if_t<has_to_log_string<T>::value>* = nullptr>
Sebastian Janssonb1138622019-04-11 16:48:15 +0200307ToStringVal MakeVal(const T& x) {
308 return {ToLogString(x)};
309}
310
Artem Titov6a4a1462019-11-26 16:24:46 +0100311#if RTC_LOG_ENABLED()
Karl Wibergcefc4652018-05-23 23:20:38 +0200312void Log(const LogArgType* fmt, ...);
Artem Titov6a4a1462019-11-26 16:24:46 +0100313#else
314inline void Log(const LogArgType* fmt, ...) {
315 // Do nothing, shouldn't be invoked
316}
317#endif
Karl Wibergcefc4652018-05-23 23:20:38 +0200318
319// Ephemeral type that represents the result of the logging << operator.
320template <typename... Ts>
321class LogStreamer;
322
323// Base case: Before the first << argument.
324template <>
325class LogStreamer<> final {
326 public:
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<<(U arg) const {
332 return LogStreamer<V>(MakeVal(arg), this);
Karl Wibergcefc4652018-05-23 23:20:38 +0200333 }
334
Amit Hilbuch10c5a932019-03-18 13:09:18 -0700335 template <typename U,
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200336 typename V = decltype(MakeVal(std::declval<U>())),
337 absl::enable_if_t<!std::is_arithmetic<U>::value &&
338 !std::is_enum<U>::value>* = nullptr>
339 RTC_FORCE_INLINE LogStreamer<V> operator<<(const U& arg) const {
340 return LogStreamer<V>(MakeVal(arg), this);
Karl Wibergcefc4652018-05-23 23:20:38 +0200341 }
342
343 template <typename... Us>
344 RTC_FORCE_INLINE static void Call(const Us&... args) {
345 static constexpr LogArgType t[] = {Us::Type()..., LogArgType::kEnd};
346 Log(t, args.GetVal()...);
347 }
348};
349
350// Inductive case: We've already seen at least one << argument. The most recent
351// one had type `T`, and the earlier ones had types `Ts`.
352template <typename T, typename... Ts>
353class LogStreamer<T, Ts...> final {
354 public:
355 RTC_FORCE_INLINE LogStreamer(T arg, const LogStreamer<Ts...>* prior)
356 : arg_(arg), prior_(prior) {}
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<<(U arg) const {
363 return LogStreamer<V, T, Ts...>(MakeVal(arg), this);
Karl Wibergcefc4652018-05-23 23:20:38 +0200364 }
365
Amit Hilbuch10c5a932019-03-18 13:09:18 -0700366 template <typename U,
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200367 typename V = decltype(MakeVal(std::declval<U>())),
368 absl::enable_if_t<!std::is_arithmetic<U>::value &&
369 !std::is_enum<U>::value>* = nullptr>
370 RTC_FORCE_INLINE LogStreamer<V, T, Ts...> operator<<(const U& arg) const {
371 return LogStreamer<V, T, Ts...>(MakeVal(arg), this);
Karl Wibergcefc4652018-05-23 23:20:38 +0200372 }
373
374 template <typename... Us>
375 RTC_FORCE_INLINE void Call(const Us&... args) const {
376 prior_->Call(arg_, args...);
377 }
378
379 private:
380 // The most recent argument.
381 T arg_;
382
383 // Earlier arguments.
384 const LogStreamer<Ts...>* prior_;
385};
386
387class LogCall final {
388 public:
389 // This can be any binary operator with precedence lower than <<.
Artem Titov6a4a1462019-11-26 16:24:46 +0100390 // We return bool here to be able properly remove logging if
391 // RTC_DISABLE_LOGGING is defined.
Karl Wibergcefc4652018-05-23 23:20:38 +0200392 template <typename... Ts>
Artem Titov6a4a1462019-11-26 16:24:46 +0100393 RTC_FORCE_INLINE bool operator&(const LogStreamer<Ts...>& streamer) {
Karl Wibergcefc4652018-05-23 23:20:38 +0200394 streamer.Call();
Artem Titov6a4a1462019-11-26 16:24:46 +0100395 return true;
Karl Wibergcefc4652018-05-23 23:20:38 +0200396 }
397};
398
Mirko Bonadeif1df04b2020-03-23 19:53:23 +0100399// This class is used to explicitly ignore values in the conditional
400// logging macros. This avoids compiler warnings like "value computed
401// is not used" and "statement has no effect".
402class LogMessageVoidify {
403 public:
404 LogMessageVoidify() = default;
405 // This has to be an operator with a precedence lower than << but
406 // higher than ?:
407 template <typename... Ts>
408 void operator&(LogStreamer<Ts...>&& streamer) {}
409};
410
Karl Wibergcefc4652018-05-23 23:20:38 +0200411} // namespace webrtc_logging_impl
412
413// Direct use of this class is deprecated; please use the logging macros
414// instead.
415// TODO(bugs.webrtc.org/9278): Move this class to an unnamed namespace in the
416// .cc file.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200417class LogMessage {
418 public:
Karl Wiberg1ffb3742018-05-04 15:04:48 +0200419 // Same as the above, but using a compile-time constant for the logging
420 // severity. This saves space at the call site, since passing an empty struct
421 // is generally the same as not passing an argument at all.
422 template <LoggingSeverity S>
423 RTC_NO_INLINE LogMessage(const char* file,
424 int line,
425 std::integral_constant<LoggingSeverity, S>)
426 : LogMessage(file, line, S) {}
427
Artem Titov6a4a1462019-11-26 16:24:46 +0100428#if RTC_LOG_ENABLED()
429 LogMessage(const char* file, int line, LoggingSeverity sev);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200430 LogMessage(const char* file,
431 int line,
432 LoggingSeverity sev,
Karl Wibergab4f1c12018-05-04 10:42:28 +0200433 LogErrorContext err_ctx,
434 int err);
Tommie51a0a82018-02-27 15:30:29 +0100435#if defined(WEBRTC_ANDROID)
436 LogMessage(const char* file, int line, LoggingSeverity sev, const char* tag);
437#endif
Tommie51a0a82018-02-27 15:30:29 +0100438 // DEPRECATED - DO NOT USE - PLEASE USE THE MACROS INSTEAD OF THE CLASS.
439 // Android code should use the 'const char*' version since tags are static
440 // and we want to avoid allocating a std::string copy per log line.
Danil Chapovalove9041612021-02-22 12:43:39 +0100441 ABSL_DEPRECATED("Use RTC_LOG macros instead of accessing this class directly")
Yves Gerey665174f2018-06-19 15:03:05 +0200442 LogMessage(const char* file,
443 int line,
444 LoggingSeverity sev,
Philip Eliasson278aa422018-02-26 14:54:45 +0000445 const std::string& tag);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200446 ~LogMessage();
447
Karl Wibergcefc4652018-05-23 23:20:38 +0200448 void AddTag(const char* tag);
Jonas Olssond8c50782018-09-07 11:21:28 +0200449 rtc::StringBuilder& stream();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200450 // Returns the time at which this function was called for the first time.
451 // The time will be used as the logging start time.
452 // If this is not called externally, the LogMessage ctor also calls it, in
453 // which case the logging start time will be the time of the first LogMessage
454 // instance is created.
455 static int64_t LogStartTime();
Artem Titov96e3b992021-07-26 16:03:14 +0200456 // Returns the wall clock equivalent of `LogStartTime`, in seconds from the
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200457 // epoch.
458 static uint32_t WallClockStartTime();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200459 // LogThreads: Display the thread identifier of the current thread
460 static void LogThreads(bool on = true);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200461 // LogTimestamps: Display the elapsed time of the program
462 static void LogTimestamps(bool on = true);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200463 // These are the available logging channels
464 // Debug: Debug console on Windows, otherwise stderr
465 static void LogToDebug(LoggingSeverity min_sev);
Jonas Olsson2b6f1352018-02-15 11:57:03 +0100466 static LoggingSeverity GetLogToDebug();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200467 // Sets whether logs will be directed to stderr in debug mode.
468 static void SetLogToStderr(bool log_to_stderr);
Danil Chapovalovb9f69022019-10-21 09:19:10 +0200469 // Stream: Any non-blocking stream interface.
Artem Titov96e3b992021-07-26 16:03:14 +0200470 // Installs the `stream` to collect logs with severtiy `min_sev` or higher.
471 // `stream` must live until deinstalled by RemoveLogToStream.
472 // If `stream` is the first stream added to the system, we might miss some
Markus Handell531bd0f2020-07-08 10:58:19 +0200473 // early concurrent log statement happening from another thread happening near
474 // this instant.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200475 static void AddLogToStream(LogSink* stream, LoggingSeverity min_sev);
Markus Handell531bd0f2020-07-08 10:58:19 +0200476 // Removes the specified stream, without destroying it. When the method
Artem Titov96e3b992021-07-26 16:03:14 +0200477 // has completed, it's guaranteed that `stream` will receive no more logging
Markus Handell531bd0f2020-07-08 10:58:19 +0200478 // calls.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200479 static void RemoveLogToStream(LogSink* stream);
Danil Chapovalovb9f69022019-10-21 09:19:10 +0200480 // Returns the severity for the specified stream, of if none is specified,
481 // the minimum stream severity.
482 static int GetLogToStream(LogSink* stream = nullptr);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200483 // Testing against MinLogSeverity allows code to avoid potentially expensive
484 // logging operations by pre-checking the logging level.
Jonas Olsson2b6f1352018-02-15 11:57:03 +0100485 static int GetMinLogSeverity();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200486 // Parses the provided parameter stream to configure the options above.
487 // Useful for configuring logging from the command line.
488 static void ConfigureLogging(const char* params);
Artem Titov96e3b992021-07-26 16:03:14 +0200489 // Checks the current global debug severity and if the `streams_` collection
490 // is empty. If `severity` is smaller than the global severity and if the
491 // `streams_` collection is empty, the LogMessage will be considered a noop
Jonas Olssond8c50782018-09-07 11:21:28 +0200492 // LogMessage.
493 static bool IsNoop(LoggingSeverity severity);
Karl Wibergdd7df5c2020-09-22 11:07:53 +0200494 // Version of IsNoop that uses fewer instructions at the call site, since the
495 // caller doesn't have to pass an argument.
496 template <LoggingSeverity S>
497 RTC_NO_INLINE static bool IsNoop() {
498 return IsNoop(S);
499 }
Artem Titov6a4a1462019-11-26 16:24:46 +0100500#else
501 // Next methods do nothing; no one will call these functions.
502 LogMessage(const char* file, int line, LoggingSeverity sev) {}
503 LogMessage(const char* file,
504 int line,
505 LoggingSeverity sev,
506 LogErrorContext err_ctx,
507 int err) {}
508#if defined(WEBRTC_ANDROID)
509 LogMessage(const char* file, int line, LoggingSeverity sev, const char* tag) {
510 }
511#endif
512 // DEPRECATED - DO NOT USE - PLEASE USE THE MACROS INSTEAD OF THE CLASS.
513 // Android code should use the 'const char*' version since tags are static
514 // and we want to avoid allocating a std::string copy per log line.
Danil Chapovalove9041612021-02-22 12:43:39 +0100515 ABSL_DEPRECATED("Use RTC_LOG macros instead of accessing this class directly")
Artem Titov6a4a1462019-11-26 16:24:46 +0100516 LogMessage(const char* file,
517 int line,
518 LoggingSeverity sev,
519 const std::string& tag) {}
520 ~LogMessage() = default;
521
522 inline void AddTag(const char* tag) {}
523 inline rtc::StringBuilder& stream() { return print_stream_; }
524 inline static int64_t LogStartTime() { return 0; }
525 inline static uint32_t WallClockStartTime() { return 0; }
526 inline static void LogThreads(bool on = true) {}
527 inline static void LogTimestamps(bool on = true) {}
528 inline static void LogToDebug(LoggingSeverity min_sev) {}
529 inline static LoggingSeverity GetLogToDebug() {
530 return LoggingSeverity::LS_INFO;
531 }
532 inline static void SetLogToStderr(bool log_to_stderr) {}
533 inline static void AddLogToStream(LogSink* stream, LoggingSeverity min_sev) {}
534 inline static void RemoveLogToStream(LogSink* stream) {}
535 inline static int GetLogToStream(LogSink* stream = nullptr) { return 0; }
536 inline static int GetMinLogSeverity() { return 0; }
537 inline static void ConfigureLogging(const char* params) {}
Karl Wiberg0e884382020-09-22 09:34:45 +0200538 static constexpr bool IsNoop(LoggingSeverity severity) { return true; }
Karl Wibergdd7df5c2020-09-22 11:07:53 +0200539 template <LoggingSeverity S>
540 static constexpr bool IsNoop() {
541 return IsNoop(S);
542 }
Artem Titov6a4a1462019-11-26 16:24:46 +0100543#endif // RTC_LOG_ENABLED()
Jonas Olssond8c50782018-09-07 11:21:28 +0200544
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200545 private:
Tommifef05002018-02-27 13:51:08 +0100546 friend class LogMessageForTesting;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200547
Artem Titov6a4a1462019-11-26 16:24:46 +0100548#if RTC_LOG_ENABLED()
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200549 // Updates min_sev_ appropriately when debug sinks change.
550 static void UpdateMinLogSeverity();
551
Yves Gerey665174f2018-06-19 15:03:05 +0200552// These write out the actual log messages.
Tommie51a0a82018-02-27 15:30:29 +0100553#if defined(WEBRTC_ANDROID)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200554 static void OutputToDebug(const std::string& msg,
555 LoggingSeverity severity,
Tommie51a0a82018-02-27 15:30:29 +0100556 const char* tag);
557#else
558 static void OutputToDebug(const std::string& msg, LoggingSeverity severity);
Artem Titov6a4a1462019-11-26 16:24:46 +0100559#endif // defined(WEBRTC_ANDROID)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200560
Tommifef05002018-02-27 13:51:08 +0100561 // Called from the dtor (or from a test) to append optional extra error
562 // information to the log stream and a newline character.
563 void FinishPrintStream();
564
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200565 // The severity level of this message
566 LoggingSeverity severity_;
567
Tommie51a0a82018-02-27 15:30:29 +0100568#if defined(WEBRTC_ANDROID)
Paulina Hensmanf1e3cb42018-06-20 14:07:05 +0200569 // The default Android debug output tag.
Tommie51a0a82018-02-27 15:30:29 +0100570 const char* tag_ = "libjingle";
571#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200572
573 // String data generated in the constructor, that should be appended to
574 // the message before output.
575 std::string extra_;
576
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200577 // The output streams and their associated severities
Danil Chapovalovb9f69022019-10-21 09:19:10 +0200578 static LogSink* streams_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200579
Artem Titov96e3b992021-07-26 16:03:14 +0200580 // Holds true with high probability if `streams_` is empty, false with high
Markus Handellce1ff6f2020-07-08 08:52:48 +0200581 // probability otherwise. Operated on with std::memory_order_relaxed because
Markus Handell531bd0f2020-07-08 10:58:19 +0200582 // it's ok to lose or log some additional statements near the instant streams
Markus Handellce1ff6f2020-07-08 08:52:48 +0200583 // are added/removed.
584 static std::atomic<bool> streams_empty_;
585
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200586 // Flags for formatting options
587 static bool thread_, timestamp_;
588
589 // Determines if logs will be directed to stderr in debug mode.
590 static bool log_to_stderr_;
Artem Titov6a4a1462019-11-26 16:24:46 +0100591#else // RTC_LOG_ENABLED()
592 // Next methods do nothing; no one will call these functions.
593 inline static void UpdateMinLogSeverity() {}
594#if defined(WEBRTC_ANDROID)
595 inline static void OutputToDebug(const std::string& msg,
596 LoggingSeverity severity,
597 const char* tag) {}
598#else
599 inline static void OutputToDebug(const std::string& msg,
600 LoggingSeverity severity) {}
601#endif // defined(WEBRTC_ANDROID)
602 inline void FinishPrintStream() {}
603#endif // RTC_LOG_ENABLED()
604
605 // The stringbuilder that buffers the formatted message before output
606 rtc::StringBuilder print_stream_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200607
608 RTC_DISALLOW_COPY_AND_ASSIGN(LogMessage);
609};
610
611//////////////////////////////////////////////////////////////////////
612// Logging Helpers
613//////////////////////////////////////////////////////////////////////
614
Karl Wibergdd7df5c2020-09-22 11:07:53 +0200615#define RTC_LOG_FILE_LINE(sev, file, line) \
616 ::rtc::webrtc_logging_impl::LogCall() & \
617 ::rtc::webrtc_logging_impl::LogStreamer<>() \
618 << ::rtc::webrtc_logging_impl::LogMetadata(file, line, sev)
Jonas Olsson8a7916b2018-09-06 09:50:23 +0200619
Karl Wibergdd7df5c2020-09-22 11:07:53 +0200620#define RTC_LOG(sev) \
621 !rtc::LogMessage::IsNoop<::rtc::sev>() && \
622 RTC_LOG_FILE_LINE(::rtc::sev, __FILE__, __LINE__)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200623
Karl Wibergcefc4652018-05-23 23:20:38 +0200624// The _V version is for when a variable is passed in.
Karl Wibergdd7df5c2020-09-22 11:07:53 +0200625#define RTC_LOG_V(sev) \
626 !rtc::LogMessage::IsNoop(sev) && RTC_LOG_FILE_LINE(sev, __FILE__, __LINE__)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200627
628// The _F version prefixes the message with the current function name.
629#if (defined(__GNUC__) && !defined(NDEBUG)) || defined(WANT_PRETTY_LOG_F)
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200630#define RTC_LOG_F(sev) RTC_LOG(sev) << __PRETTY_FUNCTION__ << ": "
Yves Gerey665174f2018-06-19 15:03:05 +0200631#define RTC_LOG_T_F(sev) \
632 RTC_LOG(sev) << this << ": " << __PRETTY_FUNCTION__ << ": "
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200633#else
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200634#define RTC_LOG_F(sev) RTC_LOG(sev) << __FUNCTION__ << ": "
Patrik Höglundc2962552017-11-17 13:40:22 +0000635#define RTC_LOG_T_F(sev) RTC_LOG(sev) << this << ": " << __FUNCTION__ << ": "
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200636#endif
637
Jiawei Ou14e5f0b2020-03-04 13:38:02 -0800638#define RTC_LOG_CHECK_LEVEL(sev) ::rtc::LogCheckLevel(::rtc::sev)
639#define RTC_LOG_CHECK_LEVEL_V(sev) ::rtc::LogCheckLevel(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200640
641inline bool LogCheckLevel(LoggingSeverity sev) {
642 return (LogMessage::GetMinLogSeverity() <= sev);
643}
644
Karl Wiberg92e37962020-09-22 13:22:20 +0200645#define RTC_LOG_E(sev, ctx, err) \
646 !rtc::LogMessage::IsNoop<::rtc::sev>() && \
647 ::rtc::webrtc_logging_impl::LogCall() & \
648 ::rtc::webrtc_logging_impl::LogStreamer<>() \
649 << ::rtc::webrtc_logging_impl::LogMetadataErr { \
650 {__FILE__, __LINE__, ::rtc::sev}, ::rtc::ERRCTX_##ctx, (err) \
Jonas Olssona4d87372019-07-05 19:08:33 +0200651 }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200652
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200653#define RTC_LOG_T(sev) RTC_LOG(sev) << this << ": "
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200654
Yves Gerey665174f2018-06-19 15:03:05 +0200655#define RTC_LOG_ERRNO_EX(sev, err) RTC_LOG_E(sev, ERRNO, err)
656#define RTC_LOG_ERRNO(sev) RTC_LOG_ERRNO_EX(sev, errno)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200657
658#if defined(WEBRTC_WIN)
Yves Gerey665174f2018-06-19 15:03:05 +0200659#define RTC_LOG_GLE_EX(sev, err) RTC_LOG_E(sev, HRESULT, err)
Karl Wibergcefc4652018-05-23 23:20:38 +0200660#define RTC_LOG_GLE(sev) RTC_LOG_GLE_EX(sev, static_cast<int>(GetLastError()))
Yves Gerey665174f2018-06-19 15:03:05 +0200661#define RTC_LOG_ERR_EX(sev, err) RTC_LOG_GLE_EX(sev, err)
662#define RTC_LOG_ERR(sev) RTC_LOG_GLE(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200663#elif defined(__native_client__) && __native_client__
Yves Gerey665174f2018-06-19 15:03:05 +0200664#define RTC_LOG_ERR_EX(sev, err) RTC_LOG(sev)
665#define RTC_LOG_ERR(sev) RTC_LOG(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200666#elif defined(WEBRTC_POSIX)
Yves Gerey665174f2018-06-19 15:03:05 +0200667#define RTC_LOG_ERR_EX(sev, err) RTC_LOG_ERRNO_EX(sev, err)
668#define RTC_LOG_ERR(sev) RTC_LOG_ERRNO(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200669#endif // WEBRTC_WIN
670
Karl Wibergcefc4652018-05-23 23:20:38 +0200671#ifdef WEBRTC_ANDROID
672
673namespace webrtc_logging_impl {
674// TODO(kwiberg): Replace these with absl::string_view.
Yves Gerey665174f2018-06-19 15:03:05 +0200675inline const char* AdaptString(const char* str) {
676 return str;
677}
678inline const char* AdaptString(const std::string& str) {
679 return str.c_str();
680}
Karl Wibergcefc4652018-05-23 23:20:38 +0200681} // namespace webrtc_logging_impl
682
Karl Wiberg92e37962020-09-22 13:22:20 +0200683#define RTC_LOG_TAG(sev, tag) \
684 !rtc::LogMessage::IsNoop(sev) && \
685 ::rtc::webrtc_logging_impl::LogCall() & \
686 ::rtc::webrtc_logging_impl::LogStreamer<>() \
687 << ::rtc::webrtc_logging_impl::LogMetadataTag { \
688 sev, ::rtc::webrtc_logging_impl::AdaptString(tag) \
Jonas Olssona4d87372019-07-05 19:08:33 +0200689 }
Karl Wibergcefc4652018-05-23 23:20:38 +0200690
Tommie51a0a82018-02-27 15:30:29 +0100691#else
Karl Wibergcefc4652018-05-23 23:20:38 +0200692
Tommie51a0a82018-02-27 15:30:29 +0100693// DEPRECATED. This macro is only intended for Android.
Karl Wibergcefc4652018-05-23 23:20:38 +0200694#define RTC_LOG_TAG(sev, tag) RTC_LOG_V(sev)
695
Tommie51a0a82018-02-27 15:30:29 +0100696#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200697
Fredrik Solenbergb3d7cac2017-11-17 15:22:37 +0100698// The RTC_DLOG macros are equivalent to their RTC_LOG counterparts except that
699// they only generate code in debug builds.
700#if RTC_DLOG_IS_ON
701#define RTC_DLOG(sev) RTC_LOG(sev)
702#define RTC_DLOG_V(sev) RTC_LOG_V(sev)
703#define RTC_DLOG_F(sev) RTC_LOG_F(sev)
704#else
Mirko Bonadeif1df04b2020-03-23 19:53:23 +0100705#define RTC_DLOG_EAT_STREAM_PARAMS() \
706 while (false) \
707 ::rtc::webrtc_logging_impl::LogMessageVoidify() & \
708 (::rtc::webrtc_logging_impl::LogStreamer<>())
Karl Wibergcefc4652018-05-23 23:20:38 +0200709#define RTC_DLOG(sev) RTC_DLOG_EAT_STREAM_PARAMS()
710#define RTC_DLOG_V(sev) RTC_DLOG_EAT_STREAM_PARAMS()
711#define RTC_DLOG_F(sev) RTC_DLOG_EAT_STREAM_PARAMS()
Fredrik Solenbergb3d7cac2017-11-17 15:22:37 +0100712#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200713
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200714} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000715
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200716#endif // RTC_BASE_LOGGING_H_