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