blob: 4f74dcfbcecf127c983fdc348991fd0d871dcf0f [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
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020049#include <list>
50#include <sstream>
51#include <string>
52#include <utility>
53
54#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS)
55#include <CoreServices/CoreServices.h>
56#endif
57
Jonas Olssonf2ce37c2018-09-12 15:32:47 +020058#include "absl/strings/string_view.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020059#include "rtc_base/constructormagic.h"
Tommie51a0a82018-02-27 15:30:29 +010060#include "rtc_base/deprecation.h"
Jonas Olssond8c50782018-09-07 11:21:28 +020061#include "rtc_base/strings/string_builder.h"
Karl Wibergcefc4652018-05-23 23:20:38 +020062#include "rtc_base/system/inline.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020063#include "rtc_base/thread_annotations.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020064
Fredrik Solenbergb3d7cac2017-11-17 15:22:37 +010065#if !defined(NDEBUG) || defined(DLOG_ALWAYS_ON)
66#define RTC_DLOG_IS_ON 1
67#else
68#define RTC_DLOG_IS_ON 0
69#endif
70
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020071namespace rtc {
72
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020073#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS)
74// Returns a UTF8 description from an OS X Status error.
75std::string DescriptionFromOSStatus(OSStatus err);
76#endif
77
78//////////////////////////////////////////////////////////////////////
79
80// Note that the non-standard LoggingSeverity aliases exist because they are
81// still in broad use. The meanings of the levels are:
82// LS_SENSITIVE: Information which should only be logged with the consent
83// of the user, due to privacy concerns.
84// LS_VERBOSE: This level is for data which we do not want to appear in the
85// normal debug log, but should appear in diagnostic logs.
86// LS_INFO: Chatty level used in debugging for all sorts of things, the default
87// in debug builds.
88// LS_WARNING: Something that may warrant investigation.
89// LS_ERROR: Something that should not have occurred.
90// LS_NONE: Don't log.
91enum LoggingSeverity {
92 LS_SENSITIVE,
93 LS_VERBOSE,
94 LS_INFO,
95 LS_WARNING,
96 LS_ERROR,
97 LS_NONE,
98 INFO = LS_INFO,
99 WARNING = LS_WARNING,
100 LERROR = LS_ERROR
101};
102
103// LogErrorContext assists in interpreting the meaning of an error value.
104enum LogErrorContext {
105 ERRCTX_NONE,
106 ERRCTX_ERRNO, // System-local errno
107 ERRCTX_HRESULT, // Windows HRESULT
108 ERRCTX_OSSTATUS, // MacOS OSStatus
109
110 // Abbreviations for LOG_E macro
111 ERRCTX_EN = ERRCTX_ERRNO, // LOG_E(sev, EN, x)
112 ERRCTX_HR = ERRCTX_HRESULT, // LOG_E(sev, HR, x)
113 ERRCTX_OS = ERRCTX_OSSTATUS, // LOG_E(sev, OS, x)
114};
115
116// Virtual sink interface that can receive log messages.
117class LogSink {
118 public:
119 LogSink() {}
120 virtual ~LogSink() {}
Paulina Hensmanf1e3cb42018-06-20 14:07:05 +0200121 virtual void OnLogMessage(const std::string& msg,
122 LoggingSeverity severity,
123 const char* tag);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200124 virtual void OnLogMessage(const std::string& message) = 0;
125};
126
Karl Wibergcefc4652018-05-23 23:20:38 +0200127namespace webrtc_logging_impl {
128
129class LogMetadata {
130 public:
131 LogMetadata(const char* file, int line, LoggingSeverity severity)
132 : file_(file),
133 line_and_sev_(static_cast<uint32_t>(line) << 3 | severity) {}
134 LogMetadata() = default;
135
136 const char* File() const { return file_; }
137 int Line() const { return line_and_sev_ >> 3; }
138 LoggingSeverity Severity() const {
139 return static_cast<LoggingSeverity>(line_and_sev_ & 0x7);
140 }
141
142 private:
143 const char* file_;
144
145 // Line number and severity, the former in the most significant 29 bits, the
146 // latter in the least significant 3 bits. (This is an optimization; since
147 // both numbers are usually compile-time constants, this way we can load them
148 // both with a single instruction.)
149 uint32_t line_and_sev_;
150};
151static_assert(std::is_trivial<LogMetadata>::value, "");
152
153struct LogMetadataErr {
154 LogMetadata meta;
155 LogErrorContext err_ctx;
156 int err;
157};
158
159#ifdef WEBRTC_ANDROID
160struct LogMetadataTag {
161 LoggingSeverity severity;
162 const char* tag;
163};
164#endif
165
166enum class LogArgType : int8_t {
167 kEnd = 0,
168 kInt,
169 kLong,
170 kLongLong,
171 kUInt,
172 kULong,
173 kULongLong,
174 kDouble,
175 kLongDouble,
176 kCharP,
177 kStdString,
Jonas Olssonf2ce37c2018-09-12 15:32:47 +0200178 kStringView,
Karl Wibergcefc4652018-05-23 23:20:38 +0200179 kVoidP,
180 kLogMetadata,
181 kLogMetadataErr,
182#ifdef WEBRTC_ANDROID
183 kLogMetadataTag,
184#endif
185};
186
187// Wrapper for log arguments. Only ever make values of this type with the
188// MakeVal() functions.
189template <LogArgType N, typename T>
190struct Val {
191 static constexpr LogArgType Type() { return N; }
192 T GetVal() const { return val; }
193 T val;
194};
195
196// TODO(bugs.webrtc.org/9278): Get rid of this specialization when callers
197// don't need it anymore. No in-tree caller does, but some external callers
198// still do.
199template <>
200struct Val<LogArgType::kStdString, std::string> {
201 static constexpr LogArgType Type() { return LogArgType::kStdString; }
202 const std::string* GetVal() const { return &val; }
203 std::string val;
204};
205
206inline Val<LogArgType::kInt, int> MakeVal(int x) {
207 return {x};
208}
209inline Val<LogArgType::kLong, long> MakeVal(long x) {
210 return {x};
211}
212inline Val<LogArgType::kLongLong, long long> MakeVal(long long x) {
213 return {x};
214}
215inline Val<LogArgType::kUInt, unsigned int> MakeVal(unsigned int x) {
216 return {x};
217}
218inline Val<LogArgType::kULong, unsigned long> MakeVal(unsigned long x) {
219 return {x};
220}
221inline Val<LogArgType::kULongLong, unsigned long long> MakeVal(
222 unsigned long long x) {
223 return {x};
224}
225
226inline Val<LogArgType::kDouble, double> MakeVal(double x) {
227 return {x};
228}
229inline Val<LogArgType::kLongDouble, long double> MakeVal(long double x) {
230 return {x};
231}
232
233inline Val<LogArgType::kCharP, const char*> MakeVal(const char* x) {
234 return {x};
235}
236inline Val<LogArgType::kStdString, const std::string*> MakeVal(
237 const std::string& x) {
238 return {&x};
239}
Jonas Olssonf2ce37c2018-09-12 15:32:47 +0200240inline Val<LogArgType::kStringView, const absl::string_view*> MakeVal(
241 const absl::string_view& x) {
242 return {&x};
243}
Karl Wibergcefc4652018-05-23 23:20:38 +0200244
245inline Val<LogArgType::kVoidP, const void*> MakeVal(const void* x) {
246 return {x};
247}
248
249inline Val<LogArgType::kLogMetadata, LogMetadata> MakeVal(
250 const LogMetadata& x) {
251 return {x};
252}
253inline Val<LogArgType::kLogMetadataErr, LogMetadataErr> MakeVal(
254 const LogMetadataErr& x) {
255 return {x};
256}
257
258#ifdef WEBRTC_ANDROID
259inline Val<LogArgType::kLogMetadataTag, LogMetadataTag> MakeVal(
260 const LogMetadataTag& x) {
261 return {x};
262}
263#endif
264
265// Handle arbitrary types other than the above by falling back to stringstream.
266// TODO(bugs.webrtc.org/9278): Get rid of this overload when callers don't need
267// it anymore. No in-tree caller does, but some external callers still do.
268template <
269 typename T,
270 typename T1 =
271 typename std::remove_cv<typename std::remove_reference<T>::type>::type,
272 typename std::enable_if<
273 std::is_class<T1>::value && !std::is_same<T1, std::string>::value &&
274 !std::is_same<T1, LogMetadata>::value &&
275#ifdef WEBRTC_ANDROID
276 !std::is_same<T1, LogMetadataTag>::value &&
277#endif
278 !std::is_same<T1, LogMetadataErr>::value>::type* = nullptr>
279Val<LogArgType::kStdString, std::string> MakeVal(const T& x) {
280 std::ostringstream os; // no-presubmit-check TODO(webrtc:8982)
281 os << x;
282 return {os.str()};
283}
284
285void Log(const LogArgType* fmt, ...);
286
287// Ephemeral type that represents the result of the logging << operator.
288template <typename... Ts>
289class LogStreamer;
290
291// Base case: Before the first << argument.
292template <>
293class LogStreamer<> final {
294 public:
295 template <
296 typename U,
297 typename std::enable_if<std::is_arithmetic<U>::value>::type* = nullptr>
298 RTC_FORCE_INLINE LogStreamer<decltype(MakeVal(std::declval<U>()))> operator<<(
299 U arg) const {
300 return LogStreamer<decltype(MakeVal(std::declval<U>()))>(MakeVal(arg),
301 this);
302 }
303
304 template <
305 typename U,
306 typename std::enable_if<!std::is_arithmetic<U>::value>::type* = nullptr>
307 RTC_FORCE_INLINE LogStreamer<decltype(MakeVal(std::declval<U>()))> operator<<(
308 const U& arg) const {
309 return LogStreamer<decltype(MakeVal(std::declval<U>()))>(MakeVal(arg),
310 this);
311 }
312
313 template <typename... Us>
314 RTC_FORCE_INLINE static void Call(const Us&... args) {
315 static constexpr LogArgType t[] = {Us::Type()..., LogArgType::kEnd};
316 Log(t, args.GetVal()...);
317 }
318};
319
320// Inductive case: We've already seen at least one << argument. The most recent
321// one had type `T`, and the earlier ones had types `Ts`.
322template <typename T, typename... Ts>
323class LogStreamer<T, Ts...> final {
324 public:
325 RTC_FORCE_INLINE LogStreamer(T arg, const LogStreamer<Ts...>* prior)
326 : arg_(arg), prior_(prior) {}
327
328 template <
329 typename U,
330 typename std::enable_if<std::is_arithmetic<U>::value>::type* = nullptr>
331 RTC_FORCE_INLINE LogStreamer<decltype(MakeVal(std::declval<U>())), T, Ts...>
332 operator<<(U arg) const {
333 return LogStreamer<decltype(MakeVal(std::declval<U>())), T, Ts...>(
334 MakeVal(arg), this);
335 }
336
337 template <
338 typename U,
339 typename std::enable_if<!std::is_arithmetic<U>::value>::type* = nullptr>
340 RTC_FORCE_INLINE LogStreamer<decltype(MakeVal(std::declval<U>())), T, Ts...>
341 operator<<(const U& arg) const {
342 return LogStreamer<decltype(MakeVal(std::declval<U>())), T, Ts...>(
343 MakeVal(arg), this);
344 }
345
346 template <typename... Us>
347 RTC_FORCE_INLINE void Call(const Us&... args) const {
348 prior_->Call(arg_, args...);
349 }
350
351 private:
352 // The most recent argument.
353 T arg_;
354
355 // Earlier arguments.
356 const LogStreamer<Ts...>* prior_;
357};
358
359class LogCall final {
360 public:
361 // This can be any binary operator with precedence lower than <<.
362 template <typename... Ts>
363 RTC_FORCE_INLINE void operator&(const LogStreamer<Ts...>& streamer) {
364 streamer.Call();
365 }
366};
367
368// TODO(bugs.webrtc.org/9278): Remove this once it's no longer used.
369struct LogMessageVoidify {
370 void operator&(std::ostream&) {} // no-presubmit-check TODO(webrtc:8982)
371};
372
373} // namespace webrtc_logging_impl
374
375// Direct use of this class is deprecated; please use the logging macros
376// instead.
377// TODO(bugs.webrtc.org/9278): Move this class to an unnamed namespace in the
378// .cc file.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200379class LogMessage {
380 public:
Karl Wibergab4f1c12018-05-04 10:42:28 +0200381 LogMessage(const char* file, int line, LoggingSeverity sev);
Karl Wiberg1ffb3742018-05-04 15:04:48 +0200382
383 // Same as the above, but using a compile-time constant for the logging
384 // severity. This saves space at the call site, since passing an empty struct
385 // is generally the same as not passing an argument at all.
386 template <LoggingSeverity S>
387 RTC_NO_INLINE LogMessage(const char* file,
388 int line,
389 std::integral_constant<LoggingSeverity, S>)
390 : LogMessage(file, line, S) {}
391
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200392 LogMessage(const char* file,
393 int line,
394 LoggingSeverity sev,
Karl Wibergab4f1c12018-05-04 10:42:28 +0200395 LogErrorContext err_ctx,
396 int err);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200397
Tommie51a0a82018-02-27 15:30:29 +0100398#if defined(WEBRTC_ANDROID)
399 LogMessage(const char* file, int line, LoggingSeverity sev, const char* tag);
400#endif
401
402 // DEPRECATED - DO NOT USE - PLEASE USE THE MACROS INSTEAD OF THE CLASS.
403 // Android code should use the 'const char*' version since tags are static
404 // and we want to avoid allocating a std::string copy per log line.
405 RTC_DEPRECATED
Yves Gerey665174f2018-06-19 15:03:05 +0200406 LogMessage(const char* file,
407 int line,
408 LoggingSeverity sev,
Philip Eliasson278aa422018-02-26 14:54:45 +0000409 const std::string& tag);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200410
411 ~LogMessage();
412
Karl Wibergcefc4652018-05-23 23:20:38 +0200413 void AddTag(const char* tag);
414
Jonas Olssond8c50782018-09-07 11:21:28 +0200415 rtc::StringBuilder& stream();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200416
417 // Returns the time at which this function was called for the first time.
418 // The time will be used as the logging start time.
419 // If this is not called externally, the LogMessage ctor also calls it, in
420 // which case the logging start time will be the time of the first LogMessage
421 // instance is created.
422 static int64_t LogStartTime();
423
424 // Returns the wall clock equivalent of |LogStartTime|, in seconds from the
425 // epoch.
426 static uint32_t WallClockStartTime();
427
428 // LogThreads: Display the thread identifier of the current thread
429 static void LogThreads(bool on = true);
430
431 // LogTimestamps: Display the elapsed time of the program
432 static void LogTimestamps(bool on = true);
433
434 // These are the available logging channels
435 // Debug: Debug console on Windows, otherwise stderr
436 static void LogToDebug(LoggingSeverity min_sev);
Jonas Olsson2b6f1352018-02-15 11:57:03 +0100437 static LoggingSeverity GetLogToDebug();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200438
439 // Sets whether logs will be directed to stderr in debug mode.
440 static void SetLogToStderr(bool log_to_stderr);
441
442 // Stream: Any non-blocking stream interface. LogMessage takes ownership of
443 // the stream. Multiple streams may be specified by using AddLogToStream.
444 // LogToStream is retained for backwards compatibility; when invoked, it
445 // will discard any previously set streams and install the specified stream.
446 // GetLogToStream gets the severity for the specified stream, of if none
447 // is specified, the minimum stream severity.
448 // RemoveLogToStream removes the specified stream, without destroying it.
449 static int GetLogToStream(LogSink* stream = nullptr);
450 static void AddLogToStream(LogSink* stream, LoggingSeverity min_sev);
451 static void RemoveLogToStream(LogSink* stream);
452
453 // Testing against MinLogSeverity allows code to avoid potentially expensive
454 // logging operations by pre-checking the logging level.
Jonas Olsson2b6f1352018-02-15 11:57:03 +0100455 static int GetMinLogSeverity();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200456
457 // Parses the provided parameter stream to configure the options above.
458 // Useful for configuring logging from the command line.
459 static void ConfigureLogging(const char* params);
460
Jonas Olssond8c50782018-09-07 11:21:28 +0200461 // Checks the current global debug severity and if the |streams_| collection
462 // is empty. If |severity| is smaller than the global severity and if the
463 // |streams_| collection is empty, the LogMessage will be considered a noop
464 // LogMessage.
465 static bool IsNoop(LoggingSeverity severity);
466
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200467 private:
Tommifef05002018-02-27 13:51:08 +0100468 friend class LogMessageForTesting;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200469 typedef std::pair<LogSink*, LoggingSeverity> StreamAndSeverity;
470 typedef std::list<StreamAndSeverity> StreamList;
471
472 // Updates min_sev_ appropriately when debug sinks change.
473 static void UpdateMinLogSeverity();
474
Yves Gerey665174f2018-06-19 15:03:05 +0200475// These write out the actual log messages.
Tommie51a0a82018-02-27 15:30:29 +0100476#if defined(WEBRTC_ANDROID)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200477 static void OutputToDebug(const std::string& msg,
478 LoggingSeverity severity,
Tommie51a0a82018-02-27 15:30:29 +0100479 const char* tag);
480#else
481 static void OutputToDebug(const std::string& msg, LoggingSeverity severity);
482#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200483
Tommifef05002018-02-27 13:51:08 +0100484 // Called from the dtor (or from a test) to append optional extra error
485 // information to the log stream and a newline character.
486 void FinishPrintStream();
487
Jonas Olssond8c50782018-09-07 11:21:28 +0200488 // The stringbuilder that buffers the formatted message before output
489 rtc::StringBuilder print_stream_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200490
491 // The severity level of this message
492 LoggingSeverity severity_;
493
Tommie51a0a82018-02-27 15:30:29 +0100494#if defined(WEBRTC_ANDROID)
Paulina Hensmanf1e3cb42018-06-20 14:07:05 +0200495 // The default Android debug output tag.
Tommie51a0a82018-02-27 15:30:29 +0100496 const char* tag_ = "libjingle";
497#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200498
499 // String data generated in the constructor, that should be appended to
500 // the message before output.
501 std::string extra_;
502
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200503 // The output streams and their associated severities
504 static StreamList streams_;
505
506 // Flags for formatting options
507 static bool thread_, timestamp_;
508
509 // Determines if logs will be directed to stderr in debug mode.
510 static bool log_to_stderr_;
511
512 RTC_DISALLOW_COPY_AND_ASSIGN(LogMessage);
513};
514
515//////////////////////////////////////////////////////////////////////
516// Logging Helpers
517//////////////////////////////////////////////////////////////////////
518
Karl Wibergcefc4652018-05-23 23:20:38 +0200519// DEPRECATED.
520// TODO(bugs.webrtc.org/9278): Remove once there are no more users.
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200521#define RTC_LOG_SEVERITY_PRECONDITION(sev) \
Jonas Olssond8c50782018-09-07 11:21:28 +0200522 (rtc::LogMessage::IsNoop(sev)) \
Karl Wibergcefc4652018-05-23 23:20:38 +0200523 ? static_cast<void>(0) \
524 : rtc::webrtc_logging_impl::LogMessageVoidify()&
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200525
Jonas Olsson8a7916b2018-09-06 09:50:23 +0200526#define RTC_LOG_FILE_LINE(sev, file, line) \
Jonas Olsson8a7916b2018-09-06 09:50:23 +0200527 rtc::webrtc_logging_impl::LogCall() & \
528 rtc::webrtc_logging_impl::LogStreamer<>() \
529 << rtc::webrtc_logging_impl::LogMetadata(__FILE__, __LINE__, sev)
530
531#define RTC_LOG(sev) RTC_LOG_FILE_LINE(rtc::sev, __FILE__, __LINE__)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200532
Karl Wibergcefc4652018-05-23 23:20:38 +0200533// The _V version is for when a variable is passed in.
Jonas Olssond8c50782018-09-07 11:21:28 +0200534#define RTC_LOG_V(sev) RTC_LOG_FILE_LINE(sev, __FILE__, __LINE__)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200535
536// The _F version prefixes the message with the current function name.
537#if (defined(__GNUC__) && !defined(NDEBUG)) || defined(WANT_PRETTY_LOG_F)
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200538#define RTC_LOG_F(sev) RTC_LOG(sev) << __PRETTY_FUNCTION__ << ": "
Yves Gerey665174f2018-06-19 15:03:05 +0200539#define RTC_LOG_T_F(sev) \
540 RTC_LOG(sev) << this << ": " << __PRETTY_FUNCTION__ << ": "
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200541#else
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200542#define RTC_LOG_F(sev) RTC_LOG(sev) << __FUNCTION__ << ": "
Patrik Höglundc2962552017-11-17 13:40:22 +0000543#define RTC_LOG_T_F(sev) RTC_LOG(sev) << this << ": " << __FUNCTION__ << ": "
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200544#endif
545
Yves Gerey665174f2018-06-19 15:03:05 +0200546#define RTC_LOG_CHECK_LEVEL(sev) rtc::LogCheckLevel(rtc::sev)
547#define RTC_LOG_CHECK_LEVEL_V(sev) rtc::LogCheckLevel(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200548
549inline bool LogCheckLevel(LoggingSeverity sev) {
550 return (LogMessage::GetMinLogSeverity() <= sev);
551}
552
Karl Wibergcefc4652018-05-23 23:20:38 +0200553#define RTC_LOG_E(sev, ctx, err) \
Karl Wibergcefc4652018-05-23 23:20:38 +0200554 rtc::webrtc_logging_impl::LogCall() & \
555 rtc::webrtc_logging_impl::LogStreamer<>() \
556 << rtc::webrtc_logging_impl::LogMetadataErr { \
557 {__FILE__, __LINE__, rtc::sev}, rtc::ERRCTX_##ctx, (err) \
558 }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200559
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200560#define RTC_LOG_T(sev) RTC_LOG(sev) << this << ": "
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200561
Yves Gerey665174f2018-06-19 15:03:05 +0200562#define RTC_LOG_ERRNO_EX(sev, err) RTC_LOG_E(sev, ERRNO, err)
563#define RTC_LOG_ERRNO(sev) RTC_LOG_ERRNO_EX(sev, errno)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200564
565#if defined(WEBRTC_WIN)
Yves Gerey665174f2018-06-19 15:03:05 +0200566#define RTC_LOG_GLE_EX(sev, err) RTC_LOG_E(sev, HRESULT, err)
Karl Wibergcefc4652018-05-23 23:20:38 +0200567#define RTC_LOG_GLE(sev) RTC_LOG_GLE_EX(sev, static_cast<int>(GetLastError()))
Yves Gerey665174f2018-06-19 15:03:05 +0200568#define RTC_LOG_ERR_EX(sev, err) RTC_LOG_GLE_EX(sev, err)
569#define RTC_LOG_ERR(sev) RTC_LOG_GLE(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200570#elif defined(__native_client__) && __native_client__
Yves Gerey665174f2018-06-19 15:03:05 +0200571#define RTC_LOG_ERR_EX(sev, err) RTC_LOG(sev)
572#define RTC_LOG_ERR(sev) RTC_LOG(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200573#elif defined(WEBRTC_POSIX)
Yves Gerey665174f2018-06-19 15:03:05 +0200574#define RTC_LOG_ERR_EX(sev, err) RTC_LOG_ERRNO_EX(sev, err)
575#define RTC_LOG_ERR(sev) RTC_LOG_ERRNO(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200576#endif // WEBRTC_WIN
577
Karl Wibergcefc4652018-05-23 23:20:38 +0200578#ifdef WEBRTC_ANDROID
579
580namespace webrtc_logging_impl {
581// TODO(kwiberg): Replace these with absl::string_view.
Yves Gerey665174f2018-06-19 15:03:05 +0200582inline const char* AdaptString(const char* str) {
583 return str;
584}
585inline const char* AdaptString(const std::string& str) {
586 return str.c_str();
587}
Karl Wibergcefc4652018-05-23 23:20:38 +0200588} // namespace webrtc_logging_impl
589
590#define RTC_LOG_TAG(sev, tag) \
Karl Wibergcefc4652018-05-23 23:20:38 +0200591 rtc::webrtc_logging_impl::LogCall() & \
592 rtc::webrtc_logging_impl::LogStreamer<>() \
593 << rtc::webrtc_logging_impl::LogMetadataTag { \
594 sev, rtc::webrtc_logging_impl::AdaptString(tag) \
595 }
596
Tommie51a0a82018-02-27 15:30:29 +0100597#else
Karl Wibergcefc4652018-05-23 23:20:38 +0200598
Tommie51a0a82018-02-27 15:30:29 +0100599// DEPRECATED. This macro is only intended for Android.
Karl Wibergcefc4652018-05-23 23:20:38 +0200600#define RTC_LOG_TAG(sev, tag) RTC_LOG_V(sev)
601
Tommie51a0a82018-02-27 15:30:29 +0100602#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200603
Fredrik Solenbergb3d7cac2017-11-17 15:22:37 +0100604// The RTC_DLOG macros are equivalent to their RTC_LOG counterparts except that
605// they only generate code in debug builds.
606#if RTC_DLOG_IS_ON
607#define RTC_DLOG(sev) RTC_LOG(sev)
608#define RTC_DLOG_V(sev) RTC_LOG_V(sev)
609#define RTC_DLOG_F(sev) RTC_LOG_F(sev)
610#else
Karl Wibergcefc4652018-05-23 23:20:38 +0200611#define RTC_DLOG_EAT_STREAM_PARAMS() \
612 while (false) \
613 rtc::webrtc_logging_impl::LogStreamer<>()
614#define RTC_DLOG(sev) RTC_DLOG_EAT_STREAM_PARAMS()
615#define RTC_DLOG_V(sev) RTC_DLOG_EAT_STREAM_PARAMS()
616#define RTC_DLOG_F(sev) RTC_DLOG_EAT_STREAM_PARAMS()
Fredrik Solenbergb3d7cac2017-11-17 15:22:37 +0100617#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200618
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200619} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000620
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200621#endif // RTC_BASE_LOGGING_H_