blob: e3879fc05bbf009131219e36b4ef5fb270e608e2 [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.
29// RTC_LOG_GLE(M)(sev [, mod]) attempt to add a string description of the
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000030// HRESULT returned by GetLastError. The "M" variant allows searching of a
31// DLL's string table for the error description.
Mirko Bonadei8ed8e562017-10-27 09:43:53 +020032// RTC_LOG_ERRNO(sev) attempts to add a string description of an errno-derived
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000033// error. errno and associated facilities exist on both Windows and POSIX,
34// but on Windows they only apply to the C/C++ runtime.
Mirko Bonadei8ed8e562017-10-27 09:43:53 +020035// RTC_LOG_ERR(sev) is an alias for the platform's normal error system, i.e.
36// _GLE on Windows and _ERRNO on POSIX.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000037// (The above three also all have _EX versions that let you specify the error
38// code, rather than using the last one.)
Mirko Bonadei8ed8e562017-10-27 09:43:53 +020039// RTC_LOG_E(sev, ctx, err, ...) logs a detailed error interpreted using the
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000040// specified context.
Mirko Bonadei8ed8e562017-10-27 09:43:53 +020041// RTC_LOG_CHECK_LEVEL(sev) (and RTC_LOG_CHECK_LEVEL_V(sev)) can be used as a
42// test before performing expensive or sensitive operations whose sole
43// purpose is to output logging data at the desired level.
44// Lastly, RTC_PLOG(sev, err) is an alias for RTC_LOG_ERR_EX.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000045
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020046#ifndef RTC_BASE_LOGGING_H_
47#define RTC_BASE_LOGGING_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000048
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020049#include <errno.h>
mostynbe38e4f62016-05-12 01:08:20 -070050
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020051#include <list>
52#include <sstream>
53#include <string>
54#include <utility>
55
56#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS)
57#include <CoreServices/CoreServices.h>
58#endif
59
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020060#include "rtc_base/basictypes.h"
61#include "rtc_base/constructormagic.h"
62#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
72///////////////////////////////////////////////////////////////////////////////
73// ConstantLabel can be used to easily generate string names from constant
74// values. This can be useful for logging descriptive names of error messages.
75// Usage:
76// const ConstantLabel LIBRARY_ERRORS[] = {
77// KLABEL(SOME_ERROR),
78// KLABEL(SOME_OTHER_ERROR),
79// ...
80// LASTLABEL
81// }
82//
83// int err = LibraryFunc();
84// LOG(LS_ERROR) << "LibraryFunc returned: "
85// << ErrorName(err, LIBRARY_ERRORS);
86
87struct ConstantLabel { int value; const char * label; };
88#define KLABEL(x) { x, #x }
89#define TLABEL(x, y) { x, y }
90#define LASTLABEL { 0, 0 }
91
92const char* FindLabel(int value, const ConstantLabel entries[]);
93std::string ErrorName(int err, const ConstantLabel* err_table);
94
95#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS)
96// Returns a UTF8 description from an OS X Status error.
97std::string DescriptionFromOSStatus(OSStatus err);
98#endif
99
100//////////////////////////////////////////////////////////////////////
101
102// Note that the non-standard LoggingSeverity aliases exist because they are
103// still in broad use. The meanings of the levels are:
104// LS_SENSITIVE: Information which should only be logged with the consent
105// of the user, due to privacy concerns.
106// LS_VERBOSE: This level is for data which we do not want to appear in the
107// normal debug log, but should appear in diagnostic logs.
108// LS_INFO: Chatty level used in debugging for all sorts of things, the default
109// in debug builds.
110// LS_WARNING: Something that may warrant investigation.
111// LS_ERROR: Something that should not have occurred.
112// LS_NONE: Don't log.
113enum LoggingSeverity {
114 LS_SENSITIVE,
115 LS_VERBOSE,
116 LS_INFO,
117 LS_WARNING,
118 LS_ERROR,
119 LS_NONE,
120 INFO = LS_INFO,
121 WARNING = LS_WARNING,
122 LERROR = LS_ERROR
123};
124
125// LogErrorContext assists in interpreting the meaning of an error value.
126enum LogErrorContext {
127 ERRCTX_NONE,
128 ERRCTX_ERRNO, // System-local errno
129 ERRCTX_HRESULT, // Windows HRESULT
130 ERRCTX_OSSTATUS, // MacOS OSStatus
131
132 // Abbreviations for LOG_E macro
133 ERRCTX_EN = ERRCTX_ERRNO, // LOG_E(sev, EN, x)
134 ERRCTX_HR = ERRCTX_HRESULT, // LOG_E(sev, HR, x)
135 ERRCTX_OS = ERRCTX_OSSTATUS, // LOG_E(sev, OS, x)
136};
137
138// Virtual sink interface that can receive log messages.
139class LogSink {
140 public:
141 LogSink() {}
142 virtual ~LogSink() {}
143 virtual void OnLogMessage(const std::string& message) = 0;
144};
145
146class LogMessage {
147 public:
148 LogMessage(const char* file,
149 int line,
150 LoggingSeverity sev,
151 LogErrorContext err_ctx = ERRCTX_NONE,
152 int err = 0,
153 const char* module = nullptr);
154
155 LogMessage(const char* file,
156 int line,
157 LoggingSeverity sev,
158 const std::string& tag);
159
160 ~LogMessage();
161
162 static inline bool Loggable(LoggingSeverity sev) { return (sev >= min_sev_); }
163 std::ostream& stream() { return print_stream_; }
164
165 // Returns the time at which this function was called for the first time.
166 // The time will be used as the logging start time.
167 // If this is not called externally, the LogMessage ctor also calls it, in
168 // which case the logging start time will be the time of the first LogMessage
169 // instance is created.
170 static int64_t LogStartTime();
171
172 // Returns the wall clock equivalent of |LogStartTime|, in seconds from the
173 // epoch.
174 static uint32_t WallClockStartTime();
175
176 // LogThreads: Display the thread identifier of the current thread
177 static void LogThreads(bool on = true);
178
179 // LogTimestamps: Display the elapsed time of the program
180 static void LogTimestamps(bool on = true);
181
182 // These are the available logging channels
183 // Debug: Debug console on Windows, otherwise stderr
184 static void LogToDebug(LoggingSeverity min_sev);
185 static LoggingSeverity GetLogToDebug() { return dbg_sev_; }
186
187 // Sets whether logs will be directed to stderr in debug mode.
188 static void SetLogToStderr(bool log_to_stderr);
189
190 // Stream: Any non-blocking stream interface. LogMessage takes ownership of
191 // the stream. Multiple streams may be specified by using AddLogToStream.
192 // LogToStream is retained for backwards compatibility; when invoked, it
193 // will discard any previously set streams and install the specified stream.
194 // GetLogToStream gets the severity for the specified stream, of if none
195 // is specified, the minimum stream severity.
196 // RemoveLogToStream removes the specified stream, without destroying it.
197 static int GetLogToStream(LogSink* stream = nullptr);
198 static void AddLogToStream(LogSink* stream, LoggingSeverity min_sev);
199 static void RemoveLogToStream(LogSink* stream);
200
201 // Testing against MinLogSeverity allows code to avoid potentially expensive
202 // logging operations by pre-checking the logging level.
203 static int GetMinLogSeverity() { return min_sev_; }
204
205 // Parses the provided parameter stream to configure the options above.
206 // Useful for configuring logging from the command line.
207 static void ConfigureLogging(const char* params);
208
209 private:
210 typedef std::pair<LogSink*, LoggingSeverity> StreamAndSeverity;
211 typedef std::list<StreamAndSeverity> StreamList;
212
213 // Updates min_sev_ appropriately when debug sinks change.
214 static void UpdateMinLogSeverity();
215
216 // These write out the actual log messages.
217 static void OutputToDebug(const std::string& msg,
218 LoggingSeverity severity,
219 const std::string& tag);
220
221 // The ostream that buffers the formatted message before output
222 std::ostringstream print_stream_;
223
224 // The severity level of this message
225 LoggingSeverity severity_;
226
227 // The Android debug output tag.
228 std::string tag_;
229
230 // String data generated in the constructor, that should be appended to
231 // the message before output.
232 std::string extra_;
233
234 // dbg_sev_ is the thresholds for those output targets
235 // min_sev_ is the minimum (most verbose) of those levels, and is used
236 // as a short-circuit in the logging macros to identify messages that won't
237 // be logged.
238 // ctx_sev_ is the minimum level at which file context is displayed
239 static LoggingSeverity min_sev_, dbg_sev_, ctx_sev_;
240
241 // The output streams and their associated severities
242 static StreamList streams_;
243
244 // Flags for formatting options
245 static bool thread_, timestamp_;
246
247 // Determines if logs will be directed to stderr in debug mode.
248 static bool log_to_stderr_;
249
250 RTC_DISALLOW_COPY_AND_ASSIGN(LogMessage);
251};
252
253//////////////////////////////////////////////////////////////////////
254// Logging Helpers
255//////////////////////////////////////////////////////////////////////
256
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200257// The following non-obvious technique for implementation of a
258// conditional log stream was stolen from google3/base/logging.h.
259
260// This class is used to explicitly ignore values in the conditional
261// logging macros. This avoids compiler warnings like "value computed
262// is not used" and "statement has no effect".
263
264class LogMessageVoidify {
265 public:
266 LogMessageVoidify() { }
267 // This has to be an operator with a precedence lower than << but
268 // higher than ?:
269 void operator&(std::ostream&) { }
270};
271
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200272#define RTC_LOG_SEVERITY_PRECONDITION(sev) \
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200273 !(rtc::LogMessage::Loggable(sev)) \
274 ? (void) 0 \
275 : rtc::LogMessageVoidify() &
276
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200277#define RTC_LOG(sev) \
278 RTC_LOG_SEVERITY_PRECONDITION(rtc::sev) \
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200279 rtc::LogMessage(__FILE__, __LINE__, rtc::sev).stream()
280
281// The _V version is for when a variable is passed in. It doesn't do the
282// namespace concatination.
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200283#define RTC_LOG_V(sev) \
284 RTC_LOG_SEVERITY_PRECONDITION(sev) \
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200285 rtc::LogMessage(__FILE__, __LINE__, sev).stream()
286
287// The _F version prefixes the message with the current function name.
288#if (defined(__GNUC__) && !defined(NDEBUG)) || defined(WANT_PRETTY_LOG_F)
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200289#define RTC_LOG_F(sev) RTC_LOG(sev) << __PRETTY_FUNCTION__ << ": "
Patrik Höglundc2962552017-11-17 13:40:22 +0000290#define RTC_LOG_T_F(sev) RTC_LOG(sev) << this << ": " \
291 << __PRETTY_FUNCTION__ << ": "
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200292#else
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200293#define RTC_LOG_F(sev) RTC_LOG(sev) << __FUNCTION__ << ": "
Patrik Höglundc2962552017-11-17 13:40:22 +0000294#define RTC_LOG_T_F(sev) RTC_LOG(sev) << this << ": " << __FUNCTION__ << ": "
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200295#endif
296
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200297#define RTC_LOG_CHECK_LEVEL(sev) \
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200298 rtc::LogCheckLevel(rtc::sev)
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200299#define RTC_LOG_CHECK_LEVEL_V(sev) \
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200300 rtc::LogCheckLevel(sev)
301
302inline bool LogCheckLevel(LoggingSeverity sev) {
303 return (LogMessage::GetMinLogSeverity() <= sev);
304}
305
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200306#define RTC_LOG_E(sev, ctx, err, ...) \
307 RTC_LOG_SEVERITY_PRECONDITION(rtc::sev) \
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200308 rtc::LogMessage(__FILE__, __LINE__, rtc::sev, \
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200309 rtc::ERRCTX_ ## ctx, err , ##__VA_ARGS__) \
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200310 .stream()
311
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200312#define RTC_LOG_T(sev) RTC_LOG(sev) << this << ": "
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200313
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200314#define RTC_LOG_ERRNO_EX(sev, err) \
315 RTC_LOG_E(sev, ERRNO, err)
316#define RTC_LOG_ERRNO(sev) \
317 RTC_LOG_ERRNO_EX(sev, errno)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200318
319#if defined(WEBRTC_WIN)
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200320#define RTC_LOG_GLE_EX(sev, err) \
321 RTC_LOG_E(sev, HRESULT, err)
322#define RTC_LOG_GLE(sev) \
323 RTC_LOG_GLE_EX(sev, GetLastError())
324#define RTC_LOG_GLEM(sev, mod) \
325 RTC_LOG_E(sev, HRESULT, GetLastError(), mod)
326#define RTC_LOG_ERR_EX(sev, err) \
327 RTC_LOG_GLE_EX(sev, err)
328#define RTC_LOG_ERR(sev) \
329 RTC_LOG_GLE(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200330#elif defined(__native_client__) && __native_client__
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200331#define RTC_LOG_ERR_EX(sev, err) \
332 RTC_LOG(sev)
333#define RTC_LOG_ERR(sev) \
334 RTC_LOG(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200335#elif defined(WEBRTC_POSIX)
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200336#define RTC_LOG_ERR_EX(sev, err) \
337 RTC_LOG_ERRNO_EX(sev, err)
338#define RTC_LOG_ERR(sev) \
339 RTC_LOG_ERRNO(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200340#endif // WEBRTC_WIN
341
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200342#define RTC_LOG_TAG(sev, tag) \
343 RTC_LOG_SEVERITY_PRECONDITION(sev) \
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200344 rtc::LogMessage(nullptr, 0, sev, tag).stream()
345
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200346#define RTC_PLOG(sev, err) \
347 RTC_LOG_ERR_EX(sev, err)
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) \
357 (true ? true : ((void)(rtc::sev), true)) \
358 ? static_cast<void>(0) \
359 : rtc::LogMessageVoidify() & \
360 rtc::LogMessage(__FILE__, __LINE__, rtc::sev).stream()
361#define RTC_DLOG(sev) RTC_DLOG_EAT_STREAM_PARAMS(sev)
362#define RTC_DLOG_V(sev) RTC_DLOG_EAT_STREAM_PARAMS(sev)
363#define RTC_DLOG_F(sev) RTC_DLOG_EAT_STREAM_PARAMS(sev)
364#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_