andrew@webrtc.org | 50419b0 | 2012-11-14 19:07:54 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 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 | |
| 11 | // This is a highly stripped-down version of libjingle's talk/base/logging.h. |
| 12 | // It is a thin wrapper around WEBRTC_TRACE, maintaining the libjingle log |
| 13 | // semantics to ease a transition to that format. |
| 14 | |
andrew@webrtc.org | 655d8f5 | 2012-11-20 07:34:45 +0000 | [diff] [blame] | 15 | // NOTE: LS_INFO maps to a new trace level which should be reserved for |
| 16 | // infrequent, non-verbose logs. The other levels below kTraceWarning have been |
| 17 | // rendered essentially useless due to their verbosity. Carefully consider the |
| 18 | // impact of adding a new LS_INFO log. If it will be logged at anything |
| 19 | // approaching a frame or packet frequency, use LS_VERBOSE if necessary, or |
| 20 | // preferably, do not log at all. |
| 21 | |
andrew@webrtc.org | 50419b0 | 2012-11-14 19:07:54 +0000 | [diff] [blame] | 22 | // LOG(...) an ostream target that can be used to send formatted |
| 23 | // output to a variety of logging targets, such as debugger console, stderr, |
| 24 | // file, or any StreamInterface. |
| 25 | // The severity level passed as the first argument to the LOGging |
| 26 | // functions is used as a filter, to limit the verbosity of the logging. |
| 27 | // Static members of LogMessage documented below are used to control the |
| 28 | // verbosity and target of the output. |
| 29 | // There are several variations on the LOG macro which facilitate logging |
| 30 | // of common error conditions, detailed below. |
| 31 | |
| 32 | // LOG(sev) logs the given stream at severity "sev", which must be a |
| 33 | // compile-time constant of the LoggingSeverity type, without the namespace |
| 34 | // prefix. |
| 35 | // LOG_V(sev) Like LOG(), but sev is a run-time variable of the LoggingSeverity |
| 36 | // type (basically, it just doesn't prepend the namespace). |
| 37 | // LOG_F(sev) Like LOG(), but includes the name of the current function. |
| 38 | |
kjellander | d56d68c | 2015-11-02 02:12:41 -0800 | [diff] [blame] | 39 | #ifndef WEBRTC_SYSTEM_WRAPPERS_INCLUDE_LOGGING_H_ |
| 40 | #define WEBRTC_SYSTEM_WRAPPERS_INCLUDE_LOGGING_H_ |
andrew@webrtc.org | 50419b0 | 2012-11-14 19:07:54 +0000 | [diff] [blame] | 41 | |
| 42 | #include <sstream> |
| 43 | |
| 44 | namespace webrtc { |
| 45 | |
| 46 | ////////////////////////////////////////////////////////////////////// |
| 47 | |
| 48 | // Note that the non-standard LoggingSeverity aliases exist because they are |
| 49 | // still in broad use. The meanings of the levels are: |
| 50 | // LS_SENSITIVE: Information which should only be logged with the consent |
| 51 | // of the user, due to privacy concerns. |
| 52 | // LS_VERBOSE: This level is for data which we do not want to appear in the |
| 53 | // normal debug log, but should appear in diagnostic logs. |
| 54 | // LS_INFO: Chatty level used in debugging for all sorts of things, the default |
| 55 | // in debug builds. |
| 56 | // LS_WARNING: Something that may warrant investigation. |
| 57 | // LS_ERROR: Something that should not have occurred. |
andrew@webrtc.org | 655d8f5 | 2012-11-20 07:34:45 +0000 | [diff] [blame] | 58 | enum LoggingSeverity { |
| 59 | LS_SENSITIVE, LS_VERBOSE, LS_INFO, LS_WARNING, LS_ERROR |
| 60 | }; |
andrew@webrtc.org | 50419b0 | 2012-11-14 19:07:54 +0000 | [diff] [blame] | 61 | |
| 62 | class LogMessage { |
| 63 | public: |
| 64 | LogMessage(const char* file, int line, LoggingSeverity sev); |
| 65 | ~LogMessage(); |
| 66 | |
andrew@webrtc.org | 9080518 | 2013-09-05 16:40:43 +0000 | [diff] [blame] | 67 | static bool Loggable(LoggingSeverity sev); |
andrew@webrtc.org | 50419b0 | 2012-11-14 19:07:54 +0000 | [diff] [blame] | 68 | std::ostream& stream() { return print_stream_; } |
| 69 | |
| 70 | private: |
andrew@webrtc.org | 50419b0 | 2012-11-14 19:07:54 +0000 | [diff] [blame] | 71 | // The ostream that buffers the formatted message before output |
| 72 | std::ostringstream print_stream_; |
| 73 | |
| 74 | // The severity level of this message |
| 75 | LoggingSeverity severity_; |
| 76 | }; |
| 77 | |
| 78 | ////////////////////////////////////////////////////////////////////// |
andrew@webrtc.org | c3e5d34 | 2012-11-23 19:30:59 +0000 | [diff] [blame] | 79 | // Macros which automatically disable logging when WEBRTC_LOGGING == 0 |
andrew@webrtc.org | 50419b0 | 2012-11-14 19:07:54 +0000 | [diff] [blame] | 80 | ////////////////////////////////////////////////////////////////////// |
| 81 | |
andrew@webrtc.org | 50419b0 | 2012-11-14 19:07:54 +0000 | [diff] [blame] | 82 | #ifndef LOG |
andrew@webrtc.org | 50419b0 | 2012-11-14 19:07:54 +0000 | [diff] [blame] | 83 | // The following non-obvious technique for implementation of a |
| 84 | // conditional log stream was stolen from google3/base/logging.h. |
| 85 | |
| 86 | // This class is used to explicitly ignore values in the conditional |
| 87 | // logging macros. This avoids compiler warnings like "value computed |
| 88 | // is not used" and "statement has no effect". |
| 89 | |
| 90 | class LogMessageVoidify { |
| 91 | public: |
| 92 | LogMessageVoidify() { } |
| 93 | // This has to be an operator with a precedence lower than << but |
| 94 | // higher than ?: |
| 95 | void operator&(std::ostream&) { } |
| 96 | }; |
| 97 | |
andrew@webrtc.org | f1a4817 | 2013-11-07 23:47:26 +0000 | [diff] [blame] | 98 | #if defined(WEBRTC_RESTRICT_LOGGING) |
| 99 | // This should compile away logs matching the following condition. |
| 100 | #define RESTRICT_LOGGING_PRECONDITION(sev) \ |
henrikg@webrtc.org | bff9620 | 2013-11-08 10:37:27 +0000 | [diff] [blame] | 101 | sev < webrtc::LS_INFO ? (void) 0 : |
andrew@webrtc.org | f1a4817 | 2013-11-07 23:47:26 +0000 | [diff] [blame] | 102 | #else |
| 103 | #define RESTRICT_LOGGING_PRECONDITION(sev) |
| 104 | #endif |
| 105 | |
andrew@webrtc.org | 9080518 | 2013-09-05 16:40:43 +0000 | [diff] [blame] | 106 | #define LOG_SEVERITY_PRECONDITION(sev) \ |
andrew@webrtc.org | f1a4817 | 2013-11-07 23:47:26 +0000 | [diff] [blame] | 107 | RESTRICT_LOGGING_PRECONDITION(sev) !(webrtc::LogMessage::Loggable(sev)) \ |
andrew@webrtc.org | 9080518 | 2013-09-05 16:40:43 +0000 | [diff] [blame] | 108 | ? (void) 0 \ |
| 109 | : webrtc::LogMessageVoidify() & |
| 110 | |
andrew@webrtc.org | 50419b0 | 2012-11-14 19:07:54 +0000 | [diff] [blame] | 111 | #define LOG(sev) \ |
andrew@webrtc.org | 9080518 | 2013-09-05 16:40:43 +0000 | [diff] [blame] | 112 | LOG_SEVERITY_PRECONDITION(webrtc::sev) \ |
andrew@webrtc.org | 50419b0 | 2012-11-14 19:07:54 +0000 | [diff] [blame] | 113 | webrtc::LogMessage(__FILE__, __LINE__, webrtc::sev).stream() |
| 114 | |
| 115 | // The _V version is for when a variable is passed in. It doesn't do the |
| 116 | // namespace concatination. |
| 117 | #define LOG_V(sev) \ |
andrew@webrtc.org | 9080518 | 2013-09-05 16:40:43 +0000 | [diff] [blame] | 118 | LOG_SEVERITY_PRECONDITION(sev) \ |
andrew@webrtc.org | 50419b0 | 2012-11-14 19:07:54 +0000 | [diff] [blame] | 119 | webrtc::LogMessage(__FILE__, __LINE__, sev).stream() |
| 120 | |
| 121 | // The _F version prefixes the message with the current function name. |
tfarina | a41ab93 | 2015-10-30 16:08:48 -0700 | [diff] [blame] | 122 | #if (defined(__GNUC__) && !defined(NDEBUG)) || defined(WANT_PRETTY_LOG_F) |
andrew@webrtc.org | 50419b0 | 2012-11-14 19:07:54 +0000 | [diff] [blame] | 123 | #define LOG_F(sev) LOG(sev) << __PRETTY_FUNCTION__ << ": " |
| 124 | #else |
| 125 | #define LOG_F(sev) LOG(sev) << __FUNCTION__ << ": " |
| 126 | #endif |
| 127 | |
andrew@webrtc.org | 50419b0 | 2012-11-14 19:07:54 +0000 | [diff] [blame] | 128 | #endif // LOG |
| 129 | |
| 130 | } // namespace webrtc |
| 131 | |
kjellander | d56d68c | 2015-11-02 02:12:41 -0800 | [diff] [blame] | 132 | #endif // WEBRTC_SYSTEM_WRAPPERS_INCLUDE_LOGGING_H_ |