blob: e2ee1150638f1afa6c18ded8d8dfc6050de666a8 [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>
24static const char kLibjingle[] = "libjingle";
25// Android has a 1024 limit on log inputs. We use 60 chars as an
26// approx for the header/tag portion.
27// See android/system/core/liblog/logd_write.c
28static const int kMaxLogLineSize = 1024 - 60;
29#endif // WEBRTC_MAC && !defined(WEBRTC_IOS) || WEBRTC_ANDROID
30
31#include <time.h>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000032#include <limits.h>
andresp@webrtc.orgff689be2015-02-12 11:54:26 +000033
34#include <algorithm>
35#include <iomanip>
36#include <ostream>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000037#include <vector>
38
39#include "webrtc/base/logging.h"
henrikaba35d052015-07-14 17:04:08 +020040#include "webrtc/base/platform_thread.h"
Tommi0eefb4d2015-05-23 09:54:07 +020041#include "webrtc/base/scoped_ptr.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000042#include "webrtc/base/stringencode.h"
43#include "webrtc/base/stringutils.h"
44#include "webrtc/base/timeutils.h"
45
46namespace rtc {
andrew88703d72015-09-07 00:34:56 -070047namespace {
48
49// Return the filename portion of the string (that following the last slash).
50const char* FilenameFromPath(const char* file) {
51 const char* end1 = ::strrchr(file, '/');
52 const char* end2 = ::strrchr(file, '\\');
53 if (!end1 && !end2)
54 return file;
55 else
56 return (end1 > end2) ? end1 + 1 : end2 + 1;
57}
58
59} // namespace
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000060
61/////////////////////////////////////////////////////////////////////////////
62// Constant Labels
63/////////////////////////////////////////////////////////////////////////////
64
andrew88703d72015-09-07 00:34:56 -070065const char* FindLabel(int value, const ConstantLabel entries[]) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000066 for (int i = 0; entries[i].label; ++i) {
67 if (value == entries[i].value) {
68 return entries[i].label;
69 }
70 }
71 return 0;
72}
73
andrew88703d72015-09-07 00:34:56 -070074std::string ErrorName(int err, const ConstantLabel* err_table) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000075 if (err == 0)
76 return "No error";
77
78 if (err_table != 0) {
andrew88703d72015-09-07 00:34:56 -070079 if (const char* value = FindLabel(err, err_table))
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000080 return value;
81 }
82
83 char buffer[16];
84 snprintf(buffer, sizeof(buffer), "0x%08x", err);
85 return buffer;
86}
87
88/////////////////////////////////////////////////////////////////////////////
89// LogMessage
90/////////////////////////////////////////////////////////////////////////////
91
Tommi0eefb4d2015-05-23 09:54:07 +020092// By default, release builds don't log, debug builds at info level
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000093#if _DEBUG
Tommi0eefb4d2015-05-23 09:54:07 +020094LoggingSeverity LogMessage::min_sev_ = LS_INFO;
95LoggingSeverity LogMessage::dbg_sev_ = LS_INFO;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000096#else // !_DEBUG
Tommi0eefb4d2015-05-23 09:54:07 +020097LoggingSeverity LogMessage::min_sev_ = LS_NONE;
98LoggingSeverity LogMessage::dbg_sev_ = LS_NONE;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000099#endif // !_DEBUG
andrew88703d72015-09-07 00:34:56 -0700100bool LogMessage::log_to_stderr_ = true;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000101
102// Global lock for log subsystem, only needed to serialize access to streams_.
103CriticalSection LogMessage::crit_;
104
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000105// The list of logging streams currently configured.
106// Note: we explicitly do not clean this up, because of the uncertain ordering
107// of destructors at program exit. Let the person who sets the stream trigger
108// cleanup by setting to NULL, or let it leak (safe at program exit).
Tommi00aac5a2015-05-25 11:25:59 +0200109LogMessage::StreamList LogMessage::streams_ GUARDED_BY(LogMessage::crit_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000110
111// Boolean options default to false (0)
112bool LogMessage::thread_, LogMessage::timestamp_;
113
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000114LogMessage::LogMessage(const char* file, int line, LoggingSeverity sev,
115 LogErrorContext err_ctx, int err, const char* module)
116 : severity_(sev),
117 warn_slow_logs_delay_(WARN_SLOW_LOGS_DELAY) {
118 if (timestamp_) {
119 uint32 time = TimeSince(LogStartTime());
120 // Also ensure WallClockStartTime is initialized, so that it matches
121 // LogStartTime.
122 WallClockStartTime();
123 print_stream_ << "[" << std::setfill('0') << std::setw(3) << (time / 1000)
124 << ":" << std::setw(3) << (time % 1000) << std::setfill(' ')
125 << "] ";
126 }
127
128 if (thread_) {
henrikaba35d052015-07-14 17:04:08 +0200129 PlatformThreadId id = CurrentThreadId();
130 print_stream_ << "[" << std::dec << id << "] ";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000131 }
132
andrew88703d72015-09-07 00:34:56 -0700133 print_stream_ << "(" << FilenameFromPath(file) << ":" << line << "): ";
134
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000135 if (err_ctx != ERRCTX_NONE) {
136 std::ostringstream tmp;
137 tmp << "[0x" << std::setfill('0') << std::hex << std::setw(8) << err << "]";
138 switch (err_ctx) {
139 case ERRCTX_ERRNO:
140 tmp << " " << strerror(err);
141 break;
142#if WEBRTC_WIN
143 case ERRCTX_HRESULT: {
144 char msgbuf[256];
145 DWORD flags = FORMAT_MESSAGE_FROM_SYSTEM;
146 HMODULE hmod = GetModuleHandleA(module);
147 if (hmod)
148 flags |= FORMAT_MESSAGE_FROM_HMODULE;
149 if (DWORD len = FormatMessageA(
150 flags, hmod, err,
151 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
152 msgbuf, sizeof(msgbuf) / sizeof(msgbuf[0]), NULL)) {
153 while ((len > 0) &&
154 isspace(static_cast<unsigned char>(msgbuf[len-1]))) {
155 msgbuf[--len] = 0;
156 }
157 tmp << " " << msgbuf;
158 }
159 break;
160 }
Tommi0eefb4d2015-05-23 09:54:07 +0200161#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000162#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS)
163 case ERRCTX_OSSTATUS: {
164 tmp << " " << nonnull(GetMacOSStatusErrorString(err), "Unknown error");
165 if (const char* desc = GetMacOSStatusCommentString(err)) {
166 tmp << ": " << desc;
167 }
168 break;
169 }
170#endif // WEBRTC_MAC && !defined(WEBRTC_IOS)
171 default:
172 break;
173 }
174 extra_ = tmp.str();
175 }
176}
177
178LogMessage::~LogMessage() {
179 if (!extra_.empty())
180 print_stream_ << " : " << extra_;
181 print_stream_ << std::endl;
182
183 const std::string& str = print_stream_.str();
184 if (severity_ >= dbg_sev_) {
185 OutputToDebug(str, severity_);
186 }
187
188 uint32 before = Time();
189 // Must lock streams_ before accessing
190 CritScope cs(&crit_);
191 for (StreamList::iterator it = streams_.begin(); it != streams_.end(); ++it) {
192 if (severity_ >= it->second) {
Tommi0eefb4d2015-05-23 09:54:07 +0200193 it->first->OnLogMessage(str);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000194 }
195 }
196 uint32 delay = TimeSince(before);
197 if (delay >= warn_slow_logs_delay_) {
tommi9a78d222015-09-10 01:41:55 -0700198 LogMessage slow_log_warning =
199 rtc::LogMessage(__FILE__, __LINE__, LS_WARNING);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000200 // If our warning is slow, we don't want to warn about it, because
201 // that would lead to inifinite recursion. So, give a really big
202 // number for the delay threshold.
203 slow_log_warning.warn_slow_logs_delay_ = UINT_MAX;
204 slow_log_warning.stream() << "Slow log: took " << delay << "ms to write "
205 << str.size() << " bytes.";
206 }
207}
208
209uint32 LogMessage::LogStartTime() {
210 static const uint32 g_start = Time();
211 return g_start;
212}
213
214uint32 LogMessage::WallClockStartTime() {
215 static const uint32 g_start_wallclock = time(NULL);
216 return g_start_wallclock;
217}
218
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000219void LogMessage::LogThreads(bool on) {
220 thread_ = on;
221}
222
223void LogMessage::LogTimestamps(bool on) {
224 timestamp_ = on;
225}
226
Tommi0eefb4d2015-05-23 09:54:07 +0200227void LogMessage::LogToDebug(LoggingSeverity min_sev) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000228 dbg_sev_ = min_sev;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000229 CritScope cs(&crit_);
Tommi00aac5a2015-05-25 11:25:59 +0200230 UpdateMinLogSeverity();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000231}
232
andrew88703d72015-09-07 00:34:56 -0700233void LogMessage::SetLogToStderr(bool log_to_stderr) {
234 log_to_stderr_ = log_to_stderr;
235}
236
Tommi0eefb4d2015-05-23 09:54:07 +0200237int LogMessage::GetLogToStream(LogSink* stream) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000238 CritScope cs(&crit_);
Tommi0eefb4d2015-05-23 09:54:07 +0200239 LoggingSeverity sev = LS_NONE;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000240 for (StreamList::iterator it = streams_.begin(); it != streams_.end(); ++it) {
241 if (!stream || stream == it->first) {
andresp@webrtc.orgff689be2015-02-12 11:54:26 +0000242 sev = std::min(sev, it->second);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000243 }
244 }
245 return sev;
246}
247
Tommi0eefb4d2015-05-23 09:54:07 +0200248void LogMessage::AddLogToStream(LogSink* stream, LoggingSeverity min_sev) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000249 CritScope cs(&crit_);
250 streams_.push_back(std::make_pair(stream, min_sev));
251 UpdateMinLogSeverity();
252}
253
Tommi0eefb4d2015-05-23 09:54:07 +0200254void LogMessage::RemoveLogToStream(LogSink* stream) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000255 CritScope cs(&crit_);
256 for (StreamList::iterator it = streams_.begin(); it != streams_.end(); ++it) {
257 if (stream == it->first) {
258 streams_.erase(it);
259 break;
260 }
261 }
262 UpdateMinLogSeverity();
263}
264
Tommi0eefb4d2015-05-23 09:54:07 +0200265void LogMessage::ConfigureLogging(const char* params) {
266 LoggingSeverity current_level = LS_VERBOSE;
267 LoggingSeverity debug_level = GetLogToDebug();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000268
269 std::vector<std::string> tokens;
270 tokenize(params, ' ', &tokens);
271
Tommi0eefb4d2015-05-23 09:54:07 +0200272 for (const std::string& token : tokens) {
273 if (token.empty())
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000274 continue;
275
276 // Logging features
Tommi0eefb4d2015-05-23 09:54:07 +0200277 if (token == "tstamp") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000278 LogTimestamps();
Tommi0eefb4d2015-05-23 09:54:07 +0200279 } else if (token == "thread") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000280 LogThreads();
281
282 // Logging levels
Tommi0eefb4d2015-05-23 09:54:07 +0200283 } else if (token == "sensitive") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000284 current_level = LS_SENSITIVE;
Tommi0eefb4d2015-05-23 09:54:07 +0200285 } else if (token == "verbose") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000286 current_level = LS_VERBOSE;
Tommi0eefb4d2015-05-23 09:54:07 +0200287 } else if (token == "info") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000288 current_level = LS_INFO;
Tommi0eefb4d2015-05-23 09:54:07 +0200289 } else if (token == "warning") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000290 current_level = LS_WARNING;
Tommi0eefb4d2015-05-23 09:54:07 +0200291 } else if (token == "error") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000292 current_level = LS_ERROR;
Tommi0eefb4d2015-05-23 09:54:07 +0200293 } else if (token == "none") {
294 current_level = LS_NONE;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000295
296 // Logging targets
Tommi0eefb4d2015-05-23 09:54:07 +0200297 } else if (token == "debug") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000298 debug_level = current_level;
299 }
300 }
301
302#if defined(WEBRTC_WIN)
Tommi0eefb4d2015-05-23 09:54:07 +0200303 if ((LS_NONE != debug_level) && !::IsDebuggerPresent()) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000304 // First, attempt to attach to our parent's console... so if you invoke
305 // from the command line, we'll see the output there. Otherwise, create
306 // our own console window.
307 // Note: These methods fail if a console already exists, which is fine.
308 bool success = false;
309 typedef BOOL (WINAPI* PFN_AttachConsole)(DWORD);
310 if (HINSTANCE kernel32 = ::LoadLibrary(L"kernel32.dll")) {
311 // AttachConsole is defined on WinXP+.
312 if (PFN_AttachConsole attach_console = reinterpret_cast<PFN_AttachConsole>
313 (::GetProcAddress(kernel32, "AttachConsole"))) {
314 success = (FALSE != attach_console(ATTACH_PARENT_PROCESS));
315 }
316 ::FreeLibrary(kernel32);
317 }
318 if (!success) {
319 ::AllocConsole();
320 }
321 }
Tommi0eefb4d2015-05-23 09:54:07 +0200322#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000323
324 LogToDebug(debug_level);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000325}
326
Tommi00aac5a2015-05-25 11:25:59 +0200327void LogMessage::UpdateMinLogSeverity() EXCLUSIVE_LOCKS_REQUIRED(crit_) {
Tommi0eefb4d2015-05-23 09:54:07 +0200328 LoggingSeverity min_sev = dbg_sev_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000329 for (StreamList::iterator it = streams_.begin(); it != streams_.end(); ++it) {
andresp@webrtc.orgff689be2015-02-12 11:54:26 +0000330 min_sev = std::min(dbg_sev_, it->second);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000331 }
332 min_sev_ = min_sev;
333}
334
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000335void LogMessage::OutputToDebug(const std::string& str,
336 LoggingSeverity severity) {
andrew88703d72015-09-07 00:34:56 -0700337 bool log_to_stderr = log_to_stderr_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000338#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) && (!defined(DEBUG) || defined(NDEBUG))
339 // On the Mac, all stderr output goes to the Console log and causes clutter.
340 // So in opt builds, don't log to stderr unless the user specifically sets
341 // a preference to do so.
342 CFStringRef key = CFStringCreateWithCString(kCFAllocatorDefault,
343 "logToStdErr",
344 kCFStringEncodingUTF8);
345 CFStringRef domain = CFBundleGetIdentifier(CFBundleGetMainBundle());
346 if (key != NULL && domain != NULL) {
347 Boolean exists_and_is_valid;
348 Boolean should_log =
349 CFPreferencesGetAppBooleanValue(key, domain, &exists_and_is_valid);
350 // If the key doesn't exist or is invalid or is false, we will not log to
351 // stderr.
352 log_to_stderr = exists_and_is_valid && should_log;
353 }
354 if (key != NULL) {
355 CFRelease(key);
356 }
357#endif
358#if defined(WEBRTC_WIN)
359 // Always log to the debugger.
360 // Perhaps stderr should be controlled by a preference, as on Mac?
361 OutputDebugStringA(str.c_str());
362 if (log_to_stderr) {
363 // This handles dynamically allocated consoles, too.
364 if (HANDLE error_handle = ::GetStdHandle(STD_ERROR_HANDLE)) {
365 log_to_stderr = false;
366 DWORD written = 0;
367 ::WriteFile(error_handle, str.data(), static_cast<DWORD>(str.size()),
368 &written, 0);
369 }
370 }
Tommi0eefb4d2015-05-23 09:54:07 +0200371#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000372#if defined(WEBRTC_ANDROID)
373 // Android's logging facility uses severity to log messages but we
374 // need to map libjingle's severity levels to Android ones first.
375 // Also write to stderr which maybe available to executable started
376 // from the shell.
377 int prio;
378 switch (severity) {
379 case LS_SENSITIVE:
380 __android_log_write(ANDROID_LOG_INFO, kLibjingle, "SENSITIVE");
381 if (log_to_stderr) {
382 fprintf(stderr, "SENSITIVE");
383 fflush(stderr);
384 }
385 return;
386 case LS_VERBOSE:
387 prio = ANDROID_LOG_VERBOSE;
388 break;
389 case LS_INFO:
390 prio = ANDROID_LOG_INFO;
391 break;
392 case LS_WARNING:
393 prio = ANDROID_LOG_WARN;
394 break;
395 case LS_ERROR:
396 prio = ANDROID_LOG_ERROR;
397 break;
398 default:
399 prio = ANDROID_LOG_UNKNOWN;
400 }
401
402 int size = str.size();
403 int line = 0;
404 int idx = 0;
405 const int max_lines = size / kMaxLogLineSize + 1;
406 if (max_lines == 1) {
407 __android_log_print(prio, kLibjingle, "%.*s", size, str.c_str());
408 } else {
409 while (size > 0) {
410 const int len = std::min(size, kMaxLogLineSize);
411 // Use the size of the string in the format (str may have \0 in the
412 // middle).
413 __android_log_print(prio, kLibjingle, "[%d/%d] %.*s",
414 line + 1, max_lines,
415 len, str.c_str() + idx);
416 idx += len;
417 size -= len;
418 ++line;
419 }
420 }
421#endif // WEBRTC_ANDROID
422 if (log_to_stderr) {
423 fprintf(stderr, "%s", str.c_str());
424 fflush(stderr);
425 }
426}
427
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000428//////////////////////////////////////////////////////////////////////
429// Logging Helpers
430//////////////////////////////////////////////////////////////////////
431
432void LogMultiline(LoggingSeverity level, const char* label, bool input,
433 const void* data, size_t len, bool hex_mode,
434 LogMultilineState* state) {
435 if (!LOG_CHECK_LEVEL_V(level))
436 return;
437
438 const char * direction = (input ? " << " : " >> ");
439
440 // NULL data means to flush our count of unprintable characters.
441 if (!data) {
442 if (state && state->unprintable_count_[input]) {
443 LOG_V(level) << label << direction << "## "
444 << state->unprintable_count_[input]
445 << " consecutive unprintable ##";
446 state->unprintable_count_[input] = 0;
447 }
448 return;
449 }
450
451 // The ctype classification functions want unsigned chars.
452 const unsigned char* udata = static_cast<const unsigned char*>(data);
453
454 if (hex_mode) {
455 const size_t LINE_SIZE = 24;
456 char hex_line[LINE_SIZE * 9 / 4 + 2], asc_line[LINE_SIZE + 1];
457 while (len > 0) {
458 memset(asc_line, ' ', sizeof(asc_line));
459 memset(hex_line, ' ', sizeof(hex_line));
andresp@webrtc.orgff689be2015-02-12 11:54:26 +0000460 size_t line_len = std::min(len, LINE_SIZE);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000461 for (size_t i = 0; i < line_len; ++i) {
462 unsigned char ch = udata[i];
463 asc_line[i] = isprint(ch) ? ch : '.';
464 hex_line[i*2 + i/4] = hex_encode(ch >> 4);
465 hex_line[i*2 + i/4 + 1] = hex_encode(ch & 0xf);
466 }
467 asc_line[sizeof(asc_line)-1] = 0;
468 hex_line[sizeof(hex_line)-1] = 0;
469 LOG_V(level) << label << direction
470 << asc_line << " " << hex_line << " ";
471 udata += line_len;
472 len -= line_len;
473 }
474 return;
475 }
476
477 size_t consecutive_unprintable = state ? state->unprintable_count_[input] : 0;
478
479 const unsigned char* end = udata + len;
480 while (udata < end) {
481 const unsigned char* line = udata;
482 const unsigned char* end_of_line = strchrn<unsigned char>(udata,
483 end - udata,
484 '\n');
485 if (!end_of_line) {
486 udata = end_of_line = end;
487 } else {
488 udata = end_of_line + 1;
489 }
490
491 bool is_printable = true;
492
493 // If we are in unprintable mode, we need to see a line of at least
494 // kMinPrintableLine characters before we'll switch back.
495 const ptrdiff_t kMinPrintableLine = 4;
496 if (consecutive_unprintable && ((end_of_line - line) < kMinPrintableLine)) {
497 is_printable = false;
498 } else {
499 // Determine if the line contains only whitespace and printable
500 // characters.
501 bool is_entirely_whitespace = true;
502 for (const unsigned char* pos = line; pos < end_of_line; ++pos) {
503 if (isspace(*pos))
504 continue;
505 is_entirely_whitespace = false;
506 if (!isprint(*pos)) {
507 is_printable = false;
508 break;
509 }
510 }
511 // Treat an empty line following unprintable data as unprintable.
512 if (consecutive_unprintable && is_entirely_whitespace) {
513 is_printable = false;
514 }
515 }
516 if (!is_printable) {
517 consecutive_unprintable += (udata - line);
518 continue;
519 }
520 // Print out the current line, but prefix with a count of prior unprintable
521 // characters.
522 if (consecutive_unprintable) {
523 LOG_V(level) << label << direction << "## " << consecutive_unprintable
524 << " consecutive unprintable ##";
525 consecutive_unprintable = 0;
526 }
527 // Strip off trailing whitespace.
528 while ((end_of_line > line) && isspace(*(end_of_line-1))) {
529 --end_of_line;
530 }
531 // Filter out any private data
532 std::string substr(reinterpret_cast<const char*>(line), end_of_line - line);
533 std::string::size_type pos_private = substr.find("Email");
534 if (pos_private == std::string::npos) {
535 pos_private = substr.find("Passwd");
536 }
537 if (pos_private == std::string::npos) {
538 LOG_V(level) << label << direction << substr;
539 } else {
540 LOG_V(level) << label << direction << "## omitted for privacy ##";
541 }
542 }
543
544 if (state) {
545 state->unprintable_count_[input] = consecutive_unprintable;
546 }
547}
548
549//////////////////////////////////////////////////////////////////////
550
551} // namespace rtc