|
OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
Sergey Ulanov
2012/01/11 19:54:48
2012
hellner
2012/01/11 23:53:31
Done.
| |
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 std::string GenerateExtra(LogErrorContext err_ctx, int err, | |
Sergey Ulanov
2012/01/11 19:54:48
nit: mark as static or move to nameless namespace
hellner
2012/01/11 23:53:31
Done.
| |
53 const char* module) { | |
54 if (err_ctx != ERRCTX_NONE) { | |
55 std::ostringstream tmp; | |
56 tmp << ": "; | |
57 tmp << "[0x" << std::setfill('0') << std::hex << std::setw(8) << err << "]"; | |
58 switch (err_ctx) { | |
59 case ERRCTX_ERRNO: | |
60 tmp << " " << strerror(err); | |
61 break; | |
62 #if OS_WIN32 | |
Sergey Ulanov
2012/01/11 19:54:48
It is OS_WIN, not OS_WIN32. The canonical form is
hellner
2012/01/11 23:53:31
Done.
| |
63 case ERRCTX_HRESULT: { | |
64 char msgbuf[256]; | |
65 DWORD flags = FORMAT_MESSAGE_FROM_SYSTEM; | |
66 HMODULE hmod = GetModuleHandleA(module); | |
67 if (hmod) | |
68 flags |= FORMAT_MESSAGE_FROM_HMODULE; | |
69 if (DWORD len = FormatMessageA( | |
70 flags, hmod, err, | |
71 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), | |
72 msgbuf, sizeof(msgbuf) / sizeof(msgbuf[0]), NULL)) { | |
73 while ((len > 0) && | |
74 isspace(static_cast<unsigned char>(msgbuf[len-1]))) { | |
75 msgbuf[--len] = 0; | |
76 } | |
77 tmp << " " << msgbuf; | |
78 } | |
79 break; | |
80 } | |
81 #endif // OS_WIN32 | |
82 #if OS_MACOSX | |
Sergey Ulanov
2012/01/11 19:54:48
same here
hellner
2012/01/11 23:53:31
Done.
| |
83 case ERRCTX_OSSTATUS: { | |
84 tmp << " " << nonnull(GetMacOSStatusErrorString(err), "Unknown error"); | |
85 if (const char* desc = GetMacOSStatusCommentString(err)) { | |
86 tmp << ": " << desc; | |
87 } | |
88 break; | |
89 } | |
90 #endif // OS_MACOSX | |
91 default: | |
92 break; | |
93 } | |
94 return tmp.str(); | |
95 } | |
96 return ""; | |
97 } | |
98 | |
99 LogEHelper::LogEHelper(const char* file, | |
100 int line, | |
101 LoggingSeverity severity, | |
102 LogErrorContext err_ctx, | |
103 int err, | |
104 const char* module) | |
105 : file_name_(file), | |
106 line_(line), | |
107 severity_(severity) { | |
108 extra_ = GenerateExtra(err_ctx, err, module); | |
109 } | |
110 | |
111 LogEHelper::~LogEHelper() { | |
112 print_stream_ << extra_; | |
113 const std::string& str = print_stream_.str(); | |
114 LOG_E_BASE(file_name_.c_str(), line_, severity_) << str; | |
115 } | |
116 | |
117 // Note: this function is a copy from the overriden libjingle implementation. | |
118 void LogMultiline(LoggingSeverity level, const char* label, bool input, | |
119 const void* data, size_t len, bool hex_mode, | |
120 LogMultilineState* state) { | |
121 if (!LOG_CHECK_LEVEL_V(level)) | |
122 return; | |
123 | |
124 const char * direction = (input ? " << " : " >> "); | |
125 | |
126 // NULL data means to flush our count of unprintable characters. | |
127 if (!data) { | |
128 if (state && state->unprintable_count_[input]) { | |
129 LOG_V(level) << label << direction << "## " | |
130 << state->unprintable_count_[input] | |
131 << " consecutive unprintable ##"; | |
132 state->unprintable_count_[input] = 0; | |
133 } | |
134 return; | |
135 } | |
136 | |
137 // The ctype classification functions want unsigned chars. | |
138 const unsigned char* udata = static_cast<const unsigned char*>(data); | |
139 | |
140 if (hex_mode) { | |
141 const size_t LINE_SIZE = 24; | |
142 char hex_line[LINE_SIZE * 9 / 4 + 2], asc_line[LINE_SIZE + 1]; | |
143 while (len > 0) { | |
144 memset(asc_line, ' ', sizeof(asc_line)); | |
145 memset(hex_line, ' ', sizeof(hex_line)); | |
146 size_t line_len = _min(len, LINE_SIZE); | |
147 for (size_t i = 0; i < line_len; ++i) { | |
148 unsigned char ch = udata[i]; | |
149 asc_line[i] = isprint(ch) ? ch : '.'; | |
150 hex_line[i*2 + i/4] = hex_encode(ch >> 4); | |
151 hex_line[i*2 + i/4 + 1] = hex_encode(ch & 0xf); | |
152 } | |
153 asc_line[sizeof(asc_line)-1] = 0; | |
154 hex_line[sizeof(hex_line)-1] = 0; | |
155 LOG_V(level) << label << direction | |
156 << asc_line << " " << hex_line << " "; | |
157 udata += line_len; | |
158 len -= line_len; | |
159 } | |
160 return; | |
161 } | |
162 | |
163 size_t consecutive_unprintable = state ? state->unprintable_count_[input] : 0; | |
164 | |
165 const unsigned char* end = udata + len; | |
166 while (udata < end) { | |
167 const unsigned char* line = udata; | |
168 const unsigned char* end_of_line = strchrn<unsigned char>(udata, | |
169 end - udata, | |
170 '\n'); | |
171 if (!end_of_line) { | |
172 udata = end_of_line = end; | |
173 } else { | |
174 udata = end_of_line + 1; | |
175 } | |
176 | |
177 bool is_printable = true; | |
178 | |
179 // If we are in unprintable mode, we need to see a line of at least | |
180 // kMinPrintableLine characters before we'll switch back. | |
181 const ptrdiff_t kMinPrintableLine = 4; | |
182 if (consecutive_unprintable && ((end_of_line - line) < kMinPrintableLine)) { | |
183 is_printable = false; | |
184 } else { | |
185 // Determine if the line contains only whitespace and printable | |
186 // characters. | |
187 bool is_entirely_whitespace = true; | |
188 for (const unsigned char* pos = line; pos < end_of_line; ++pos) { | |
189 if (isspace(*pos)) | |
190 continue; | |
191 is_entirely_whitespace = false; | |
192 if (!isprint(*pos)) { | |
193 is_printable = false; | |
194 break; | |
195 } | |
196 } | |
197 // Treat an empty line following unprintable data as unprintable. | |
198 if (consecutive_unprintable && is_entirely_whitespace) { | |
199 is_printable = false; | |
200 } | |
201 } | |
202 if (!is_printable) { | |
203 consecutive_unprintable += (udata - line); | |
204 continue; | |
205 } | |
206 // Print out the current line, but prefix with a count of prior unprintable | |
207 // characters. | |
208 if (consecutive_unprintable) { | |
209 LOG_V(level) << label << direction << "## " << consecutive_unprintable | |
210 << " consecutive unprintable ##"; | |
211 consecutive_unprintable = 0; | |
212 } | |
213 // Strip off trailing whitespace. | |
214 while ((end_of_line > line) && isspace(*(end_of_line-1))) { | |
215 --end_of_line; | |
216 } | |
217 // Filter out any private data | |
218 std::string substr(reinterpret_cast<const char*>(line), end_of_line - line); | |
219 std::string::size_type pos_private = substr.find("Email"); | |
220 if (pos_private == std::string::npos) { | |
221 pos_private = substr.find("Passwd"); | |
222 } | |
223 if (pos_private == std::string::npos) { | |
224 LOG_V(level) << label << direction << substr; | |
225 } else { | |
226 LOG_V(level) << label << direction << "## omitted for privacy ##"; | |
227 } | |
228 } | |
229 | |
230 if (state) { | |
231 state->unprintable_count_[input] = consecutive_unprintable; | |
232 } | |
233 } | |
234 | |
235 } // namespace talk_base | |
OLD | NEW |