henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 8ed8e56 | 2017-10-27 09:43:53 +0200 | [diff] [blame] | 11 | // RTC_LOG(...) an ostream target that can be used to send formatted |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 12 | // output to a variety of logging targets, such as debugger console, stderr, |
Tommi | 0eefb4d | 2015-05-23 09:54:07 +0200 | [diff] [blame] | 13 | // or any LogSink. |
Mirko Bonadei | 8ed8e56 | 2017-10-27 09:43:53 +0200 | [diff] [blame] | 14 | // The severity level passed as the first argument to the logging |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 15 | // functions is used as a filter, to limit the verbosity of the logging. |
Mirko Bonadei | 8ed8e56 | 2017-10-27 09:43:53 +0200 | [diff] [blame] | 16 | // Static members of LogMessage documented below are used to control the |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 17 | // verbosity and target of the output. |
Mirko Bonadei | 8ed8e56 | 2017-10-27 09:43:53 +0200 | [diff] [blame] | 18 | // There are several variations on the RTC_LOG macro which facilitate logging |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 19 | // of common error conditions, detailed below. |
| 20 | |
Mirko Bonadei | 8ed8e56 | 2017-10-27 09:43:53 +0200 | [diff] [blame] | 21 | // RTC_LOG(sev) logs the given stream at severity "sev", which must be a |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 22 | // compile-time constant of the LoggingSeverity type, without the namespace |
| 23 | // prefix. |
Mirko Bonadei | 8ed8e56 | 2017-10-27 09:43:53 +0200 | [diff] [blame] | 24 | // 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. |
Tommi | e51a0a8 | 2018-02-27 15:30:29 +0100 | [diff] [blame] | 29 | // RTC_LOG_GLE(sev [, mod]) attempt to add a string description of the |
| 30 | // HRESULT returned by GetLastError. |
Mirko Bonadei | 8ed8e56 | 2017-10-27 09:43:53 +0200 | [diff] [blame] | 31 | // RTC_LOG_ERRNO(sev) attempts to add a string description of an errno-derived |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 32 | // error. errno and associated facilities exist on both Windows and POSIX, |
| 33 | // but on Windows they only apply to the C/C++ runtime. |
Mirko Bonadei | 8ed8e56 | 2017-10-27 09:43:53 +0200 | [diff] [blame] | 34 | // 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.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 36 | // (The above three also all have _EX versions that let you specify the error |
| 37 | // code, rather than using the last one.) |
Mirko Bonadei | 8ed8e56 | 2017-10-27 09:43:53 +0200 | [diff] [blame] | 38 | // RTC_LOG_E(sev, ctx, err, ...) logs a detailed error interpreted using the |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 39 | // specified context. |
Mirko Bonadei | 8ed8e56 | 2017-10-27 09:43:53 +0200 | [diff] [blame] | 40 | // 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.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 43 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 44 | #ifndef RTC_BASE_LOGGING_H_ |
| 45 | #define RTC_BASE_LOGGING_H_ |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 46 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 47 | #include <errno.h> |
mostynb | e38e4f6 | 2016-05-12 01:08:20 -0700 | [diff] [blame] | 48 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 49 | #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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 58 | #include "rtc_base/basictypes.h" |
| 59 | #include "rtc_base/constructormagic.h" |
Tommi | e51a0a8 | 2018-02-27 15:30:29 +0100 | [diff] [blame] | 60 | #include "rtc_base/deprecation.h" |
Karl Wiberg | ee10ea8 | 2018-05-04 13:27:48 +0200 | [diff] [blame] | 61 | #include "rtc_base/system/no_inline.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 62 | #include "rtc_base/thread_annotations.h" |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 63 | |
Fredrik Solenberg | b3d7cac | 2017-11-17 15:22:37 +0100 | [diff] [blame] | 64 | #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 Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 70 | namespace rtc { |
| 71 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 72 | #if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) |
| 73 | // Returns a UTF8 description from an OS X Status error. |
| 74 | std::string DescriptionFromOSStatus(OSStatus err); |
| 75 | #endif |
| 76 | |
| 77 | ////////////////////////////////////////////////////////////////////// |
| 78 | |
| 79 | // Note that the non-standard LoggingSeverity aliases exist because they are |
| 80 | // still in broad use. The meanings of the levels are: |
| 81 | // LS_SENSITIVE: Information which should only be logged with the consent |
| 82 | // of the user, due to privacy concerns. |
| 83 | // LS_VERBOSE: This level is for data which we do not want to appear in the |
| 84 | // normal debug log, but should appear in diagnostic logs. |
| 85 | // LS_INFO: Chatty level used in debugging for all sorts of things, the default |
| 86 | // in debug builds. |
| 87 | // LS_WARNING: Something that may warrant investigation. |
| 88 | // LS_ERROR: Something that should not have occurred. |
| 89 | // LS_NONE: Don't log. |
| 90 | enum LoggingSeverity { |
| 91 | LS_SENSITIVE, |
| 92 | LS_VERBOSE, |
| 93 | LS_INFO, |
| 94 | LS_WARNING, |
| 95 | LS_ERROR, |
| 96 | LS_NONE, |
| 97 | INFO = LS_INFO, |
| 98 | WARNING = LS_WARNING, |
| 99 | LERROR = LS_ERROR |
| 100 | }; |
| 101 | |
| 102 | // LogErrorContext assists in interpreting the meaning of an error value. |
| 103 | enum LogErrorContext { |
| 104 | ERRCTX_NONE, |
| 105 | ERRCTX_ERRNO, // System-local errno |
| 106 | ERRCTX_HRESULT, // Windows HRESULT |
| 107 | ERRCTX_OSSTATUS, // MacOS OSStatus |
| 108 | |
| 109 | // Abbreviations for LOG_E macro |
| 110 | ERRCTX_EN = ERRCTX_ERRNO, // LOG_E(sev, EN, x) |
| 111 | ERRCTX_HR = ERRCTX_HRESULT, // LOG_E(sev, HR, x) |
| 112 | ERRCTX_OS = ERRCTX_OSSTATUS, // LOG_E(sev, OS, x) |
| 113 | }; |
| 114 | |
| 115 | // Virtual sink interface that can receive log messages. |
| 116 | class LogSink { |
| 117 | public: |
| 118 | LogSink() {} |
| 119 | virtual ~LogSink() {} |
| 120 | virtual void OnLogMessage(const std::string& message) = 0; |
| 121 | }; |
| 122 | |
| 123 | class LogMessage { |
| 124 | public: |
Karl Wiberg | ab4f1c1 | 2018-05-04 10:42:28 +0200 | [diff] [blame] | 125 | LogMessage(const char* file, int line, LoggingSeverity sev); |
Karl Wiberg | 1ffb374 | 2018-05-04 15:04:48 +0200 | [diff] [blame] | 126 | |
| 127 | // Same as the above, but using a compile-time constant for the logging |
| 128 | // severity. This saves space at the call site, since passing an empty struct |
| 129 | // is generally the same as not passing an argument at all. |
| 130 | template <LoggingSeverity S> |
| 131 | RTC_NO_INLINE LogMessage(const char* file, |
| 132 | int line, |
| 133 | std::integral_constant<LoggingSeverity, S>) |
| 134 | : LogMessage(file, line, S) {} |
| 135 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 136 | LogMessage(const char* file, |
| 137 | int line, |
| 138 | LoggingSeverity sev, |
Karl Wiberg | ab4f1c1 | 2018-05-04 10:42:28 +0200 | [diff] [blame] | 139 | LogErrorContext err_ctx, |
| 140 | int err); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 141 | |
Tommi | e51a0a8 | 2018-02-27 15:30:29 +0100 | [diff] [blame] | 142 | #if defined(WEBRTC_ANDROID) |
| 143 | LogMessage(const char* file, int line, LoggingSeverity sev, const char* tag); |
| 144 | #endif |
| 145 | |
| 146 | // DEPRECATED - DO NOT USE - PLEASE USE THE MACROS INSTEAD OF THE CLASS. |
| 147 | // Android code should use the 'const char*' version since tags are static |
| 148 | // and we want to avoid allocating a std::string copy per log line. |
| 149 | RTC_DEPRECATED |
| 150 | LogMessage(const char* file, int line, LoggingSeverity sev, |
Philip Eliasson | 278aa42 | 2018-02-26 14:54:45 +0000 | [diff] [blame] | 151 | const std::string& tag); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 152 | |
| 153 | ~LogMessage(); |
| 154 | |
Jonas Olsson | 2b6f135 | 2018-02-15 11:57:03 +0100 | [diff] [blame] | 155 | static bool Loggable(LoggingSeverity sev); |
Karl Wiberg | ee10ea8 | 2018-05-04 13:27:48 +0200 | [diff] [blame] | 156 | |
| 157 | // Same as the above, but using a template argument instead of a function |
| 158 | // argument. (When the logging severity is statically known, passing it as a |
| 159 | // template argument instead of as a function argument saves space at the |
| 160 | // call site.) |
| 161 | template <LoggingSeverity S> |
| 162 | RTC_NO_INLINE static bool Loggable() { |
| 163 | return Loggable(S); |
| 164 | } |
| 165 | |
Tommi | fef0500 | 2018-02-27 13:51:08 +0100 | [diff] [blame] | 166 | std::ostream& stream(); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 167 | |
| 168 | // Returns the time at which this function was called for the first time. |
| 169 | // The time will be used as the logging start time. |
| 170 | // If this is not called externally, the LogMessage ctor also calls it, in |
| 171 | // which case the logging start time will be the time of the first LogMessage |
| 172 | // instance is created. |
| 173 | static int64_t LogStartTime(); |
| 174 | |
| 175 | // Returns the wall clock equivalent of |LogStartTime|, in seconds from the |
| 176 | // epoch. |
| 177 | static uint32_t WallClockStartTime(); |
| 178 | |
| 179 | // LogThreads: Display the thread identifier of the current thread |
| 180 | static void LogThreads(bool on = true); |
| 181 | |
| 182 | // LogTimestamps: Display the elapsed time of the program |
| 183 | static void LogTimestamps(bool on = true); |
| 184 | |
| 185 | // These are the available logging channels |
| 186 | // Debug: Debug console on Windows, otherwise stderr |
| 187 | static void LogToDebug(LoggingSeverity min_sev); |
Jonas Olsson | 2b6f135 | 2018-02-15 11:57:03 +0100 | [diff] [blame] | 188 | static LoggingSeverity GetLogToDebug(); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 189 | |
| 190 | // Sets whether logs will be directed to stderr in debug mode. |
| 191 | static void SetLogToStderr(bool log_to_stderr); |
| 192 | |
| 193 | // Stream: Any non-blocking stream interface. LogMessage takes ownership of |
| 194 | // the stream. Multiple streams may be specified by using AddLogToStream. |
| 195 | // LogToStream is retained for backwards compatibility; when invoked, it |
| 196 | // will discard any previously set streams and install the specified stream. |
| 197 | // GetLogToStream gets the severity for the specified stream, of if none |
| 198 | // is specified, the minimum stream severity. |
| 199 | // RemoveLogToStream removes the specified stream, without destroying it. |
| 200 | static int GetLogToStream(LogSink* stream = nullptr); |
| 201 | static void AddLogToStream(LogSink* stream, LoggingSeverity min_sev); |
| 202 | static void RemoveLogToStream(LogSink* stream); |
| 203 | |
| 204 | // Testing against MinLogSeverity allows code to avoid potentially expensive |
| 205 | // logging operations by pre-checking the logging level. |
Jonas Olsson | 2b6f135 | 2018-02-15 11:57:03 +0100 | [diff] [blame] | 206 | static int GetMinLogSeverity(); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 207 | |
| 208 | // Parses the provided parameter stream to configure the options above. |
| 209 | // Useful for configuring logging from the command line. |
| 210 | static void ConfigureLogging(const char* params); |
| 211 | |
| 212 | private: |
Tommi | fef0500 | 2018-02-27 13:51:08 +0100 | [diff] [blame] | 213 | friend class LogMessageForTesting; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 214 | typedef std::pair<LogSink*, LoggingSeverity> StreamAndSeverity; |
| 215 | typedef std::list<StreamAndSeverity> StreamList; |
| 216 | |
| 217 | // Updates min_sev_ appropriately when debug sinks change. |
| 218 | static void UpdateMinLogSeverity(); |
| 219 | |
| 220 | // These write out the actual log messages. |
Tommi | e51a0a8 | 2018-02-27 15:30:29 +0100 | [diff] [blame] | 221 | #if defined(WEBRTC_ANDROID) |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 222 | static void OutputToDebug(const std::string& msg, |
| 223 | LoggingSeverity severity, |
Tommi | e51a0a8 | 2018-02-27 15:30:29 +0100 | [diff] [blame] | 224 | const char* tag); |
| 225 | #else |
| 226 | static void OutputToDebug(const std::string& msg, LoggingSeverity severity); |
| 227 | #endif |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 228 | |
Tommi | fef0500 | 2018-02-27 13:51:08 +0100 | [diff] [blame] | 229 | // Checks the current global debug severity and if the |streams_| collection |
| 230 | // is empty. If |severity| is smaller than the global severity and if the |
| 231 | // |streams_| collection is empty, the LogMessage will be considered a noop |
| 232 | // LogMessage. |
| 233 | static bool IsNoop(LoggingSeverity severity); |
| 234 | |
| 235 | // Called from the dtor (or from a test) to append optional extra error |
| 236 | // information to the log stream and a newline character. |
| 237 | void FinishPrintStream(); |
| 238 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 239 | // The ostream that buffers the formatted message before output |
| 240 | std::ostringstream print_stream_; |
| 241 | |
| 242 | // The severity level of this message |
| 243 | LoggingSeverity severity_; |
| 244 | |
Tommi | e51a0a8 | 2018-02-27 15:30:29 +0100 | [diff] [blame] | 245 | #if defined(WEBRTC_ANDROID) |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 246 | // The Android debug output tag. |
Tommi | e51a0a8 | 2018-02-27 15:30:29 +0100 | [diff] [blame] | 247 | const char* tag_ = "libjingle"; |
| 248 | #endif |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 249 | |
| 250 | // String data generated in the constructor, that should be appended to |
| 251 | // the message before output. |
| 252 | std::string extra_; |
| 253 | |
Tommi | fef0500 | 2018-02-27 13:51:08 +0100 | [diff] [blame] | 254 | const bool is_noop_; |
| 255 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 256 | // The output streams and their associated severities |
| 257 | static StreamList streams_; |
| 258 | |
| 259 | // Flags for formatting options |
| 260 | static bool thread_, timestamp_; |
| 261 | |
| 262 | // Determines if logs will be directed to stderr in debug mode. |
| 263 | static bool log_to_stderr_; |
| 264 | |
| 265 | RTC_DISALLOW_COPY_AND_ASSIGN(LogMessage); |
| 266 | }; |
| 267 | |
| 268 | ////////////////////////////////////////////////////////////////////// |
| 269 | // Logging Helpers |
| 270 | ////////////////////////////////////////////////////////////////////// |
| 271 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 272 | // The following non-obvious technique for implementation of a |
| 273 | // conditional log stream was stolen from google3/base/logging.h. |
| 274 | |
| 275 | // This class is used to explicitly ignore values in the conditional |
| 276 | // logging macros. This avoids compiler warnings like "value computed |
| 277 | // is not used" and "statement has no effect". |
| 278 | |
| 279 | class LogMessageVoidify { |
| 280 | public: |
| 281 | LogMessageVoidify() { } |
| 282 | // This has to be an operator with a precedence lower than << but |
| 283 | // higher than ?: |
| 284 | void operator&(std::ostream&) { } |
| 285 | }; |
| 286 | |
Mirko Bonadei | 8ed8e56 | 2017-10-27 09:43:53 +0200 | [diff] [blame] | 287 | #define RTC_LOG_SEVERITY_PRECONDITION(sev) \ |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 288 | !(rtc::LogMessage::Loggable(sev)) \ |
| 289 | ? (void) 0 \ |
| 290 | : rtc::LogMessageVoidify() & |
| 291 | |
Karl Wiberg | ee10ea8 | 2018-05-04 13:27:48 +0200 | [diff] [blame] | 292 | #define RTC_LOG_SEVERITY_PRECONDITION_C(sev) \ |
| 293 | !(rtc::LogMessage::Loggable<rtc::sev>()) ? (void)0 : rtc::LogMessageVoidify()& |
Karl Wiberg | 1ffb374 | 2018-05-04 15:04:48 +0200 | [diff] [blame] | 294 | #define RTC_LOG(sev) \ |
| 295 | RTC_LOG_SEVERITY_PRECONDITION_C(sev) \ |
| 296 | rtc::LogMessage(__FILE__, __LINE__, \ |
| 297 | std::integral_constant<rtc::LoggingSeverity, rtc::sev>()) \ |
| 298 | .stream() |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 299 | |
| 300 | // The _V version is for when a variable is passed in. It doesn't do the |
Jonas Olsson | 24ea822 | 2018-01-25 10:14:29 +0100 | [diff] [blame] | 301 | // namespace concatenation. |
Mirko Bonadei | 8ed8e56 | 2017-10-27 09:43:53 +0200 | [diff] [blame] | 302 | #define RTC_LOG_V(sev) \ |
| 303 | RTC_LOG_SEVERITY_PRECONDITION(sev) \ |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 304 | rtc::LogMessage(__FILE__, __LINE__, sev).stream() |
| 305 | |
| 306 | // The _F version prefixes the message with the current function name. |
| 307 | #if (defined(__GNUC__) && !defined(NDEBUG)) || defined(WANT_PRETTY_LOG_F) |
Mirko Bonadei | 8ed8e56 | 2017-10-27 09:43:53 +0200 | [diff] [blame] | 308 | #define RTC_LOG_F(sev) RTC_LOG(sev) << __PRETTY_FUNCTION__ << ": " |
Patrik Höglund | c296255 | 2017-11-17 13:40:22 +0000 | [diff] [blame] | 309 | #define RTC_LOG_T_F(sev) RTC_LOG(sev) << this << ": " \ |
| 310 | << __PRETTY_FUNCTION__ << ": " |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 311 | #else |
Mirko Bonadei | 8ed8e56 | 2017-10-27 09:43:53 +0200 | [diff] [blame] | 312 | #define RTC_LOG_F(sev) RTC_LOG(sev) << __FUNCTION__ << ": " |
Patrik Höglund | c296255 | 2017-11-17 13:40:22 +0000 | [diff] [blame] | 313 | #define RTC_LOG_T_F(sev) RTC_LOG(sev) << this << ": " << __FUNCTION__ << ": " |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 314 | #endif |
| 315 | |
Mirko Bonadei | 8ed8e56 | 2017-10-27 09:43:53 +0200 | [diff] [blame] | 316 | #define RTC_LOG_CHECK_LEVEL(sev) \ |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 317 | rtc::LogCheckLevel(rtc::sev) |
Mirko Bonadei | 8ed8e56 | 2017-10-27 09:43:53 +0200 | [diff] [blame] | 318 | #define RTC_LOG_CHECK_LEVEL_V(sev) \ |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 319 | rtc::LogCheckLevel(sev) |
| 320 | |
| 321 | inline bool LogCheckLevel(LoggingSeverity sev) { |
| 322 | return (LogMessage::GetMinLogSeverity() <= sev); |
| 323 | } |
| 324 | |
Karl Wiberg | ee10ea8 | 2018-05-04 13:27:48 +0200 | [diff] [blame] | 325 | #define RTC_LOG_E(sev, ctx, err, ...) \ |
| 326 | RTC_LOG_SEVERITY_PRECONDITION_C(sev) \ |
| 327 | rtc::LogMessage(__FILE__, __LINE__, rtc::sev, rtc::ERRCTX_##ctx, err, \ |
| 328 | ##__VA_ARGS__) \ |
| 329 | .stream() |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 330 | |
Mirko Bonadei | 8ed8e56 | 2017-10-27 09:43:53 +0200 | [diff] [blame] | 331 | #define RTC_LOG_T(sev) RTC_LOG(sev) << this << ": " |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 332 | |
Mirko Bonadei | 8ed8e56 | 2017-10-27 09:43:53 +0200 | [diff] [blame] | 333 | #define RTC_LOG_ERRNO_EX(sev, err) \ |
| 334 | RTC_LOG_E(sev, ERRNO, err) |
| 335 | #define RTC_LOG_ERRNO(sev) \ |
| 336 | RTC_LOG_ERRNO_EX(sev, errno) |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 337 | |
| 338 | #if defined(WEBRTC_WIN) |
Mirko Bonadei | 8ed8e56 | 2017-10-27 09:43:53 +0200 | [diff] [blame] | 339 | #define RTC_LOG_GLE_EX(sev, err) \ |
| 340 | RTC_LOG_E(sev, HRESULT, err) |
| 341 | #define RTC_LOG_GLE(sev) \ |
| 342 | RTC_LOG_GLE_EX(sev, GetLastError()) |
Mirko Bonadei | 8ed8e56 | 2017-10-27 09:43:53 +0200 | [diff] [blame] | 343 | #define RTC_LOG_ERR_EX(sev, err) \ |
| 344 | RTC_LOG_GLE_EX(sev, err) |
| 345 | #define RTC_LOG_ERR(sev) \ |
| 346 | RTC_LOG_GLE(sev) |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 347 | #elif defined(__native_client__) && __native_client__ |
Mirko Bonadei | 8ed8e56 | 2017-10-27 09:43:53 +0200 | [diff] [blame] | 348 | #define RTC_LOG_ERR_EX(sev, err) \ |
| 349 | RTC_LOG(sev) |
| 350 | #define RTC_LOG_ERR(sev) \ |
| 351 | RTC_LOG(sev) |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 352 | #elif defined(WEBRTC_POSIX) |
Mirko Bonadei | 8ed8e56 | 2017-10-27 09:43:53 +0200 | [diff] [blame] | 353 | #define RTC_LOG_ERR_EX(sev, err) \ |
| 354 | RTC_LOG_ERRNO_EX(sev, err) |
| 355 | #define RTC_LOG_ERR(sev) \ |
| 356 | RTC_LOG_ERRNO(sev) |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 357 | #endif // WEBRTC_WIN |
| 358 | |
Tommi | e51a0a8 | 2018-02-27 15:30:29 +0100 | [diff] [blame] | 359 | #if defined(WEBRTC_ANDROID) |
| 360 | namespace internal { |
| 361 | // Inline adapters provided for backwards compatibility for downstream projects. |
| 362 | inline const char* AdaptString(const char* str) { return str; } |
| 363 | inline const char* AdaptString(const std::string& str) { return str.c_str(); } |
| 364 | } // namespace internal |
Mirko Bonadei | 8ed8e56 | 2017-10-27 09:43:53 +0200 | [diff] [blame] | 365 | #define RTC_LOG_TAG(sev, tag) \ |
| 366 | RTC_LOG_SEVERITY_PRECONDITION(sev) \ |
Tommi | e51a0a8 | 2018-02-27 15:30:29 +0100 | [diff] [blame] | 367 | rtc::LogMessage(nullptr, 0, sev, rtc::internal::AdaptString(tag)).stream() |
| 368 | #else |
| 369 | // DEPRECATED. This macro is only intended for Android. |
| 370 | #define RTC_LOG_TAG(sev, tag) \ |
| 371 | RTC_LOG_SEVERITY_PRECONDITION(sev) \ |
| 372 | rtc::LogMessage(nullptr, 0, sev).stream() |
| 373 | #endif |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 374 | |
Fredrik Solenberg | b3d7cac | 2017-11-17 15:22:37 +0100 | [diff] [blame] | 375 | // The RTC_DLOG macros are equivalent to their RTC_LOG counterparts except that |
| 376 | // they only generate code in debug builds. |
| 377 | #if RTC_DLOG_IS_ON |
| 378 | #define RTC_DLOG(sev) RTC_LOG(sev) |
| 379 | #define RTC_DLOG_V(sev) RTC_LOG_V(sev) |
| 380 | #define RTC_DLOG_F(sev) RTC_LOG_F(sev) |
| 381 | #else |
| 382 | #define RTC_DLOG_EAT_STREAM_PARAMS(sev) \ |
Jonas Olsson | 24ea822 | 2018-01-25 10:14:29 +0100 | [diff] [blame] | 383 | (true ? true : ((void)(sev), true)) \ |
| 384 | ? static_cast<void>(0) \ |
| 385 | : rtc::LogMessageVoidify() & \ |
| 386 | rtc::LogMessage(__FILE__, __LINE__, sev).stream() |
| 387 | #define RTC_DLOG(sev) RTC_DLOG_EAT_STREAM_PARAMS(rtc::sev) |
Fredrik Solenberg | b3d7cac | 2017-11-17 15:22:37 +0100 | [diff] [blame] | 388 | #define RTC_DLOG_V(sev) RTC_DLOG_EAT_STREAM_PARAMS(sev) |
Jonas Olsson | 24ea822 | 2018-01-25 10:14:29 +0100 | [diff] [blame] | 389 | #define RTC_DLOG_F(sev) RTC_DLOG_EAT_STREAM_PARAMS(rtc::sev) |
Fredrik Solenberg | b3d7cac | 2017-11-17 15:22:37 +0100 | [diff] [blame] | 390 | #endif |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 391 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 392 | } // namespace rtc |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 393 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 394 | #endif // RTC_BASE_LOGGING_H_ |