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