blob: 0865379a4b5bd37ac7f3b065e2f6ff638b58b3f5 [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
Jonas Olsson2b6f1352018-02-15 11:57:03 +0100161 static bool Loggable(LoggingSeverity sev);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200162 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);
Jonas Olsson2b6f1352018-02-15 11:57:03 +0100184 static LoggingSeverity GetLogToDebug();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200185
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.
Jonas Olsson2b6f1352018-02-15 11:57:03 +0100202 static int GetMinLogSeverity();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200203
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
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200233 // The output streams and their associated severities
234 static StreamList streams_;
235
236 // Flags for formatting options
237 static bool thread_, timestamp_;
238
239 // Determines if logs will be directed to stderr in debug mode.
240 static bool log_to_stderr_;
241
242 RTC_DISALLOW_COPY_AND_ASSIGN(LogMessage);
243};
244
245//////////////////////////////////////////////////////////////////////
246// Logging Helpers
247//////////////////////////////////////////////////////////////////////
248
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200249// The following non-obvious technique for implementation of a
250// conditional log stream was stolen from google3/base/logging.h.
251
252// This class is used to explicitly ignore values in the conditional
253// logging macros. This avoids compiler warnings like "value computed
254// is not used" and "statement has no effect".
255
256class LogMessageVoidify {
257 public:
258 LogMessageVoidify() { }
259 // This has to be an operator with a precedence lower than << but
260 // higher than ?:
261 void operator&(std::ostream&) { }
262};
263
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200264#define RTC_LOG_SEVERITY_PRECONDITION(sev) \
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200265 !(rtc::LogMessage::Loggable(sev)) \
266 ? (void) 0 \
267 : rtc::LogMessageVoidify() &
268
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200269#define RTC_LOG(sev) \
270 RTC_LOG_SEVERITY_PRECONDITION(rtc::sev) \
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200271 rtc::LogMessage(__FILE__, __LINE__, rtc::sev).stream()
272
273// The _V version is for when a variable is passed in. It doesn't do the
Jonas Olsson24ea8222018-01-25 10:14:29 +0100274// namespace concatenation.
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200275#define RTC_LOG_V(sev) \
276 RTC_LOG_SEVERITY_PRECONDITION(sev) \
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200277 rtc::LogMessage(__FILE__, __LINE__, sev).stream()
278
279// The _F version prefixes the message with the current function name.
280#if (defined(__GNUC__) && !defined(NDEBUG)) || defined(WANT_PRETTY_LOG_F)
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200281#define RTC_LOG_F(sev) RTC_LOG(sev) << __PRETTY_FUNCTION__ << ": "
Patrik Höglundc2962552017-11-17 13:40:22 +0000282#define RTC_LOG_T_F(sev) RTC_LOG(sev) << this << ": " \
283 << __PRETTY_FUNCTION__ << ": "
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200284#else
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200285#define RTC_LOG_F(sev) RTC_LOG(sev) << __FUNCTION__ << ": "
Patrik Höglundc2962552017-11-17 13:40:22 +0000286#define RTC_LOG_T_F(sev) RTC_LOG(sev) << this << ": " << __FUNCTION__ << ": "
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200287#endif
288
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200289#define RTC_LOG_CHECK_LEVEL(sev) \
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200290 rtc::LogCheckLevel(rtc::sev)
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200291#define RTC_LOG_CHECK_LEVEL_V(sev) \
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200292 rtc::LogCheckLevel(sev)
293
294inline bool LogCheckLevel(LoggingSeverity sev) {
295 return (LogMessage::GetMinLogSeverity() <= sev);
296}
297
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200298#define RTC_LOG_E(sev, ctx, err, ...) \
299 RTC_LOG_SEVERITY_PRECONDITION(rtc::sev) \
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200300 rtc::LogMessage(__FILE__, __LINE__, rtc::sev, \
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200301 rtc::ERRCTX_ ## ctx, err , ##__VA_ARGS__) \
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200302 .stream()
303
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200304#define RTC_LOG_T(sev) RTC_LOG(sev) << this << ": "
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200305
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200306#define RTC_LOG_ERRNO_EX(sev, err) \
307 RTC_LOG_E(sev, ERRNO, err)
308#define RTC_LOG_ERRNO(sev) \
309 RTC_LOG_ERRNO_EX(sev, errno)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200310
311#if defined(WEBRTC_WIN)
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200312#define RTC_LOG_GLE_EX(sev, err) \
313 RTC_LOG_E(sev, HRESULT, err)
314#define RTC_LOG_GLE(sev) \
315 RTC_LOG_GLE_EX(sev, GetLastError())
316#define RTC_LOG_GLEM(sev, mod) \
317 RTC_LOG_E(sev, HRESULT, GetLastError(), mod)
318#define RTC_LOG_ERR_EX(sev, err) \
319 RTC_LOG_GLE_EX(sev, err)
320#define RTC_LOG_ERR(sev) \
321 RTC_LOG_GLE(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200322#elif defined(__native_client__) && __native_client__
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200323#define RTC_LOG_ERR_EX(sev, err) \
324 RTC_LOG(sev)
325#define RTC_LOG_ERR(sev) \
326 RTC_LOG(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200327#elif defined(WEBRTC_POSIX)
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200328#define RTC_LOG_ERR_EX(sev, err) \
329 RTC_LOG_ERRNO_EX(sev, err)
330#define RTC_LOG_ERR(sev) \
331 RTC_LOG_ERRNO(sev)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200332#endif // WEBRTC_WIN
333
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200334#define RTC_LOG_TAG(sev, tag) \
335 RTC_LOG_SEVERITY_PRECONDITION(sev) \
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200336 rtc::LogMessage(nullptr, 0, sev, tag).stream()
337
Fredrik Solenbergb3d7cac2017-11-17 15:22:37 +0100338// The RTC_DLOG macros are equivalent to their RTC_LOG counterparts except that
339// they only generate code in debug builds.
340#if RTC_DLOG_IS_ON
341#define RTC_DLOG(sev) RTC_LOG(sev)
342#define RTC_DLOG_V(sev) RTC_LOG_V(sev)
343#define RTC_DLOG_F(sev) RTC_LOG_F(sev)
344#else
345#define RTC_DLOG_EAT_STREAM_PARAMS(sev) \
Jonas Olsson24ea8222018-01-25 10:14:29 +0100346 (true ? true : ((void)(sev), true)) \
347 ? static_cast<void>(0) \
348 : rtc::LogMessageVoidify() & \
349 rtc::LogMessage(__FILE__, __LINE__, sev).stream()
350#define RTC_DLOG(sev) RTC_DLOG_EAT_STREAM_PARAMS(rtc::sev)
Fredrik Solenbergb3d7cac2017-11-17 15:22:37 +0100351#define RTC_DLOG_V(sev) RTC_DLOG_EAT_STREAM_PARAMS(sev)
Jonas Olsson24ea8222018-01-25 10:14:29 +0100352#define RTC_DLOG_F(sev) RTC_DLOG_EAT_STREAM_PARAMS(rtc::sev)
Fredrik Solenbergb3d7cac2017-11-17 15:22:37 +0100353#endif
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200354
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200355} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000356
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200357#endif // RTC_BASE_LOGGING_H_