blob: 5f2aadb6f55ab72a97e7497dbf8797752b93fe26 [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"
Jonas Olsson4092cd62018-09-04 11:16:41 +020060#include "rtc_base/strings/string_builder.h"
Karl Wibergcefc4652018-05-23 23:20:38 +020061#include "rtc_base/system/inline.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020062#include "rtc_base/thread_annotations.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020063
Fredrik Solenbergb3d7cac2017-11-17 15:22:37 +010064#if !defined(NDEBUG) || defined(DLOG_ALWAYS_ON)
65#define RTC_DLOG_IS_ON 1
66#else
67#define RTC_DLOG_IS_ON 0
68#endif
69
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020070namespace rtc {
71
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020072#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS)
73// Returns a UTF8 description from an OS X Status error.
74std::string DescriptionFromOSStatus(OSStatus err);
75#endif
76
77//////////////////////////////////////////////////////////////////////
78
79// Note that the non-standard LoggingSeverity aliases exist because they are
80// still in broad use. The meanings of the levels are:
81// LS_SENSITIVE: Information which should only be logged with the consent
82// of the user, due to privacy concerns.
83// LS_VERBOSE: This level is for data which we do not want to appear in the
84// normal debug log, but should appear in diagnostic logs.
85// LS_INFO: Chatty level used in debugging for all sorts of things, the default
86// in debug builds.
87// LS_WARNING: Something that may warrant investigation.
88// LS_ERROR: Something that should not have occurred.
89// LS_NONE: Don't log.
90enum LoggingSeverity {
91 LS_SENSITIVE,
92 LS_VERBOSE,
93 LS_INFO,
94 LS_WARNING,
95 LS_ERROR,
96 LS_NONE,
97 INFO = LS_INFO,
98 WARNING = LS_WARNING,
99 LERROR = LS_ERROR
100};
101
102// LogErrorContext assists in interpreting the meaning of an error value.
103enum LogErrorContext {
104 ERRCTX_NONE,
105 ERRCTX_ERRNO, // System-local errno
106 ERRCTX_HRESULT, // Windows HRESULT
107 ERRCTX_OSSTATUS, // MacOS OSStatus
108
109 // Abbreviations for LOG_E macro
110 ERRCTX_EN = ERRCTX_ERRNO, // LOG_E(sev, EN, x)
111 ERRCTX_HR = ERRCTX_HRESULT, // LOG_E(sev, HR, x)
112 ERRCTX_OS = ERRCTX_OSSTATUS, // LOG_E(sev, OS, x)
113};
114
115// Virtual sink interface that can receive log messages.
116class LogSink {
117 public:
118 LogSink() {}
119 virtual ~LogSink() {}
Paulina Hensmanf1e3cb42018-06-20 14:07:05 +0200120 virtual void OnLogMessage(const std::string& msg,
121 LoggingSeverity severity,
122 const char* tag);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200123 virtual void OnLogMessage(const std::string& message) = 0;
124};
125
Karl Wibergcefc4652018-05-23 23:20:38 +0200126namespace webrtc_logging_impl {
127
128class LogMetadata {
129 public:
130 LogMetadata(const char* file, int line, LoggingSeverity severity)
131 : file_(file),
132 line_and_sev_(static_cast<uint32_t>(line) << 3 | severity) {}
133 LogMetadata() = default;
134
135 const char* File() const { return file_; }
136 int Line() const { return line_and_sev_ >> 3; }
137 LoggingSeverity Severity() const {
138 return static_cast<LoggingSeverity>(line_and_sev_ & 0x7);
139 }
140
141 private:
142 const char* file_;
143
144 // Line number and severity, the former in the most significant 29 bits, the
145 // latter in the least significant 3 bits. (This is an optimization; since
146 // both numbers are usually compile-time constants, this way we can load them
147 // both with a single instruction.)
148 uint32_t line_and_sev_;
149};
150static_assert(std::is_trivial<LogMetadata>::value, "");
151
152struct LogMetadataErr {
153 LogMetadata meta;
154 LogErrorContext err_ctx;
155 int err;
156};
157
158#ifdef WEBRTC_ANDROID
159struct LogMetadataTag {
160 LoggingSeverity severity;
161 const char* tag;
162};
163#endif
164
165enum class LogArgType : int8_t {
166 kEnd = 0,
167 kInt,
168 kLong,
169 kLongLong,
170 kUInt,
171 kULong,
172 kULongLong,
173 kDouble,
174 kLongDouble,
175 kCharP,
176 kStdString,
177 // TODO(kwiberg): Add absl::StringView.
178 kVoidP,
179 kLogMetadata,
180 kLogMetadataErr,
181#ifdef WEBRTC_ANDROID
182 kLogMetadataTag,
183#endif
184};
185
186// Wrapper for log arguments. Only ever make values of this type with the
187// MakeVal() functions.
188template <LogArgType N, typename T>
189struct Val {
190 static constexpr LogArgType Type() { return N; }
191 T GetVal() const { return val; }
192 T val;
193};
194
195// TODO(bugs.webrtc.org/9278): Get rid of this specialization when callers
196// don't need it anymore. No in-tree caller does, but some external callers
197// still do.
198template <>
199struct Val<LogArgType::kStdString, std::string> {
200 static constexpr LogArgType Type() { return LogArgType::kStdString; }
201 const std::string* GetVal() const { return &val; }
202 std::string val;
203};
204
205inline Val<LogArgType::kInt, int> MakeVal(int x) {
206 return {x};
207}
208inline Val<LogArgType::kLong, long> MakeVal(long x) {
209 return {x};
210}
211inline Val<LogArgType::kLongLong, long long> MakeVal(long long x) {
212 return {x};
213}
214inline Val<LogArgType::kUInt, unsigned int> MakeVal(unsigned int x) {
215 return {x};
216}
217inline Val<LogArgType::kULong, unsigned long> MakeVal(unsigned long x) {
218 return {x};
219}
220inline Val<LogArgType::kULongLong, unsigned long long> MakeVal(
221 unsigned long long x) {
222 return {x};
223}
224
225inline Val<LogArgType::kDouble, double> MakeVal(double x) {
226 return {x};
227}
228inline Val<LogArgType::kLongDouble, long double> MakeVal(long double x) {
229 return {x};
230}
231
232inline Val<LogArgType::kCharP, const char*> MakeVal(const char* x) {
233 return {x};
234}
235inline Val<LogArgType::kStdString, const std::string*> MakeVal(
236 const std::string& x) {
237 return {&x};
238}
239// TODO(kwiberg): Add absl::string_view
240
241inline Val<LogArgType::kVoidP, const void*> MakeVal(const void* x) {
242 return {x};
243}
244
245inline Val<LogArgType::kLogMetadata, LogMetadata> MakeVal(
246 const LogMetadata& x) {
247 return {x};
248}
249inline Val<LogArgType::kLogMetadataErr, LogMetadataErr> MakeVal(
250 const LogMetadataErr& x) {
251 return {x};
252}
253
254#ifdef WEBRTC_ANDROID
255inline Val<LogArgType::kLogMetadataTag, LogMetadataTag> MakeVal(
256 const LogMetadataTag& x) {
257 return {x};
258}
259#endif
260
261// Handle arbitrary types other than the above by falling back to stringstream.
262// TODO(bugs.webrtc.org/9278): Get rid of this overload when callers don't need
263// it anymore. No in-tree caller does, but some external callers still do.
264template <
265 typename T,
266 typename T1 =
267 typename std::remove_cv<typename std::remove_reference<T>::type>::type,
268 typename std::enable_if<
269 std::is_class<T1>::value && !std::is_same<T1, std::string>::value &&
270 !std::is_same<T1, LogMetadata>::value &&
271#ifdef WEBRTC_ANDROID
272 !std::is_same<T1, LogMetadataTag>::value &&
273#endif
274 !std::is_same<T1, LogMetadataErr>::value>::type* = nullptr>
275Val<LogArgType::kStdString, std::string> MakeVal(const T& x) {
276 std::ostringstream os; // no-presubmit-check TODO(webrtc:8982)
277 os << x;
278 return {os.str()};
279}
280
281void Log(const LogArgType* fmt, ...);
282
283// Ephemeral type that represents the result of the logging << operator.
284template <typename... Ts>
285class LogStreamer;
286
287// Base case: Before the first << argument.
288template <>
289class LogStreamer<> final {
290 public:
291 template <
292 typename U,
293 typename std::enable_if<std::is_arithmetic<U>::value>::type* = nullptr>
294 RTC_FORCE_INLINE LogStreamer<decltype(MakeVal(std::declval<U>()))> operator<<(
295 U arg) const {
296 return LogStreamer<decltype(MakeVal(std::declval<U>()))>(MakeVal(arg),
297 this);
298 }
299
300 template <
301 typename U,
302 typename std::enable_if<!std::is_arithmetic<U>::value>::type* = nullptr>
303 RTC_FORCE_INLINE LogStreamer<decltype(MakeVal(std::declval<U>()))> operator<<(
304 const U& arg) const {
305 return LogStreamer<decltype(MakeVal(std::declval<U>()))>(MakeVal(arg),
306 this);
307 }
308
309 template <typename... Us>
310 RTC_FORCE_INLINE static void Call(const Us&... args) {
311 static constexpr LogArgType t[] = {Us::Type()..., LogArgType::kEnd};
312 Log(t, args.GetVal()...);
313 }
314};
315
316// Inductive case: We've already seen at least one << argument. The most recent
317// one had type `T`, and the earlier ones had types `Ts`.
318template <typename T, typename... Ts>
319class LogStreamer<T, Ts...> final {
320 public:
321 RTC_FORCE_INLINE LogStreamer(T arg, const LogStreamer<Ts...>* prior)
322 : arg_(arg), prior_(prior) {}
323
324 template <
325 typename U,
326 typename std::enable_if<std::is_arithmetic<U>::value>::type* = nullptr>
327 RTC_FORCE_INLINE LogStreamer<decltype(MakeVal(std::declval<U>())), T, Ts...>
328 operator<<(U arg) const {
329 return LogStreamer<decltype(MakeVal(std::declval<U>())), T, Ts...>(
330 MakeVal(arg), this);
331 }
332
333 template <
334 typename U,
335 typename std::enable_if<!std::is_arithmetic<U>::value>::type* = nullptr>
336 RTC_FORCE_INLINE LogStreamer<decltype(MakeVal(std::declval<U>())), T, Ts...>
337 operator<<(const U& arg) const {
338 return LogStreamer<decltype(MakeVal(std::declval<U>())), T, Ts...>(
339 MakeVal(arg), this);
340 }
341
342 template <typename... Us>
343 RTC_FORCE_INLINE void Call(const Us&... args) const {
344 prior_->Call(arg_, args...);
345 }
346
347 private:
348 // The most recent argument.
349 T arg_;
350
351 // Earlier arguments.
352 const LogStreamer<Ts...>* prior_;
353};
354
355class LogCall final {
356 public:
357 // This can be any binary operator with precedence lower than <<.
358 template <typename... Ts>
359 RTC_FORCE_INLINE void operator&(const LogStreamer<Ts...>& streamer) {
360 streamer.Call();
361 }
362};
363
364// TODO(bugs.webrtc.org/9278): Remove this once it's no longer used.
365struct LogMessageVoidify {
366 void operator&(std::ostream&) {} // no-presubmit-check TODO(webrtc:8982)
367};
368
369} // namespace webrtc_logging_impl
370
371// Direct use of this class is deprecated; please use the logging macros
372// instead.
373// TODO(bugs.webrtc.org/9278): Move this class to an unnamed namespace in the
374// .cc file.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200375class LogMessage {
376 public:
Karl Wibergab4f1c12018-05-04 10:42:28 +0200377 LogMessage(const char* file, int line, LoggingSeverity sev);
Karl Wiberg1ffb3742018-05-04 15:04:48 +0200378
379 // Same as the above, but using a compile-time constant for the logging
380 // severity. This saves space at the call site, since passing an empty struct
381 // is generally the same as not passing an argument at all.
382 template <LoggingSeverity S>
383 RTC_NO_INLINE LogMessage(const char* file,
384 int line,
385 std::integral_constant<LoggingSeverity, S>)
386 : LogMessage(file, line, S) {}
387
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200388 LogMessage(const char* file,
389 int line,
390 LoggingSeverity sev,
Karl Wibergab4f1c12018-05-04 10:42:28 +0200391 LogErrorContext err_ctx,
392 int err);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200393
Tommie51a0a82018-02-27 15:30:29 +0100394#if defined(WEBRTC_ANDROID)
395 LogMessage(const char* file, int line, LoggingSeverity sev, const char* tag);
396#endif
397
398 // DEPRECATED - DO NOT USE - PLEASE USE THE MACROS INSTEAD OF THE CLASS.
399 // Android code should use the 'const char*' version since tags are static
400 // and we want to avoid allocating a std::string copy per log line.
401 RTC_DEPRECATED
Yves Gerey665174f2018-06-19 15:03:05 +0200402 LogMessage(const char* file,
403 int line,
404 LoggingSeverity sev,
Philip Eliasson278aa422018-02-26 14:54:45 +0000405 const std::string& tag);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200406
407 ~LogMessage();
408
Karl Wibergcefc4652018-05-23 23:20:38 +0200409 void AddTag(const char* tag);
410
Jonas Olsson4092cd62018-09-04 11:16:41 +0200411 rtc::StringBuilder& stream();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200412
413 // Returns the time at which this function was called for the first time.
414 // The time will be used as the logging start time.
415 // If this is not called externally, the LogMessage ctor also calls it, in
416 // which case the logging start time will be the time of the first LogMessage
417 // instance is created.
418 static int64_t LogStartTime();
419
420 // Returns the wall clock equivalent of |LogStartTime|, in seconds from the
421 // epoch.
422 static uint32_t WallClockStartTime();
423
424 // LogThreads: Display the thread identifier of the current thread
425 static void LogThreads(bool on = true);
426
427 // LogTimestamps: Display the elapsed time of the program
428 static void LogTimestamps(bool on = true);
429
430 // These are the available logging channels
431 // Debug: Debug console on Windows, otherwise stderr
432 static void LogToDebug(LoggingSeverity min_sev);
Jonas Olsson2b6f1352018-02-15 11:57:03 +0100433 static LoggingSeverity GetLogToDebug();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200434
435 // Sets whether logs will be directed to stderr in debug mode.
436 static void SetLogToStderr(bool log_to_stderr);
437
438 // Stream: Any non-blocking stream interface. LogMessage takes ownership of
439 // the stream. Multiple streams may be specified by using AddLogToStream.
440 // LogToStream is retained for backwards compatibility; when invoked, it
441 // will discard any previously set streams and install the specified stream.
442 // GetLogToStream gets the severity for the specified stream, of if none
443 // is specified, the minimum stream severity.
444 // RemoveLogToStream removes the specified stream, without destroying it.
445 static int GetLogToStream(LogSink* stream = nullptr);
446 static void AddLogToStream(LogSink* stream, LoggingSeverity min_sev);
447 static void RemoveLogToStream(LogSink* stream);
448
449 // Testing against MinLogSeverity allows code to avoid potentially expensive
450 // logging operations by pre-checking the logging level.
Jonas Olsson2b6f1352018-02-15 11:57:03 +0100451 static int GetMinLogSeverity();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200452
453 // Parses the provided parameter stream to configure the options above.
454 // Useful for configuring logging from the command line.
455 static void ConfigureLogging(const char* params);
456
Jonas Olsson4092cd62018-09-04 11:16:41 +0200457 // Checks the current global debug severity and if the |streams_| collection
458 // is empty. If |severity| is smaller than the global severity and if the
459 // |streams_| collection is empty, the LogMessage will be considered a noop
460 // LogMessage.
461 static bool IsNoop(LoggingSeverity severity);
462
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200463 private:
Tommifef05002018-02-27 13:51:08 +0100464 friend class LogMessageForTesting;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200465 typedef std::pair<LogSink*, LoggingSeverity> StreamAndSeverity;
466 typedef std::list<StreamAndSeverity> StreamList;
467
468 // Updates min_sev_ appropriately when debug sinks change.
469 static void UpdateMinLogSeverity();
470
Yves Gerey665174f2018-06-19 15:03:05 +0200471// These write out the actual log messages.
Tommie51a0a82018-02-27 15:30:29 +0100472#if defined(WEBRTC_ANDROID)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200473 static void OutputToDebug(const std::string& msg,
474 LoggingSeverity severity,
Tommie51a0a82018-02-27 15:30:29 +0100475 const char* tag);
476#else
477 static void OutputToDebug(const std::string& msg, LoggingSeverity severity);
478#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200479
Tommifef05002018-02-27 13:51:08 +0100480 // Called from the dtor (or from a test) to append optional extra error
481 // information to the log stream and a newline character.
482 void FinishPrintStream();
483
Jonas Olsson4092cd62018-09-04 11:16:41 +0200484 // The stringbuilder that buffers the formatted message before output
485 rtc::StringBuilder print_stream_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200486
487 // The severity level of this message
488 LoggingSeverity severity_;
489
Tommie51a0a82018-02-27 15:30:29 +0100490#if defined(WEBRTC_ANDROID)
Paulina Hensmanf1e3cb42018-06-20 14:07:05 +0200491 // The default Android debug output tag.
Tommie51a0a82018-02-27 15:30:29 +0100492 const char* tag_ = "libjingle";
493#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200494
495 // String data generated in the constructor, that should be appended to
496 // the message before output.
497 std::string extra_;
498
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200499 // The output streams and their associated severities
500 static StreamList streams_;
501
502 // Flags for formatting options
503 static bool thread_, timestamp_;
504
505 // Determines if logs will be directed to stderr in debug mode.
506 static bool log_to_stderr_;
507
508 RTC_DISALLOW_COPY_AND_ASSIGN(LogMessage);
509};
510
511//////////////////////////////////////////////////////////////////////
512// Logging Helpers
513//////////////////////////////////////////////////////////////////////
514
Karl Wibergcefc4652018-05-23 23:20:38 +0200515// DEPRECATED.
516// TODO(bugs.webrtc.org/9278): Remove once there are no more users.
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200517#define RTC_LOG_SEVERITY_PRECONDITION(sev) \
Jonas Olsson4092cd62018-09-04 11:16:41 +0200518 (rtc::LogMessage::IsNoop(sev)) \
Karl Wibergcefc4652018-05-23 23:20:38 +0200519 ? static_cast<void>(0) \
520 : rtc::webrtc_logging_impl::LogMessageVoidify()&
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200521
Karl Wibergcefc4652018-05-23 23:20:38 +0200522#define RTC_LOG(sev) \
Karl Wibergcefc4652018-05-23 23:20:38 +0200523 rtc::webrtc_logging_impl::LogCall() & \
524 rtc::webrtc_logging_impl::LogStreamer<>() \
525 << rtc::webrtc_logging_impl::LogMetadata(__FILE__, __LINE__, \
526 rtc::sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200527
Karl Wibergcefc4652018-05-23 23:20:38 +0200528// The _V version is for when a variable is passed in.
529#define RTC_LOG_V(sev) \
Karl Wibergcefc4652018-05-23 23:20:38 +0200530 rtc::webrtc_logging_impl::LogCall() & \
531 rtc::webrtc_logging_impl::LogStreamer<>() \
532 << rtc::webrtc_logging_impl::LogMetadata(__FILE__, __LINE__, sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200533
534// The _F version prefixes the message with the current function name.
535#if (defined(__GNUC__) && !defined(NDEBUG)) || defined(WANT_PRETTY_LOG_F)
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200536#define RTC_LOG_F(sev) RTC_LOG(sev) << __PRETTY_FUNCTION__ << ": "
Yves Gerey665174f2018-06-19 15:03:05 +0200537#define RTC_LOG_T_F(sev) \
538 RTC_LOG(sev) << this << ": " << __PRETTY_FUNCTION__ << ": "
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200539#else
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200540#define RTC_LOG_F(sev) RTC_LOG(sev) << __FUNCTION__ << ": "
Patrik Höglundc2962552017-11-17 13:40:22 +0000541#define RTC_LOG_T_F(sev) RTC_LOG(sev) << this << ": " << __FUNCTION__ << ": "
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200542#endif
543
Yves Gerey665174f2018-06-19 15:03:05 +0200544#define RTC_LOG_CHECK_LEVEL(sev) rtc::LogCheckLevel(rtc::sev)
545#define RTC_LOG_CHECK_LEVEL_V(sev) rtc::LogCheckLevel(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200546
547inline bool LogCheckLevel(LoggingSeverity sev) {
548 return (LogMessage::GetMinLogSeverity() <= sev);
549}
550
Karl Wibergcefc4652018-05-23 23:20:38 +0200551#define RTC_LOG_E(sev, ctx, err) \
Karl Wibergcefc4652018-05-23 23:20:38 +0200552 rtc::webrtc_logging_impl::LogCall() & \
553 rtc::webrtc_logging_impl::LogStreamer<>() \
554 << rtc::webrtc_logging_impl::LogMetadataErr { \
555 {__FILE__, __LINE__, rtc::sev}, rtc::ERRCTX_##ctx, (err) \
556 }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200557
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200558#define RTC_LOG_T(sev) RTC_LOG(sev) << this << ": "
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200559
Yves Gerey665174f2018-06-19 15:03:05 +0200560#define RTC_LOG_ERRNO_EX(sev, err) RTC_LOG_E(sev, ERRNO, err)
561#define RTC_LOG_ERRNO(sev) RTC_LOG_ERRNO_EX(sev, errno)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200562
563#if defined(WEBRTC_WIN)
Yves Gerey665174f2018-06-19 15:03:05 +0200564#define RTC_LOG_GLE_EX(sev, err) RTC_LOG_E(sev, HRESULT, err)
Karl Wibergcefc4652018-05-23 23:20:38 +0200565#define RTC_LOG_GLE(sev) RTC_LOG_GLE_EX(sev, static_cast<int>(GetLastError()))
Yves Gerey665174f2018-06-19 15:03:05 +0200566#define RTC_LOG_ERR_EX(sev, err) RTC_LOG_GLE_EX(sev, err)
567#define RTC_LOG_ERR(sev) RTC_LOG_GLE(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200568#elif defined(__native_client__) && __native_client__
Yves Gerey665174f2018-06-19 15:03:05 +0200569#define RTC_LOG_ERR_EX(sev, err) RTC_LOG(sev)
570#define RTC_LOG_ERR(sev) RTC_LOG(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200571#elif defined(WEBRTC_POSIX)
Yves Gerey665174f2018-06-19 15:03:05 +0200572#define RTC_LOG_ERR_EX(sev, err) RTC_LOG_ERRNO_EX(sev, err)
573#define RTC_LOG_ERR(sev) RTC_LOG_ERRNO(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200574#endif // WEBRTC_WIN
575
Karl Wibergcefc4652018-05-23 23:20:38 +0200576#ifdef WEBRTC_ANDROID
577
578namespace webrtc_logging_impl {
579// TODO(kwiberg): Replace these with absl::string_view.
Yves Gerey665174f2018-06-19 15:03:05 +0200580inline const char* AdaptString(const char* str) {
581 return str;
582}
583inline const char* AdaptString(const std::string& str) {
584 return str.c_str();
585}
Karl Wibergcefc4652018-05-23 23:20:38 +0200586} // namespace webrtc_logging_impl
587
588#define RTC_LOG_TAG(sev, tag) \
Karl Wibergcefc4652018-05-23 23:20:38 +0200589 rtc::webrtc_logging_impl::LogCall() & \
590 rtc::webrtc_logging_impl::LogStreamer<>() \
591 << rtc::webrtc_logging_impl::LogMetadataTag { \
592 sev, rtc::webrtc_logging_impl::AdaptString(tag) \
593 }
594
Tommie51a0a82018-02-27 15:30:29 +0100595#else
Karl Wibergcefc4652018-05-23 23:20:38 +0200596
Tommie51a0a82018-02-27 15:30:29 +0100597// DEPRECATED. This macro is only intended for Android.
Karl Wibergcefc4652018-05-23 23:20:38 +0200598#define RTC_LOG_TAG(sev, tag) RTC_LOG_V(sev)
599
Tommie51a0a82018-02-27 15:30:29 +0100600#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200601
Fredrik Solenbergb3d7cac2017-11-17 15:22:37 +0100602// The RTC_DLOG macros are equivalent to their RTC_LOG counterparts except that
603// they only generate code in debug builds.
604#if RTC_DLOG_IS_ON
605#define RTC_DLOG(sev) RTC_LOG(sev)
606#define RTC_DLOG_V(sev) RTC_LOG_V(sev)
607#define RTC_DLOG_F(sev) RTC_LOG_F(sev)
608#else
Karl Wibergcefc4652018-05-23 23:20:38 +0200609#define RTC_DLOG_EAT_STREAM_PARAMS() \
610 while (false) \
611 rtc::webrtc_logging_impl::LogStreamer<>()
612#define RTC_DLOG(sev) RTC_DLOG_EAT_STREAM_PARAMS()
613#define RTC_DLOG_V(sev) RTC_DLOG_EAT_STREAM_PARAMS()
614#define RTC_DLOG_F(sev) RTC_DLOG_EAT_STREAM_PARAMS()
Fredrik Solenbergb3d7cac2017-11-17 15:22:37 +0100615#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200616
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200617} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000618
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200619#endif // RTC_BASE_LOGGING_H_