blob: 7e308c2693185cdf8b15863ef47b5544ae3468c6 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#if defined(WEBRTC_WIN)
Tommi23edcff2015-05-25 10:45:43 +020012#if !defined(WIN32_LEAN_AND_MEAN)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013#define WIN32_LEAN_AND_MEAN
Tommi23edcff2015-05-25 10:45:43 +020014#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000015#include <windows.h>
16#define snprintf _snprintf
17#undef ERROR // wingdi.h
18#endif
19
20#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS)
21#include <CoreServices/CoreServices.h>
22#elif defined(WEBRTC_ANDROID)
23#include <android/log.h>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000024// Android has a 1024 limit on log inputs. We use 60 chars as an
25// approx for the header/tag portion.
26// See android/system/core/liblog/logd_write.c
27static const int kMaxLogLineSize = 1024 - 60;
28#endif // WEBRTC_MAC && !defined(WEBRTC_IOS) || WEBRTC_ANDROID
29
jiayl66f0da22015-09-14 15:06:39 -070030static const char kLibjingle[] = "libjingle";
31
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000032#include <time.h>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000033#include <limits.h>
andresp@webrtc.orgff689be2015-02-12 11:54:26 +000034
35#include <algorithm>
36#include <iomanip>
37#include <ostream>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000038#include <vector>
39
40#include "webrtc/base/logging.h"
henrikaba35d052015-07-14 17:04:08 +020041#include "webrtc/base/platform_thread.h"
Tommi0eefb4d2015-05-23 09:54:07 +020042#include "webrtc/base/scoped_ptr.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000043#include "webrtc/base/stringencode.h"
44#include "webrtc/base/stringutils.h"
45#include "webrtc/base/timeutils.h"
46
47namespace rtc {
andrew88703d72015-09-07 00:34:56 -070048namespace {
49
50// Return the filename portion of the string (that following the last slash).
51const char* FilenameFromPath(const char* file) {
52 const char* end1 = ::strrchr(file, '/');
53 const char* end2 = ::strrchr(file, '\\');
54 if (!end1 && !end2)
55 return file;
56 else
57 return (end1 > end2) ? end1 + 1 : end2 + 1;
58}
59
60} // namespace
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000061
62/////////////////////////////////////////////////////////////////////////////
63// Constant Labels
64/////////////////////////////////////////////////////////////////////////////
65
andrew88703d72015-09-07 00:34:56 -070066const char* FindLabel(int value, const ConstantLabel entries[]) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000067 for (int i = 0; entries[i].label; ++i) {
68 if (value == entries[i].value) {
69 return entries[i].label;
70 }
71 }
72 return 0;
73}
74
andrew88703d72015-09-07 00:34:56 -070075std::string ErrorName(int err, const ConstantLabel* err_table) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000076 if (err == 0)
77 return "No error";
78
79 if (err_table != 0) {
andrew88703d72015-09-07 00:34:56 -070080 if (const char* value = FindLabel(err, err_table))
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000081 return value;
82 }
83
84 char buffer[16];
85 snprintf(buffer, sizeof(buffer), "0x%08x", err);
86 return buffer;
87}
88
89/////////////////////////////////////////////////////////////////////////////
90// LogMessage
91/////////////////////////////////////////////////////////////////////////////
92
Tommi0eefb4d2015-05-23 09:54:07 +020093// By default, release builds don't log, debug builds at info level
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000094#if _DEBUG
Tommi0eefb4d2015-05-23 09:54:07 +020095LoggingSeverity LogMessage::min_sev_ = LS_INFO;
96LoggingSeverity LogMessage::dbg_sev_ = LS_INFO;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000097#else // !_DEBUG
Tommi0eefb4d2015-05-23 09:54:07 +020098LoggingSeverity LogMessage::min_sev_ = LS_NONE;
99LoggingSeverity LogMessage::dbg_sev_ = LS_NONE;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000100#endif // !_DEBUG
andrew88703d72015-09-07 00:34:56 -0700101bool LogMessage::log_to_stderr_ = true;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000102
103// Global lock for log subsystem, only needed to serialize access to streams_.
104CriticalSection LogMessage::crit_;
105
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000106// The list of logging streams currently configured.
107// Note: we explicitly do not clean this up, because of the uncertain ordering
108// of destructors at program exit. Let the person who sets the stream trigger
109// cleanup by setting to NULL, or let it leak (safe at program exit).
Tommi00aac5a2015-05-25 11:25:59 +0200110LogMessage::StreamList LogMessage::streams_ GUARDED_BY(LogMessage::crit_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000111
112// Boolean options default to false (0)
113bool LogMessage::thread_, LogMessage::timestamp_;
114
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000115LogMessage::LogMessage(const char* file, int line, LoggingSeverity sev,
116 LogErrorContext err_ctx, int err, const char* module)
117 : severity_(sev),
jiayl66f0da22015-09-14 15:06:39 -0700118 tag_(kLibjingle),
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000119 warn_slow_logs_delay_(WARN_SLOW_LOGS_DELAY) {
120 if (timestamp_) {
121 uint32 time = TimeSince(LogStartTime());
122 // Also ensure WallClockStartTime is initialized, so that it matches
123 // LogStartTime.
124 WallClockStartTime();
125 print_stream_ << "[" << std::setfill('0') << std::setw(3) << (time / 1000)
126 << ":" << std::setw(3) << (time % 1000) << std::setfill(' ')
127 << "] ";
128 }
129
130 if (thread_) {
henrikaba35d052015-07-14 17:04:08 +0200131 PlatformThreadId id = CurrentThreadId();
132 print_stream_ << "[" << std::dec << id << "] ";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000133 }
134
Alex Glaznevebed24d2015-09-15 11:05:24 -0700135 if (file != NULL)
136 print_stream_ << "(" << FilenameFromPath(file) << ":" << line << "): ";
andrew88703d72015-09-07 00:34:56 -0700137
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000138 if (err_ctx != ERRCTX_NONE) {
139 std::ostringstream tmp;
140 tmp << "[0x" << std::setfill('0') << std::hex << std::setw(8) << err << "]";
141 switch (err_ctx) {
142 case ERRCTX_ERRNO:
143 tmp << " " << strerror(err);
144 break;
145#if WEBRTC_WIN
146 case ERRCTX_HRESULT: {
147 char msgbuf[256];
148 DWORD flags = FORMAT_MESSAGE_FROM_SYSTEM;
149 HMODULE hmod = GetModuleHandleA(module);
150 if (hmod)
151 flags |= FORMAT_MESSAGE_FROM_HMODULE;
152 if (DWORD len = FormatMessageA(
153 flags, hmod, err,
154 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
155 msgbuf, sizeof(msgbuf) / sizeof(msgbuf[0]), NULL)) {
156 while ((len > 0) &&
157 isspace(static_cast<unsigned char>(msgbuf[len-1]))) {
158 msgbuf[--len] = 0;
159 }
160 tmp << " " << msgbuf;
161 }
162 break;
163 }
Tommi0eefb4d2015-05-23 09:54:07 +0200164#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000165#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS)
166 case ERRCTX_OSSTATUS: {
167 tmp << " " << nonnull(GetMacOSStatusErrorString(err), "Unknown error");
168 if (const char* desc = GetMacOSStatusCommentString(err)) {
169 tmp << ": " << desc;
170 }
171 break;
172 }
173#endif // WEBRTC_MAC && !defined(WEBRTC_IOS)
174 default:
175 break;
176 }
177 extra_ = tmp.str();
178 }
179}
180
jiayl66f0da22015-09-14 15:06:39 -0700181LogMessage::LogMessage(const char* file,
182 int line,
183 LoggingSeverity sev,
184 const std::string& tag)
185 : LogMessage(file, line, sev, ERRCTX_NONE, 0 /* err */, NULL /* module */) {
186 tag_ = tag;
187}
188
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000189LogMessage::~LogMessage() {
190 if (!extra_.empty())
191 print_stream_ << " : " << extra_;
192 print_stream_ << std::endl;
193
194 const std::string& str = print_stream_.str();
195 if (severity_ >= dbg_sev_) {
jiayl66f0da22015-09-14 15:06:39 -0700196 OutputToDebug(str, severity_, tag_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000197 }
198
199 uint32 before = Time();
200 // Must lock streams_ before accessing
201 CritScope cs(&crit_);
202 for (StreamList::iterator it = streams_.begin(); it != streams_.end(); ++it) {
203 if (severity_ >= it->second) {
Tommi0eefb4d2015-05-23 09:54:07 +0200204 it->first->OnLogMessage(str);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000205 }
206 }
207 uint32 delay = TimeSince(before);
208 if (delay >= warn_slow_logs_delay_) {
henrikg38419432015-09-16 06:33:20 -0700209 rtc::LogMessage slow_log_warning(__FILE__, __LINE__, LS_WARNING);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000210 // If our warning is slow, we don't want to warn about it, because
211 // that would lead to inifinite recursion. So, give a really big
212 // number for the delay threshold.
213 slow_log_warning.warn_slow_logs_delay_ = UINT_MAX;
214 slow_log_warning.stream() << "Slow log: took " << delay << "ms to write "
215 << str.size() << " bytes.";
216 }
217}
218
219uint32 LogMessage::LogStartTime() {
220 static const uint32 g_start = Time();
221 return g_start;
222}
223
224uint32 LogMessage::WallClockStartTime() {
225 static const uint32 g_start_wallclock = time(NULL);
226 return g_start_wallclock;
227}
228
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000229void LogMessage::LogThreads(bool on) {
230 thread_ = on;
231}
232
233void LogMessage::LogTimestamps(bool on) {
234 timestamp_ = on;
235}
236
Tommi0eefb4d2015-05-23 09:54:07 +0200237void LogMessage::LogToDebug(LoggingSeverity min_sev) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000238 dbg_sev_ = min_sev;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000239 CritScope cs(&crit_);
Tommi00aac5a2015-05-25 11:25:59 +0200240 UpdateMinLogSeverity();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000241}
242
andrew88703d72015-09-07 00:34:56 -0700243void LogMessage::SetLogToStderr(bool log_to_stderr) {
244 log_to_stderr_ = log_to_stderr;
245}
246
Tommi0eefb4d2015-05-23 09:54:07 +0200247int LogMessage::GetLogToStream(LogSink* stream) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000248 CritScope cs(&crit_);
Tommi0eefb4d2015-05-23 09:54:07 +0200249 LoggingSeverity sev = LS_NONE;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000250 for (StreamList::iterator it = streams_.begin(); it != streams_.end(); ++it) {
251 if (!stream || stream == it->first) {
andresp@webrtc.orgff689be2015-02-12 11:54:26 +0000252 sev = std::min(sev, it->second);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000253 }
254 }
255 return sev;
256}
257
Tommi0eefb4d2015-05-23 09:54:07 +0200258void LogMessage::AddLogToStream(LogSink* stream, LoggingSeverity min_sev) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000259 CritScope cs(&crit_);
260 streams_.push_back(std::make_pair(stream, min_sev));
261 UpdateMinLogSeverity();
262}
263
Tommi0eefb4d2015-05-23 09:54:07 +0200264void LogMessage::RemoveLogToStream(LogSink* stream) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000265 CritScope cs(&crit_);
266 for (StreamList::iterator it = streams_.begin(); it != streams_.end(); ++it) {
267 if (stream == it->first) {
268 streams_.erase(it);
269 break;
270 }
271 }
272 UpdateMinLogSeverity();
273}
274
Tommi0eefb4d2015-05-23 09:54:07 +0200275void LogMessage::ConfigureLogging(const char* params) {
276 LoggingSeverity current_level = LS_VERBOSE;
277 LoggingSeverity debug_level = GetLogToDebug();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000278
279 std::vector<std::string> tokens;
280 tokenize(params, ' ', &tokens);
281
Tommi0eefb4d2015-05-23 09:54:07 +0200282 for (const std::string& token : tokens) {
283 if (token.empty())
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000284 continue;
285
286 // Logging features
Tommi0eefb4d2015-05-23 09:54:07 +0200287 if (token == "tstamp") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000288 LogTimestamps();
Tommi0eefb4d2015-05-23 09:54:07 +0200289 } else if (token == "thread") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000290 LogThreads();
291
292 // Logging levels
Tommi0eefb4d2015-05-23 09:54:07 +0200293 } else if (token == "sensitive") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000294 current_level = LS_SENSITIVE;
Tommi0eefb4d2015-05-23 09:54:07 +0200295 } else if (token == "verbose") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000296 current_level = LS_VERBOSE;
Tommi0eefb4d2015-05-23 09:54:07 +0200297 } else if (token == "info") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000298 current_level = LS_INFO;
Tommi0eefb4d2015-05-23 09:54:07 +0200299 } else if (token == "warning") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000300 current_level = LS_WARNING;
Tommi0eefb4d2015-05-23 09:54:07 +0200301 } else if (token == "error") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000302 current_level = LS_ERROR;
Tommi0eefb4d2015-05-23 09:54:07 +0200303 } else if (token == "none") {
304 current_level = LS_NONE;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000305
306 // Logging targets
Tommi0eefb4d2015-05-23 09:54:07 +0200307 } else if (token == "debug") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000308 debug_level = current_level;
309 }
310 }
311
312#if defined(WEBRTC_WIN)
Tommi0eefb4d2015-05-23 09:54:07 +0200313 if ((LS_NONE != debug_level) && !::IsDebuggerPresent()) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000314 // First, attempt to attach to our parent's console... so if you invoke
315 // from the command line, we'll see the output there. Otherwise, create
316 // our own console window.
317 // Note: These methods fail if a console already exists, which is fine.
318 bool success = false;
319 typedef BOOL (WINAPI* PFN_AttachConsole)(DWORD);
320 if (HINSTANCE kernel32 = ::LoadLibrary(L"kernel32.dll")) {
321 // AttachConsole is defined on WinXP+.
322 if (PFN_AttachConsole attach_console = reinterpret_cast<PFN_AttachConsole>
323 (::GetProcAddress(kernel32, "AttachConsole"))) {
324 success = (FALSE != attach_console(ATTACH_PARENT_PROCESS));
325 }
326 ::FreeLibrary(kernel32);
327 }
328 if (!success) {
329 ::AllocConsole();
330 }
331 }
Tommi0eefb4d2015-05-23 09:54:07 +0200332#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000333
334 LogToDebug(debug_level);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000335}
336
Tommi00aac5a2015-05-25 11:25:59 +0200337void LogMessage::UpdateMinLogSeverity() EXCLUSIVE_LOCKS_REQUIRED(crit_) {
Tommi0eefb4d2015-05-23 09:54:07 +0200338 LoggingSeverity min_sev = dbg_sev_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000339 for (StreamList::iterator it = streams_.begin(); it != streams_.end(); ++it) {
andresp@webrtc.orgff689be2015-02-12 11:54:26 +0000340 min_sev = std::min(dbg_sev_, it->second);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000341 }
342 min_sev_ = min_sev;
343}
344
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000345void LogMessage::OutputToDebug(const std::string& str,
jiayl66f0da22015-09-14 15:06:39 -0700346 LoggingSeverity severity,
347 const std::string& tag) {
andrew88703d72015-09-07 00:34:56 -0700348 bool log_to_stderr = log_to_stderr_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000349#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) && (!defined(DEBUG) || defined(NDEBUG))
350 // On the Mac, all stderr output goes to the Console log and causes clutter.
351 // So in opt builds, don't log to stderr unless the user specifically sets
352 // a preference to do so.
353 CFStringRef key = CFStringCreateWithCString(kCFAllocatorDefault,
354 "logToStdErr",
355 kCFStringEncodingUTF8);
356 CFStringRef domain = CFBundleGetIdentifier(CFBundleGetMainBundle());
357 if (key != NULL && domain != NULL) {
358 Boolean exists_and_is_valid;
359 Boolean should_log =
360 CFPreferencesGetAppBooleanValue(key, domain, &exists_and_is_valid);
361 // If the key doesn't exist or is invalid or is false, we will not log to
362 // stderr.
363 log_to_stderr = exists_and_is_valid && should_log;
364 }
365 if (key != NULL) {
366 CFRelease(key);
367 }
368#endif
369#if defined(WEBRTC_WIN)
370 // Always log to the debugger.
371 // Perhaps stderr should be controlled by a preference, as on Mac?
372 OutputDebugStringA(str.c_str());
373 if (log_to_stderr) {
374 // This handles dynamically allocated consoles, too.
375 if (HANDLE error_handle = ::GetStdHandle(STD_ERROR_HANDLE)) {
376 log_to_stderr = false;
377 DWORD written = 0;
378 ::WriteFile(error_handle, str.data(), static_cast<DWORD>(str.size()),
379 &written, 0);
380 }
381 }
Tommi0eefb4d2015-05-23 09:54:07 +0200382#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000383#if defined(WEBRTC_ANDROID)
384 // Android's logging facility uses severity to log messages but we
385 // need to map libjingle's severity levels to Android ones first.
386 // Also write to stderr which maybe available to executable started
387 // from the shell.
388 int prio;
389 switch (severity) {
390 case LS_SENSITIVE:
jiayl66f0da22015-09-14 15:06:39 -0700391 __android_log_write(ANDROID_LOG_INFO, tag.c_str(), "SENSITIVE");
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000392 if (log_to_stderr) {
393 fprintf(stderr, "SENSITIVE");
394 fflush(stderr);
395 }
396 return;
397 case LS_VERBOSE:
398 prio = ANDROID_LOG_VERBOSE;
399 break;
400 case LS_INFO:
401 prio = ANDROID_LOG_INFO;
402 break;
403 case LS_WARNING:
404 prio = ANDROID_LOG_WARN;
405 break;
406 case LS_ERROR:
407 prio = ANDROID_LOG_ERROR;
408 break;
409 default:
410 prio = ANDROID_LOG_UNKNOWN;
411 }
412
413 int size = str.size();
414 int line = 0;
415 int idx = 0;
416 const int max_lines = size / kMaxLogLineSize + 1;
417 if (max_lines == 1) {
jiayl66f0da22015-09-14 15:06:39 -0700418 __android_log_print(prio, tag.c_str(), "%.*s", size, str.c_str());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000419 } else {
420 while (size > 0) {
421 const int len = std::min(size, kMaxLogLineSize);
422 // Use the size of the string in the format (str may have \0 in the
423 // middle).
jiayl66f0da22015-09-14 15:06:39 -0700424 __android_log_print(prio, tag.c_str(), "[%d/%d] %.*s",
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000425 line + 1, max_lines,
426 len, str.c_str() + idx);
427 idx += len;
428 size -= len;
429 ++line;
430 }
431 }
432#endif // WEBRTC_ANDROID
433 if (log_to_stderr) {
434 fprintf(stderr, "%s", str.c_str());
435 fflush(stderr);
436 }
437}
438
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000439//////////////////////////////////////////////////////////////////////
440// Logging Helpers
441//////////////////////////////////////////////////////////////////////
442
443void LogMultiline(LoggingSeverity level, const char* label, bool input,
444 const void* data, size_t len, bool hex_mode,
445 LogMultilineState* state) {
446 if (!LOG_CHECK_LEVEL_V(level))
447 return;
448
449 const char * direction = (input ? " << " : " >> ");
450
451 // NULL data means to flush our count of unprintable characters.
452 if (!data) {
453 if (state && state->unprintable_count_[input]) {
454 LOG_V(level) << label << direction << "## "
455 << state->unprintable_count_[input]
456 << " consecutive unprintable ##";
457 state->unprintable_count_[input] = 0;
458 }
459 return;
460 }
461
462 // The ctype classification functions want unsigned chars.
463 const unsigned char* udata = static_cast<const unsigned char*>(data);
464
465 if (hex_mode) {
466 const size_t LINE_SIZE = 24;
467 char hex_line[LINE_SIZE * 9 / 4 + 2], asc_line[LINE_SIZE + 1];
468 while (len > 0) {
469 memset(asc_line, ' ', sizeof(asc_line));
470 memset(hex_line, ' ', sizeof(hex_line));
andresp@webrtc.orgff689be2015-02-12 11:54:26 +0000471 size_t line_len = std::min(len, LINE_SIZE);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000472 for (size_t i = 0; i < line_len; ++i) {
473 unsigned char ch = udata[i];
474 asc_line[i] = isprint(ch) ? ch : '.';
475 hex_line[i*2 + i/4] = hex_encode(ch >> 4);
476 hex_line[i*2 + i/4 + 1] = hex_encode(ch & 0xf);
477 }
478 asc_line[sizeof(asc_line)-1] = 0;
479 hex_line[sizeof(hex_line)-1] = 0;
480 LOG_V(level) << label << direction
481 << asc_line << " " << hex_line << " ";
482 udata += line_len;
483 len -= line_len;
484 }
485 return;
486 }
487
488 size_t consecutive_unprintable = state ? state->unprintable_count_[input] : 0;
489
490 const unsigned char* end = udata + len;
491 while (udata < end) {
492 const unsigned char* line = udata;
493 const unsigned char* end_of_line = strchrn<unsigned char>(udata,
494 end - udata,
495 '\n');
496 if (!end_of_line) {
497 udata = end_of_line = end;
498 } else {
499 udata = end_of_line + 1;
500 }
501
502 bool is_printable = true;
503
504 // If we are in unprintable mode, we need to see a line of at least
505 // kMinPrintableLine characters before we'll switch back.
506 const ptrdiff_t kMinPrintableLine = 4;
507 if (consecutive_unprintable && ((end_of_line - line) < kMinPrintableLine)) {
508 is_printable = false;
509 } else {
510 // Determine if the line contains only whitespace and printable
511 // characters.
512 bool is_entirely_whitespace = true;
513 for (const unsigned char* pos = line; pos < end_of_line; ++pos) {
514 if (isspace(*pos))
515 continue;
516 is_entirely_whitespace = false;
517 if (!isprint(*pos)) {
518 is_printable = false;
519 break;
520 }
521 }
522 // Treat an empty line following unprintable data as unprintable.
523 if (consecutive_unprintable && is_entirely_whitespace) {
524 is_printable = false;
525 }
526 }
527 if (!is_printable) {
528 consecutive_unprintable += (udata - line);
529 continue;
530 }
531 // Print out the current line, but prefix with a count of prior unprintable
532 // characters.
533 if (consecutive_unprintable) {
534 LOG_V(level) << label << direction << "## " << consecutive_unprintable
535 << " consecutive unprintable ##";
536 consecutive_unprintable = 0;
537 }
538 // Strip off trailing whitespace.
539 while ((end_of_line > line) && isspace(*(end_of_line-1))) {
540 --end_of_line;
541 }
542 // Filter out any private data
543 std::string substr(reinterpret_cast<const char*>(line), end_of_line - line);
544 std::string::size_type pos_private = substr.find("Email");
545 if (pos_private == std::string::npos) {
546 pos_private = substr.find("Passwd");
547 }
548 if (pos_private == std::string::npos) {
549 LOG_V(level) << label << direction << substr;
550 } else {
551 LOG_V(level) << label << direction << "## omitted for privacy ##";
552 }
553 }
554
555 if (state) {
556 state->unprintable_count_[input] = consecutive_unprintable;
557 }
558}
559
560//////////////////////////////////////////////////////////////////////
561
562} // namespace rtc