OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef THIRD_PARTY_WEBRTC_OVERRIDES_WEBRTC_BASE_DIAGNOSTIC_LOGGING_H_ |
| 6 #define THIRD_PARTY_WEBRTC_OVERRIDES_WEBRTC_BASE_DIAGNOSTIC_LOGGING_H_ |
| 7 |
| 8 #include <sstream> |
| 9 #include <string> |
| 10 |
| 11 #include "third_party/webrtc/base/checks.h" |
| 12 #include "third_party/webrtc/base/scoped_ref_ptr.h" |
| 13 |
| 14 namespace rtc { |
| 15 |
| 16 /////////////////////////////////////////////////////////////////////////////// |
| 17 // ConstantLabel can be used to easily generate string names from constant |
| 18 // values. This can be useful for logging descriptive names of error messages. |
| 19 // Usage: |
| 20 // const ConstantLabel LIBRARY_ERRORS[] = { |
| 21 // KLABEL(SOME_ERROR), |
| 22 // KLABEL(SOME_OTHER_ERROR), |
| 23 // ... |
| 24 // LASTLABEL |
| 25 // } |
| 26 // |
| 27 // int err = LibraryFunc(); |
| 28 // LOG(LS_ERROR) << "LibraryFunc returned: " |
| 29 // << ErrorName(err, LIBRARY_ERRORS); |
| 30 |
| 31 struct ConstantLabel { |
| 32 int value; |
| 33 const char* label; |
| 34 }; |
| 35 #define KLABEL(x) { x, #x } |
| 36 #define LASTLABEL { 0, 0 } |
| 37 |
| 38 const char* FindLabel(int value, const ConstantLabel entries[]); |
| 39 std::string ErrorName(int err, const ConstantLabel* err_table); |
| 40 |
| 41 ////////////////////////////////////////////////////////////////////// |
| 42 // Note that the non-standard LoggingSeverity aliases exist because they are |
| 43 // still in broad use. The meanings of the levels are: |
| 44 // LS_SENSITIVE: Information which should only be logged with the consent |
| 45 // of the user, due to privacy concerns. |
| 46 // LS_VERBOSE: This level is for data which we do not want to appear in the |
| 47 // normal debug log, but should appear in diagnostic logs. |
| 48 // LS_INFO: Chatty level used in debugging for all sorts of things, the default |
| 49 // in debug builds. |
| 50 // LS_WARNING: Something that may warrant investigation. |
| 51 // LS_ERROR: Something that should not have occurred. |
| 52 // Note that LoggingSeverity is mapped over to chromiums verbosity levels where |
| 53 // anything lower than or equal to the current verbosity level is written to |
| 54 // file which is the opposite of logging severity in libjingle where higher |
| 55 // severity numbers than or equal to the current severity level are written to |
| 56 // file. Also, note that the values are explicitly defined here for convenience |
| 57 // since the command line flag must be set using numerical values. |
| 58 // TODO(tommi): To keep things simple, we should just use the same values for |
| 59 // these constants as Chrome does. |
| 60 enum LoggingSeverity { LS_ERROR = 1, |
| 61 LS_WARNING = 2, |
| 62 LS_INFO = 3, |
| 63 LS_VERBOSE = 4, |
| 64 LS_SENSITIVE = 5, |
| 65 INFO = LS_INFO, |
| 66 WARNING = LS_WARNING, |
| 67 LERROR = LS_ERROR }; |
| 68 |
| 69 // LogErrorContext assists in interpreting the meaning of an error value. |
| 70 enum LogErrorContext { |
| 71 ERRCTX_NONE, |
| 72 ERRCTX_ERRNO, // System-local errno |
| 73 ERRCTX_HRESULT, // Windows HRESULT |
| 74 ERRCTX_OSSTATUS, // MacOS OSStatus |
| 75 |
| 76 // Abbreviations for LOG_E macro |
| 77 ERRCTX_EN = ERRCTX_ERRNO, // LOG_E(sev, EN, x) |
| 78 ERRCTX_HR = ERRCTX_HRESULT, // LOG_E(sev, HR, x) |
| 79 ERRCTX_OS = ERRCTX_OSSTATUS, // LOG_E(sev, OS, x) |
| 80 }; |
| 81 |
| 82 // Class that writes a log message to the logging delegate ("WebRTC logging |
| 83 // stream" in Chrome) and to Chrome's logging stream. |
| 84 class DiagnosticLogMessage { |
| 85 public: |
| 86 DiagnosticLogMessage(const char* file, int line, LoggingSeverity severity, |
| 87 LogErrorContext err_ctx, int err); |
| 88 DiagnosticLogMessage(const char* file, int line, LoggingSeverity severity, |
| 89 LogErrorContext err_ctx, int err, const char* module); |
| 90 ~DiagnosticLogMessage(); |
| 91 |
| 92 void CreateTimestamp(); |
| 93 |
| 94 std::ostream& stream() { return print_stream_; } |
| 95 |
| 96 private: |
| 97 const char* file_name_; |
| 98 const int line_; |
| 99 const LoggingSeverity severity_; |
| 100 const bool log_to_chrome_; |
| 101 |
| 102 std::string extra_; |
| 103 |
| 104 std::ostringstream print_stream_; |
| 105 }; |
| 106 |
| 107 // This class is used to explicitly ignore values in the conditional |
| 108 // logging macros. This avoids compiler warnings like "value computed |
| 109 // is not used" and "statement has no effect". |
| 110 class LogMessageVoidify { |
| 111 public: |
| 112 LogMessageVoidify() { } |
| 113 // This has to be an operator with a precedence lower than << but |
| 114 // higher than ?: |
| 115 void operator&(std::ostream&) { } |
| 116 }; |
| 117 |
| 118 ////////////////////////////////////////////////////////////////////// |
| 119 // Logging Helpers |
| 120 ////////////////////////////////////////////////////////////////////// |
| 121 |
| 122 class LogMultilineState { |
| 123 public: |
| 124 size_t unprintable_count_[2]; |
| 125 LogMultilineState() { |
| 126 unprintable_count_[0] = unprintable_count_[1] = 0; |
| 127 } |
| 128 }; |
| 129 |
| 130 class LogMessage { |
| 131 public: |
| 132 static void LogToDebug(int min_sev); |
| 133 }; |
| 134 |
| 135 // When possible, pass optional state variable to track various data across |
| 136 // multiple calls to LogMultiline. Otherwise, pass NULL. |
| 137 void LogMultiline(LoggingSeverity level, const char* label, bool input, |
| 138 const void* data, size_t len, bool hex_mode, |
| 139 LogMultilineState* state); |
| 140 |
| 141 // TODO(grunell): Change name to InitDiagnosticLoggingDelegate or |
| 142 // InitDiagnosticLogging. Change also in init_webrtc.h/cc. |
| 143 // TODO(grunell): typedef the delegate function. |
| 144 void InitDiagnosticLoggingDelegateFunction( |
| 145 void (*delegate)(const std::string&)); |
| 146 |
| 147 void SetExtraLoggingInit( |
| 148 void (*function)(void (*delegate)(const std::string&))); |
| 149 |
| 150 } // namespace rtc |
| 151 |
| 152 #endif // THIRD_PARTY_WEBRTC_OVERRIDES_WEBRTC_BASE_DIAGNOSTIC_LOGGING_H_ |
OLD | NEW |