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