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