blob: d91db9abf53955eb067d6ab8d71e631c39caf6a0 [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.
Tommi9ecdcdf2018-02-24 15:52:11 +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"
60#include "rtc_base/thread_annotations.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020061
Fredrik Solenbergb3d7cac2017-11-17 15:22:37 +010062#if !defined(NDEBUG) || defined(DLOG_ALWAYS_ON)
63#define RTC_DLOG_IS_ON 1
64#else
65#define RTC_DLOG_IS_ON 0
66#endif
67
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020068namespace rtc {
69
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020070#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS)
71// Returns a UTF8 description from an OS X Status error.
72std::string DescriptionFromOSStatus(OSStatus err);
73#endif
74
75//////////////////////////////////////////////////////////////////////
76
77// Note that the non-standard LoggingSeverity aliases exist because they are
78// still in broad use. The meanings of the levels are:
79// LS_SENSITIVE: Information which should only be logged with the consent
80// of the user, due to privacy concerns.
81// LS_VERBOSE: This level is for data which we do not want to appear in the
82// normal debug log, but should appear in diagnostic logs.
83// LS_INFO: Chatty level used in debugging for all sorts of things, the default
84// in debug builds.
85// LS_WARNING: Something that may warrant investigation.
86// LS_ERROR: Something that should not have occurred.
87// LS_NONE: Don't log.
88enum LoggingSeverity {
89 LS_SENSITIVE,
90 LS_VERBOSE,
91 LS_INFO,
92 LS_WARNING,
93 LS_ERROR,
94 LS_NONE,
95 INFO = LS_INFO,
96 WARNING = LS_WARNING,
97 LERROR = LS_ERROR
98};
99
100// LogErrorContext assists in interpreting the meaning of an error value.
101enum LogErrorContext {
102 ERRCTX_NONE,
103 ERRCTX_ERRNO, // System-local errno
104 ERRCTX_HRESULT, // Windows HRESULT
105 ERRCTX_OSSTATUS, // MacOS OSStatus
106
107 // Abbreviations for LOG_E macro
108 ERRCTX_EN = ERRCTX_ERRNO, // LOG_E(sev, EN, x)
109 ERRCTX_HR = ERRCTX_HRESULT, // LOG_E(sev, HR, x)
110 ERRCTX_OS = ERRCTX_OSSTATUS, // LOG_E(sev, OS, x)
111};
112
113// Virtual sink interface that can receive log messages.
114class LogSink {
115 public:
116 LogSink() {}
117 virtual ~LogSink() {}
118 virtual void OnLogMessage(const std::string& message) = 0;
119};
120
121class LogMessage {
122 public:
123 LogMessage(const char* file,
124 int line,
125 LoggingSeverity sev,
126 LogErrorContext err_ctx = ERRCTX_NONE,
Tommi9ecdcdf2018-02-24 15:52:11 +0100127 int err = 0);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200128
Tommi9ecdcdf2018-02-24 15:52:11 +0100129#if defined(WEBRTC_ANDROID)
130 LogMessage(const char* file, int line, LoggingSeverity sev, const char* tag);
131#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200132
133 ~LogMessage();
134
Jonas Olsson2b6f1352018-02-15 11:57:03 +0100135 static bool Loggable(LoggingSeverity sev);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200136 std::ostream& stream() { return print_stream_; }
137
138 // Returns the time at which this function was called for the first time.
139 // The time will be used as the logging start time.
140 // If this is not called externally, the LogMessage ctor also calls it, in
141 // which case the logging start time will be the time of the first LogMessage
142 // instance is created.
143 static int64_t LogStartTime();
144
145 // Returns the wall clock equivalent of |LogStartTime|, in seconds from the
146 // epoch.
147 static uint32_t WallClockStartTime();
148
149 // LogThreads: Display the thread identifier of the current thread
150 static void LogThreads(bool on = true);
151
152 // LogTimestamps: Display the elapsed time of the program
153 static void LogTimestamps(bool on = true);
154
155 // These are the available logging channels
156 // Debug: Debug console on Windows, otherwise stderr
157 static void LogToDebug(LoggingSeverity min_sev);
Jonas Olsson2b6f1352018-02-15 11:57:03 +0100158 static LoggingSeverity GetLogToDebug();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200159
160 // Sets whether logs will be directed to stderr in debug mode.
161 static void SetLogToStderr(bool log_to_stderr);
162
163 // Stream: Any non-blocking stream interface. LogMessage takes ownership of
164 // the stream. Multiple streams may be specified by using AddLogToStream.
165 // LogToStream is retained for backwards compatibility; when invoked, it
166 // will discard any previously set streams and install the specified stream.
167 // GetLogToStream gets the severity for the specified stream, of if none
168 // is specified, the minimum stream severity.
169 // RemoveLogToStream removes the specified stream, without destroying it.
170 static int GetLogToStream(LogSink* stream = nullptr);
171 static void AddLogToStream(LogSink* stream, LoggingSeverity min_sev);
172 static void RemoveLogToStream(LogSink* stream);
173
174 // Testing against MinLogSeverity allows code to avoid potentially expensive
175 // logging operations by pre-checking the logging level.
Jonas Olsson2b6f1352018-02-15 11:57:03 +0100176 static int GetMinLogSeverity();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200177
178 // Parses the provided parameter stream to configure the options above.
179 // Useful for configuring logging from the command line.
180 static void ConfigureLogging(const char* params);
181
182 private:
183 typedef std::pair<LogSink*, LoggingSeverity> StreamAndSeverity;
184 typedef std::list<StreamAndSeverity> StreamList;
185
186 // Updates min_sev_ appropriately when debug sinks change.
187 static void UpdateMinLogSeverity();
188
189 // These write out the actual log messages.
Tommi9ecdcdf2018-02-24 15:52:11 +0100190#if defined(WEBRTC_ANDROID)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200191 static void OutputToDebug(const std::string& msg,
192 LoggingSeverity severity,
Tommi9ecdcdf2018-02-24 15:52:11 +0100193 const char* tag);
194#else
195 static void OutputToDebug(const std::string& msg, LoggingSeverity severity);
196#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200197
198 // The ostream that buffers the formatted message before output
199 std::ostringstream print_stream_;
200
201 // The severity level of this message
202 LoggingSeverity severity_;
203
Tommi9ecdcdf2018-02-24 15:52:11 +0100204#if defined(WEBRTC_ANDROID)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200205 // The Android debug output tag.
Tommi9ecdcdf2018-02-24 15:52:11 +0100206 const char* tag_ = "libjingle";
207#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200208
209 // String data generated in the constructor, that should be appended to
210 // the message before output.
211 std::string extra_;
212
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200213 // The output streams and their associated severities
214 static StreamList streams_;
215
216 // Flags for formatting options
217 static bool thread_, timestamp_;
218
219 // Determines if logs will be directed to stderr in debug mode.
220 static bool log_to_stderr_;
221
222 RTC_DISALLOW_COPY_AND_ASSIGN(LogMessage);
223};
224
225//////////////////////////////////////////////////////////////////////
226// Logging Helpers
227//////////////////////////////////////////////////////////////////////
228
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200229// The following non-obvious technique for implementation of a
230// conditional log stream was stolen from google3/base/logging.h.
231
232// This class is used to explicitly ignore values in the conditional
233// logging macros. This avoids compiler warnings like "value computed
234// is not used" and "statement has no effect".
235
236class LogMessageVoidify {
237 public:
238 LogMessageVoidify() { }
239 // This has to be an operator with a precedence lower than << but
240 // higher than ?:
241 void operator&(std::ostream&) { }
242};
243
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200244#define RTC_LOG_SEVERITY_PRECONDITION(sev) \
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200245 !(rtc::LogMessage::Loggable(sev)) \
246 ? (void) 0 \
247 : rtc::LogMessageVoidify() &
248
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200249#define RTC_LOG(sev) \
250 RTC_LOG_SEVERITY_PRECONDITION(rtc::sev) \
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200251 rtc::LogMessage(__FILE__, __LINE__, rtc::sev).stream()
252
253// The _V version is for when a variable is passed in. It doesn't do the
Jonas Olsson24ea8222018-01-25 10:14:29 +0100254// namespace concatenation.
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200255#define RTC_LOG_V(sev) \
256 RTC_LOG_SEVERITY_PRECONDITION(sev) \
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200257 rtc::LogMessage(__FILE__, __LINE__, sev).stream()
258
259// The _F version prefixes the message with the current function name.
260#if (defined(__GNUC__) && !defined(NDEBUG)) || defined(WANT_PRETTY_LOG_F)
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200261#define RTC_LOG_F(sev) RTC_LOG(sev) << __PRETTY_FUNCTION__ << ": "
Patrik Höglundc2962552017-11-17 13:40:22 +0000262#define RTC_LOG_T_F(sev) RTC_LOG(sev) << this << ": " \
263 << __PRETTY_FUNCTION__ << ": "
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200264#else
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200265#define RTC_LOG_F(sev) RTC_LOG(sev) << __FUNCTION__ << ": "
Patrik Höglundc2962552017-11-17 13:40:22 +0000266#define RTC_LOG_T_F(sev) RTC_LOG(sev) << this << ": " << __FUNCTION__ << ": "
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200267#endif
268
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200269#define RTC_LOG_CHECK_LEVEL(sev) \
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200270 rtc::LogCheckLevel(rtc::sev)
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200271#define RTC_LOG_CHECK_LEVEL_V(sev) \
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200272 rtc::LogCheckLevel(sev)
273
274inline bool LogCheckLevel(LoggingSeverity sev) {
275 return (LogMessage::GetMinLogSeverity() <= sev);
276}
277
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200278#define RTC_LOG_E(sev, ctx, err, ...) \
279 RTC_LOG_SEVERITY_PRECONDITION(rtc::sev) \
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200280 rtc::LogMessage(__FILE__, __LINE__, rtc::sev, \
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200281 rtc::ERRCTX_ ## ctx, err , ##__VA_ARGS__) \
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200282 .stream()
283
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200284#define RTC_LOG_T(sev) RTC_LOG(sev) << this << ": "
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200285
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200286#define RTC_LOG_ERRNO_EX(sev, err) \
287 RTC_LOG_E(sev, ERRNO, err)
288#define RTC_LOG_ERRNO(sev) \
289 RTC_LOG_ERRNO_EX(sev, errno)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200290
291#if defined(WEBRTC_WIN)
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200292#define RTC_LOG_GLE_EX(sev, err) \
293 RTC_LOG_E(sev, HRESULT, err)
294#define RTC_LOG_GLE(sev) \
295 RTC_LOG_GLE_EX(sev, GetLastError())
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200296#define RTC_LOG_ERR_EX(sev, err) \
297 RTC_LOG_GLE_EX(sev, err)
298#define RTC_LOG_ERR(sev) \
299 RTC_LOG_GLE(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200300#elif defined(__native_client__) && __native_client__
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200301#define RTC_LOG_ERR_EX(sev, err) \
302 RTC_LOG(sev)
303#define RTC_LOG_ERR(sev) \
304 RTC_LOG(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200305#elif defined(WEBRTC_POSIX)
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200306#define RTC_LOG_ERR_EX(sev, err) \
307 RTC_LOG_ERRNO_EX(sev, err)
308#define RTC_LOG_ERR(sev) \
309 RTC_LOG_ERRNO(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200310#endif // WEBRTC_WIN
311
Tommi9ecdcdf2018-02-24 15:52:11 +0100312#if defined(WEBRTC_ANDROID)
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200313#define RTC_LOG_TAG(sev, tag) \
314 RTC_LOG_SEVERITY_PRECONDITION(sev) \
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200315 rtc::LogMessage(nullptr, 0, sev, tag).stream()
Tommi9ecdcdf2018-02-24 15:52:11 +0100316#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200317
Fredrik Solenbergb3d7cac2017-11-17 15:22:37 +0100318// The RTC_DLOG macros are equivalent to their RTC_LOG counterparts except that
319// they only generate code in debug builds.
320#if RTC_DLOG_IS_ON
321#define RTC_DLOG(sev) RTC_LOG(sev)
322#define RTC_DLOG_V(sev) RTC_LOG_V(sev)
323#define RTC_DLOG_F(sev) RTC_LOG_F(sev)
324#else
325#define RTC_DLOG_EAT_STREAM_PARAMS(sev) \
Jonas Olsson24ea8222018-01-25 10:14:29 +0100326 (true ? true : ((void)(sev), true)) \
327 ? static_cast<void>(0) \
328 : rtc::LogMessageVoidify() & \
329 rtc::LogMessage(__FILE__, __LINE__, sev).stream()
330#define RTC_DLOG(sev) RTC_DLOG_EAT_STREAM_PARAMS(rtc::sev)
Fredrik Solenbergb3d7cac2017-11-17 15:22:37 +0100331#define RTC_DLOG_V(sev) RTC_DLOG_EAT_STREAM_PARAMS(sev)
Jonas Olsson24ea8222018-01-25 10:14:29 +0100332#define RTC_DLOG_F(sev) RTC_DLOG_EAT_STREAM_PARAMS(rtc::sev)
Fredrik Solenbergb3d7cac2017-11-17 15:22:37 +0100333#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200334
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200335} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000336
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200337#endif // RTC_BASE_LOGGING_H_