blob: 8f9d440fc2a23c7a76d76e822e63175806865de3 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020058#include "rtc_base/constructormagic.h"
Tommie51a0a82018-02-27 15:30:29 +010059#include "rtc_base/deprecation.h"
Karl Wibergcefc4652018-05-23 23:20:38 +020060#include "rtc_base/system/inline.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020061#include "rtc_base/thread_annotations.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020062
Fredrik Solenbergb3d7cac2017-11-17 15:22:37 +010063#if !defined(NDEBUG) || defined(DLOG_ALWAYS_ON)
64#define RTC_DLOG_IS_ON 1
65#else
66#define RTC_DLOG_IS_ON 0
67#endif
68
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020069namespace rtc {
70
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020071#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS)
72// Returns a UTF8 description from an OS X Status error.
73std::string DescriptionFromOSStatus(OSStatus err);
74#endif
75
76//////////////////////////////////////////////////////////////////////
77
78// Note that the non-standard LoggingSeverity aliases exist because they are
79// still in broad use. The meanings of the levels are:
80// LS_SENSITIVE: Information which should only be logged with the consent
81// of the user, due to privacy concerns.
82// LS_VERBOSE: This level is for data which we do not want to appear in the
83// normal debug log, but should appear in diagnostic logs.
84// LS_INFO: Chatty level used in debugging for all sorts of things, the default
85// in debug builds.
86// LS_WARNING: Something that may warrant investigation.
87// LS_ERROR: Something that should not have occurred.
88// LS_NONE: Don't log.
89enum LoggingSeverity {
90 LS_SENSITIVE,
91 LS_VERBOSE,
92 LS_INFO,
93 LS_WARNING,
94 LS_ERROR,
95 LS_NONE,
96 INFO = LS_INFO,
97 WARNING = LS_WARNING,
98 LERROR = LS_ERROR
99};
100
101// LogErrorContext assists in interpreting the meaning of an error value.
102enum LogErrorContext {
103 ERRCTX_NONE,
104 ERRCTX_ERRNO, // System-local errno
105 ERRCTX_HRESULT, // Windows HRESULT
106 ERRCTX_OSSTATUS, // MacOS OSStatus
107
108 // Abbreviations for LOG_E macro
109 ERRCTX_EN = ERRCTX_ERRNO, // LOG_E(sev, EN, x)
110 ERRCTX_HR = ERRCTX_HRESULT, // LOG_E(sev, HR, x)
111 ERRCTX_OS = ERRCTX_OSSTATUS, // LOG_E(sev, OS, x)
112};
113
114// Virtual sink interface that can receive log messages.
115class LogSink {
116 public:
117 LogSink() {}
118 virtual ~LogSink() {}
119 virtual void OnLogMessage(const std::string& message) = 0;
120};
121
Karl Wibergcefc4652018-05-23 23:20:38 +0200122namespace webrtc_logging_impl {
123
124class LogMetadata {
125 public:
126 LogMetadata(const char* file, int line, LoggingSeverity severity)
127 : file_(file),
128 line_and_sev_(static_cast<uint32_t>(line) << 3 | severity) {}
129 LogMetadata() = default;
130
131 const char* File() const { return file_; }
132 int Line() const { return line_and_sev_ >> 3; }
133 LoggingSeverity Severity() const {
134 return static_cast<LoggingSeverity>(line_and_sev_ & 0x7);
135 }
136
137 private:
138 const char* file_;
139
140 // Line number and severity, the former in the most significant 29 bits, the
141 // latter in the least significant 3 bits. (This is an optimization; since
142 // both numbers are usually compile-time constants, this way we can load them
143 // both with a single instruction.)
144 uint32_t line_and_sev_;
145};
146static_assert(std::is_trivial<LogMetadata>::value, "");
147
148struct LogMetadataErr {
149 LogMetadata meta;
150 LogErrorContext err_ctx;
151 int err;
152};
153
154#ifdef WEBRTC_ANDROID
155struct LogMetadataTag {
156 LoggingSeverity severity;
157 const char* tag;
158};
159#endif
160
161enum class LogArgType : int8_t {
162 kEnd = 0,
163 kInt,
164 kLong,
165 kLongLong,
166 kUInt,
167 kULong,
168 kULongLong,
169 kDouble,
170 kLongDouble,
171 kCharP,
172 kStdString,
173 // TODO(kwiberg): Add absl::StringView.
174 kVoidP,
175 kLogMetadata,
176 kLogMetadataErr,
177#ifdef WEBRTC_ANDROID
178 kLogMetadataTag,
179#endif
180};
181
182// Wrapper for log arguments. Only ever make values of this type with the
183// MakeVal() functions.
184template <LogArgType N, typename T>
185struct Val {
186 static constexpr LogArgType Type() { return N; }
187 T GetVal() const { return val; }
188 T val;
189};
190
191// TODO(bugs.webrtc.org/9278): Get rid of this specialization when callers
192// don't need it anymore. No in-tree caller does, but some external callers
193// still do.
194template <>
195struct Val<LogArgType::kStdString, std::string> {
196 static constexpr LogArgType Type() { return LogArgType::kStdString; }
197 const std::string* GetVal() const { return &val; }
198 std::string val;
199};
200
201inline Val<LogArgType::kInt, int> MakeVal(int x) {
202 return {x};
203}
204inline Val<LogArgType::kLong, long> MakeVal(long x) {
205 return {x};
206}
207inline Val<LogArgType::kLongLong, long long> MakeVal(long long x) {
208 return {x};
209}
210inline Val<LogArgType::kUInt, unsigned int> MakeVal(unsigned int x) {
211 return {x};
212}
213inline Val<LogArgType::kULong, unsigned long> MakeVal(unsigned long x) {
214 return {x};
215}
216inline Val<LogArgType::kULongLong, unsigned long long> MakeVal(
217 unsigned long long x) {
218 return {x};
219}
220
221inline Val<LogArgType::kDouble, double> MakeVal(double x) {
222 return {x};
223}
224inline Val<LogArgType::kLongDouble, long double> MakeVal(long double x) {
225 return {x};
226}
227
228inline Val<LogArgType::kCharP, const char*> MakeVal(const char* x) {
229 return {x};
230}
231inline Val<LogArgType::kStdString, const std::string*> MakeVal(
232 const std::string& x) {
233 return {&x};
234}
235// TODO(kwiberg): Add absl::string_view
236
237inline Val<LogArgType::kVoidP, const void*> MakeVal(const void* x) {
238 return {x};
239}
240
241inline Val<LogArgType::kLogMetadata, LogMetadata> MakeVal(
242 const LogMetadata& x) {
243 return {x};
244}
245inline Val<LogArgType::kLogMetadataErr, LogMetadataErr> MakeVal(
246 const LogMetadataErr& x) {
247 return {x};
248}
249
250#ifdef WEBRTC_ANDROID
251inline Val<LogArgType::kLogMetadataTag, LogMetadataTag> MakeVal(
252 const LogMetadataTag& x) {
253 return {x};
254}
255#endif
256
257// Handle arbitrary types other than the above by falling back to stringstream.
258// TODO(bugs.webrtc.org/9278): Get rid of this overload when callers don't need
259// it anymore. No in-tree caller does, but some external callers still do.
260template <
261 typename T,
262 typename T1 =
263 typename std::remove_cv<typename std::remove_reference<T>::type>::type,
264 typename std::enable_if<
265 std::is_class<T1>::value && !std::is_same<T1, std::string>::value &&
266 !std::is_same<T1, LogMetadata>::value &&
267#ifdef WEBRTC_ANDROID
268 !std::is_same<T1, LogMetadataTag>::value &&
269#endif
270 !std::is_same<T1, LogMetadataErr>::value>::type* = nullptr>
271Val<LogArgType::kStdString, std::string> MakeVal(const T& x) {
272 std::ostringstream os; // no-presubmit-check TODO(webrtc:8982)
273 os << x;
274 return {os.str()};
275}
276
277void Log(const LogArgType* fmt, ...);
278
279// Ephemeral type that represents the result of the logging << operator.
280template <typename... Ts>
281class LogStreamer;
282
283// Base case: Before the first << argument.
284template <>
285class LogStreamer<> final {
286 public:
287 template <
288 typename U,
289 typename std::enable_if<std::is_arithmetic<U>::value>::type* = nullptr>
290 RTC_FORCE_INLINE LogStreamer<decltype(MakeVal(std::declval<U>()))> operator<<(
291 U arg) const {
292 return LogStreamer<decltype(MakeVal(std::declval<U>()))>(MakeVal(arg),
293 this);
294 }
295
296 template <
297 typename U,
298 typename std::enable_if<!std::is_arithmetic<U>::value>::type* = nullptr>
299 RTC_FORCE_INLINE LogStreamer<decltype(MakeVal(std::declval<U>()))> operator<<(
300 const U& arg) const {
301 return LogStreamer<decltype(MakeVal(std::declval<U>()))>(MakeVal(arg),
302 this);
303 }
304
305 template <typename... Us>
306 RTC_FORCE_INLINE static void Call(const Us&... args) {
307 static constexpr LogArgType t[] = {Us::Type()..., LogArgType::kEnd};
308 Log(t, args.GetVal()...);
309 }
310};
311
312// Inductive case: We've already seen at least one << argument. The most recent
313// one had type `T`, and the earlier ones had types `Ts`.
314template <typename T, typename... Ts>
315class LogStreamer<T, Ts...> final {
316 public:
317 RTC_FORCE_INLINE LogStreamer(T arg, const LogStreamer<Ts...>* prior)
318 : arg_(arg), prior_(prior) {}
319
320 template <
321 typename U,
322 typename std::enable_if<std::is_arithmetic<U>::value>::type* = nullptr>
323 RTC_FORCE_INLINE LogStreamer<decltype(MakeVal(std::declval<U>())), T, Ts...>
324 operator<<(U arg) const {
325 return LogStreamer<decltype(MakeVal(std::declval<U>())), T, Ts...>(
326 MakeVal(arg), this);
327 }
328
329 template <
330 typename U,
331 typename std::enable_if<!std::is_arithmetic<U>::value>::type* = nullptr>
332 RTC_FORCE_INLINE LogStreamer<decltype(MakeVal(std::declval<U>())), T, Ts...>
333 operator<<(const U& arg) const {
334 return LogStreamer<decltype(MakeVal(std::declval<U>())), T, Ts...>(
335 MakeVal(arg), this);
336 }
337
338 template <typename... Us>
339 RTC_FORCE_INLINE void Call(const Us&... args) const {
340 prior_->Call(arg_, args...);
341 }
342
343 private:
344 // The most recent argument.
345 T arg_;
346
347 // Earlier arguments.
348 const LogStreamer<Ts...>* prior_;
349};
350
351class LogCall final {
352 public:
353 // This can be any binary operator with precedence lower than <<.
354 template <typename... Ts>
355 RTC_FORCE_INLINE void operator&(const LogStreamer<Ts...>& streamer) {
356 streamer.Call();
357 }
358};
359
360// TODO(bugs.webrtc.org/9278): Remove this once it's no longer used.
361struct LogMessageVoidify {
362 void operator&(std::ostream&) {} // no-presubmit-check TODO(webrtc:8982)
363};
364
365} // namespace webrtc_logging_impl
366
367// Direct use of this class is deprecated; please use the logging macros
368// instead.
369// TODO(bugs.webrtc.org/9278): Move this class to an unnamed namespace in the
370// .cc file.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200371class LogMessage {
372 public:
Karl Wibergab4f1c12018-05-04 10:42:28 +0200373 LogMessage(const char* file, int line, LoggingSeverity sev);
Karl Wiberg1ffb3742018-05-04 15:04:48 +0200374
375 // Same as the above, but using a compile-time constant for the logging
376 // severity. This saves space at the call site, since passing an empty struct
377 // is generally the same as not passing an argument at all.
378 template <LoggingSeverity S>
379 RTC_NO_INLINE LogMessage(const char* file,
380 int line,
381 std::integral_constant<LoggingSeverity, S>)
382 : LogMessage(file, line, S) {}
383
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200384 LogMessage(const char* file,
385 int line,
386 LoggingSeverity sev,
Karl Wibergab4f1c12018-05-04 10:42:28 +0200387 LogErrorContext err_ctx,
388 int err);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200389
Tommie51a0a82018-02-27 15:30:29 +0100390#if defined(WEBRTC_ANDROID)
391 LogMessage(const char* file, int line, LoggingSeverity sev, const char* tag);
392#endif
393
394 // DEPRECATED - DO NOT USE - PLEASE USE THE MACROS INSTEAD OF THE CLASS.
395 // Android code should use the 'const char*' version since tags are static
396 // and we want to avoid allocating a std::string copy per log line.
397 RTC_DEPRECATED
398 LogMessage(const char* file, int line, LoggingSeverity sev,
Philip Eliasson278aa422018-02-26 14:54:45 +0000399 const std::string& tag);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200400
401 ~LogMessage();
402
Karl Wibergcefc4652018-05-23 23:20:38 +0200403 void AddTag(const char* tag);
404
Jonas Olsson2b6f1352018-02-15 11:57:03 +0100405 static bool Loggable(LoggingSeverity sev);
Karl Wibergee10ea82018-05-04 13:27:48 +0200406
407 // Same as the above, but using a template argument instead of a function
408 // argument. (When the logging severity is statically known, passing it as a
409 // template argument instead of as a function argument saves space at the
410 // call site.)
411 template <LoggingSeverity S>
412 RTC_NO_INLINE static bool Loggable() {
413 return Loggable(S);
414 }
415
Tommifef05002018-02-27 13:51:08 +0100416 std::ostream& stream();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200417
418 // Returns the time at which this function was called for the first time.
419 // The time will be used as the logging start time.
420 // If this is not called externally, the LogMessage ctor also calls it, in
421 // which case the logging start time will be the time of the first LogMessage
422 // instance is created.
423 static int64_t LogStartTime();
424
425 // Returns the wall clock equivalent of |LogStartTime|, in seconds from the
426 // epoch.
427 static uint32_t WallClockStartTime();
428
429 // LogThreads: Display the thread identifier of the current thread
430 static void LogThreads(bool on = true);
431
432 // LogTimestamps: Display the elapsed time of the program
433 static void LogTimestamps(bool on = true);
434
435 // These are the available logging channels
436 // Debug: Debug console on Windows, otherwise stderr
437 static void LogToDebug(LoggingSeverity min_sev);
Jonas Olsson2b6f1352018-02-15 11:57:03 +0100438 static LoggingSeverity GetLogToDebug();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200439
440 // Sets whether logs will be directed to stderr in debug mode.
441 static void SetLogToStderr(bool log_to_stderr);
442
443 // Stream: Any non-blocking stream interface. LogMessage takes ownership of
444 // the stream. Multiple streams may be specified by using AddLogToStream.
445 // LogToStream is retained for backwards compatibility; when invoked, it
446 // will discard any previously set streams and install the specified stream.
447 // GetLogToStream gets the severity for the specified stream, of if none
448 // is specified, the minimum stream severity.
449 // RemoveLogToStream removes the specified stream, without destroying it.
450 static int GetLogToStream(LogSink* stream = nullptr);
451 static void AddLogToStream(LogSink* stream, LoggingSeverity min_sev);
452 static void RemoveLogToStream(LogSink* stream);
453
454 // Testing against MinLogSeverity allows code to avoid potentially expensive
455 // logging operations by pre-checking the logging level.
Jonas Olsson2b6f1352018-02-15 11:57:03 +0100456 static int GetMinLogSeverity();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200457
458 // Parses the provided parameter stream to configure the options above.
459 // Useful for configuring logging from the command line.
460 static void ConfigureLogging(const char* params);
461
462 private:
Tommifef05002018-02-27 13:51:08 +0100463 friend class LogMessageForTesting;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200464 typedef std::pair<LogSink*, LoggingSeverity> StreamAndSeverity;
465 typedef std::list<StreamAndSeverity> StreamList;
466
467 // Updates min_sev_ appropriately when debug sinks change.
468 static void UpdateMinLogSeverity();
469
470 // These write out the actual log messages.
Tommie51a0a82018-02-27 15:30:29 +0100471#if defined(WEBRTC_ANDROID)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200472 static void OutputToDebug(const std::string& msg,
473 LoggingSeverity severity,
Tommie51a0a82018-02-27 15:30:29 +0100474 const char* tag);
475#else
476 static void OutputToDebug(const std::string& msg, LoggingSeverity severity);
477#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200478
Tommifef05002018-02-27 13:51:08 +0100479 // Checks the current global debug severity and if the |streams_| collection
480 // is empty. If |severity| is smaller than the global severity and if the
481 // |streams_| collection is empty, the LogMessage will be considered a noop
482 // LogMessage.
483 static bool IsNoop(LoggingSeverity severity);
484
485 // Called from the dtor (or from a test) to append optional extra error
486 // information to the log stream and a newline character.
487 void FinishPrintStream();
488
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200489 // The ostream that buffers the formatted message before output
490 std::ostringstream print_stream_;
491
492 // The severity level of this message
493 LoggingSeverity severity_;
494
Tommie51a0a82018-02-27 15:30:29 +0100495#if defined(WEBRTC_ANDROID)
Alex Glaznev5abd78b2018-06-01 19:12:58 +0000496 // The Android debug output tag.
Tommie51a0a82018-02-27 15:30:29 +0100497 const char* tag_ = "libjingle";
498#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200499
500 // String data generated in the constructor, that should be appended to
501 // the message before output.
502 std::string extra_;
503
Tommifef05002018-02-27 13:51:08 +0100504 const bool is_noop_;
505
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200506 // The output streams and their associated severities
507 static StreamList streams_;
508
509 // Flags for formatting options
510 static bool thread_, timestamp_;
511
512 // Determines if logs will be directed to stderr in debug mode.
513 static bool log_to_stderr_;
514
515 RTC_DISALLOW_COPY_AND_ASSIGN(LogMessage);
516};
517
518//////////////////////////////////////////////////////////////////////
519// Logging Helpers
520//////////////////////////////////////////////////////////////////////
521
Karl Wibergcefc4652018-05-23 23:20:38 +0200522// DEPRECATED.
523// TODO(bugs.webrtc.org/9278): Remove once there are no more users.
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200524#define RTC_LOG_SEVERITY_PRECONDITION(sev) \
Karl Wibergcefc4652018-05-23 23:20:38 +0200525 !(rtc::LogMessage::Loggable(sev)) \
526 ? static_cast<void>(0) \
527 : rtc::webrtc_logging_impl::LogMessageVoidify()&
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200528
Karl Wibergcefc4652018-05-23 23:20:38 +0200529#define RTC_LOG(sev) \
530 for (bool do_log = rtc::LogMessage::Loggable<rtc::sev>(); do_log; \
531 do_log = false) \
532 rtc::webrtc_logging_impl::LogCall() & \
533 rtc::webrtc_logging_impl::LogStreamer<>() \
534 << rtc::webrtc_logging_impl::LogMetadata(__FILE__, __LINE__, \
535 rtc::sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200536
Karl Wibergcefc4652018-05-23 23:20:38 +0200537// The _V version is for when a variable is passed in.
538#define RTC_LOG_V(sev) \
539 for (bool do_log = rtc::LogMessage::Loggable(sev); do_log; do_log = false) \
540 rtc::webrtc_logging_impl::LogCall() & \
541 rtc::webrtc_logging_impl::LogStreamer<>() \
542 << rtc::webrtc_logging_impl::LogMetadata(__FILE__, __LINE__, sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200543
544// The _F version prefixes the message with the current function name.
545#if (defined(__GNUC__) && !defined(NDEBUG)) || defined(WANT_PRETTY_LOG_F)
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200546#define RTC_LOG_F(sev) RTC_LOG(sev) << __PRETTY_FUNCTION__ << ": "
Patrik Höglundc2962552017-11-17 13:40:22 +0000547#define RTC_LOG_T_F(sev) RTC_LOG(sev) << this << ": " \
548 << __PRETTY_FUNCTION__ << ": "
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200549#else
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200550#define RTC_LOG_F(sev) RTC_LOG(sev) << __FUNCTION__ << ": "
Patrik Höglundc2962552017-11-17 13:40:22 +0000551#define RTC_LOG_T_F(sev) RTC_LOG(sev) << this << ": " << __FUNCTION__ << ": "
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200552#endif
553
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200554#define RTC_LOG_CHECK_LEVEL(sev) \
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200555 rtc::LogCheckLevel(rtc::sev)
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200556#define RTC_LOG_CHECK_LEVEL_V(sev) \
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200557 rtc::LogCheckLevel(sev)
558
559inline bool LogCheckLevel(LoggingSeverity sev) {
560 return (LogMessage::GetMinLogSeverity() <= sev);
561}
562
Karl Wibergcefc4652018-05-23 23:20:38 +0200563#define RTC_LOG_E(sev, ctx, err) \
564 for (bool do_log = rtc::LogMessage::Loggable<rtc::sev>(); do_log; \
565 do_log = false) \
566 rtc::webrtc_logging_impl::LogCall() & \
567 rtc::webrtc_logging_impl::LogStreamer<>() \
568 << rtc::webrtc_logging_impl::LogMetadataErr { \
569 {__FILE__, __LINE__, rtc::sev}, rtc::ERRCTX_##ctx, (err) \
570 }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200571
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200572#define RTC_LOG_T(sev) RTC_LOG(sev) << this << ": "
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200573
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200574#define RTC_LOG_ERRNO_EX(sev, err) \
575 RTC_LOG_E(sev, ERRNO, err)
576#define RTC_LOG_ERRNO(sev) \
577 RTC_LOG_ERRNO_EX(sev, errno)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200578
579#if defined(WEBRTC_WIN)
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200580#define RTC_LOG_GLE_EX(sev, err) \
581 RTC_LOG_E(sev, HRESULT, err)
Karl Wibergcefc4652018-05-23 23:20:38 +0200582#define RTC_LOG_GLE(sev) RTC_LOG_GLE_EX(sev, static_cast<int>(GetLastError()))
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200583#define RTC_LOG_ERR_EX(sev, err) \
584 RTC_LOG_GLE_EX(sev, err)
585#define RTC_LOG_ERR(sev) \
586 RTC_LOG_GLE(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200587#elif defined(__native_client__) && __native_client__
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200588#define RTC_LOG_ERR_EX(sev, err) \
589 RTC_LOG(sev)
590#define RTC_LOG_ERR(sev) \
591 RTC_LOG(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200592#elif defined(WEBRTC_POSIX)
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200593#define RTC_LOG_ERR_EX(sev, err) \
594 RTC_LOG_ERRNO_EX(sev, err)
595#define RTC_LOG_ERR(sev) \
596 RTC_LOG_ERRNO(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200597#endif // WEBRTC_WIN
598
Karl Wibergcefc4652018-05-23 23:20:38 +0200599#ifdef WEBRTC_ANDROID
600
601namespace webrtc_logging_impl {
602// TODO(kwiberg): Replace these with absl::string_view.
Tommie51a0a82018-02-27 15:30:29 +0100603inline const char* AdaptString(const char* str) { return str; }
604inline const char* AdaptString(const std::string& str) { return str.c_str(); }
Karl Wibergcefc4652018-05-23 23:20:38 +0200605} // namespace webrtc_logging_impl
606
607#define RTC_LOG_TAG(sev, tag) \
608 for (bool do_log = rtc::LogMessage::Loggable(sev); do_log; do_log = false) \
609 rtc::webrtc_logging_impl::LogCall() & \
610 rtc::webrtc_logging_impl::LogStreamer<>() \
611 << rtc::webrtc_logging_impl::LogMetadataTag { \
612 sev, rtc::webrtc_logging_impl::AdaptString(tag) \
613 }
614
Tommie51a0a82018-02-27 15:30:29 +0100615#else
Karl Wibergcefc4652018-05-23 23:20:38 +0200616
Tommie51a0a82018-02-27 15:30:29 +0100617// DEPRECATED. This macro is only intended for Android.
Karl Wibergcefc4652018-05-23 23:20:38 +0200618#define RTC_LOG_TAG(sev, tag) RTC_LOG_V(sev)
619
Tommie51a0a82018-02-27 15:30:29 +0100620#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200621
Fredrik Solenbergb3d7cac2017-11-17 15:22:37 +0100622// The RTC_DLOG macros are equivalent to their RTC_LOG counterparts except that
623// they only generate code in debug builds.
624#if RTC_DLOG_IS_ON
625#define RTC_DLOG(sev) RTC_LOG(sev)
626#define RTC_DLOG_V(sev) RTC_LOG_V(sev)
627#define RTC_DLOG_F(sev) RTC_LOG_F(sev)
628#else
Karl Wibergcefc4652018-05-23 23:20:38 +0200629#define RTC_DLOG_EAT_STREAM_PARAMS() \
630 while (false) \
631 rtc::webrtc_logging_impl::LogStreamer<>()
632#define RTC_DLOG(sev) RTC_DLOG_EAT_STREAM_PARAMS()
633#define RTC_DLOG_V(sev) RTC_DLOG_EAT_STREAM_PARAMS()
634#define RTC_DLOG_F(sev) RTC_DLOG_EAT_STREAM_PARAMS()
Fredrik Solenbergb3d7cac2017-11-17 15:22:37 +0100635#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200636
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200637} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000638
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200639#endif // RTC_BASE_LOGGING_H_