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 // NOTE: |
| 6 // Since this file includes Chromium headers, it must not include |
| 7 // third_party/webrtc/base/logging.h since it defines some of the same macros as |
| 8 // Chromium does and we'll run into conflicts. |
| 9 |
| 10 #if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) |
| 11 #include <CoreServices/CoreServices.h> |
| 12 #endif // OS_MACOSX |
| 13 |
| 14 #include <algorithm> |
| 15 #include <iomanip> |
| 16 |
| 17 #include "base/atomicops.h" |
| 18 #include "base/logging.h" |
| 19 #include "base/strings/string_util.h" |
| 20 #include "base/threading/platform_thread.h" |
| 21 #include "third_party/webrtc/base/ipaddress.h" |
| 22 #include "third_party/webrtc/base/stream.h" |
| 23 #include "third_party/webrtc/base/stringencode.h" |
| 24 #include "third_party/webrtc/base/stringutils.h" |
| 25 #include "third_party/webrtc/base/timeutils.h" |
| 26 #include "third_party/webrtc_overrides/webrtc/base/diagnostic_logging.h" |
| 27 |
| 28 // From this file we can't use VLOG since it expands into usage of the __FILE__ |
| 29 // macro (for correct filtering). The actual logging call from DIAGNOSTIC_LOG in |
| 30 // ~DiagnosticLogMessage. Note that the second parameter to the LAZY_STREAM |
| 31 // macro is true since the filter check has already been done for |
| 32 // DIAGNOSTIC_LOG. |
| 33 #define LOG_LAZY_STREAM_DIRECT(file_name, line_number, sev) \ |
| 34 LAZY_STREAM(logging::LogMessage(file_name, line_number, sev).stream(), \ |
| 35 true) |
| 36 |
| 37 namespace rtc { |
| 38 |
| 39 void (*g_logging_delegate_function)(const std::string&) = NULL; |
| 40 void (*g_extra_logging_init_function)( |
| 41 void (*logging_delegate_function)(const std::string&)) = NULL; |
| 42 #ifndef NDEBUG |
| 43 static_assert(sizeof(base::subtle::Atomic32) == sizeof(base::PlatformThreadId), |
| 44 "Atomic32 not same size as PlatformThreadId"); |
| 45 base::subtle::Atomic32 g_init_logging_delegate_thread_id = 0; |
| 46 #endif |
| 47 |
| 48 ///////////////////////////////////////////////////////////////////////////// |
| 49 // Constant Labels |
| 50 ///////////////////////////////////////////////////////////////////////////// |
| 51 |
| 52 const char* FindLabel(int value, const ConstantLabel entries[]) { |
| 53 for (int i = 0; entries[i].label; ++i) { |
| 54 if (value == entries[i].value) return entries[i].label; |
| 55 } |
| 56 return 0; |
| 57 } |
| 58 |
| 59 std::string ErrorName(int err, const ConstantLabel* err_table) { |
| 60 if (err == 0) |
| 61 return "No error"; |
| 62 |
| 63 if (err_table != 0) { |
| 64 if (const char * value = FindLabel(err, err_table)) |
| 65 return value; |
| 66 } |
| 67 |
| 68 char buffer[16]; |
| 69 base::snprintf(buffer, sizeof(buffer), "0x%08x", err); |
| 70 return buffer; |
| 71 } |
| 72 |
| 73 ///////////////////////////////////////////////////////////////////////////// |
| 74 // Log helper functions |
| 75 ///////////////////////////////////////////////////////////////////////////// |
| 76 |
| 77 inline int WebRtcSevToChromeSev(LoggingSeverity sev) { |
| 78 switch (sev) { |
| 79 case LS_ERROR: |
| 80 return ::logging::LOG_ERROR; |
| 81 case LS_WARNING: |
| 82 return ::logging::LOG_WARNING; |
| 83 case LS_INFO: |
| 84 return ::logging::LOG_INFO; |
| 85 case LS_VERBOSE: |
| 86 case LS_SENSITIVE: |
| 87 return ::logging::LOG_VERBOSE; |
| 88 default: |
| 89 NOTREACHED(); |
| 90 return ::logging::LOG_FATAL; |
| 91 } |
| 92 } |
| 93 |
| 94 inline int WebRtcVerbosityLevel(LoggingSeverity sev) { |
| 95 switch (sev) { |
| 96 case LS_ERROR: |
| 97 return -2; |
| 98 case LS_WARNING: |
| 99 return -1; |
| 100 case LS_INFO: // We treat 'info' and 'verbose' as the same verbosity level. |
| 101 case LS_VERBOSE: |
| 102 return 1; |
| 103 case LS_SENSITIVE: |
| 104 return 2; |
| 105 default: |
| 106 NOTREACHED(); |
| 107 return 0; |
| 108 } |
| 109 } |
| 110 |
| 111 // Generates extra information for LOG_E. |
| 112 static std::string GenerateExtra(LogErrorContext err_ctx, |
| 113 int err, |
| 114 const char* module) { |
| 115 if (err_ctx != ERRCTX_NONE) { |
| 116 std::ostringstream tmp; |
| 117 tmp << ": "; |
| 118 tmp << "[0x" << std::setfill('0') << std::hex << std::setw(8) << err << "]"; |
| 119 switch (err_ctx) { |
| 120 case ERRCTX_ERRNO: |
| 121 tmp << " " << strerror(err); |
| 122 break; |
| 123 #if defined(WEBRTC_WIN) |
| 124 case ERRCTX_HRESULT: { |
| 125 char msgbuf[256]; |
| 126 DWORD flags = FORMAT_MESSAGE_FROM_SYSTEM; |
| 127 HMODULE hmod = GetModuleHandleA(module); |
| 128 if (hmod) |
| 129 flags |= FORMAT_MESSAGE_FROM_HMODULE; |
| 130 if (DWORD len = FormatMessageA( |
| 131 flags, hmod, err, |
| 132 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
| 133 msgbuf, sizeof(msgbuf) / sizeof(msgbuf[0]), NULL)) { |
| 134 while ((len > 0) && |
| 135 isspace(static_cast<unsigned char>(msgbuf[len-1]))) { |
| 136 msgbuf[--len] = 0; |
| 137 } |
| 138 tmp << " " << msgbuf; |
| 139 } |
| 140 break; |
| 141 } |
| 142 #endif // OS_WIN |
| 143 #if defined(WEBRTC_IOS) |
| 144 case ERRCTX_OSSTATUS: |
| 145 tmp << " " << "Unknown LibJingle error: " << err; |
| 146 break; |
| 147 #elif defined(WEBRTC_MAC) |
| 148 case ERRCTX_OSSTATUS: { |
| 149 tmp << " " << nonnull(GetMacOSStatusErrorString(err), "Unknown error"); |
| 150 if (const char* desc = GetMacOSStatusCommentString(err)) { |
| 151 tmp << ": " << desc; |
| 152 } |
| 153 break; |
| 154 } |
| 155 #endif // OS_MACOSX |
| 156 default: |
| 157 break; |
| 158 } |
| 159 return tmp.str(); |
| 160 } |
| 161 return ""; |
| 162 } |
| 163 |
| 164 DiagnosticLogMessage::DiagnosticLogMessage(const char* file, |
| 165 int line, |
| 166 LoggingSeverity severity, |
| 167 LogErrorContext err_ctx, |
| 168 int err) |
| 169 : file_name_(file), |
| 170 line_(line), |
| 171 severity_(severity), |
| 172 log_to_chrome_(CheckVlogIsOnHelper(severity, file, strlen(file) + 1)) { |
| 173 extra_ = GenerateExtra(err_ctx, err, NULL); |
| 174 } |
| 175 |
| 176 DiagnosticLogMessage::DiagnosticLogMessage(const char* file, |
| 177 int line, |
| 178 LoggingSeverity severity, |
| 179 LogErrorContext err_ctx, |
| 180 int err, |
| 181 const char* module) |
| 182 : file_name_(file), |
| 183 line_(line), |
| 184 severity_(severity), |
| 185 log_to_chrome_(CheckVlogIsOnHelper(severity, file, strlen(file) + 1)) { |
| 186 extra_ = GenerateExtra(err_ctx, err, module); |
| 187 } |
| 188 |
| 189 DiagnosticLogMessage::~DiagnosticLogMessage() { |
| 190 const bool call_delegate = |
| 191 g_logging_delegate_function && severity_ <= LS_INFO; |
| 192 |
| 193 if (call_delegate || log_to_chrome_) { |
| 194 print_stream_ << extra_; |
| 195 const std::string& str = print_stream_.str(); |
| 196 if (log_to_chrome_) { |
| 197 LOG_LAZY_STREAM_DIRECT(file_name_, line_, |
| 198 rtc::WebRtcSevToChromeSev(severity_)) << str; |
| 199 } |
| 200 |
| 201 if (g_logging_delegate_function && severity_ <= LS_INFO) { |
| 202 g_logging_delegate_function(str); |
| 203 } |
| 204 } |
| 205 } |
| 206 |
| 207 // static |
| 208 void LogMessage::LogToDebug(int min_sev) { |
| 209 logging::SetMinLogLevel(min_sev); |
| 210 } |
| 211 |
| 212 // Note: this function is a copy from the overriden libjingle implementation. |
| 213 void LogMultiline(LoggingSeverity level, const char* label, bool input, |
| 214 const void* data, size_t len, bool hex_mode, |
| 215 LogMultilineState* state) { |
| 216 // TODO(grunell): This will not do the expected verbosity level checking. We |
| 217 // need a macro for the multiline logging. |
| 218 // https://code.google.com/p/webrtc/issues/detail?id=5011 |
| 219 if (!LOG_CHECK_LEVEL_V(level)) |
| 220 return; |
| 221 |
| 222 const char * direction = (input ? " << " : " >> "); |
| 223 |
| 224 // NULL data means to flush our count of unprintable characters. |
| 225 if (!data) { |
| 226 if (state && state->unprintable_count_[input]) { |
| 227 LOG_V(level) << label << direction << "## " |
| 228 << state->unprintable_count_[input] |
| 229 << " consecutive unprintable ##"; |
| 230 state->unprintable_count_[input] = 0; |
| 231 } |
| 232 return; |
| 233 } |
| 234 |
| 235 // The ctype classification functions want unsigned chars. |
| 236 const unsigned char* udata = static_cast<const unsigned char*>(data); |
| 237 |
| 238 if (hex_mode) { |
| 239 const size_t LINE_SIZE = 24; |
| 240 char hex_line[LINE_SIZE * 9 / 4 + 2], asc_line[LINE_SIZE + 1]; |
| 241 while (len > 0) { |
| 242 memset(asc_line, ' ', sizeof(asc_line)); |
| 243 memset(hex_line, ' ', sizeof(hex_line)); |
| 244 size_t line_len = std::min(len, LINE_SIZE); |
| 245 for (size_t i = 0; i < line_len; ++i) { |
| 246 unsigned char ch = udata[i]; |
| 247 asc_line[i] = isprint(ch) ? ch : '.'; |
| 248 hex_line[i*2 + i/4] = hex_encode(ch >> 4); |
| 249 hex_line[i*2 + i/4 + 1] = hex_encode(ch & 0xf); |
| 250 } |
| 251 asc_line[sizeof(asc_line)-1] = 0; |
| 252 hex_line[sizeof(hex_line)-1] = 0; |
| 253 LOG_V(level) << label << direction |
| 254 << asc_line << " " << hex_line << " "; |
| 255 udata += line_len; |
| 256 len -= line_len; |
| 257 } |
| 258 return; |
| 259 } |
| 260 |
| 261 size_t consecutive_unprintable = state ? state->unprintable_count_[input] : 0; |
| 262 |
| 263 const unsigned char* end = udata + len; |
| 264 while (udata < end) { |
| 265 const unsigned char* line = udata; |
| 266 const unsigned char* end_of_line = strchrn<unsigned char>(udata, |
| 267 end - udata, |
| 268 '\n'); |
| 269 if (!end_of_line) { |
| 270 udata = end_of_line = end; |
| 271 } else { |
| 272 udata = end_of_line + 1; |
| 273 } |
| 274 |
| 275 bool is_printable = true; |
| 276 |
| 277 // If we are in unprintable mode, we need to see a line of at least |
| 278 // kMinPrintableLine characters before we'll switch back. |
| 279 const ptrdiff_t kMinPrintableLine = 4; |
| 280 if (consecutive_unprintable && ((end_of_line - line) < kMinPrintableLine)) { |
| 281 is_printable = false; |
| 282 } else { |
| 283 // Determine if the line contains only whitespace and printable |
| 284 // characters. |
| 285 bool is_entirely_whitespace = true; |
| 286 for (const unsigned char* pos = line; pos < end_of_line; ++pos) { |
| 287 if (isspace(*pos)) |
| 288 continue; |
| 289 is_entirely_whitespace = false; |
| 290 if (!isprint(*pos)) { |
| 291 is_printable = false; |
| 292 break; |
| 293 } |
| 294 } |
| 295 // Treat an empty line following unprintable data as unprintable. |
| 296 if (consecutive_unprintable && is_entirely_whitespace) { |
| 297 is_printable = false; |
| 298 } |
| 299 } |
| 300 if (!is_printable) { |
| 301 consecutive_unprintable += (udata - line); |
| 302 continue; |
| 303 } |
| 304 // Print out the current line, but prefix with a count of prior unprintable |
| 305 // characters. |
| 306 if (consecutive_unprintable) { |
| 307 LOG_V(level) << label << direction << "## " << consecutive_unprintable |
| 308 << " consecutive unprintable ##"; |
| 309 consecutive_unprintable = 0; |
| 310 } |
| 311 // Strip off trailing whitespace. |
| 312 while ((end_of_line > line) && isspace(*(end_of_line-1))) { |
| 313 --end_of_line; |
| 314 } |
| 315 // Filter out any private data |
| 316 std::string substr(reinterpret_cast<const char*>(line), end_of_line - line); |
| 317 std::string::size_type pos_private = substr.find("Email"); |
| 318 if (pos_private == std::string::npos) { |
| 319 pos_private = substr.find("Passwd"); |
| 320 } |
| 321 if (pos_private == std::string::npos) { |
| 322 LOG_V(level) << label << direction << substr; |
| 323 } else { |
| 324 LOG_V(level) << label << direction << "## omitted for privacy ##"; |
| 325 } |
| 326 } |
| 327 |
| 328 if (state) { |
| 329 state->unprintable_count_[input] = consecutive_unprintable; |
| 330 } |
| 331 } |
| 332 |
| 333 void InitDiagnosticLoggingDelegateFunction( |
| 334 void (*delegate)(const std::string&)) { |
| 335 #ifndef NDEBUG |
| 336 // Ensure that this function is always called from the same thread. |
| 337 base::subtle::NoBarrier_CompareAndSwap(&g_init_logging_delegate_thread_id, 0, |
| 338 static_cast<base::subtle::Atomic32>(base::PlatformThread::CurrentId())); |
| 339 DCHECK_EQ( |
| 340 g_init_logging_delegate_thread_id, |
| 341 static_cast<base::subtle::Atomic32>(base::PlatformThread::CurrentId())); |
| 342 #endif |
| 343 CHECK(delegate); |
| 344 // This function may be called with the same argument several times if the |
| 345 // page is reloaded or there are several PeerConnections on one page with |
| 346 // logging enabled. This is OK, we simply don't have to do anything. |
| 347 if (delegate == g_logging_delegate_function) |
| 348 return; |
| 349 CHECK(!g_logging_delegate_function); |
| 350 #ifdef NDEBUG |
| 351 IPAddress::set_strip_sensitive(true); |
| 352 #endif |
| 353 g_logging_delegate_function = delegate; |
| 354 |
| 355 if (g_extra_logging_init_function) |
| 356 g_extra_logging_init_function(delegate); |
| 357 } |
| 358 |
| 359 void SetExtraLoggingInit( |
| 360 void (*function)(void (*delegate)(const std::string&))) { |
| 361 CHECK(function); |
| 362 CHECK(!g_extra_logging_init_function); |
| 363 g_extra_logging_init_function = function; |
| 364 } |
| 365 |
| 366 bool CheckVlogIsOnHelper( |
| 367 rtc::LoggingSeverity severity, const char* file, size_t N) { |
| 368 return rtc::WebRtcVerbosityLevel(severity) <= |
| 369 ::logging::GetVlogLevelHelper(file, N); |
| 370 } |
| 371 |
| 372 } // namespace rtc |
OLD | NEW |