blob: efaadc5af13f8660d76d8546de4d8b2cb076949a [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"
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
122class LogMessage {
123 public:
124 LogMessage(const char* file,
125 int line,
126 LoggingSeverity sev,
127 LogErrorContext err_ctx = ERRCTX_NONE,
Tommie51a0a82018-02-27 15:30:29 +0100128 int err = 0);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200129
Tommie51a0a82018-02-27 15:30:29 +0100130#if defined(WEBRTC_ANDROID)
131 LogMessage(const char* file, int line, LoggingSeverity sev, const char* tag);
132#endif
133
134 // DEPRECATED - DO NOT USE - PLEASE USE THE MACROS INSTEAD OF THE CLASS.
135 // Android code should use the 'const char*' version since tags are static
136 // and we want to avoid allocating a std::string copy per log line.
137 RTC_DEPRECATED
138 LogMessage(const char* file, int line, LoggingSeverity sev,
Philip Eliasson278aa422018-02-26 14:54:45 +0000139 const std::string& tag);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200140
141 ~LogMessage();
142
Jonas Olsson2b6f1352018-02-15 11:57:03 +0100143 static bool Loggable(LoggingSeverity sev);
Tommifef05002018-02-27 13:51:08 +0100144 std::ostream& stream();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200145
146 // Returns the time at which this function was called for the first time.
147 // The time will be used as the logging start time.
148 // If this is not called externally, the LogMessage ctor also calls it, in
149 // which case the logging start time will be the time of the first LogMessage
150 // instance is created.
151 static int64_t LogStartTime();
152
153 // Returns the wall clock equivalent of |LogStartTime|, in seconds from the
154 // epoch.
155 static uint32_t WallClockStartTime();
156
157 // LogThreads: Display the thread identifier of the current thread
158 static void LogThreads(bool on = true);
159
160 // LogTimestamps: Display the elapsed time of the program
161 static void LogTimestamps(bool on = true);
162
163 // These are the available logging channels
164 // Debug: Debug console on Windows, otherwise stderr
165 static void LogToDebug(LoggingSeverity min_sev);
Jonas Olsson2b6f1352018-02-15 11:57:03 +0100166 static LoggingSeverity GetLogToDebug();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200167
168 // Sets whether logs will be directed to stderr in debug mode.
169 static void SetLogToStderr(bool log_to_stderr);
170
171 // Stream: Any non-blocking stream interface. LogMessage takes ownership of
172 // the stream. Multiple streams may be specified by using AddLogToStream.
173 // LogToStream is retained for backwards compatibility; when invoked, it
174 // will discard any previously set streams and install the specified stream.
175 // GetLogToStream gets the severity for the specified stream, of if none
176 // is specified, the minimum stream severity.
177 // RemoveLogToStream removes the specified stream, without destroying it.
178 static int GetLogToStream(LogSink* stream = nullptr);
179 static void AddLogToStream(LogSink* stream, LoggingSeverity min_sev);
180 static void RemoveLogToStream(LogSink* stream);
181
182 // Testing against MinLogSeverity allows code to avoid potentially expensive
183 // logging operations by pre-checking the logging level.
Jonas Olsson2b6f1352018-02-15 11:57:03 +0100184 static int GetMinLogSeverity();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200185
186 // Parses the provided parameter stream to configure the options above.
187 // Useful for configuring logging from the command line.
188 static void ConfigureLogging(const char* params);
189
190 private:
Tommifef05002018-02-27 13:51:08 +0100191 friend class LogMessageForTesting;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200192 typedef std::pair<LogSink*, LoggingSeverity> StreamAndSeverity;
193 typedef std::list<StreamAndSeverity> StreamList;
194
195 // Updates min_sev_ appropriately when debug sinks change.
196 static void UpdateMinLogSeverity();
197
198 // These write out the actual log messages.
Tommie51a0a82018-02-27 15:30:29 +0100199#if defined(WEBRTC_ANDROID)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200200 static void OutputToDebug(const std::string& msg,
201 LoggingSeverity severity,
Tommie51a0a82018-02-27 15:30:29 +0100202 const char* tag);
203#else
204 static void OutputToDebug(const std::string& msg, LoggingSeverity severity);
205#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200206
Tommifef05002018-02-27 13:51:08 +0100207 // Checks the current global debug severity and if the |streams_| collection
208 // is empty. If |severity| is smaller than the global severity and if the
209 // |streams_| collection is empty, the LogMessage will be considered a noop
210 // LogMessage.
211 static bool IsNoop(LoggingSeverity severity);
212
213 // Called from the dtor (or from a test) to append optional extra error
214 // information to the log stream and a newline character.
215 void FinishPrintStream();
216
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200217 // The ostream that buffers the formatted message before output
218 std::ostringstream print_stream_;
219
220 // The severity level of this message
221 LoggingSeverity severity_;
222
Tommie51a0a82018-02-27 15:30:29 +0100223#if defined(WEBRTC_ANDROID)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200224 // The Android debug output tag.
Tommie51a0a82018-02-27 15:30:29 +0100225 const char* tag_ = "libjingle";
226#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200227
228 // String data generated in the constructor, that should be appended to
229 // the message before output.
230 std::string extra_;
231
Tommifef05002018-02-27 13:51:08 +0100232 const bool is_noop_;
233
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200234 // The output streams and their associated severities
235 static StreamList streams_;
236
237 // Flags for formatting options
238 static bool thread_, timestamp_;
239
240 // Determines if logs will be directed to stderr in debug mode.
241 static bool log_to_stderr_;
242
243 RTC_DISALLOW_COPY_AND_ASSIGN(LogMessage);
244};
245
246//////////////////////////////////////////////////////////////////////
247// Logging Helpers
248//////////////////////////////////////////////////////////////////////
249
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200250// The following non-obvious technique for implementation of a
251// conditional log stream was stolen from google3/base/logging.h.
252
253// This class is used to explicitly ignore values in the conditional
254// logging macros. This avoids compiler warnings like "value computed
255// is not used" and "statement has no effect".
256
257class LogMessageVoidify {
258 public:
259 LogMessageVoidify() { }
260 // This has to be an operator with a precedence lower than << but
261 // higher than ?:
262 void operator&(std::ostream&) { }
263};
264
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200265#define RTC_LOG_SEVERITY_PRECONDITION(sev) \
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200266 !(rtc::LogMessage::Loggable(sev)) \
267 ? (void) 0 \
268 : rtc::LogMessageVoidify() &
269
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200270#define RTC_LOG(sev) \
271 RTC_LOG_SEVERITY_PRECONDITION(rtc::sev) \
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200272 rtc::LogMessage(__FILE__, __LINE__, rtc::sev).stream()
273
274// The _V version is for when a variable is passed in. It doesn't do the
Jonas Olsson24ea8222018-01-25 10:14:29 +0100275// namespace concatenation.
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200276#define RTC_LOG_V(sev) \
277 RTC_LOG_SEVERITY_PRECONDITION(sev) \
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200278 rtc::LogMessage(__FILE__, __LINE__, sev).stream()
279
280// The _F version prefixes the message with the current function name.
281#if (defined(__GNUC__) && !defined(NDEBUG)) || defined(WANT_PRETTY_LOG_F)
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200282#define RTC_LOG_F(sev) RTC_LOG(sev) << __PRETTY_FUNCTION__ << ": "
Patrik Höglundc2962552017-11-17 13:40:22 +0000283#define RTC_LOG_T_F(sev) RTC_LOG(sev) << this << ": " \
284 << __PRETTY_FUNCTION__ << ": "
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200285#else
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200286#define RTC_LOG_F(sev) RTC_LOG(sev) << __FUNCTION__ << ": "
Patrik Höglundc2962552017-11-17 13:40:22 +0000287#define RTC_LOG_T_F(sev) RTC_LOG(sev) << this << ": " << __FUNCTION__ << ": "
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200288#endif
289
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200290#define RTC_LOG_CHECK_LEVEL(sev) \
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200291 rtc::LogCheckLevel(rtc::sev)
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200292#define RTC_LOG_CHECK_LEVEL_V(sev) \
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200293 rtc::LogCheckLevel(sev)
294
295inline bool LogCheckLevel(LoggingSeverity sev) {
296 return (LogMessage::GetMinLogSeverity() <= sev);
297}
298
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200299#define RTC_LOG_E(sev, ctx, err, ...) \
300 RTC_LOG_SEVERITY_PRECONDITION(rtc::sev) \
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200301 rtc::LogMessage(__FILE__, __LINE__, rtc::sev, \
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200302 rtc::ERRCTX_ ## ctx, err , ##__VA_ARGS__) \
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200303 .stream()
304
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200305#define RTC_LOG_T(sev) RTC_LOG(sev) << this << ": "
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200306
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200307#define RTC_LOG_ERRNO_EX(sev, err) \
308 RTC_LOG_E(sev, ERRNO, err)
309#define RTC_LOG_ERRNO(sev) \
310 RTC_LOG_ERRNO_EX(sev, errno)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200311
312#if defined(WEBRTC_WIN)
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200313#define RTC_LOG_GLE_EX(sev, err) \
314 RTC_LOG_E(sev, HRESULT, err)
315#define RTC_LOG_GLE(sev) \
316 RTC_LOG_GLE_EX(sev, GetLastError())
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200317#define RTC_LOG_ERR_EX(sev, err) \
318 RTC_LOG_GLE_EX(sev, err)
319#define RTC_LOG_ERR(sev) \
320 RTC_LOG_GLE(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200321#elif defined(__native_client__) && __native_client__
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200322#define RTC_LOG_ERR_EX(sev, err) \
323 RTC_LOG(sev)
324#define RTC_LOG_ERR(sev) \
325 RTC_LOG(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200326#elif defined(WEBRTC_POSIX)
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200327#define RTC_LOG_ERR_EX(sev, err) \
328 RTC_LOG_ERRNO_EX(sev, err)
329#define RTC_LOG_ERR(sev) \
330 RTC_LOG_ERRNO(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200331#endif // WEBRTC_WIN
332
Tommie51a0a82018-02-27 15:30:29 +0100333#if defined(WEBRTC_ANDROID)
334namespace internal {
335// Inline adapters provided for backwards compatibility for downstream projects.
336inline const char* AdaptString(const char* str) { return str; }
337inline const char* AdaptString(const std::string& str) { return str.c_str(); }
338} // namespace internal
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200339#define RTC_LOG_TAG(sev, tag) \
340 RTC_LOG_SEVERITY_PRECONDITION(sev) \
Tommie51a0a82018-02-27 15:30:29 +0100341 rtc::LogMessage(nullptr, 0, sev, rtc::internal::AdaptString(tag)).stream()
342#else
343// DEPRECATED. This macro is only intended for Android.
344#define RTC_LOG_TAG(sev, tag) \
345 RTC_LOG_SEVERITY_PRECONDITION(sev) \
346 rtc::LogMessage(nullptr, 0, sev).stream()
347#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200348
Fredrik Solenbergb3d7cac2017-11-17 15:22:37 +0100349// The RTC_DLOG macros are equivalent to their RTC_LOG counterparts except that
350// they only generate code in debug builds.
351#if RTC_DLOG_IS_ON
352#define RTC_DLOG(sev) RTC_LOG(sev)
353#define RTC_DLOG_V(sev) RTC_LOG_V(sev)
354#define RTC_DLOG_F(sev) RTC_LOG_F(sev)
355#else
356#define RTC_DLOG_EAT_STREAM_PARAMS(sev) \
Jonas Olsson24ea8222018-01-25 10:14:29 +0100357 (true ? true : ((void)(sev), true)) \
358 ? static_cast<void>(0) \
359 : rtc::LogMessageVoidify() & \
360 rtc::LogMessage(__FILE__, __LINE__, sev).stream()
361#define RTC_DLOG(sev) RTC_DLOG_EAT_STREAM_PARAMS(rtc::sev)
Fredrik Solenbergb3d7cac2017-11-17 15:22:37 +0100362#define RTC_DLOG_V(sev) RTC_DLOG_EAT_STREAM_PARAMS(sev)
Jonas Olsson24ea8222018-01-25 10:14:29 +0100363#define RTC_DLOG_F(sev) RTC_DLOG_EAT_STREAM_PARAMS(rtc::sev)
Fredrik Solenbergb3d7cac2017-11-17 15:22:37 +0100364#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200365
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200366} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000367
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200368#endif // RTC_BASE_LOGGING_H_