blob: e28b2e104748526a22d7caff314893f4d26b9fad [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_) {
henrikg0de8ff42015-09-09 23:43:40 -0700198 rtc::LogMessage slow_log_warning(__FILE__, __LINE__, LS_WARNING);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000199 // If our warning is slow, we don't want to warn about it, because
200 // that would lead to inifinite recursion. So, give a really big
201 // number for the delay threshold.
202 slow_log_warning.warn_slow_logs_delay_ = UINT_MAX;
203 slow_log_warning.stream() << "Slow log: took " << delay << "ms to write "
204 << str.size() << " bytes.";
205 }
206}
207
208uint32 LogMessage::LogStartTime() {
209 static const uint32 g_start = Time();
210 return g_start;
211}
212
213uint32 LogMessage::WallClockStartTime() {
214 static const uint32 g_start_wallclock = time(NULL);
215 return g_start_wallclock;
216}
217
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000218void LogMessage::LogThreads(bool on) {
219 thread_ = on;
220}
221
222void LogMessage::LogTimestamps(bool on) {
223 timestamp_ = on;
224}
225
Tommi0eefb4d2015-05-23 09:54:07 +0200226void LogMessage::LogToDebug(LoggingSeverity min_sev) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000227 dbg_sev_ = min_sev;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000228 CritScope cs(&crit_);
Tommi00aac5a2015-05-25 11:25:59 +0200229 UpdateMinLogSeverity();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000230}
231
andrew88703d72015-09-07 00:34:56 -0700232void LogMessage::SetLogToStderr(bool log_to_stderr) {
233 log_to_stderr_ = log_to_stderr;
234}
235
Tommi0eefb4d2015-05-23 09:54:07 +0200236int LogMessage::GetLogToStream(LogSink* stream) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000237 CritScope cs(&crit_);
Tommi0eefb4d2015-05-23 09:54:07 +0200238 LoggingSeverity sev = LS_NONE;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000239 for (StreamList::iterator it = streams_.begin(); it != streams_.end(); ++it) {
240 if (!stream || stream == it->first) {
andresp@webrtc.orgff689be2015-02-12 11:54:26 +0000241 sev = std::min(sev, it->second);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000242 }
243 }
244 return sev;
245}
246
Tommi0eefb4d2015-05-23 09:54:07 +0200247void LogMessage::AddLogToStream(LogSink* stream, LoggingSeverity min_sev) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000248 CritScope cs(&crit_);
249 streams_.push_back(std::make_pair(stream, min_sev));
250 UpdateMinLogSeverity();
251}
252
Tommi0eefb4d2015-05-23 09:54:07 +0200253void LogMessage::RemoveLogToStream(LogSink* stream) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000254 CritScope cs(&crit_);
255 for (StreamList::iterator it = streams_.begin(); it != streams_.end(); ++it) {
256 if (stream == it->first) {
257 streams_.erase(it);
258 break;
259 }
260 }
261 UpdateMinLogSeverity();
262}
263
Tommi0eefb4d2015-05-23 09:54:07 +0200264void LogMessage::ConfigureLogging(const char* params) {
265 LoggingSeverity current_level = LS_VERBOSE;
266 LoggingSeverity debug_level = GetLogToDebug();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000267
268 std::vector<std::string> tokens;
269 tokenize(params, ' ', &tokens);
270
Tommi0eefb4d2015-05-23 09:54:07 +0200271 for (const std::string& token : tokens) {
272 if (token.empty())
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000273 continue;
274
275 // Logging features
Tommi0eefb4d2015-05-23 09:54:07 +0200276 if (token == "tstamp") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000277 LogTimestamps();
Tommi0eefb4d2015-05-23 09:54:07 +0200278 } else if (token == "thread") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000279 LogThreads();
280
281 // Logging levels
Tommi0eefb4d2015-05-23 09:54:07 +0200282 } else if (token == "sensitive") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000283 current_level = LS_SENSITIVE;
Tommi0eefb4d2015-05-23 09:54:07 +0200284 } else if (token == "verbose") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000285 current_level = LS_VERBOSE;
Tommi0eefb4d2015-05-23 09:54:07 +0200286 } else if (token == "info") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000287 current_level = LS_INFO;
Tommi0eefb4d2015-05-23 09:54:07 +0200288 } else if (token == "warning") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000289 current_level = LS_WARNING;
Tommi0eefb4d2015-05-23 09:54:07 +0200290 } else if (token == "error") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000291 current_level = LS_ERROR;
Tommi0eefb4d2015-05-23 09:54:07 +0200292 } else if (token == "none") {
293 current_level = LS_NONE;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000294
295 // Logging targets
Tommi0eefb4d2015-05-23 09:54:07 +0200296 } else if (token == "debug") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000297 debug_level = current_level;
298 }
299 }
300
301#if defined(WEBRTC_WIN)
Tommi0eefb4d2015-05-23 09:54:07 +0200302 if ((LS_NONE != debug_level) && !::IsDebuggerPresent()) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000303 // First, attempt to attach to our parent's console... so if you invoke
304 // from the command line, we'll see the output there. Otherwise, create
305 // our own console window.
306 // Note: These methods fail if a console already exists, which is fine.
307 bool success = false;
308 typedef BOOL (WINAPI* PFN_AttachConsole)(DWORD);
309 if (HINSTANCE kernel32 = ::LoadLibrary(L"kernel32.dll")) {
310 // AttachConsole is defined on WinXP+.
311 if (PFN_AttachConsole attach_console = reinterpret_cast<PFN_AttachConsole>
312 (::GetProcAddress(kernel32, "AttachConsole"))) {
313 success = (FALSE != attach_console(ATTACH_PARENT_PROCESS));
314 }
315 ::FreeLibrary(kernel32);
316 }
317 if (!success) {
318 ::AllocConsole();
319 }
320 }
Tommi0eefb4d2015-05-23 09:54:07 +0200321#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000322
323 LogToDebug(debug_level);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000324}
325
Tommi00aac5a2015-05-25 11:25:59 +0200326void LogMessage::UpdateMinLogSeverity() EXCLUSIVE_LOCKS_REQUIRED(crit_) {
Tommi0eefb4d2015-05-23 09:54:07 +0200327 LoggingSeverity min_sev = dbg_sev_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000328 for (StreamList::iterator it = streams_.begin(); it != streams_.end(); ++it) {
andresp@webrtc.orgff689be2015-02-12 11:54:26 +0000329 min_sev = std::min(dbg_sev_, it->second);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000330 }
331 min_sev_ = min_sev;
332}
333
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000334void LogMessage::OutputToDebug(const std::string& str,
335 LoggingSeverity severity) {
andrew88703d72015-09-07 00:34:56 -0700336 bool log_to_stderr = log_to_stderr_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000337#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) && (!defined(DEBUG) || defined(NDEBUG))
338 // On the Mac, all stderr output goes to the Console log and causes clutter.
339 // So in opt builds, don't log to stderr unless the user specifically sets
340 // a preference to do so.
341 CFStringRef key = CFStringCreateWithCString(kCFAllocatorDefault,
342 "logToStdErr",
343 kCFStringEncodingUTF8);
344 CFStringRef domain = CFBundleGetIdentifier(CFBundleGetMainBundle());
345 if (key != NULL && domain != NULL) {
346 Boolean exists_and_is_valid;
347 Boolean should_log =
348 CFPreferencesGetAppBooleanValue(key, domain, &exists_and_is_valid);
349 // If the key doesn't exist or is invalid or is false, we will not log to
350 // stderr.
351 log_to_stderr = exists_and_is_valid && should_log;
352 }
353 if (key != NULL) {
354 CFRelease(key);
355 }
356#endif
357#if defined(WEBRTC_WIN)
358 // Always log to the debugger.
359 // Perhaps stderr should be controlled by a preference, as on Mac?
360 OutputDebugStringA(str.c_str());
361 if (log_to_stderr) {
362 // This handles dynamically allocated consoles, too.
363 if (HANDLE error_handle = ::GetStdHandle(STD_ERROR_HANDLE)) {
364 log_to_stderr = false;
365 DWORD written = 0;
366 ::WriteFile(error_handle, str.data(), static_cast<DWORD>(str.size()),
367 &written, 0);
368 }
369 }
Tommi0eefb4d2015-05-23 09:54:07 +0200370#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000371#if defined(WEBRTC_ANDROID)
372 // Android's logging facility uses severity to log messages but we
373 // need to map libjingle's severity levels to Android ones first.
374 // Also write to stderr which maybe available to executable started
375 // from the shell.
376 int prio;
377 switch (severity) {
378 case LS_SENSITIVE:
379 __android_log_write(ANDROID_LOG_INFO, kLibjingle, "SENSITIVE");
380 if (log_to_stderr) {
381 fprintf(stderr, "SENSITIVE");
382 fflush(stderr);
383 }
384 return;
385 case LS_VERBOSE:
386 prio = ANDROID_LOG_VERBOSE;
387 break;
388 case LS_INFO:
389 prio = ANDROID_LOG_INFO;
390 break;
391 case LS_WARNING:
392 prio = ANDROID_LOG_WARN;
393 break;
394 case LS_ERROR:
395 prio = ANDROID_LOG_ERROR;
396 break;
397 default:
398 prio = ANDROID_LOG_UNKNOWN;
399 }
400
401 int size = str.size();
402 int line = 0;
403 int idx = 0;
404 const int max_lines = size / kMaxLogLineSize + 1;
405 if (max_lines == 1) {
406 __android_log_print(prio, kLibjingle, "%.*s", size, str.c_str());
407 } else {
408 while (size > 0) {
409 const int len = std::min(size, kMaxLogLineSize);
410 // Use the size of the string in the format (str may have \0 in the
411 // middle).
412 __android_log_print(prio, kLibjingle, "[%d/%d] %.*s",
413 line + 1, max_lines,
414 len, str.c_str() + idx);
415 idx += len;
416 size -= len;
417 ++line;
418 }
419 }
420#endif // WEBRTC_ANDROID
421 if (log_to_stderr) {
422 fprintf(stderr, "%s", str.c_str());
423 fflush(stderr);
424 }
425}
426
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000427//////////////////////////////////////////////////////////////////////
428// Logging Helpers
429//////////////////////////////////////////////////////////////////////
430
431void LogMultiline(LoggingSeverity level, const char* label, bool input,
432 const void* data, size_t len, bool hex_mode,
433 LogMultilineState* state) {
434 if (!LOG_CHECK_LEVEL_V(level))
435 return;
436
437 const char * direction = (input ? " << " : " >> ");
438
439 // NULL data means to flush our count of unprintable characters.
440 if (!data) {
441 if (state && state->unprintable_count_[input]) {
442 LOG_V(level) << label << direction << "## "
443 << state->unprintable_count_[input]
444 << " consecutive unprintable ##";
445 state->unprintable_count_[input] = 0;
446 }
447 return;
448 }
449
450 // The ctype classification functions want unsigned chars.
451 const unsigned char* udata = static_cast<const unsigned char*>(data);
452
453 if (hex_mode) {
454 const size_t LINE_SIZE = 24;
455 char hex_line[LINE_SIZE * 9 / 4 + 2], asc_line[LINE_SIZE + 1];
456 while (len > 0) {
457 memset(asc_line, ' ', sizeof(asc_line));
458 memset(hex_line, ' ', sizeof(hex_line));
andresp@webrtc.orgff689be2015-02-12 11:54:26 +0000459 size_t line_len = std::min(len, LINE_SIZE);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000460 for (size_t i = 0; i < line_len; ++i) {
461 unsigned char ch = udata[i];
462 asc_line[i] = isprint(ch) ? ch : '.';
463 hex_line[i*2 + i/4] = hex_encode(ch >> 4);
464 hex_line[i*2 + i/4 + 1] = hex_encode(ch & 0xf);
465 }
466 asc_line[sizeof(asc_line)-1] = 0;
467 hex_line[sizeof(hex_line)-1] = 0;
468 LOG_V(level) << label << direction
469 << asc_line << " " << hex_line << " ";
470 udata += line_len;
471 len -= line_len;
472 }
473 return;
474 }
475
476 size_t consecutive_unprintable = state ? state->unprintable_count_[input] : 0;
477
478 const unsigned char* end = udata + len;
479 while (udata < end) {
480 const unsigned char* line = udata;
481 const unsigned char* end_of_line = strchrn<unsigned char>(udata,
482 end - udata,
483 '\n');
484 if (!end_of_line) {
485 udata = end_of_line = end;
486 } else {
487 udata = end_of_line + 1;
488 }
489
490 bool is_printable = true;
491
492 // If we are in unprintable mode, we need to see a line of at least
493 // kMinPrintableLine characters before we'll switch back.
494 const ptrdiff_t kMinPrintableLine = 4;
495 if (consecutive_unprintable && ((end_of_line - line) < kMinPrintableLine)) {
496 is_printable = false;
497 } else {
498 // Determine if the line contains only whitespace and printable
499 // characters.
500 bool is_entirely_whitespace = true;
501 for (const unsigned char* pos = line; pos < end_of_line; ++pos) {
502 if (isspace(*pos))
503 continue;
504 is_entirely_whitespace = false;
505 if (!isprint(*pos)) {
506 is_printable = false;
507 break;
508 }
509 }
510 // Treat an empty line following unprintable data as unprintable.
511 if (consecutive_unprintable && is_entirely_whitespace) {
512 is_printable = false;
513 }
514 }
515 if (!is_printable) {
516 consecutive_unprintable += (udata - line);
517 continue;
518 }
519 // Print out the current line, but prefix with a count of prior unprintable
520 // characters.
521 if (consecutive_unprintable) {
522 LOG_V(level) << label << direction << "## " << consecutive_unprintable
523 << " consecutive unprintable ##";
524 consecutive_unprintable = 0;
525 }
526 // Strip off trailing whitespace.
527 while ((end_of_line > line) && isspace(*(end_of_line-1))) {
528 --end_of_line;
529 }
530 // Filter out any private data
531 std::string substr(reinterpret_cast<const char*>(line), end_of_line - line);
532 std::string::size_type pos_private = substr.find("Email");
533 if (pos_private == std::string::npos) {
534 pos_private = substr.find("Passwd");
535 }
536 if (pos_private == std::string::npos) {
537 LOG_V(level) << label << direction << substr;
538 } else {
539 LOG_V(level) << label << direction << "## omitted for privacy ##";
540 }
541 }
542
543 if (state) {
544 state->unprintable_count_[input] = consecutive_unprintable;
545 }
546}
547
548//////////////////////////////////////////////////////////////////////
549
550} // namespace rtc