blob: 1df8cd63f9912a0a68f90d531345d82dda55081b [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/basictypes.h"
59#include "rtc_base/constructormagic.h"
Tommie51a0a82018-02-27 15:30:29 +010060#include "rtc_base/deprecation.h"
Karl Wibergee10ea82018-05-04 13:27:48 +020061#include "rtc_base/system/no_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 Hensman21219a0e2018-05-18 14:32:50 +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
126class LogMessage {
127 public:
Karl Wibergab4f1c12018-05-04 10:42:28 +0200128 LogMessage(const char* file, int line, LoggingSeverity sev);
Karl Wiberg1ffb3742018-05-04 15:04:48 +0200129
130 // Same as the above, but using a compile-time constant for the logging
131 // severity. This saves space at the call site, since passing an empty struct
132 // is generally the same as not passing an argument at all.
133 template <LoggingSeverity S>
134 RTC_NO_INLINE LogMessage(const char* file,
135 int line,
136 std::integral_constant<LoggingSeverity, S>)
137 : LogMessage(file, line, S) {}
138
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200139 LogMessage(const char* file,
140 int line,
141 LoggingSeverity sev,
Karl Wibergab4f1c12018-05-04 10:42:28 +0200142 LogErrorContext err_ctx,
143 int err);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200144
Tommie51a0a82018-02-27 15:30:29 +0100145#if defined(WEBRTC_ANDROID)
146 LogMessage(const char* file, int line, LoggingSeverity sev, const char* tag);
147#endif
148
149 // DEPRECATED - DO NOT USE - PLEASE USE THE MACROS INSTEAD OF THE CLASS.
150 // Android code should use the 'const char*' version since tags are static
151 // and we want to avoid allocating a std::string copy per log line.
152 RTC_DEPRECATED
153 LogMessage(const char* file, int line, LoggingSeverity sev,
Philip Eliasson278aa422018-02-26 14:54:45 +0000154 const std::string& tag);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200155
156 ~LogMessage();
157
Jonas Olsson2b6f1352018-02-15 11:57:03 +0100158 static bool Loggable(LoggingSeverity sev);
Karl Wibergee10ea82018-05-04 13:27:48 +0200159
160 // Same as the above, but using a template argument instead of a function
161 // argument. (When the logging severity is statically known, passing it as a
162 // template argument instead of as a function argument saves space at the
163 // call site.)
164 template <LoggingSeverity S>
165 RTC_NO_INLINE static bool Loggable() {
166 return Loggable(S);
167 }
168
Tommifef05002018-02-27 13:51:08 +0100169 std::ostream& stream();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200170
171 // Returns the time at which this function was called for the first time.
172 // The time will be used as the logging start time.
173 // If this is not called externally, the LogMessage ctor also calls it, in
174 // which case the logging start time will be the time of the first LogMessage
175 // instance is created.
176 static int64_t LogStartTime();
177
178 // Returns the wall clock equivalent of |LogStartTime|, in seconds from the
179 // epoch.
180 static uint32_t WallClockStartTime();
181
182 // LogThreads: Display the thread identifier of the current thread
183 static void LogThreads(bool on = true);
184
185 // LogTimestamps: Display the elapsed time of the program
186 static void LogTimestamps(bool on = true);
187
188 // These are the available logging channels
189 // Debug: Debug console on Windows, otherwise stderr
190 static void LogToDebug(LoggingSeverity min_sev);
Jonas Olsson2b6f1352018-02-15 11:57:03 +0100191 static LoggingSeverity GetLogToDebug();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200192
193 // Sets whether logs will be directed to stderr in debug mode.
194 static void SetLogToStderr(bool log_to_stderr);
195
196 // Stream: Any non-blocking stream interface. LogMessage takes ownership of
197 // the stream. Multiple streams may be specified by using AddLogToStream.
198 // LogToStream is retained for backwards compatibility; when invoked, it
199 // will discard any previously set streams and install the specified stream.
200 // GetLogToStream gets the severity for the specified stream, of if none
201 // is specified, the minimum stream severity.
202 // RemoveLogToStream removes the specified stream, without destroying it.
203 static int GetLogToStream(LogSink* stream = nullptr);
204 static void AddLogToStream(LogSink* stream, LoggingSeverity min_sev);
205 static void RemoveLogToStream(LogSink* stream);
206
207 // Testing against MinLogSeverity allows code to avoid potentially expensive
208 // logging operations by pre-checking the logging level.
Jonas Olsson2b6f1352018-02-15 11:57:03 +0100209 static int GetMinLogSeverity();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200210
211 // Parses the provided parameter stream to configure the options above.
212 // Useful for configuring logging from the command line.
213 static void ConfigureLogging(const char* params);
214
215 private:
Tommifef05002018-02-27 13:51:08 +0100216 friend class LogMessageForTesting;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200217 typedef std::pair<LogSink*, LoggingSeverity> StreamAndSeverity;
218 typedef std::list<StreamAndSeverity> StreamList;
219
220 // Updates min_sev_ appropriately when debug sinks change.
221 static void UpdateMinLogSeverity();
222
223 // These write out the actual log messages.
Tommie51a0a82018-02-27 15:30:29 +0100224#if defined(WEBRTC_ANDROID)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200225 static void OutputToDebug(const std::string& msg,
226 LoggingSeverity severity,
Tommie51a0a82018-02-27 15:30:29 +0100227 const char* tag);
228#else
229 static void OutputToDebug(const std::string& msg, LoggingSeverity severity);
230#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200231
Tommifef05002018-02-27 13:51:08 +0100232 // Checks the current global debug severity and if the |streams_| collection
233 // is empty. If |severity| is smaller than the global severity and if the
234 // |streams_| collection is empty, the LogMessage will be considered a noop
235 // LogMessage.
236 static bool IsNoop(LoggingSeverity severity);
237
238 // Called from the dtor (or from a test) to append optional extra error
239 // information to the log stream and a newline character.
240 void FinishPrintStream();
241
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200242 // The ostream that buffers the formatted message before output
243 std::ostringstream print_stream_;
244
245 // The severity level of this message
246 LoggingSeverity severity_;
247
Tommie51a0a82018-02-27 15:30:29 +0100248#if defined(WEBRTC_ANDROID)
Paulina Hensman21219a0e2018-05-18 14:32:50 +0200249 // The default Android debug output tag.
Tommie51a0a82018-02-27 15:30:29 +0100250 const char* tag_ = "libjingle";
251#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200252
253 // String data generated in the constructor, that should be appended to
254 // the message before output.
255 std::string extra_;
256
Tommifef05002018-02-27 13:51:08 +0100257 const bool is_noop_;
258
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200259 // The output streams and their associated severities
260 static StreamList streams_;
261
262 // Flags for formatting options
263 static bool thread_, timestamp_;
264
265 // Determines if logs will be directed to stderr in debug mode.
266 static bool log_to_stderr_;
267
268 RTC_DISALLOW_COPY_AND_ASSIGN(LogMessage);
269};
270
271//////////////////////////////////////////////////////////////////////
272// Logging Helpers
273//////////////////////////////////////////////////////////////////////
274
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200275// The following non-obvious technique for implementation of a
276// conditional log stream was stolen from google3/base/logging.h.
277
278// This class is used to explicitly ignore values in the conditional
279// logging macros. This avoids compiler warnings like "value computed
280// is not used" and "statement has no effect".
281
282class LogMessageVoidify {
283 public:
284 LogMessageVoidify() { }
285 // This has to be an operator with a precedence lower than << but
286 // higher than ?:
287 void operator&(std::ostream&) { }
288};
289
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200290#define RTC_LOG_SEVERITY_PRECONDITION(sev) \
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200291 !(rtc::LogMessage::Loggable(sev)) \
292 ? (void) 0 \
293 : rtc::LogMessageVoidify() &
294
Karl Wibergee10ea82018-05-04 13:27:48 +0200295#define RTC_LOG_SEVERITY_PRECONDITION_C(sev) \
296 !(rtc::LogMessage::Loggable<rtc::sev>()) ? (void)0 : rtc::LogMessageVoidify()&
Karl Wiberg1ffb3742018-05-04 15:04:48 +0200297#define RTC_LOG(sev) \
298 RTC_LOG_SEVERITY_PRECONDITION_C(sev) \
299 rtc::LogMessage(__FILE__, __LINE__, \
300 std::integral_constant<rtc::LoggingSeverity, rtc::sev>()) \
301 .stream()
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200302
303// The _V version is for when a variable is passed in. It doesn't do the
Jonas Olsson24ea8222018-01-25 10:14:29 +0100304// namespace concatenation.
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200305#define RTC_LOG_V(sev) \
306 RTC_LOG_SEVERITY_PRECONDITION(sev) \
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200307 rtc::LogMessage(__FILE__, __LINE__, sev).stream()
308
309// The _F version prefixes the message with the current function name.
310#if (defined(__GNUC__) && !defined(NDEBUG)) || defined(WANT_PRETTY_LOG_F)
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200311#define RTC_LOG_F(sev) RTC_LOG(sev) << __PRETTY_FUNCTION__ << ": "
Patrik Höglundc2962552017-11-17 13:40:22 +0000312#define RTC_LOG_T_F(sev) RTC_LOG(sev) << this << ": " \
313 << __PRETTY_FUNCTION__ << ": "
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200314#else
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200315#define RTC_LOG_F(sev) RTC_LOG(sev) << __FUNCTION__ << ": "
Patrik Höglundc2962552017-11-17 13:40:22 +0000316#define RTC_LOG_T_F(sev) RTC_LOG(sev) << this << ": " << __FUNCTION__ << ": "
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200317#endif
318
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200319#define RTC_LOG_CHECK_LEVEL(sev) \
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200320 rtc::LogCheckLevel(rtc::sev)
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200321#define RTC_LOG_CHECK_LEVEL_V(sev) \
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200322 rtc::LogCheckLevel(sev)
323
324inline bool LogCheckLevel(LoggingSeverity sev) {
325 return (LogMessage::GetMinLogSeverity() <= sev);
326}
327
Karl Wibergee10ea82018-05-04 13:27:48 +0200328#define RTC_LOG_E(sev, ctx, err, ...) \
329 RTC_LOG_SEVERITY_PRECONDITION_C(sev) \
330 rtc::LogMessage(__FILE__, __LINE__, rtc::sev, rtc::ERRCTX_##ctx, err, \
331 ##__VA_ARGS__) \
332 .stream()
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200333
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200334#define RTC_LOG_T(sev) RTC_LOG(sev) << this << ": "
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200335
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200336#define RTC_LOG_ERRNO_EX(sev, err) \
337 RTC_LOG_E(sev, ERRNO, err)
338#define RTC_LOG_ERRNO(sev) \
339 RTC_LOG_ERRNO_EX(sev, errno)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200340
341#if defined(WEBRTC_WIN)
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200342#define RTC_LOG_GLE_EX(sev, err) \
343 RTC_LOG_E(sev, HRESULT, err)
344#define RTC_LOG_GLE(sev) \
345 RTC_LOG_GLE_EX(sev, GetLastError())
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200346#define RTC_LOG_ERR_EX(sev, err) \
347 RTC_LOG_GLE_EX(sev, err)
348#define RTC_LOG_ERR(sev) \
349 RTC_LOG_GLE(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200350#elif defined(__native_client__) && __native_client__
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200351#define RTC_LOG_ERR_EX(sev, err) \
352 RTC_LOG(sev)
353#define RTC_LOG_ERR(sev) \
354 RTC_LOG(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200355#elif defined(WEBRTC_POSIX)
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200356#define RTC_LOG_ERR_EX(sev, err) \
357 RTC_LOG_ERRNO_EX(sev, err)
358#define RTC_LOG_ERR(sev) \
359 RTC_LOG_ERRNO(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200360#endif // WEBRTC_WIN
361
Tommie51a0a82018-02-27 15:30:29 +0100362#if defined(WEBRTC_ANDROID)
363namespace internal {
364// Inline adapters provided for backwards compatibility for downstream projects.
365inline const char* AdaptString(const char* str) { return str; }
366inline const char* AdaptString(const std::string& str) { return str.c_str(); }
367} // namespace internal
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200368#define RTC_LOG_TAG(sev, tag) \
369 RTC_LOG_SEVERITY_PRECONDITION(sev) \
Tommie51a0a82018-02-27 15:30:29 +0100370 rtc::LogMessage(nullptr, 0, sev, rtc::internal::AdaptString(tag)).stream()
371#else
372// DEPRECATED. This macro is only intended for Android.
373#define RTC_LOG_TAG(sev, tag) \
374 RTC_LOG_SEVERITY_PRECONDITION(sev) \
375 rtc::LogMessage(nullptr, 0, sev).stream()
376#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200377
Fredrik Solenbergb3d7cac2017-11-17 15:22:37 +0100378// The RTC_DLOG macros are equivalent to their RTC_LOG counterparts except that
379// they only generate code in debug builds.
380#if RTC_DLOG_IS_ON
381#define RTC_DLOG(sev) RTC_LOG(sev)
382#define RTC_DLOG_V(sev) RTC_LOG_V(sev)
383#define RTC_DLOG_F(sev) RTC_LOG_F(sev)
384#else
385#define RTC_DLOG_EAT_STREAM_PARAMS(sev) \
Jonas Olsson24ea8222018-01-25 10:14:29 +0100386 (true ? true : ((void)(sev), true)) \
387 ? static_cast<void>(0) \
388 : rtc::LogMessageVoidify() & \
389 rtc::LogMessage(__FILE__, __LINE__, sev).stream()
390#define RTC_DLOG(sev) RTC_DLOG_EAT_STREAM_PARAMS(rtc::sev)
Fredrik Solenbergb3d7cac2017-11-17 15:22:37 +0100391#define RTC_DLOG_V(sev) RTC_DLOG_EAT_STREAM_PARAMS(sev)
Jonas Olsson24ea8222018-01-25 10:14:29 +0100392#define RTC_DLOG_F(sev) RTC_DLOG_EAT_STREAM_PARAMS(rtc::sev)
Fredrik Solenbergb3d7cac2017-11-17 15:22:37 +0100393#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200394
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200395} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000396
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200397#endif // RTC_BASE_LOGGING_H_