blob: b5af95928b590ced6766287439491163417bf937 [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() {}
Paulina Hensmanf1e3cb42018-06-20 14:07:05 +0200119 virtual void OnLogMessage(const std::string& msg,
120 LoggingSeverity severity,
121 const char* tag);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200122 virtual void OnLogMessage(const std::string& message) = 0;
123};
124
Karl Wibergcefc4652018-05-23 23:20:38 +0200125namespace webrtc_logging_impl {
126
127class LogMetadata {
128 public:
129 LogMetadata(const char* file, int line, LoggingSeverity severity)
130 : file_(file),
131 line_and_sev_(static_cast<uint32_t>(line) << 3 | severity) {}
132 LogMetadata() = default;
133
134 const char* File() const { return file_; }
135 int Line() const { return line_and_sev_ >> 3; }
136 LoggingSeverity Severity() const {
137 return static_cast<LoggingSeverity>(line_and_sev_ & 0x7);
138 }
139
140 private:
141 const char* file_;
142
143 // Line number and severity, the former in the most significant 29 bits, the
144 // latter in the least significant 3 bits. (This is an optimization; since
145 // both numbers are usually compile-time constants, this way we can load them
146 // both with a single instruction.)
147 uint32_t line_and_sev_;
148};
149static_assert(std::is_trivial<LogMetadata>::value, "");
150
151struct LogMetadataErr {
152 LogMetadata meta;
153 LogErrorContext err_ctx;
154 int err;
155};
156
157#ifdef WEBRTC_ANDROID
158struct LogMetadataTag {
159 LoggingSeverity severity;
160 const char* tag;
161};
162#endif
163
164enum class LogArgType : int8_t {
165 kEnd = 0,
166 kInt,
167 kLong,
168 kLongLong,
169 kUInt,
170 kULong,
171 kULongLong,
172 kDouble,
173 kLongDouble,
174 kCharP,
175 kStdString,
176 // TODO(kwiberg): Add absl::StringView.
177 kVoidP,
178 kLogMetadata,
179 kLogMetadataErr,
180#ifdef WEBRTC_ANDROID
181 kLogMetadataTag,
182#endif
183};
184
185// Wrapper for log arguments. Only ever make values of this type with the
186// MakeVal() functions.
187template <LogArgType N, typename T>
188struct Val {
189 static constexpr LogArgType Type() { return N; }
190 T GetVal() const { return val; }
191 T val;
192};
193
194// TODO(bugs.webrtc.org/9278): Get rid of this specialization when callers
195// don't need it anymore. No in-tree caller does, but some external callers
196// still do.
197template <>
198struct Val<LogArgType::kStdString, std::string> {
199 static constexpr LogArgType Type() { return LogArgType::kStdString; }
200 const std::string* GetVal() const { return &val; }
201 std::string val;
202};
203
204inline Val<LogArgType::kInt, int> MakeVal(int x) {
205 return {x};
206}
207inline Val<LogArgType::kLong, long> MakeVal(long x) {
208 return {x};
209}
210inline Val<LogArgType::kLongLong, long long> MakeVal(long long x) {
211 return {x};
212}
213inline Val<LogArgType::kUInt, unsigned int> MakeVal(unsigned int x) {
214 return {x};
215}
216inline Val<LogArgType::kULong, unsigned long> MakeVal(unsigned long x) {
217 return {x};
218}
219inline Val<LogArgType::kULongLong, unsigned long long> MakeVal(
220 unsigned long long x) {
221 return {x};
222}
223
224inline Val<LogArgType::kDouble, double> MakeVal(double x) {
225 return {x};
226}
227inline Val<LogArgType::kLongDouble, long double> MakeVal(long double x) {
228 return {x};
229}
230
231inline Val<LogArgType::kCharP, const char*> MakeVal(const char* x) {
232 return {x};
233}
234inline Val<LogArgType::kStdString, const std::string*> MakeVal(
235 const std::string& x) {
236 return {&x};
237}
238// TODO(kwiberg): Add absl::string_view
239
240inline Val<LogArgType::kVoidP, const void*> MakeVal(const void* x) {
241 return {x};
242}
243
244inline Val<LogArgType::kLogMetadata, LogMetadata> MakeVal(
245 const LogMetadata& x) {
246 return {x};
247}
248inline Val<LogArgType::kLogMetadataErr, LogMetadataErr> MakeVal(
249 const LogMetadataErr& x) {
250 return {x};
251}
252
253#ifdef WEBRTC_ANDROID
254inline Val<LogArgType::kLogMetadataTag, LogMetadataTag> MakeVal(
255 const LogMetadataTag& x) {
256 return {x};
257}
258#endif
259
260// Handle arbitrary types other than the above by falling back to stringstream.
261// TODO(bugs.webrtc.org/9278): Get rid of this overload when callers don't need
262// it anymore. No in-tree caller does, but some external callers still do.
263template <
264 typename T,
265 typename T1 =
266 typename std::remove_cv<typename std::remove_reference<T>::type>::type,
267 typename std::enable_if<
268 std::is_class<T1>::value && !std::is_same<T1, std::string>::value &&
269 !std::is_same<T1, LogMetadata>::value &&
270#ifdef WEBRTC_ANDROID
271 !std::is_same<T1, LogMetadataTag>::value &&
272#endif
273 !std::is_same<T1, LogMetadataErr>::value>::type* = nullptr>
274Val<LogArgType::kStdString, std::string> MakeVal(const T& x) {
275 std::ostringstream os; // no-presubmit-check TODO(webrtc:8982)
276 os << x;
277 return {os.str()};
278}
279
280void Log(const LogArgType* fmt, ...);
281
282// Ephemeral type that represents the result of the logging << operator.
283template <typename... Ts>
284class LogStreamer;
285
286// Base case: Before the first << argument.
287template <>
288class LogStreamer<> final {
289 public:
290 template <
291 typename U,
292 typename std::enable_if<std::is_arithmetic<U>::value>::type* = nullptr>
293 RTC_FORCE_INLINE LogStreamer<decltype(MakeVal(std::declval<U>()))> operator<<(
294 U arg) const {
295 return LogStreamer<decltype(MakeVal(std::declval<U>()))>(MakeVal(arg),
296 this);
297 }
298
299 template <
300 typename U,
301 typename std::enable_if<!std::is_arithmetic<U>::value>::type* = nullptr>
302 RTC_FORCE_INLINE LogStreamer<decltype(MakeVal(std::declval<U>()))> operator<<(
303 const U& arg) const {
304 return LogStreamer<decltype(MakeVal(std::declval<U>()))>(MakeVal(arg),
305 this);
306 }
307
308 template <typename... Us>
309 RTC_FORCE_INLINE static void Call(const Us&... args) {
310 static constexpr LogArgType t[] = {Us::Type()..., LogArgType::kEnd};
311 Log(t, args.GetVal()...);
312 }
313};
314
315// Inductive case: We've already seen at least one << argument. The most recent
316// one had type `T`, and the earlier ones had types `Ts`.
317template <typename T, typename... Ts>
318class LogStreamer<T, Ts...> final {
319 public:
320 RTC_FORCE_INLINE LogStreamer(T arg, const LogStreamer<Ts...>* prior)
321 : arg_(arg), prior_(prior) {}
322
323 template <
324 typename U,
325 typename std::enable_if<std::is_arithmetic<U>::value>::type* = nullptr>
326 RTC_FORCE_INLINE LogStreamer<decltype(MakeVal(std::declval<U>())), T, Ts...>
327 operator<<(U arg) const {
328 return LogStreamer<decltype(MakeVal(std::declval<U>())), T, Ts...>(
329 MakeVal(arg), this);
330 }
331
332 template <
333 typename U,
334 typename std::enable_if<!std::is_arithmetic<U>::value>::type* = nullptr>
335 RTC_FORCE_INLINE LogStreamer<decltype(MakeVal(std::declval<U>())), T, Ts...>
336 operator<<(const U& arg) const {
337 return LogStreamer<decltype(MakeVal(std::declval<U>())), T, Ts...>(
338 MakeVal(arg), this);
339 }
340
341 template <typename... Us>
342 RTC_FORCE_INLINE void Call(const Us&... args) const {
343 prior_->Call(arg_, args...);
344 }
345
346 private:
347 // The most recent argument.
348 T arg_;
349
350 // Earlier arguments.
351 const LogStreamer<Ts...>* prior_;
352};
353
354class LogCall final {
355 public:
356 // This can be any binary operator with precedence lower than <<.
357 template <typename... Ts>
358 RTC_FORCE_INLINE void operator&(const LogStreamer<Ts...>& streamer) {
359 streamer.Call();
360 }
361};
362
363// TODO(bugs.webrtc.org/9278): Remove this once it's no longer used.
364struct LogMessageVoidify {
365 void operator&(std::ostream&) {} // no-presubmit-check TODO(webrtc:8982)
366};
367
368} // namespace webrtc_logging_impl
369
370// Direct use of this class is deprecated; please use the logging macros
371// instead.
372// TODO(bugs.webrtc.org/9278): Move this class to an unnamed namespace in the
373// .cc file.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200374class LogMessage {
375 public:
Karl Wibergab4f1c12018-05-04 10:42:28 +0200376 LogMessage(const char* file, int line, LoggingSeverity sev);
Karl Wiberg1ffb3742018-05-04 15:04:48 +0200377
378 // Same as the above, but using a compile-time constant for the logging
379 // severity. This saves space at the call site, since passing an empty struct
380 // is generally the same as not passing an argument at all.
381 template <LoggingSeverity S>
382 RTC_NO_INLINE LogMessage(const char* file,
383 int line,
384 std::integral_constant<LoggingSeverity, S>)
385 : LogMessage(file, line, S) {}
386
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200387 LogMessage(const char* file,
388 int line,
389 LoggingSeverity sev,
Karl Wibergab4f1c12018-05-04 10:42:28 +0200390 LogErrorContext err_ctx,
391 int err);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200392
Tommie51a0a82018-02-27 15:30:29 +0100393#if defined(WEBRTC_ANDROID)
394 LogMessage(const char* file, int line, LoggingSeverity sev, const char* tag);
395#endif
396
397 // DEPRECATED - DO NOT USE - PLEASE USE THE MACROS INSTEAD OF THE CLASS.
398 // Android code should use the 'const char*' version since tags are static
399 // and we want to avoid allocating a std::string copy per log line.
400 RTC_DEPRECATED
Yves Gerey665174f2018-06-19 15:03:05 +0200401 LogMessage(const char* file,
402 int line,
403 LoggingSeverity sev,
Philip Eliasson278aa422018-02-26 14:54:45 +0000404 const std::string& tag);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200405
406 ~LogMessage();
407
Karl Wibergcefc4652018-05-23 23:20:38 +0200408 void AddTag(const char* tag);
409
Jonas Olsson2b6f1352018-02-15 11:57:03 +0100410 static bool Loggable(LoggingSeverity sev);
Karl Wibergee10ea82018-05-04 13:27:48 +0200411
412 // Same as the above, but using a template argument instead of a function
413 // argument. (When the logging severity is statically known, passing it as a
414 // template argument instead of as a function argument saves space at the
415 // call site.)
416 template <LoggingSeverity S>
417 RTC_NO_INLINE static bool Loggable() {
418 return Loggable(S);
419 }
420
Tommifef05002018-02-27 13:51:08 +0100421 std::ostream& stream();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200422
423 // Returns the time at which this function was called for the first time.
424 // The time will be used as the logging start time.
425 // If this is not called externally, the LogMessage ctor also calls it, in
426 // which case the logging start time will be the time of the first LogMessage
427 // instance is created.
428 static int64_t LogStartTime();
429
430 // Returns the wall clock equivalent of |LogStartTime|, in seconds from the
431 // epoch.
432 static uint32_t WallClockStartTime();
433
434 // LogThreads: Display the thread identifier of the current thread
435 static void LogThreads(bool on = true);
436
437 // LogTimestamps: Display the elapsed time of the program
438 static void LogTimestamps(bool on = true);
439
440 // These are the available logging channels
441 // Debug: Debug console on Windows, otherwise stderr
442 static void LogToDebug(LoggingSeverity min_sev);
Jonas Olsson2b6f1352018-02-15 11:57:03 +0100443 static LoggingSeverity GetLogToDebug();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200444
445 // Sets whether logs will be directed to stderr in debug mode.
446 static void SetLogToStderr(bool log_to_stderr);
447
448 // Stream: Any non-blocking stream interface. LogMessage takes ownership of
449 // the stream. Multiple streams may be specified by using AddLogToStream.
450 // LogToStream is retained for backwards compatibility; when invoked, it
451 // will discard any previously set streams and install the specified stream.
452 // GetLogToStream gets the severity for the specified stream, of if none
453 // is specified, the minimum stream severity.
454 // RemoveLogToStream removes the specified stream, without destroying it.
455 static int GetLogToStream(LogSink* stream = nullptr);
456 static void AddLogToStream(LogSink* stream, LoggingSeverity min_sev);
457 static void RemoveLogToStream(LogSink* stream);
458
459 // Testing against MinLogSeverity allows code to avoid potentially expensive
460 // logging operations by pre-checking the logging level.
Jonas Olsson2b6f1352018-02-15 11:57:03 +0100461 static int GetMinLogSeverity();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200462
463 // Parses the provided parameter stream to configure the options above.
464 // Useful for configuring logging from the command line.
465 static void ConfigureLogging(const char* params);
466
467 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 // Checks the current global debug severity and if the |streams_| collection
485 // is empty. If |severity| is smaller than the global severity and if the
486 // |streams_| collection is empty, the LogMessage will be considered a noop
487 // LogMessage.
488 static bool IsNoop(LoggingSeverity severity);
489
490 // Called from the dtor (or from a test) to append optional extra error
491 // information to the log stream and a newline character.
492 void FinishPrintStream();
493
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200494 // The ostream that buffers the formatted message before output
495 std::ostringstream print_stream_;
496
497 // The severity level of this message
498 LoggingSeverity severity_;
499
Tommie51a0a82018-02-27 15:30:29 +0100500#if defined(WEBRTC_ANDROID)
Paulina Hensmanf1e3cb42018-06-20 14:07:05 +0200501 // The default Android debug output tag.
Tommie51a0a82018-02-27 15:30:29 +0100502 const char* tag_ = "libjingle";
503#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200504
505 // String data generated in the constructor, that should be appended to
506 // the message before output.
507 std::string extra_;
508
Tommifef05002018-02-27 13:51:08 +0100509 const bool is_noop_;
510
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200511 // The output streams and their associated severities
512 static StreamList streams_;
513
514 // Flags for formatting options
515 static bool thread_, timestamp_;
516
517 // Determines if logs will be directed to stderr in debug mode.
518 static bool log_to_stderr_;
519
520 RTC_DISALLOW_COPY_AND_ASSIGN(LogMessage);
521};
522
523//////////////////////////////////////////////////////////////////////
524// Logging Helpers
525//////////////////////////////////////////////////////////////////////
526
Karl Wibergcefc4652018-05-23 23:20:38 +0200527// DEPRECATED.
528// TODO(bugs.webrtc.org/9278): Remove once there are no more users.
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200529#define RTC_LOG_SEVERITY_PRECONDITION(sev) \
Karl Wibergcefc4652018-05-23 23:20:38 +0200530 !(rtc::LogMessage::Loggable(sev)) \
531 ? static_cast<void>(0) \
532 : rtc::webrtc_logging_impl::LogMessageVoidify()&
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200533
Karl Wibergcefc4652018-05-23 23:20:38 +0200534#define RTC_LOG(sev) \
535 for (bool do_log = rtc::LogMessage::Loggable<rtc::sev>(); do_log; \
536 do_log = false) \
537 rtc::webrtc_logging_impl::LogCall() & \
538 rtc::webrtc_logging_impl::LogStreamer<>() \
539 << rtc::webrtc_logging_impl::LogMetadata(__FILE__, __LINE__, \
540 rtc::sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200541
Karl Wibergcefc4652018-05-23 23:20:38 +0200542// The _V version is for when a variable is passed in.
543#define RTC_LOG_V(sev) \
544 for (bool do_log = rtc::LogMessage::Loggable(sev); do_log; do_log = false) \
545 rtc::webrtc_logging_impl::LogCall() & \
546 rtc::webrtc_logging_impl::LogStreamer<>() \
547 << rtc::webrtc_logging_impl::LogMetadata(__FILE__, __LINE__, sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200548
549// The _F version prefixes the message with the current function name.
550#if (defined(__GNUC__) && !defined(NDEBUG)) || defined(WANT_PRETTY_LOG_F)
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200551#define RTC_LOG_F(sev) RTC_LOG(sev) << __PRETTY_FUNCTION__ << ": "
Yves Gerey665174f2018-06-19 15:03:05 +0200552#define RTC_LOG_T_F(sev) \
553 RTC_LOG(sev) << this << ": " << __PRETTY_FUNCTION__ << ": "
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200554#else
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200555#define RTC_LOG_F(sev) RTC_LOG(sev) << __FUNCTION__ << ": "
Patrik Höglundc2962552017-11-17 13:40:22 +0000556#define RTC_LOG_T_F(sev) RTC_LOG(sev) << this << ": " << __FUNCTION__ << ": "
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200557#endif
558
Yves Gerey665174f2018-06-19 15:03:05 +0200559#define RTC_LOG_CHECK_LEVEL(sev) rtc::LogCheckLevel(rtc::sev)
560#define RTC_LOG_CHECK_LEVEL_V(sev) rtc::LogCheckLevel(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200561
562inline bool LogCheckLevel(LoggingSeverity sev) {
563 return (LogMessage::GetMinLogSeverity() <= sev);
564}
565
Karl Wibergcefc4652018-05-23 23:20:38 +0200566#define RTC_LOG_E(sev, ctx, err) \
567 for (bool do_log = rtc::LogMessage::Loggable<rtc::sev>(); do_log; \
568 do_log = false) \
569 rtc::webrtc_logging_impl::LogCall() & \
570 rtc::webrtc_logging_impl::LogStreamer<>() \
571 << rtc::webrtc_logging_impl::LogMetadataErr { \
572 {__FILE__, __LINE__, rtc::sev}, rtc::ERRCTX_##ctx, (err) \
573 }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200574
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200575#define RTC_LOG_T(sev) RTC_LOG(sev) << this << ": "
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200576
Yves Gerey665174f2018-06-19 15:03:05 +0200577#define RTC_LOG_ERRNO_EX(sev, err) RTC_LOG_E(sev, ERRNO, err)
578#define RTC_LOG_ERRNO(sev) RTC_LOG_ERRNO_EX(sev, errno)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200579
580#if defined(WEBRTC_WIN)
Yves Gerey665174f2018-06-19 15:03:05 +0200581#define RTC_LOG_GLE_EX(sev, err) 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()))
Yves Gerey665174f2018-06-19 15:03:05 +0200583#define RTC_LOG_ERR_EX(sev, err) RTC_LOG_GLE_EX(sev, err)
584#define RTC_LOG_ERR(sev) RTC_LOG_GLE(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200585#elif defined(__native_client__) && __native_client__
Yves Gerey665174f2018-06-19 15:03:05 +0200586#define RTC_LOG_ERR_EX(sev, err) RTC_LOG(sev)
587#define RTC_LOG_ERR(sev) RTC_LOG(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200588#elif defined(WEBRTC_POSIX)
Yves Gerey665174f2018-06-19 15:03:05 +0200589#define RTC_LOG_ERR_EX(sev, err) RTC_LOG_ERRNO_EX(sev, err)
590#define RTC_LOG_ERR(sev) RTC_LOG_ERRNO(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200591#endif // WEBRTC_WIN
592
Karl Wibergcefc4652018-05-23 23:20:38 +0200593#ifdef WEBRTC_ANDROID
594
595namespace webrtc_logging_impl {
596// TODO(kwiberg): Replace these with absl::string_view.
Yves Gerey665174f2018-06-19 15:03:05 +0200597inline const char* AdaptString(const char* str) {
598 return str;
599}
600inline const char* AdaptString(const std::string& str) {
601 return str.c_str();
602}
Karl Wibergcefc4652018-05-23 23:20:38 +0200603} // namespace webrtc_logging_impl
604
605#define RTC_LOG_TAG(sev, tag) \
606 for (bool do_log = rtc::LogMessage::Loggable(sev); do_log; do_log = false) \
607 rtc::webrtc_logging_impl::LogCall() & \
608 rtc::webrtc_logging_impl::LogStreamer<>() \
609 << rtc::webrtc_logging_impl::LogMetadataTag { \
610 sev, rtc::webrtc_logging_impl::AdaptString(tag) \
611 }
612
Tommie51a0a82018-02-27 15:30:29 +0100613#else
Karl Wibergcefc4652018-05-23 23:20:38 +0200614
Tommie51a0a82018-02-27 15:30:29 +0100615// DEPRECATED. This macro is only intended for Android.
Karl Wibergcefc4652018-05-23 23:20:38 +0200616#define RTC_LOG_TAG(sev, tag) RTC_LOG_V(sev)
617
Tommie51a0a82018-02-27 15:30:29 +0100618#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200619
Fredrik Solenbergb3d7cac2017-11-17 15:22:37 +0100620// The RTC_DLOG macros are equivalent to their RTC_LOG counterparts except that
621// they only generate code in debug builds.
622#if RTC_DLOG_IS_ON
623#define RTC_DLOG(sev) RTC_LOG(sev)
624#define RTC_DLOG_V(sev) RTC_LOG_V(sev)
625#define RTC_DLOG_F(sev) RTC_LOG_F(sev)
626#else
Karl Wibergcefc4652018-05-23 23:20:38 +0200627#define RTC_DLOG_EAT_STREAM_PARAMS() \
628 while (false) \
629 rtc::webrtc_logging_impl::LogStreamer<>()
630#define RTC_DLOG(sev) RTC_DLOG_EAT_STREAM_PARAMS()
631#define RTC_DLOG_V(sev) RTC_DLOG_EAT_STREAM_PARAMS()
632#define RTC_DLOG_F(sev) RTC_DLOG_EAT_STREAM_PARAMS()
Fredrik Solenbergb3d7cac2017-11-17 15:22:37 +0100633#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200634
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200635} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000636
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200637#endif // RTC_BASE_LOGGING_H_