blob: 6445fe1263e793455fc55a6b7610a3b3da59bb2e [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.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000044
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020045#ifndef RTC_BASE_LOGGING_H_
46#define RTC_BASE_LOGGING_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000047
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020048#include <errno.h>
mostynbe38e4f62016-05-12 01:08:20 -070049
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020050#include <list>
51#include <sstream>
52#include <string>
53#include <utility>
54
55#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS)
56#include <CoreServices/CoreServices.h>
57#endif
58
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020059#include "rtc_base/basictypes.h"
60#include "rtc_base/constructormagic.h"
61#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
71///////////////////////////////////////////////////////////////////////////////
72// ConstantLabel can be used to easily generate string names from constant
73// values. This can be useful for logging descriptive names of error messages.
74// Usage:
75// const ConstantLabel LIBRARY_ERRORS[] = {
76// KLABEL(SOME_ERROR),
77// KLABEL(SOME_OTHER_ERROR),
78// ...
79// LASTLABEL
80// }
81//
82// int err = LibraryFunc();
83// LOG(LS_ERROR) << "LibraryFunc returned: "
84// << ErrorName(err, LIBRARY_ERRORS);
85
86struct ConstantLabel { int value; const char * label; };
87#define KLABEL(x) { x, #x }
88#define TLABEL(x, y) { x, y }
89#define LASTLABEL { 0, 0 }
90
91const char* FindLabel(int value, const ConstantLabel entries[]);
92std::string ErrorName(int err, const ConstantLabel* err_table);
93
94#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS)
95// Returns a UTF8 description from an OS X Status error.
96std::string DescriptionFromOSStatus(OSStatus err);
97#endif
98
99//////////////////////////////////////////////////////////////////////
100
101// Note that the non-standard LoggingSeverity aliases exist because they are
102// still in broad use. The meanings of the levels are:
103// LS_SENSITIVE: Information which should only be logged with the consent
104// of the user, due to privacy concerns.
105// LS_VERBOSE: This level is for data which we do not want to appear in the
106// normal debug log, but should appear in diagnostic logs.
107// LS_INFO: Chatty level used in debugging for all sorts of things, the default
108// in debug builds.
109// LS_WARNING: Something that may warrant investigation.
110// LS_ERROR: Something that should not have occurred.
111// LS_NONE: Don't log.
112enum LoggingSeverity {
113 LS_SENSITIVE,
114 LS_VERBOSE,
115 LS_INFO,
116 LS_WARNING,
117 LS_ERROR,
118 LS_NONE,
119 INFO = LS_INFO,
120 WARNING = LS_WARNING,
121 LERROR = LS_ERROR
122};
123
124// LogErrorContext assists in interpreting the meaning of an error value.
125enum LogErrorContext {
126 ERRCTX_NONE,
127 ERRCTX_ERRNO, // System-local errno
128 ERRCTX_HRESULT, // Windows HRESULT
129 ERRCTX_OSSTATUS, // MacOS OSStatus
130
131 // Abbreviations for LOG_E macro
132 ERRCTX_EN = ERRCTX_ERRNO, // LOG_E(sev, EN, x)
133 ERRCTX_HR = ERRCTX_HRESULT, // LOG_E(sev, HR, x)
134 ERRCTX_OS = ERRCTX_OSSTATUS, // LOG_E(sev, OS, x)
135};
136
137// Virtual sink interface that can receive log messages.
138class LogSink {
139 public:
140 LogSink() {}
141 virtual ~LogSink() {}
142 virtual void OnLogMessage(const std::string& message) = 0;
143};
144
145class LogMessage {
146 public:
147 LogMessage(const char* file,
148 int line,
149 LoggingSeverity sev,
150 LogErrorContext err_ctx = ERRCTX_NONE,
151 int err = 0,
152 const char* module = nullptr);
153
154 LogMessage(const char* file,
155 int line,
156 LoggingSeverity sev,
157 const std::string& tag);
158
159 ~LogMessage();
160
161 static inline bool Loggable(LoggingSeverity sev) { return (sev >= min_sev_); }
162 std::ostream& stream() { return print_stream_; }
163
164 // Returns the time at which this function was called for the first time.
165 // The time will be used as the logging start time.
166 // If this is not called externally, the LogMessage ctor also calls it, in
167 // which case the logging start time will be the time of the first LogMessage
168 // instance is created.
169 static int64_t LogStartTime();
170
171 // Returns the wall clock equivalent of |LogStartTime|, in seconds from the
172 // epoch.
173 static uint32_t WallClockStartTime();
174
175 // LogThreads: Display the thread identifier of the current thread
176 static void LogThreads(bool on = true);
177
178 // LogTimestamps: Display the elapsed time of the program
179 static void LogTimestamps(bool on = true);
180
181 // These are the available logging channels
182 // Debug: Debug console on Windows, otherwise stderr
183 static void LogToDebug(LoggingSeverity min_sev);
184 static LoggingSeverity GetLogToDebug() { return dbg_sev_; }
185
186 // Sets whether logs will be directed to stderr in debug mode.
187 static void SetLogToStderr(bool log_to_stderr);
188
189 // Stream: Any non-blocking stream interface. LogMessage takes ownership of
190 // the stream. Multiple streams may be specified by using AddLogToStream.
191 // LogToStream is retained for backwards compatibility; when invoked, it
192 // will discard any previously set streams and install the specified stream.
193 // GetLogToStream gets the severity for the specified stream, of if none
194 // is specified, the minimum stream severity.
195 // RemoveLogToStream removes the specified stream, without destroying it.
196 static int GetLogToStream(LogSink* stream = nullptr);
197 static void AddLogToStream(LogSink* stream, LoggingSeverity min_sev);
198 static void RemoveLogToStream(LogSink* stream);
199
200 // Testing against MinLogSeverity allows code to avoid potentially expensive
201 // logging operations by pre-checking the logging level.
202 static int GetMinLogSeverity() { return min_sev_; }
203
204 // Parses the provided parameter stream to configure the options above.
205 // Useful for configuring logging from the command line.
206 static void ConfigureLogging(const char* params);
207
208 private:
209 typedef std::pair<LogSink*, LoggingSeverity> StreamAndSeverity;
210 typedef std::list<StreamAndSeverity> StreamList;
211
212 // Updates min_sev_ appropriately when debug sinks change.
213 static void UpdateMinLogSeverity();
214
215 // These write out the actual log messages.
216 static void OutputToDebug(const std::string& msg,
217 LoggingSeverity severity,
218 const std::string& tag);
219
220 // The ostream that buffers the formatted message before output
221 std::ostringstream print_stream_;
222
223 // The severity level of this message
224 LoggingSeverity severity_;
225
226 // The Android debug output tag.
227 std::string tag_;
228
229 // String data generated in the constructor, that should be appended to
230 // the message before output.
231 std::string extra_;
232
233 // dbg_sev_ is the thresholds for those output targets
234 // min_sev_ is the minimum (most verbose) of those levels, and is used
235 // as a short-circuit in the logging macros to identify messages that won't
236 // be logged.
237 // ctx_sev_ is the minimum level at which file context is displayed
238 static LoggingSeverity min_sev_, dbg_sev_, ctx_sev_;
239
240 // The output streams and their associated severities
241 static StreamList streams_;
242
243 // Flags for formatting options
244 static bool thread_, timestamp_;
245
246 // Determines if logs will be directed to stderr in debug mode.
247 static bool log_to_stderr_;
248
249 RTC_DISALLOW_COPY_AND_ASSIGN(LogMessage);
250};
251
252//////////////////////////////////////////////////////////////////////
253// Logging Helpers
254//////////////////////////////////////////////////////////////////////
255
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200256// The following non-obvious technique for implementation of a
257// conditional log stream was stolen from google3/base/logging.h.
258
259// This class is used to explicitly ignore values in the conditional
260// logging macros. This avoids compiler warnings like "value computed
261// is not used" and "statement has no effect".
262
263class LogMessageVoidify {
264 public:
265 LogMessageVoidify() { }
266 // This has to be an operator with a precedence lower than << but
267 // higher than ?:
268 void operator&(std::ostream&) { }
269};
270
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200271#define RTC_LOG_SEVERITY_PRECONDITION(sev) \
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200272 !(rtc::LogMessage::Loggable(sev)) \
273 ? (void) 0 \
274 : rtc::LogMessageVoidify() &
275
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200276#define RTC_LOG(sev) \
277 RTC_LOG_SEVERITY_PRECONDITION(rtc::sev) \
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200278 rtc::LogMessage(__FILE__, __LINE__, rtc::sev).stream()
279
280// The _V version is for when a variable is passed in. It doesn't do the
Jonas Olsson24ea8222018-01-25 10:14:29 +0100281// namespace concatenation.
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200282#define RTC_LOG_V(sev) \
283 RTC_LOG_SEVERITY_PRECONDITION(sev) \
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200284 rtc::LogMessage(__FILE__, __LINE__, sev).stream()
285
286// The _F version prefixes the message with the current function name.
287#if (defined(__GNUC__) && !defined(NDEBUG)) || defined(WANT_PRETTY_LOG_F)
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200288#define RTC_LOG_F(sev) RTC_LOG(sev) << __PRETTY_FUNCTION__ << ": "
Patrik Höglundc2962552017-11-17 13:40:22 +0000289#define RTC_LOG_T_F(sev) RTC_LOG(sev) << this << ": " \
290 << __PRETTY_FUNCTION__ << ": "
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200291#else
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200292#define RTC_LOG_F(sev) RTC_LOG(sev) << __FUNCTION__ << ": "
Patrik Höglundc2962552017-11-17 13:40:22 +0000293#define RTC_LOG_T_F(sev) RTC_LOG(sev) << this << ": " << __FUNCTION__ << ": "
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200294#endif
295
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200296#define RTC_LOG_CHECK_LEVEL(sev) \
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200297 rtc::LogCheckLevel(rtc::sev)
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200298#define RTC_LOG_CHECK_LEVEL_V(sev) \
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200299 rtc::LogCheckLevel(sev)
300
301inline bool LogCheckLevel(LoggingSeverity sev) {
302 return (LogMessage::GetMinLogSeverity() <= sev);
303}
304
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200305#define RTC_LOG_E(sev, ctx, err, ...) \
306 RTC_LOG_SEVERITY_PRECONDITION(rtc::sev) \
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200307 rtc::LogMessage(__FILE__, __LINE__, rtc::sev, \
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200308 rtc::ERRCTX_ ## ctx, err , ##__VA_ARGS__) \
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200309 .stream()
310
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200311#define RTC_LOG_T(sev) RTC_LOG(sev) << this << ": "
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200312
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200313#define RTC_LOG_ERRNO_EX(sev, err) \
314 RTC_LOG_E(sev, ERRNO, err)
315#define RTC_LOG_ERRNO(sev) \
316 RTC_LOG_ERRNO_EX(sev, errno)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200317
318#if defined(WEBRTC_WIN)
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200319#define RTC_LOG_GLE_EX(sev, err) \
320 RTC_LOG_E(sev, HRESULT, err)
321#define RTC_LOG_GLE(sev) \
322 RTC_LOG_GLE_EX(sev, GetLastError())
323#define RTC_LOG_GLEM(sev, mod) \
324 RTC_LOG_E(sev, HRESULT, GetLastError(), mod)
325#define RTC_LOG_ERR_EX(sev, err) \
326 RTC_LOG_GLE_EX(sev, err)
327#define RTC_LOG_ERR(sev) \
328 RTC_LOG_GLE(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200329#elif defined(__native_client__) && __native_client__
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200330#define RTC_LOG_ERR_EX(sev, err) \
331 RTC_LOG(sev)
332#define RTC_LOG_ERR(sev) \
333 RTC_LOG(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200334#elif defined(WEBRTC_POSIX)
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200335#define RTC_LOG_ERR_EX(sev, err) \
336 RTC_LOG_ERRNO_EX(sev, err)
337#define RTC_LOG_ERR(sev) \
338 RTC_LOG_ERRNO(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200339#endif // WEBRTC_WIN
340
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200341#define RTC_LOG_TAG(sev, tag) \
342 RTC_LOG_SEVERITY_PRECONDITION(sev) \
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200343 rtc::LogMessage(nullptr, 0, sev, tag).stream()
344
Fredrik Solenbergb3d7cac2017-11-17 15:22:37 +0100345// The RTC_DLOG macros are equivalent to their RTC_LOG counterparts except that
346// they only generate code in debug builds.
347#if RTC_DLOG_IS_ON
348#define RTC_DLOG(sev) RTC_LOG(sev)
349#define RTC_DLOG_V(sev) RTC_LOG_V(sev)
350#define RTC_DLOG_F(sev) RTC_LOG_F(sev)
351#else
352#define RTC_DLOG_EAT_STREAM_PARAMS(sev) \
Jonas Olsson24ea8222018-01-25 10:14:29 +0100353 (true ? true : ((void)(sev), true)) \
354 ? static_cast<void>(0) \
355 : rtc::LogMessageVoidify() & \
356 rtc::LogMessage(__FILE__, __LINE__, sev).stream()
357#define RTC_DLOG(sev) RTC_DLOG_EAT_STREAM_PARAMS(rtc::sev)
Fredrik Solenbergb3d7cac2017-11-17 15:22:37 +0100358#define RTC_DLOG_V(sev) RTC_DLOG_EAT_STREAM_PARAMS(sev)
Jonas Olsson24ea8222018-01-25 10:14:29 +0100359#define RTC_DLOG_F(sev) RTC_DLOG_EAT_STREAM_PARAMS(rtc::sev)
Fredrik Solenbergb3d7cac2017-11-17 15:22:37 +0100360#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200361
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200362} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000363
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200364#endif // RTC_BASE_LOGGING_H_