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