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