blob: fe12068fa691303a26dfc317e0081071e16aebbd [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
Jonas Olsson941a07c2018-09-13 10:07:07 +020049#include <sstream> // no-presubmit-check TODO(webrtc:8982)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020050#include <string>
51#include <utility>
52
Sebastian Janssonf4e085a2019-05-20 18:34:07 +020053#include "absl/meta/type_traits.h"
Jonas Olssonf2ce37c2018-09-12 15:32:47 +020054#include "absl/strings/string_view.h"
Steve Anton10542f22019-01-11 09:11:00 -080055#include "rtc_base/constructor_magic.h"
Tommie51a0a82018-02-27 15:30:29 +010056#include "rtc_base/deprecation.h"
Jonas Olssond8c50782018-09-07 11:21:28 +020057#include "rtc_base/strings/string_builder.h"
Karl Wibergcefc4652018-05-23 23:20:38 +020058#include "rtc_base/system/inline.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020059
Fredrik Solenbergb3d7cac2017-11-17 15:22:37 +010060#if !defined(NDEBUG) || defined(DLOG_ALWAYS_ON)
61#define RTC_DLOG_IS_ON 1
62#else
63#define RTC_DLOG_IS_ON 0
64#endif
65
Artem Titov6a4a1462019-11-26 16:24:46 +010066#if defined(RTC_DISABLE_LOGGING)
67#define RTC_LOG_ENABLED() 0
68#else
69#define RTC_LOG_ENABLED() 1
70#endif
71
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020072namespace rtc {
73
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020074//////////////////////////////////////////////////////////////////////
75
76// Note that the non-standard LoggingSeverity aliases exist because they are
77// still in broad use. The meanings of the levels are:
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020078// LS_VERBOSE: This level is for data which we do not want to appear in the
79// normal debug log, but should appear in diagnostic logs.
80// LS_INFO: Chatty level used in debugging for all sorts of things, the default
81// in debug builds.
82// LS_WARNING: Something that may warrant investigation.
83// LS_ERROR: Something that should not have occurred.
84// LS_NONE: Don't log.
85enum LoggingSeverity {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020086 LS_VERBOSE,
87 LS_INFO,
88 LS_WARNING,
89 LS_ERROR,
90 LS_NONE,
91 INFO = LS_INFO,
92 WARNING = LS_WARNING,
93 LERROR = LS_ERROR
94};
95
96// LogErrorContext assists in interpreting the meaning of an error value.
97enum LogErrorContext {
98 ERRCTX_NONE,
Jonas Olssona4d87372019-07-05 19:08:33 +020099 ERRCTX_ERRNO, // System-local errno
100 ERRCTX_HRESULT, // Windows HRESULT
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200101
102 // Abbreviations for LOG_E macro
Jonas Olssona4d87372019-07-05 19:08:33 +0200103 ERRCTX_EN = ERRCTX_ERRNO, // LOG_E(sev, EN, x)
104 ERRCTX_HR = ERRCTX_HRESULT, // LOG_E(sev, HR, x)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200105};
106
Danil Chapovalovb9f69022019-10-21 09:19:10 +0200107class LogMessage;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200108// Virtual sink interface that can receive log messages.
109class LogSink {
110 public:
111 LogSink() {}
112 virtual ~LogSink() {}
Paulina Hensmanf1e3cb42018-06-20 14:07:05 +0200113 virtual void OnLogMessage(const std::string& msg,
114 LoggingSeverity severity,
115 const char* tag);
Jiawei Ou3ea18782018-10-31 23:14:24 -0700116 virtual void OnLogMessage(const std::string& message,
117 LoggingSeverity severity);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200118 virtual void OnLogMessage(const std::string& message) = 0;
Danil Chapovalovb9f69022019-10-21 09:19:10 +0200119
120 private:
121 friend class ::rtc::LogMessage;
Artem Titov6a4a1462019-11-26 16:24:46 +0100122#if RTC_LOG_ENABLED()
Danil Chapovalovb9f69022019-10-21 09:19:10 +0200123 // Members for LogMessage class to keep linked list of the registered sinks.
124 LogSink* next_ = nullptr;
125 LoggingSeverity min_severity_;
Artem Titov6a4a1462019-11-26 16:24:46 +0100126#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200127};
128
Karl Wibergcefc4652018-05-23 23:20:38 +0200129namespace webrtc_logging_impl {
130
131class LogMetadata {
132 public:
133 LogMetadata(const char* file, int line, LoggingSeverity severity)
134 : file_(file),
135 line_and_sev_(static_cast<uint32_t>(line) << 3 | severity) {}
136 LogMetadata() = default;
137
138 const char* File() const { return file_; }
139 int Line() const { return line_and_sev_ >> 3; }
140 LoggingSeverity Severity() const {
141 return static_cast<LoggingSeverity>(line_and_sev_ & 0x7);
142 }
143
144 private:
145 const char* file_;
146
147 // Line number and severity, the former in the most significant 29 bits, the
148 // latter in the least significant 3 bits. (This is an optimization; since
149 // both numbers are usually compile-time constants, this way we can load them
150 // both with a single instruction.)
151 uint32_t line_and_sev_;
152};
153static_assert(std::is_trivial<LogMetadata>::value, "");
154
155struct LogMetadataErr {
156 LogMetadata meta;
157 LogErrorContext err_ctx;
158 int err;
159};
160
161#ifdef WEBRTC_ANDROID
162struct LogMetadataTag {
163 LoggingSeverity severity;
164 const char* tag;
165};
166#endif
167
168enum class LogArgType : int8_t {
169 kEnd = 0,
170 kInt,
171 kLong,
172 kLongLong,
173 kUInt,
174 kULong,
175 kULongLong,
176 kDouble,
177 kLongDouble,
178 kCharP,
179 kStdString,
Jonas Olssonf2ce37c2018-09-12 15:32:47 +0200180 kStringView,
Karl Wibergcefc4652018-05-23 23:20:38 +0200181 kVoidP,
182 kLogMetadata,
183 kLogMetadataErr,
184#ifdef WEBRTC_ANDROID
185 kLogMetadataTag,
186#endif
187};
188
189// Wrapper for log arguments. Only ever make values of this type with the
190// MakeVal() functions.
191template <LogArgType N, typename T>
192struct Val {
193 static constexpr LogArgType Type() { return N; }
194 T GetVal() const { return val; }
195 T val;
196};
197
Sebastian Janssonb1138622019-04-11 16:48:15 +0200198// Case for when we need to construct a temp string and then print that.
199// (We can't use Val<CheckArgType::kStdString, const std::string*>
200// because we need somewhere to store the temp string.)
201struct ToStringVal {
Karl Wibergcefc4652018-05-23 23:20:38 +0200202 static constexpr LogArgType Type() { return LogArgType::kStdString; }
203 const std::string* GetVal() const { return &val; }
204 std::string val;
205};
206
207inline Val<LogArgType::kInt, int> MakeVal(int x) {
208 return {x};
209}
210inline Val<LogArgType::kLong, long> MakeVal(long x) {
211 return {x};
212}
213inline Val<LogArgType::kLongLong, long long> MakeVal(long long x) {
214 return {x};
215}
216inline Val<LogArgType::kUInt, unsigned int> MakeVal(unsigned int x) {
217 return {x};
218}
219inline Val<LogArgType::kULong, unsigned long> MakeVal(unsigned long x) {
220 return {x};
221}
222inline Val<LogArgType::kULongLong, unsigned long long> MakeVal(
223 unsigned long long x) {
224 return {x};
225}
226
227inline Val<LogArgType::kDouble, double> MakeVal(double x) {
228 return {x};
229}
230inline Val<LogArgType::kLongDouble, long double> MakeVal(long double x) {
231 return {x};
232}
233
234inline Val<LogArgType::kCharP, const char*> MakeVal(const char* x) {
235 return {x};
236}
237inline Val<LogArgType::kStdString, const std::string*> MakeVal(
238 const std::string& x) {
239 return {&x};
240}
Jonas Olssonf2ce37c2018-09-12 15:32:47 +0200241inline Val<LogArgType::kStringView, const absl::string_view*> MakeVal(
242 const absl::string_view& x) {
243 return {&x};
244}
Karl Wibergcefc4652018-05-23 23:20:38 +0200245
246inline Val<LogArgType::kVoidP, const void*> MakeVal(const void* x) {
247 return {x};
248}
249
250inline Val<LogArgType::kLogMetadata, LogMetadata> MakeVal(
251 const LogMetadata& x) {
252 return {x};
253}
254inline Val<LogArgType::kLogMetadataErr, LogMetadataErr> MakeVal(
255 const LogMetadataErr& x) {
256 return {x};
257}
258
Amit Hilbuch10c5a932019-03-18 13:09:18 -0700259// The enum class types are not implicitly convertible to arithmetic types.
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200260template <typename T,
261 absl::enable_if_t<std::is_enum<T>::value &&
262 !std::is_arithmetic<T>::value>* = nullptr>
263inline decltype(MakeVal(std::declval<absl::underlying_type_t<T>>())) MakeVal(
264 T x) {
265 return {static_cast<absl::underlying_type_t<T>>(x)};
Amit Hilbuch10c5a932019-03-18 13:09:18 -0700266}
267
Karl Wibergcefc4652018-05-23 23:20:38 +0200268#ifdef WEBRTC_ANDROID
269inline Val<LogArgType::kLogMetadataTag, LogMetadataTag> MakeVal(
270 const LogMetadataTag& x) {
271 return {x};
272}
273#endif
274
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200275template <typename T, class = void>
Sebastian Janssonb1138622019-04-11 16:48:15 +0200276struct has_to_log_string : std::false_type {};
277template <typename T>
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200278struct has_to_log_string<T, decltype(ToLogString(std::declval<T>()))>
Sebastian Janssonb1138622019-04-11 16:48:15 +0200279 : std::true_type {};
280
Karl Wibergcefc4652018-05-23 23:20:38 +0200281// Handle arbitrary types other than the above by falling back to stringstream.
282// TODO(bugs.webrtc.org/9278): Get rid of this overload when callers don't need
283// it anymore. No in-tree caller does, but some external callers still do.
284template <
285 typename T,
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200286 typename T1 = absl::decay_t<T>,
287 absl::enable_if_t<std::is_class<T1>::value &&
288 !std::is_same<T1, std::string>::value &&
289 !std::is_same<T1, LogMetadata>::value &&
290 !has_to_log_string<T1>::value &&
Karl Wibergcefc4652018-05-23 23:20:38 +0200291#ifdef WEBRTC_ANDROID
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200292 !std::is_same<T1, LogMetadataTag>::value &&
Karl Wibergcefc4652018-05-23 23:20:38 +0200293#endif
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200294 !std::is_same<T1, LogMetadataErr>::value>* = nullptr>
Sebastian Janssonb1138622019-04-11 16:48:15 +0200295ToStringVal MakeVal(const T& x) {
Karl Wibergcefc4652018-05-23 23:20:38 +0200296 std::ostringstream os; // no-presubmit-check TODO(webrtc:8982)
297 os << x;
298 return {os.str()};
299}
300
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200301template <typename T, absl::enable_if_t<has_to_log_string<T>::value>* = nullptr>
Sebastian Janssonb1138622019-04-11 16:48:15 +0200302ToStringVal MakeVal(const T& x) {
303 return {ToLogString(x)};
304}
305
Artem Titov6a4a1462019-11-26 16:24:46 +0100306#if RTC_LOG_ENABLED()
Karl Wibergcefc4652018-05-23 23:20:38 +0200307void Log(const LogArgType* fmt, ...);
Artem Titov6a4a1462019-11-26 16:24:46 +0100308#else
309inline void Log(const LogArgType* fmt, ...) {
310 // Do nothing, shouldn't be invoked
311}
312#endif
Karl Wibergcefc4652018-05-23 23:20:38 +0200313
314// Ephemeral type that represents the result of the logging << operator.
315template <typename... Ts>
316class LogStreamer;
317
318// Base case: Before the first << argument.
319template <>
320class LogStreamer<> final {
321 public:
Amit Hilbuch10c5a932019-03-18 13:09:18 -0700322 template <typename U,
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200323 typename V = decltype(MakeVal(std::declval<U>())),
324 absl::enable_if_t<std::is_arithmetic<U>::value ||
325 std::is_enum<U>::value>* = nullptr>
326 RTC_FORCE_INLINE LogStreamer<V> operator<<(U arg) const {
327 return LogStreamer<V>(MakeVal(arg), this);
Karl Wibergcefc4652018-05-23 23:20:38 +0200328 }
329
Amit Hilbuch10c5a932019-03-18 13:09:18 -0700330 template <typename U,
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200331 typename V = decltype(MakeVal(std::declval<U>())),
332 absl::enable_if_t<!std::is_arithmetic<U>::value &&
333 !std::is_enum<U>::value>* = nullptr>
334 RTC_FORCE_INLINE LogStreamer<V> operator<<(const U& arg) const {
335 return LogStreamer<V>(MakeVal(arg), this);
Karl Wibergcefc4652018-05-23 23:20:38 +0200336 }
337
338 template <typename... Us>
339 RTC_FORCE_INLINE static void Call(const Us&... args) {
340 static constexpr LogArgType t[] = {Us::Type()..., LogArgType::kEnd};
341 Log(t, args.GetVal()...);
342 }
343};
344
345// Inductive case: We've already seen at least one << argument. The most recent
346// one had type `T`, and the earlier ones had types `Ts`.
347template <typename T, typename... Ts>
348class LogStreamer<T, Ts...> final {
349 public:
350 RTC_FORCE_INLINE LogStreamer(T arg, const LogStreamer<Ts...>* prior)
351 : arg_(arg), prior_(prior) {}
352
Amit Hilbuch10c5a932019-03-18 13:09:18 -0700353 template <typename U,
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200354 typename V = decltype(MakeVal(std::declval<U>())),
355 absl::enable_if_t<std::is_arithmetic<U>::value ||
356 std::is_enum<U>::value>* = nullptr>
357 RTC_FORCE_INLINE LogStreamer<V, T, Ts...> operator<<(U arg) const {
358 return LogStreamer<V, T, Ts...>(MakeVal(arg), this);
Karl Wibergcefc4652018-05-23 23:20:38 +0200359 }
360
Amit Hilbuch10c5a932019-03-18 13:09:18 -0700361 template <typename U,
Sebastian Janssonf4e085a2019-05-20 18:34:07 +0200362 typename V = decltype(MakeVal(std::declval<U>())),
363 absl::enable_if_t<!std::is_arithmetic<U>::value &&
364 !std::is_enum<U>::value>* = nullptr>
365 RTC_FORCE_INLINE LogStreamer<V, T, Ts...> operator<<(const U& arg) const {
366 return LogStreamer<V, T, Ts...>(MakeVal(arg), this);
Karl Wibergcefc4652018-05-23 23:20:38 +0200367 }
368
369 template <typename... Us>
370 RTC_FORCE_INLINE void Call(const Us&... args) const {
371 prior_->Call(arg_, args...);
372 }
373
374 private:
375 // The most recent argument.
376 T arg_;
377
378 // Earlier arguments.
379 const LogStreamer<Ts...>* prior_;
380};
381
382class LogCall final {
383 public:
384 // This can be any binary operator with precedence lower than <<.
Artem Titov6a4a1462019-11-26 16:24:46 +0100385 // We return bool here to be able properly remove logging if
386 // RTC_DISABLE_LOGGING is defined.
Karl Wibergcefc4652018-05-23 23:20:38 +0200387 template <typename... Ts>
Artem Titov6a4a1462019-11-26 16:24:46 +0100388 RTC_FORCE_INLINE bool operator&(const LogStreamer<Ts...>& streamer) {
Karl Wibergcefc4652018-05-23 23:20:38 +0200389 streamer.Call();
Artem Titov6a4a1462019-11-26 16:24:46 +0100390 return true;
Karl Wibergcefc4652018-05-23 23:20:38 +0200391 }
392};
393
Karl Wibergcefc4652018-05-23 23:20:38 +0200394} // namespace webrtc_logging_impl
395
396// Direct use of this class is deprecated; please use the logging macros
397// instead.
398// TODO(bugs.webrtc.org/9278): Move this class to an unnamed namespace in the
399// .cc file.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200400class LogMessage {
401 public:
Karl Wiberg1ffb3742018-05-04 15:04:48 +0200402 // Same as the above, but using a compile-time constant for the logging
403 // severity. This saves space at the call site, since passing an empty struct
404 // is generally the same as not passing an argument at all.
405 template <LoggingSeverity S>
406 RTC_NO_INLINE LogMessage(const char* file,
407 int line,
408 std::integral_constant<LoggingSeverity, S>)
409 : LogMessage(file, line, S) {}
410
Artem Titov6a4a1462019-11-26 16:24:46 +0100411#if RTC_LOG_ENABLED()
412 LogMessage(const char* file, int line, LoggingSeverity sev);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200413 LogMessage(const char* file,
414 int line,
415 LoggingSeverity sev,
Karl Wibergab4f1c12018-05-04 10:42:28 +0200416 LogErrorContext err_ctx,
417 int err);
Tommie51a0a82018-02-27 15:30:29 +0100418#if defined(WEBRTC_ANDROID)
419 LogMessage(const char* file, int line, LoggingSeverity sev, const char* tag);
420#endif
Tommie51a0a82018-02-27 15:30:29 +0100421 // DEPRECATED - DO NOT USE - PLEASE USE THE MACROS INSTEAD OF THE CLASS.
422 // Android code should use the 'const char*' version since tags are static
423 // and we want to avoid allocating a std::string copy per log line.
424 RTC_DEPRECATED
Yves Gerey665174f2018-06-19 15:03:05 +0200425 LogMessage(const char* file,
426 int line,
427 LoggingSeverity sev,
Philip Eliasson278aa422018-02-26 14:54:45 +0000428 const std::string& tag);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200429 ~LogMessage();
430
Karl Wibergcefc4652018-05-23 23:20:38 +0200431 void AddTag(const char* tag);
Jonas Olssond8c50782018-09-07 11:21:28 +0200432 rtc::StringBuilder& stream();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200433 // Returns the time at which this function was called for the first time.
434 // The time will be used as the logging start time.
435 // If this is not called externally, the LogMessage ctor also calls it, in
436 // which case the logging start time will be the time of the first LogMessage
437 // instance is created.
438 static int64_t LogStartTime();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200439 // Returns the wall clock equivalent of |LogStartTime|, in seconds from the
440 // epoch.
441 static uint32_t WallClockStartTime();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200442 // LogThreads: Display the thread identifier of the current thread
443 static void LogThreads(bool on = true);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200444 // LogTimestamps: Display the elapsed time of the program
445 static void LogTimestamps(bool on = true);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200446 // These are the available logging channels
447 // Debug: Debug console on Windows, otherwise stderr
448 static void LogToDebug(LoggingSeverity min_sev);
Jonas Olsson2b6f1352018-02-15 11:57:03 +0100449 static LoggingSeverity GetLogToDebug();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200450 // Sets whether logs will be directed to stderr in debug mode.
451 static void SetLogToStderr(bool log_to_stderr);
Danil Chapovalovb9f69022019-10-21 09:19:10 +0200452 // Stream: Any non-blocking stream interface.
453 // Installs the |stream| to collect logs with severtiy |min_sev| or higher.
454 // |stream| must live until deinstalled by RemoveLogToStream
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200455 static void AddLogToStream(LogSink* stream, LoggingSeverity min_sev);
Danil Chapovalovb9f69022019-10-21 09:19:10 +0200456 // Removes the specified stream, without destroying it.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200457 static void RemoveLogToStream(LogSink* stream);
Danil Chapovalovb9f69022019-10-21 09:19:10 +0200458 // Returns the severity for the specified stream, of if none is specified,
459 // the minimum stream severity.
460 static int GetLogToStream(LogSink* stream = nullptr);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200461 // Testing against MinLogSeverity allows code to avoid potentially expensive
462 // logging operations by pre-checking the logging level.
Jonas Olsson2b6f1352018-02-15 11:57:03 +0100463 static int GetMinLogSeverity();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200464 // Parses the provided parameter stream to configure the options above.
465 // Useful for configuring logging from the command line.
466 static void ConfigureLogging(const char* params);
Jonas Olssond8c50782018-09-07 11:21:28 +0200467 // Checks the current global debug severity and if the |streams_| collection
468 // is empty. If |severity| is smaller than the global severity and if the
469 // |streams_| collection is empty, the LogMessage will be considered a noop
470 // LogMessage.
471 static bool IsNoop(LoggingSeverity severity);
Artem Titov6a4a1462019-11-26 16:24:46 +0100472#else
473 // Next methods do nothing; no one will call these functions.
474 LogMessage(const char* file, int line, LoggingSeverity sev) {}
475 LogMessage(const char* file,
476 int line,
477 LoggingSeverity sev,
478 LogErrorContext err_ctx,
479 int err) {}
480#if defined(WEBRTC_ANDROID)
481 LogMessage(const char* file, int line, LoggingSeverity sev, const char* tag) {
482 }
483#endif
484 // DEPRECATED - DO NOT USE - PLEASE USE THE MACROS INSTEAD OF THE CLASS.
485 // Android code should use the 'const char*' version since tags are static
486 // and we want to avoid allocating a std::string copy per log line.
487 RTC_DEPRECATED
488 LogMessage(const char* file,
489 int line,
490 LoggingSeverity sev,
491 const std::string& tag) {}
492 ~LogMessage() = default;
493
494 inline void AddTag(const char* tag) {}
495 inline rtc::StringBuilder& stream() { return print_stream_; }
496 inline static int64_t LogStartTime() { return 0; }
497 inline static uint32_t WallClockStartTime() { return 0; }
498 inline static void LogThreads(bool on = true) {}
499 inline static void LogTimestamps(bool on = true) {}
500 inline static void LogToDebug(LoggingSeverity min_sev) {}
501 inline static LoggingSeverity GetLogToDebug() {
502 return LoggingSeverity::LS_INFO;
503 }
504 inline static void SetLogToStderr(bool log_to_stderr) {}
505 inline static void AddLogToStream(LogSink* stream, LoggingSeverity min_sev) {}
506 inline static void RemoveLogToStream(LogSink* stream) {}
507 inline static int GetLogToStream(LogSink* stream = nullptr) { return 0; }
508 inline static int GetMinLogSeverity() { return 0; }
509 inline static void ConfigureLogging(const char* params) {}
510 inline static bool IsNoop(LoggingSeverity severity) { return true; }
511#endif // RTC_LOG_ENABLED()
Jonas Olssond8c50782018-09-07 11:21:28 +0200512
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200513 private:
Tommifef05002018-02-27 13:51:08 +0100514 friend class LogMessageForTesting;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200515
Artem Titov6a4a1462019-11-26 16:24:46 +0100516#if RTC_LOG_ENABLED()
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200517 // Updates min_sev_ appropriately when debug sinks change.
518 static void UpdateMinLogSeverity();
519
Yves Gerey665174f2018-06-19 15:03:05 +0200520// These write out the actual log messages.
Tommie51a0a82018-02-27 15:30:29 +0100521#if defined(WEBRTC_ANDROID)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200522 static void OutputToDebug(const std::string& msg,
523 LoggingSeverity severity,
Tommie51a0a82018-02-27 15:30:29 +0100524 const char* tag);
525#else
526 static void OutputToDebug(const std::string& msg, LoggingSeverity severity);
Artem Titov6a4a1462019-11-26 16:24:46 +0100527#endif // defined(WEBRTC_ANDROID)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200528
Tommifef05002018-02-27 13:51:08 +0100529 // Called from the dtor (or from a test) to append optional extra error
530 // information to the log stream and a newline character.
531 void FinishPrintStream();
532
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200533 // The severity level of this message
534 LoggingSeverity severity_;
535
Tommie51a0a82018-02-27 15:30:29 +0100536#if defined(WEBRTC_ANDROID)
Paulina Hensmanf1e3cb42018-06-20 14:07:05 +0200537 // The default Android debug output tag.
Tommie51a0a82018-02-27 15:30:29 +0100538 const char* tag_ = "libjingle";
539#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200540
541 // String data generated in the constructor, that should be appended to
542 // the message before output.
543 std::string extra_;
544
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200545 // The output streams and their associated severities
Danil Chapovalovb9f69022019-10-21 09:19:10 +0200546 static LogSink* streams_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200547
548 // Flags for formatting options
549 static bool thread_, timestamp_;
550
551 // Determines if logs will be directed to stderr in debug mode.
552 static bool log_to_stderr_;
Artem Titov6a4a1462019-11-26 16:24:46 +0100553#else // RTC_LOG_ENABLED()
554 // Next methods do nothing; no one will call these functions.
555 inline static void UpdateMinLogSeverity() {}
556#if defined(WEBRTC_ANDROID)
557 inline static void OutputToDebug(const std::string& msg,
558 LoggingSeverity severity,
559 const char* tag) {}
560#else
561 inline static void OutputToDebug(const std::string& msg,
562 LoggingSeverity severity) {}
563#endif // defined(WEBRTC_ANDROID)
564 inline void FinishPrintStream() {}
565#endif // RTC_LOG_ENABLED()
566
567 // The stringbuilder that buffers the formatted message before output
568 rtc::StringBuilder print_stream_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200569
570 RTC_DISALLOW_COPY_AND_ASSIGN(LogMessage);
571};
572
573//////////////////////////////////////////////////////////////////////
574// Logging Helpers
575//////////////////////////////////////////////////////////////////////
576
Artem Titov6a4a1462019-11-26 16:24:46 +0100577#define RTC_LOG_FILE_LINE(sev, file, line) \
578 RTC_LOG_ENABLED() && \
579 rtc::webrtc_logging_impl::LogCall() & \
580 rtc::webrtc_logging_impl::LogStreamer<>() \
581 << rtc::webrtc_logging_impl::LogMetadata(file, line, sev)
Jonas Olsson8a7916b2018-09-06 09:50:23 +0200582
583#define RTC_LOG(sev) RTC_LOG_FILE_LINE(rtc::sev, __FILE__, __LINE__)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200584
Karl Wibergcefc4652018-05-23 23:20:38 +0200585// The _V version is for when a variable is passed in.
Jonas Olssond8c50782018-09-07 11:21:28 +0200586#define RTC_LOG_V(sev) RTC_LOG_FILE_LINE(sev, __FILE__, __LINE__)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200587
588// The _F version prefixes the message with the current function name.
589#if (defined(__GNUC__) && !defined(NDEBUG)) || defined(WANT_PRETTY_LOG_F)
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200590#define RTC_LOG_F(sev) RTC_LOG(sev) << __PRETTY_FUNCTION__ << ": "
Yves Gerey665174f2018-06-19 15:03:05 +0200591#define RTC_LOG_T_F(sev) \
592 RTC_LOG(sev) << this << ": " << __PRETTY_FUNCTION__ << ": "
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200593#else
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200594#define RTC_LOG_F(sev) RTC_LOG(sev) << __FUNCTION__ << ": "
Patrik Höglundc2962552017-11-17 13:40:22 +0000595#define RTC_LOG_T_F(sev) RTC_LOG(sev) << this << ": " << __FUNCTION__ << ": "
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200596#endif
597
Yves Gerey665174f2018-06-19 15:03:05 +0200598#define RTC_LOG_CHECK_LEVEL(sev) rtc::LogCheckLevel(rtc::sev)
599#define RTC_LOG_CHECK_LEVEL_V(sev) rtc::LogCheckLevel(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200600
601inline bool LogCheckLevel(LoggingSeverity sev) {
602 return (LogMessage::GetMinLogSeverity() <= sev);
603}
604
Artem Titov6a4a1462019-11-26 16:24:46 +0100605#define RTC_LOG_E(sev, ctx, err) \
606 RTC_LOG_ENABLED() && rtc::webrtc_logging_impl::LogCall() & \
607 rtc::webrtc_logging_impl::LogStreamer<>() \
608 << rtc::webrtc_logging_impl::LogMetadataErr { \
609 {__FILE__, __LINE__, rtc::sev}, rtc::ERRCTX_##ctx, (err) \
Jonas Olssona4d87372019-07-05 19:08:33 +0200610 }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200611
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200612#define RTC_LOG_T(sev) RTC_LOG(sev) << this << ": "
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200613
Yves Gerey665174f2018-06-19 15:03:05 +0200614#define RTC_LOG_ERRNO_EX(sev, err) RTC_LOG_E(sev, ERRNO, err)
615#define RTC_LOG_ERRNO(sev) RTC_LOG_ERRNO_EX(sev, errno)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200616
617#if defined(WEBRTC_WIN)
Yves Gerey665174f2018-06-19 15:03:05 +0200618#define RTC_LOG_GLE_EX(sev, err) RTC_LOG_E(sev, HRESULT, err)
Karl Wibergcefc4652018-05-23 23:20:38 +0200619#define RTC_LOG_GLE(sev) RTC_LOG_GLE_EX(sev, static_cast<int>(GetLastError()))
Yves Gerey665174f2018-06-19 15:03:05 +0200620#define RTC_LOG_ERR_EX(sev, err) RTC_LOG_GLE_EX(sev, err)
621#define RTC_LOG_ERR(sev) RTC_LOG_GLE(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200622#elif defined(__native_client__) && __native_client__
Yves Gerey665174f2018-06-19 15:03:05 +0200623#define RTC_LOG_ERR_EX(sev, err) RTC_LOG(sev)
624#define RTC_LOG_ERR(sev) RTC_LOG(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200625#elif defined(WEBRTC_POSIX)
Yves Gerey665174f2018-06-19 15:03:05 +0200626#define RTC_LOG_ERR_EX(sev, err) RTC_LOG_ERRNO_EX(sev, err)
627#define RTC_LOG_ERR(sev) RTC_LOG_ERRNO(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200628#endif // WEBRTC_WIN
629
Karl Wibergcefc4652018-05-23 23:20:38 +0200630#ifdef WEBRTC_ANDROID
631
632namespace webrtc_logging_impl {
633// TODO(kwiberg): Replace these with absl::string_view.
Yves Gerey665174f2018-06-19 15:03:05 +0200634inline const char* AdaptString(const char* str) {
635 return str;
636}
637inline const char* AdaptString(const std::string& str) {
638 return str.c_str();
639}
Karl Wibergcefc4652018-05-23 23:20:38 +0200640} // namespace webrtc_logging_impl
641
Artem Titov6a4a1462019-11-26 16:24:46 +0100642#define RTC_LOG_TAG(sev, tag) \
643 RTC_LOG_ENABLED() && rtc::webrtc_logging_impl::LogCall() & \
644 rtc::webrtc_logging_impl::LogStreamer<>() \
645 << rtc::webrtc_logging_impl::LogMetadataTag { \
646 sev, rtc::webrtc_logging_impl::AdaptString(tag) \
Jonas Olssona4d87372019-07-05 19:08:33 +0200647 }
Karl Wibergcefc4652018-05-23 23:20:38 +0200648
Tommie51a0a82018-02-27 15:30:29 +0100649#else
Karl Wibergcefc4652018-05-23 23:20:38 +0200650
Tommie51a0a82018-02-27 15:30:29 +0100651// DEPRECATED. This macro is only intended for Android.
Karl Wibergcefc4652018-05-23 23:20:38 +0200652#define RTC_LOG_TAG(sev, tag) RTC_LOG_V(sev)
653
Tommie51a0a82018-02-27 15:30:29 +0100654#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200655
Fredrik Solenbergb3d7cac2017-11-17 15:22:37 +0100656// The RTC_DLOG macros are equivalent to their RTC_LOG counterparts except that
657// they only generate code in debug builds.
658#if RTC_DLOG_IS_ON
659#define RTC_DLOG(sev) RTC_LOG(sev)
660#define RTC_DLOG_V(sev) RTC_LOG_V(sev)
661#define RTC_DLOG_F(sev) RTC_LOG_F(sev)
662#else
Karl Wibergcefc4652018-05-23 23:20:38 +0200663#define RTC_DLOG_EAT_STREAM_PARAMS() \
664 while (false) \
665 rtc::webrtc_logging_impl::LogStreamer<>()
666#define RTC_DLOG(sev) RTC_DLOG_EAT_STREAM_PARAMS()
667#define RTC_DLOG_V(sev) RTC_DLOG_EAT_STREAM_PARAMS()
668#define RTC_DLOG_F(sev) RTC_DLOG_EAT_STREAM_PARAMS()
Fredrik Solenbergb3d7cac2017-11-17 15:22:37 +0100669#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200670
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200671} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000672
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200673#endif // RTC_BASE_LOGGING_H_