blob: 922e701e66b4e1f4043154c53af86127f431cec5 [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
64namespace rtc {
65
66///////////////////////////////////////////////////////////////////////////////
67// ConstantLabel can be used to easily generate string names from constant
68// values. This can be useful for logging descriptive names of error messages.
69// Usage:
70// const ConstantLabel LIBRARY_ERRORS[] = {
71// KLABEL(SOME_ERROR),
72// KLABEL(SOME_OTHER_ERROR),
73// ...
74// LASTLABEL
75// }
76//
77// int err = LibraryFunc();
78// LOG(LS_ERROR) << "LibraryFunc returned: "
79// << ErrorName(err, LIBRARY_ERRORS);
80
81struct ConstantLabel { int value; const char * label; };
82#define KLABEL(x) { x, #x }
83#define TLABEL(x, y) { x, y }
84#define LASTLABEL { 0, 0 }
85
86const char* FindLabel(int value, const ConstantLabel entries[]);
87std::string ErrorName(int err, const ConstantLabel* err_table);
88
89#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS)
90// Returns a UTF8 description from an OS X Status error.
91std::string DescriptionFromOSStatus(OSStatus err);
92#endif
93
94//////////////////////////////////////////////////////////////////////
95
96// Note that the non-standard LoggingSeverity aliases exist because they are
97// still in broad use. The meanings of the levels are:
98// LS_SENSITIVE: Information which should only be logged with the consent
99// of the user, due to privacy concerns.
100// LS_VERBOSE: This level is for data which we do not want to appear in the
101// normal debug log, but should appear in diagnostic logs.
102// LS_INFO: Chatty level used in debugging for all sorts of things, the default
103// in debug builds.
104// LS_WARNING: Something that may warrant investigation.
105// LS_ERROR: Something that should not have occurred.
106// LS_NONE: Don't log.
107enum LoggingSeverity {
108 LS_SENSITIVE,
109 LS_VERBOSE,
110 LS_INFO,
111 LS_WARNING,
112 LS_ERROR,
113 LS_NONE,
114 INFO = LS_INFO,
115 WARNING = LS_WARNING,
116 LERROR = LS_ERROR
117};
118
119// LogErrorContext assists in interpreting the meaning of an error value.
120enum LogErrorContext {
121 ERRCTX_NONE,
122 ERRCTX_ERRNO, // System-local errno
123 ERRCTX_HRESULT, // Windows HRESULT
124 ERRCTX_OSSTATUS, // MacOS OSStatus
125
126 // Abbreviations for LOG_E macro
127 ERRCTX_EN = ERRCTX_ERRNO, // LOG_E(sev, EN, x)
128 ERRCTX_HR = ERRCTX_HRESULT, // LOG_E(sev, HR, x)
129 ERRCTX_OS = ERRCTX_OSSTATUS, // LOG_E(sev, OS, x)
130};
131
132// Virtual sink interface that can receive log messages.
133class LogSink {
134 public:
135 LogSink() {}
136 virtual ~LogSink() {}
137 virtual void OnLogMessage(const std::string& message) = 0;
138};
139
140class LogMessage {
141 public:
142 LogMessage(const char* file,
143 int line,
144 LoggingSeverity sev,
145 LogErrorContext err_ctx = ERRCTX_NONE,
146 int err = 0,
147 const char* module = nullptr);
148
149 LogMessage(const char* file,
150 int line,
151 LoggingSeverity sev,
152 const std::string& tag);
153
154 ~LogMessage();
155
156 static inline bool Loggable(LoggingSeverity sev) { return (sev >= min_sev_); }
157 std::ostream& stream() { return print_stream_; }
158
159 // Returns the time at which this function was called for the first time.
160 // The time will be used as the logging start time.
161 // If this is not called externally, the LogMessage ctor also calls it, in
162 // which case the logging start time will be the time of the first LogMessage
163 // instance is created.
164 static int64_t LogStartTime();
165
166 // Returns the wall clock equivalent of |LogStartTime|, in seconds from the
167 // epoch.
168 static uint32_t WallClockStartTime();
169
170 // LogThreads: Display the thread identifier of the current thread
171 static void LogThreads(bool on = true);
172
173 // LogTimestamps: Display the elapsed time of the program
174 static void LogTimestamps(bool on = true);
175
176 // These are the available logging channels
177 // Debug: Debug console on Windows, otherwise stderr
178 static void LogToDebug(LoggingSeverity min_sev);
179 static LoggingSeverity GetLogToDebug() { return dbg_sev_; }
180
181 // Sets whether logs will be directed to stderr in debug mode.
182 static void SetLogToStderr(bool log_to_stderr);
183
184 // Stream: Any non-blocking stream interface. LogMessage takes ownership of
185 // the stream. Multiple streams may be specified by using AddLogToStream.
186 // LogToStream is retained for backwards compatibility; when invoked, it
187 // will discard any previously set streams and install the specified stream.
188 // GetLogToStream gets the severity for the specified stream, of if none
189 // is specified, the minimum stream severity.
190 // RemoveLogToStream removes the specified stream, without destroying it.
191 static int GetLogToStream(LogSink* stream = nullptr);
192 static void AddLogToStream(LogSink* stream, LoggingSeverity min_sev);
193 static void RemoveLogToStream(LogSink* stream);
194
195 // Testing against MinLogSeverity allows code to avoid potentially expensive
196 // logging operations by pre-checking the logging level.
197 static int GetMinLogSeverity() { return min_sev_; }
198
199 // Parses the provided parameter stream to configure the options above.
200 // Useful for configuring logging from the command line.
201 static void ConfigureLogging(const char* params);
202
203 private:
204 typedef std::pair<LogSink*, LoggingSeverity> StreamAndSeverity;
205 typedef std::list<StreamAndSeverity> StreamList;
206
207 // Updates min_sev_ appropriately when debug sinks change.
208 static void UpdateMinLogSeverity();
209
210 // These write out the actual log messages.
211 static void OutputToDebug(const std::string& msg,
212 LoggingSeverity severity,
213 const std::string& tag);
214
215 // The ostream that buffers the formatted message before output
216 std::ostringstream print_stream_;
217
218 // The severity level of this message
219 LoggingSeverity severity_;
220
221 // The Android debug output tag.
222 std::string tag_;
223
224 // String data generated in the constructor, that should be appended to
225 // the message before output.
226 std::string extra_;
227
228 // dbg_sev_ is the thresholds for those output targets
229 // min_sev_ is the minimum (most verbose) of those levels, and is used
230 // as a short-circuit in the logging macros to identify messages that won't
231 // be logged.
232 // ctx_sev_ is the minimum level at which file context is displayed
233 static LoggingSeverity min_sev_, dbg_sev_, ctx_sev_;
234
235 // The output streams and their associated severities
236 static StreamList streams_;
237
238 // Flags for formatting options
239 static bool thread_, timestamp_;
240
241 // Determines if logs will be directed to stderr in debug mode.
242 static bool log_to_stderr_;
243
244 RTC_DISALLOW_COPY_AND_ASSIGN(LogMessage);
245};
246
247//////////////////////////////////////////////////////////////////////
248// Logging Helpers
249//////////////////////////////////////////////////////////////////////
250
251class LogMultilineState {
252 public:
253 size_t unprintable_count_[2];
254 LogMultilineState() {
255 unprintable_count_[0] = unprintable_count_[1] = 0;
256 }
257};
258
259// When possible, pass optional state variable to track various data across
260// multiple calls to LogMultiline. Otherwise, pass null.
261void LogMultiline(LoggingSeverity level, const char* label, bool input,
262 const void* data, size_t len, bool hex_mode,
263 LogMultilineState* state);
264
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200265// The following non-obvious technique for implementation of a
266// conditional log stream was stolen from google3/base/logging.h.
267
268// This class is used to explicitly ignore values in the conditional
269// logging macros. This avoids compiler warnings like "value computed
270// is not used" and "statement has no effect".
271
272class LogMessageVoidify {
273 public:
274 LogMessageVoidify() { }
275 // This has to be an operator with a precedence lower than << but
276 // higher than ?:
277 void operator&(std::ostream&) { }
278};
279
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200280#define RTC_LOG_SEVERITY_PRECONDITION(sev) \
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200281 !(rtc::LogMessage::Loggable(sev)) \
282 ? (void) 0 \
283 : rtc::LogMessageVoidify() &
284
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200285#define RTC_LOG(sev) \
286 RTC_LOG_SEVERITY_PRECONDITION(rtc::sev) \
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200287 rtc::LogMessage(__FILE__, __LINE__, rtc::sev).stream()
288
289// The _V version is for when a variable is passed in. It doesn't do the
290// namespace concatination.
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200291#define RTC_LOG_V(sev) \
292 RTC_LOG_SEVERITY_PRECONDITION(sev) \
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200293 rtc::LogMessage(__FILE__, __LINE__, sev).stream()
294
295// The _F version prefixes the message with the current function name.
296#if (defined(__GNUC__) && !defined(NDEBUG)) || defined(WANT_PRETTY_LOG_F)
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200297#define RTC_LOG_F(sev) RTC_LOG(sev) << __PRETTY_FUNCTION__ << ": "
298#define RTC_LOG_T_F(sev) RTC_LOG(sev) << this << ": " \
299 << __PRETTY_FUNCTION__ << ": "
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200300#else
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200301#define RTC_LOG_F(sev) RTC_LOG(sev) << __FUNCTION__ << ": "
302#define RTC_LOG_T_F(sev) RTC_LOG(sev) << this << ": " << __FUNCTION__ << ": "
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200303#endif
304
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200305#define RTC_LOG_CHECK_LEVEL(sev) \
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200306 rtc::LogCheckLevel(rtc::sev)
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200307#define RTC_LOG_CHECK_LEVEL_V(sev) \
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200308 rtc::LogCheckLevel(sev)
309
310inline bool LogCheckLevel(LoggingSeverity sev) {
311 return (LogMessage::GetMinLogSeverity() <= sev);
312}
313
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200314#define RTC_LOG_E(sev, ctx, err, ...) \
315 RTC_LOG_SEVERITY_PRECONDITION(rtc::sev) \
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200316 rtc::LogMessage(__FILE__, __LINE__, rtc::sev, \
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200317 rtc::ERRCTX_ ## ctx, err , ##__VA_ARGS__) \
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200318 .stream()
319
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200320#define RTC_LOG_T(sev) RTC_LOG(sev) << this << ": "
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200321
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200322#define RTC_LOG_ERRNO_EX(sev, err) \
323 RTC_LOG_E(sev, ERRNO, err)
324#define RTC_LOG_ERRNO(sev) \
325 RTC_LOG_ERRNO_EX(sev, errno)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200326
327#if defined(WEBRTC_WIN)
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200328#define RTC_LOG_GLE_EX(sev, err) \
329 RTC_LOG_E(sev, HRESULT, err)
330#define RTC_LOG_GLE(sev) \
331 RTC_LOG_GLE_EX(sev, GetLastError())
332#define RTC_LOG_GLEM(sev, mod) \
333 RTC_LOG_E(sev, HRESULT, GetLastError(), mod)
334#define RTC_LOG_ERR_EX(sev, err) \
335 RTC_LOG_GLE_EX(sev, err)
336#define RTC_LOG_ERR(sev) \
337 RTC_LOG_GLE(sev)
338#define RTC_LAST_SYSTEM_ERROR \
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200339 (::GetLastError())
340#elif defined(__native_client__) && __native_client__
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200341#define RTC_LOG_ERR_EX(sev, err) \
342 RTC_LOG(sev)
343#define RTC_LOG_ERR(sev) \
344 RTC_LOG(sev)
345#define RTC_LAST_SYSTEM_ERROR \
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200346 (0)
347#elif defined(WEBRTC_POSIX)
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200348#define RTC_LOG_ERR_EX(sev, err) \
349 RTC_LOG_ERRNO_EX(sev, err)
350#define RTC_LOG_ERR(sev) \
351 RTC_LOG_ERRNO(sev)
352#define RTC_LAST_SYSTEM_ERROR \
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200353 (errno)
354#endif // WEBRTC_WIN
355
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200356#define RTC_LOG_TAG(sev, tag) \
357 RTC_LOG_SEVERITY_PRECONDITION(sev) \
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200358 rtc::LogMessage(nullptr, 0, sev, tag).stream()
359
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200360#define RTC_PLOG(sev, err) \
361 RTC_LOG_ERR_EX(sev, err)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200362
363// TODO(?): Add an "assert" wrapper that logs in the same manner.
364
Mirko Bonadei8ed8e562017-10-27 09:43:53 +0200365// TODO(bugs.webrtc.org/8452): Remove these backwards compatibility wrappers.
366#ifndef LOG
367
368#define LOG(sev) RTC_LOG(sev)
369#define LOG_V(sev) RTC_LOG_V(sev)
370#define LOG_F(sev) RTC_LOG_F(sev)
371#define LOG_T_F(sev) RTC_LOG_T_F(sev)
372#define LOG_CHECK_LEVEL(sev) RTC_LOG_CHECK_LEVEL(sev)
373#define LOG_CHECK_LEVEL_V(sev) RTC_LOG_CHECK_LEVEL_V(sev)
374#define LOG_E(sev, ctx, err, ...) RTC_LOG_E(sev, ctx, err, ##__VA_ARGS__)
375#define LOG_T(sev) RTC_LOG_T(sev)
376#define LOG_ERRNO_EX(sev, err) RTC_LOG_ERRNO_EX(sev, err)
377#define LOG_ERRNO(sev) RTC_LOG_ERRNO(sev)
378
379#define LAST_SYSTEM_ERROR RTC_LAST_SYSTEM_ERROR
380#define LOG_ERR_EX(sev, err) RTC_LOG_ERR_EX(sev, err)
381#define LOG_ERR(sev) RTC_LOG_ERR(sev)
382
383#if defined(WEBRTC_WIN)
384#define LOG_GLE_EX(sev, err) RTC_LOG_GLE_EX(sev, err)
385#define LOG_GLE(sev) RTC_LOG_GLE(sev)
386#define LOG_GLEM(sev, mod) RTC_LOG_GLEM(sev, mod)
387#endif // WEBRTC_WIN
388
389#define LOG_TAG(sev, tag) RTC_LOG_TAG(sev, tag)
390#define PLOG(sev, err) RTC_PLOG(sev, err)
391
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200392#endif // LOG
393
394} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000395
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200396#endif // RTC_BASE_LOGGING_H_