OLD | NEW |
(Empty) | |
| 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) |
| 8 #include <CoreServices/CoreServices.h> |
| 9 #endif // OS_MACOSX |
| 10 |
| 11 #include <iomanip> |
| 12 |
| 13 #include "third_party/libjingle/source/talk/base/stream.h" |
| 14 #include "third_party/libjingle/source/talk/base/stringencode.h" |
| 15 #include "third_party/libjingle/source/talk/base/stringutils.h" |
| 16 |
| 17 #if defined(OS_WIN) |
| 18 #define snprintf _snprintf |
| 19 #endif // OS_WIN |
| 20 |
| 21 // LOG_E can't call VLOG directly like LOG_V can since VLOG expands into usage |
| 22 // of the __FILE__ macro (for filtering) and the actual VLOG call from LOG_E |
| 23 // happens inside LogEHelper. Note that the second parameter to the LAZY_STREAM |
| 24 // macro is true since the filter check is already done for LOG_E. |
| 25 #define LOG_E_BASE(file_name, line_number, sev) \ |
| 26 LAZY_STREAM(logging::LogMessage(file_name, line_number, \ |
| 27 -sev).stream(), true) |
| 28 |
| 29 namespace talk_base { |
| 30 |
| 31 ///////////////////////////////////////////////////////////////////////////// |
| 32 // Constant Labels |
| 33 ///////////////////////////////////////////////////////////////////////////// |
| 34 |
| 35 const char* FindLabel(int value, const ConstantLabel entries[]) { |
| 36 for (int i = 0; entries[i].label; ++i) { |
| 37 if (value == entries[i].value) return entries[i].label; |
| 38 } |
| 39 return 0; |
| 40 } |
| 41 |
| 42 std::string ErrorName(int err, const ConstantLabel* err_table) { |
| 43 if (err == 0) |
| 44 return "No error"; |
| 45 |
| 46 if (err_table != 0) { |
| 47 if (const char * value = FindLabel(err, err_table)) |
| 48 return value; |
| 49 } |
| 50 |
| 51 char buffer[16]; |
| 52 snprintf(buffer, sizeof(buffer), "0x%08x", err); |
| 53 return buffer; |
| 54 } |
| 55 |
| 56 ///////////////////////////////////////////////////////////////////////////// |
| 57 // LogEHelper |
| 58 ///////////////////////////////////////////////////////////////////////////// |
| 59 |
| 60 // Summarizes LOG_E messages as strings. |
| 61 static std::string GenerateExtra(LogErrorContext err_ctx, |
| 62 int err, |
| 63 const char* module) { |
| 64 if (err_ctx != ERRCTX_NONE) { |
| 65 std::ostringstream tmp; |
| 66 tmp << ": "; |
| 67 tmp << "[0x" << std::setfill('0') << std::hex << std::setw(8) << err << "]"; |
| 68 switch (err_ctx) { |
| 69 case ERRCTX_ERRNO: |
| 70 tmp << " " << strerror(err); |
| 71 break; |
| 72 #if defined(OS_WIN) |
| 73 case ERRCTX_HRESULT: { |
| 74 char msgbuf[256]; |
| 75 DWORD flags = FORMAT_MESSAGE_FROM_SYSTEM; |
| 76 HMODULE hmod = GetModuleHandleA(module); |
| 77 if (hmod) |
| 78 flags |= FORMAT_MESSAGE_FROM_HMODULE; |
| 79 if (DWORD len = FormatMessageA( |
| 80 flags, hmod, err, |
| 81 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
| 82 msgbuf, sizeof(msgbuf) / sizeof(msgbuf[0]), NULL)) { |
| 83 while ((len > 0) && |
| 84 isspace(static_cast<unsigned char>(msgbuf[len-1]))) { |
| 85 msgbuf[--len] = 0; |
| 86 } |
| 87 tmp << " " << msgbuf; |
| 88 } |
| 89 break; |
| 90 } |
| 91 #endif // OS_WIN |
| 92 #if defined(OS_MACOSX) |
| 93 case ERRCTX_OSSTATUS: { |
| 94 tmp << " " << nonnull(GetMacOSStatusErrorString(err), "Unknown error"); |
| 95 if (const char* desc = GetMacOSStatusCommentString(err)) { |
| 96 tmp << ": " << desc; |
| 97 } |
| 98 break; |
| 99 } |
| 100 #endif // OS_MACOSX |
| 101 default: |
| 102 break; |
| 103 } |
| 104 return tmp.str(); |
| 105 } |
| 106 return ""; |
| 107 } |
| 108 |
| 109 LogEHelper::LogEHelper(const char* file, |
| 110 int line, |
| 111 LoggingSeverity severity, |
| 112 LogErrorContext err_ctx, |
| 113 int err, |
| 114 const char* module) |
| 115 : file_name_(file), |
| 116 line_(line), |
| 117 severity_(severity) { |
| 118 extra_ = GenerateExtra(err_ctx, err, module); |
| 119 } |
| 120 |
| 121 LogEHelper::~LogEHelper() { |
| 122 print_stream_ << extra_; |
| 123 const std::string& str = print_stream_.str(); |
| 124 LOG_E_BASE(file_name_.c_str(), line_, severity_) << str; |
| 125 } |
| 126 |
| 127 // Note: this function is a copy from the overriden libjingle implementation. |
| 128 void LogMultiline(LoggingSeverity level, const char* label, bool input, |
| 129 const void* data, size_t len, bool hex_mode, |
| 130 LogMultilineState* state) { |
| 131 if (!LOG_CHECK_LEVEL_V(level)) |
| 132 return; |
| 133 |
| 134 const char * direction = (input ? " << " : " >> "); |
| 135 |
| 136 // NULL data means to flush our count of unprintable characters. |
| 137 if (!data) { |
| 138 if (state && state->unprintable_count_[input]) { |
| 139 LOG_V(level) << label << direction << "## " |
| 140 << state->unprintable_count_[input] |
| 141 << " consecutive unprintable ##"; |
| 142 state->unprintable_count_[input] = 0; |
| 143 } |
| 144 return; |
| 145 } |
| 146 |
| 147 // The ctype classification functions want unsigned chars. |
| 148 const unsigned char* udata = static_cast<const unsigned char*>(data); |
| 149 |
| 150 if (hex_mode) { |
| 151 const size_t LINE_SIZE = 24; |
| 152 char hex_line[LINE_SIZE * 9 / 4 + 2], asc_line[LINE_SIZE + 1]; |
| 153 while (len > 0) { |
| 154 memset(asc_line, ' ', sizeof(asc_line)); |
| 155 memset(hex_line, ' ', sizeof(hex_line)); |
| 156 size_t line_len = _min(len, LINE_SIZE); |
| 157 for (size_t i = 0; i < line_len; ++i) { |
| 158 unsigned char ch = udata[i]; |
| 159 asc_line[i] = isprint(ch) ? ch : '.'; |
| 160 hex_line[i*2 + i/4] = hex_encode(ch >> 4); |
| 161 hex_line[i*2 + i/4 + 1] = hex_encode(ch & 0xf); |
| 162 } |
| 163 asc_line[sizeof(asc_line)-1] = 0; |
| 164 hex_line[sizeof(hex_line)-1] = 0; |
| 165 LOG_V(level) << label << direction |
| 166 << asc_line << " " << hex_line << " "; |
| 167 udata += line_len; |
| 168 len -= line_len; |
| 169 } |
| 170 return; |
| 171 } |
| 172 |
| 173 size_t consecutive_unprintable = state ? state->unprintable_count_[input] : 0; |
| 174 |
| 175 const unsigned char* end = udata + len; |
| 176 while (udata < end) { |
| 177 const unsigned char* line = udata; |
| 178 const unsigned char* end_of_line = strchrn<unsigned char>(udata, |
| 179 end - udata, |
| 180 '\n'); |
| 181 if (!end_of_line) { |
| 182 udata = end_of_line = end; |
| 183 } else { |
| 184 udata = end_of_line + 1; |
| 185 } |
| 186 |
| 187 bool is_printable = true; |
| 188 |
| 189 // If we are in unprintable mode, we need to see a line of at least |
| 190 // kMinPrintableLine characters before we'll switch back. |
| 191 const ptrdiff_t kMinPrintableLine = 4; |
| 192 if (consecutive_unprintable && ((end_of_line - line) < kMinPrintableLine)) { |
| 193 is_printable = false; |
| 194 } else { |
| 195 // Determine if the line contains only whitespace and printable |
| 196 // characters. |
| 197 bool is_entirely_whitespace = true; |
| 198 for (const unsigned char* pos = line; pos < end_of_line; ++pos) { |
| 199 if (isspace(*pos)) |
| 200 continue; |
| 201 is_entirely_whitespace = false; |
| 202 if (!isprint(*pos)) { |
| 203 is_printable = false; |
| 204 break; |
| 205 } |
| 206 } |
| 207 // Treat an empty line following unprintable data as unprintable. |
| 208 if (consecutive_unprintable && is_entirely_whitespace) { |
| 209 is_printable = false; |
| 210 } |
| 211 } |
| 212 if (!is_printable) { |
| 213 consecutive_unprintable += (udata - line); |
| 214 continue; |
| 215 } |
| 216 // Print out the current line, but prefix with a count of prior unprintable |
| 217 // characters. |
| 218 if (consecutive_unprintable) { |
| 219 LOG_V(level) << label << direction << "## " << consecutive_unprintable |
| 220 << " consecutive unprintable ##"; |
| 221 consecutive_unprintable = 0; |
| 222 } |
| 223 // Strip off trailing whitespace. |
| 224 while ((end_of_line > line) && isspace(*(end_of_line-1))) { |
| 225 --end_of_line; |
| 226 } |
| 227 // Filter out any private data |
| 228 std::string substr(reinterpret_cast<const char*>(line), end_of_line - line); |
| 229 std::string::size_type pos_private = substr.find("Email"); |
| 230 if (pos_private == std::string::npos) { |
| 231 pos_private = substr.find("Passwd"); |
| 232 } |
| 233 if (pos_private == std::string::npos) { |
| 234 LOG_V(level) << label << direction << substr; |
| 235 } else { |
| 236 LOG_V(level) << label << direction << "## omitted for privacy ##"; |
| 237 } |
| 238 } |
| 239 |
| 240 if (state) { |
| 241 state->unprintable_count_[input] = consecutive_unprintable; |
| 242 } |
| 243 } |
| 244 |
| 245 } // namespace talk_base |
OLD | NEW |