blob: 62656689f91afb3dc23c41cb20c3705efd3b2e67 [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>
conceptgenesis3f705622016-01-30 14:40:44 -080016#if _MSC_VER < 1900
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000017#define snprintf _snprintf
conceptgenesis3f705622016-01-30 14:40:44 -080018#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000019#undef ERROR // wingdi.h
20#endif
21
22#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS)
23#include <CoreServices/CoreServices.h>
24#elif defined(WEBRTC_ANDROID)
25#include <android/log.h>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000026// Android has a 1024 limit on log inputs. We use 60 chars as an
27// approx for the header/tag portion.
28// See android/system/core/liblog/logd_write.c
29static const int kMaxLogLineSize = 1024 - 60;
30#endif // WEBRTC_MAC && !defined(WEBRTC_IOS) || WEBRTC_ANDROID
31
jiayl66f0da22015-09-14 15:06:39 -070032static const char kLibjingle[] = "libjingle";
33
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000034#include <time.h>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000035#include <limits.h>
andresp@webrtc.orgff689be2015-02-12 11:54:26 +000036
37#include <algorithm>
38#include <iomanip>
39#include <ostream>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000040#include <vector>
41
Peter Boström225789d2015-10-23 15:20:56 +020042#include "webrtc/base/criticalsection.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000043#include "webrtc/base/logging.h"
henrikaba35d052015-07-14 17:04:08 +020044#include "webrtc/base/platform_thread.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000045#include "webrtc/base/stringencode.h"
46#include "webrtc/base/stringutils.h"
47#include "webrtc/base/timeutils.h"
48
49namespace rtc {
andrew88703d72015-09-07 00:34:56 -070050namespace {
51
52// Return the filename portion of the string (that following the last slash).
53const char* FilenameFromPath(const char* file) {
54 const char* end1 = ::strrchr(file, '/');
55 const char* end2 = ::strrchr(file, '\\');
56 if (!end1 && !end2)
57 return file;
58 else
59 return (end1 > end2) ? end1 + 1 : end2 + 1;
60}
61
62} // namespace
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000063
64/////////////////////////////////////////////////////////////////////////////
65// Constant Labels
66/////////////////////////////////////////////////////////////////////////////
67
andrew88703d72015-09-07 00:34:56 -070068const char* FindLabel(int value, const ConstantLabel entries[]) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000069 for (int i = 0; entries[i].label; ++i) {
70 if (value == entries[i].value) {
71 return entries[i].label;
72 }
73 }
74 return 0;
75}
76
andrew88703d72015-09-07 00:34:56 -070077std::string ErrorName(int err, const ConstantLabel* err_table) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000078 if (err == 0)
79 return "No error";
80
81 if (err_table != 0) {
andrew88703d72015-09-07 00:34:56 -070082 if (const char* value = FindLabel(err, err_table))
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000083 return value;
84 }
85
86 char buffer[16];
87 snprintf(buffer, sizeof(buffer), "0x%08x", err);
88 return buffer;
89}
90
91/////////////////////////////////////////////////////////////////////////////
92// LogMessage
93/////////////////////////////////////////////////////////////////////////////
94
Tommi0eefb4d2015-05-23 09:54:07 +020095// By default, release builds don't log, debug builds at info level
tfarinaa41ab932015-10-30 16:08:48 -070096#if !defined(NDEBUG)
Tommi0eefb4d2015-05-23 09:54:07 +020097LoggingSeverity LogMessage::min_sev_ = LS_INFO;
98LoggingSeverity LogMessage::dbg_sev_ = LS_INFO;
tfarinaa41ab932015-10-30 16:08:48 -070099#else
Tommi0eefb4d2015-05-23 09:54:07 +0200100LoggingSeverity LogMessage::min_sev_ = LS_NONE;
101LoggingSeverity LogMessage::dbg_sev_ = LS_NONE;
tfarinaa41ab932015-10-30 16:08:48 -0700102#endif
andrew88703d72015-09-07 00:34:56 -0700103bool LogMessage::log_to_stderr_ = true;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000104
Peter Boström225789d2015-10-23 15:20:56 +0200105namespace {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000106// Global lock for log subsystem, only needed to serialize access to streams_.
Peter Boström225789d2015-10-23 15:20:56 +0200107CriticalSection g_log_crit;
108} // namespace
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000109
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000110// The list of logging streams currently configured.
111// Note: we explicitly do not clean this up, because of the uncertain ordering
112// of destructors at program exit. Let the person who sets the stream trigger
113// cleanup by setting to NULL, or let it leak (safe at program exit).
Peter Boström225789d2015-10-23 15:20:56 +0200114LogMessage::StreamList LogMessage::streams_ GUARDED_BY(g_log_crit);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000115
116// Boolean options default to false (0)
117bool LogMessage::thread_, LogMessage::timestamp_;
118
Peter Boström225789d2015-10-23 15:20:56 +0200119LogMessage::LogMessage(const char* file,
120 int line,
121 LoggingSeverity sev,
122 LogErrorContext err_ctx,
123 int err,
124 const char* module)
125 : severity_(sev), tag_(kLibjingle) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000126 if (timestamp_) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200127 uint32_t time = TimeSince(LogStartTime());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000128 // Also ensure WallClockStartTime is initialized, so that it matches
129 // LogStartTime.
130 WallClockStartTime();
131 print_stream_ << "[" << std::setfill('0') << std::setw(3) << (time / 1000)
132 << ":" << std::setw(3) << (time % 1000) << std::setfill(' ')
133 << "] ";
134 }
135
136 if (thread_) {
henrikaba35d052015-07-14 17:04:08 +0200137 PlatformThreadId id = CurrentThreadId();
138 print_stream_ << "[" << std::dec << id << "] ";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000139 }
140
Alex Glaznevebed24d2015-09-15 11:05:24 -0700141 if (file != NULL)
142 print_stream_ << "(" << FilenameFromPath(file) << ":" << line << "): ";
andrew88703d72015-09-07 00:34:56 -0700143
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000144 if (err_ctx != ERRCTX_NONE) {
145 std::ostringstream tmp;
146 tmp << "[0x" << std::setfill('0') << std::hex << std::setw(8) << err << "]";
147 switch (err_ctx) {
148 case ERRCTX_ERRNO:
149 tmp << " " << strerror(err);
150 break;
151#if WEBRTC_WIN
152 case ERRCTX_HRESULT: {
153 char msgbuf[256];
154 DWORD flags = FORMAT_MESSAGE_FROM_SYSTEM;
155 HMODULE hmod = GetModuleHandleA(module);
156 if (hmod)
157 flags |= FORMAT_MESSAGE_FROM_HMODULE;
158 if (DWORD len = FormatMessageA(
159 flags, hmod, err,
160 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
161 msgbuf, sizeof(msgbuf) / sizeof(msgbuf[0]), NULL)) {
162 while ((len > 0) &&
163 isspace(static_cast<unsigned char>(msgbuf[len-1]))) {
164 msgbuf[--len] = 0;
165 }
166 tmp << " " << msgbuf;
167 }
168 break;
169 }
Tommi0eefb4d2015-05-23 09:54:07 +0200170#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000171#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS)
172 case ERRCTX_OSSTATUS: {
Tommi09ca02e2016-04-24 17:32:48 +0200173 std::string desc(DescriptionFromOSStatus(err));
174 tmp << " " << (desc.empty() ? "Unknown error" : desc.c_str());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000175 break;
176 }
177#endif // WEBRTC_MAC && !defined(WEBRTC_IOS)
178 default:
179 break;
180 }
181 extra_ = tmp.str();
182 }
183}
184
jiayl66f0da22015-09-14 15:06:39 -0700185LogMessage::LogMessage(const char* file,
186 int line,
187 LoggingSeverity sev,
188 const std::string& tag)
189 : LogMessage(file, line, sev, ERRCTX_NONE, 0 /* err */, NULL /* module */) {
190 tag_ = tag;
Jiayang Liue4ba6ce92015-09-21 15:49:24 -0700191 print_stream_ << tag << ": ";
jiayl66f0da22015-09-14 15:06:39 -0700192}
193
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000194LogMessage::~LogMessage() {
195 if (!extra_.empty())
196 print_stream_ << " : " << extra_;
197 print_stream_ << std::endl;
198
199 const std::string& str = print_stream_.str();
200 if (severity_ >= dbg_sev_) {
jiayl66f0da22015-09-14 15:06:39 -0700201 OutputToDebug(str, severity_, tag_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000202 }
203
Peter Boström225789d2015-10-23 15:20:56 +0200204 CritScope cs(&g_log_crit);
205 for (auto& kv : streams_) {
206 if (severity_ >= kv.second) {
207 kv.first->OnLogMessage(str);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000208 }
209 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000210}
211
Peter Boström0c4e06b2015-10-07 12:23:21 +0200212uint32_t LogMessage::LogStartTime() {
213 static const uint32_t g_start = Time();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000214 return g_start;
215}
216
Peter Boström0c4e06b2015-10-07 12:23:21 +0200217uint32_t LogMessage::WallClockStartTime() {
218 static const uint32_t g_start_wallclock = time(NULL);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000219 return g_start_wallclock;
220}
221
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000222void LogMessage::LogThreads(bool on) {
223 thread_ = on;
224}
225
226void LogMessage::LogTimestamps(bool on) {
227 timestamp_ = on;
228}
229
Tommi0eefb4d2015-05-23 09:54:07 +0200230void LogMessage::LogToDebug(LoggingSeverity min_sev) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000231 dbg_sev_ = min_sev;
Peter Boström225789d2015-10-23 15:20:56 +0200232 CritScope cs(&g_log_crit);
Tommi00aac5a2015-05-25 11:25:59 +0200233 UpdateMinLogSeverity();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000234}
235
andrew88703d72015-09-07 00:34:56 -0700236void LogMessage::SetLogToStderr(bool log_to_stderr) {
237 log_to_stderr_ = log_to_stderr;
238}
239
Tommi0eefb4d2015-05-23 09:54:07 +0200240int LogMessage::GetLogToStream(LogSink* stream) {
Peter Boström225789d2015-10-23 15:20:56 +0200241 CritScope cs(&g_log_crit);
Tommi0eefb4d2015-05-23 09:54:07 +0200242 LoggingSeverity sev = LS_NONE;
Peter Boström225789d2015-10-23 15:20:56 +0200243 for (auto& kv : streams_) {
244 if (!stream || stream == kv.first) {
245 sev = std::min(sev, kv.second);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000246 }
247 }
248 return sev;
249}
250
Tommi0eefb4d2015-05-23 09:54:07 +0200251void LogMessage::AddLogToStream(LogSink* stream, LoggingSeverity min_sev) {
Peter Boström225789d2015-10-23 15:20:56 +0200252 CritScope cs(&g_log_crit);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000253 streams_.push_back(std::make_pair(stream, min_sev));
254 UpdateMinLogSeverity();
255}
256
Tommi0eefb4d2015-05-23 09:54:07 +0200257void LogMessage::RemoveLogToStream(LogSink* stream) {
Peter Boström225789d2015-10-23 15:20:56 +0200258 CritScope cs(&g_log_crit);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000259 for (StreamList::iterator it = streams_.begin(); it != streams_.end(); ++it) {
260 if (stream == it->first) {
261 streams_.erase(it);
262 break;
263 }
264 }
265 UpdateMinLogSeverity();
266}
267
Tommi0eefb4d2015-05-23 09:54:07 +0200268void LogMessage::ConfigureLogging(const char* params) {
269 LoggingSeverity current_level = LS_VERBOSE;
270 LoggingSeverity debug_level = GetLogToDebug();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000271
272 std::vector<std::string> tokens;
273 tokenize(params, ' ', &tokens);
274
Tommi0eefb4d2015-05-23 09:54:07 +0200275 for (const std::string& token : tokens) {
276 if (token.empty())
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000277 continue;
278
279 // Logging features
Tommi0eefb4d2015-05-23 09:54:07 +0200280 if (token == "tstamp") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000281 LogTimestamps();
Tommi0eefb4d2015-05-23 09:54:07 +0200282 } else if (token == "thread") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000283 LogThreads();
284
285 // Logging levels
Tommi0eefb4d2015-05-23 09:54:07 +0200286 } else if (token == "sensitive") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000287 current_level = LS_SENSITIVE;
Tommi0eefb4d2015-05-23 09:54:07 +0200288 } else if (token == "verbose") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000289 current_level = LS_VERBOSE;
Tommi0eefb4d2015-05-23 09:54:07 +0200290 } else if (token == "info") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000291 current_level = LS_INFO;
Tommi0eefb4d2015-05-23 09:54:07 +0200292 } else if (token == "warning") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000293 current_level = LS_WARNING;
Tommi0eefb4d2015-05-23 09:54:07 +0200294 } else if (token == "error") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000295 current_level = LS_ERROR;
Tommi0eefb4d2015-05-23 09:54:07 +0200296 } else if (token == "none") {
297 current_level = LS_NONE;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000298
299 // Logging targets
Tommi0eefb4d2015-05-23 09:54:07 +0200300 } else if (token == "debug") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000301 debug_level = current_level;
302 }
303 }
304
305#if defined(WEBRTC_WIN)
Tommi0eefb4d2015-05-23 09:54:07 +0200306 if ((LS_NONE != debug_level) && !::IsDebuggerPresent()) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000307 // First, attempt to attach to our parent's console... so if you invoke
308 // from the command line, we'll see the output there. Otherwise, create
309 // our own console window.
310 // Note: These methods fail if a console already exists, which is fine.
311 bool success = false;
312 typedef BOOL (WINAPI* PFN_AttachConsole)(DWORD);
313 if (HINSTANCE kernel32 = ::LoadLibrary(L"kernel32.dll")) {
314 // AttachConsole is defined on WinXP+.
315 if (PFN_AttachConsole attach_console = reinterpret_cast<PFN_AttachConsole>
316 (::GetProcAddress(kernel32, "AttachConsole"))) {
317 success = (FALSE != attach_console(ATTACH_PARENT_PROCESS));
318 }
319 ::FreeLibrary(kernel32);
320 }
321 if (!success) {
322 ::AllocConsole();
323 }
324 }
Tommi0eefb4d2015-05-23 09:54:07 +0200325#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000326
327 LogToDebug(debug_level);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000328}
329
Peter Boström225789d2015-10-23 15:20:56 +0200330void LogMessage::UpdateMinLogSeverity() EXCLUSIVE_LOCKS_REQUIRED(g_log_crit) {
Tommi0eefb4d2015-05-23 09:54:07 +0200331 LoggingSeverity min_sev = dbg_sev_;
Peter Boström225789d2015-10-23 15:20:56 +0200332 for (auto& kv : streams_) {
333 min_sev = std::min(dbg_sev_, kv.second);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000334 }
335 min_sev_ = min_sev;
336}
337
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000338void LogMessage::OutputToDebug(const std::string& str,
jiayl66f0da22015-09-14 15:06:39 -0700339 LoggingSeverity severity,
340 const std::string& tag) {
andrew88703d72015-09-07 00:34:56 -0700341 bool log_to_stderr = log_to_stderr_;
tfarinaa41ab932015-10-30 16:08:48 -0700342#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) && defined(NDEBUG)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000343 // On the Mac, all stderr output goes to the Console log and causes clutter.
344 // So in opt builds, don't log to stderr unless the user specifically sets
345 // a preference to do so.
346 CFStringRef key = CFStringCreateWithCString(kCFAllocatorDefault,
347 "logToStdErr",
348 kCFStringEncodingUTF8);
349 CFStringRef domain = CFBundleGetIdentifier(CFBundleGetMainBundle());
350 if (key != NULL && domain != NULL) {
351 Boolean exists_and_is_valid;
352 Boolean should_log =
353 CFPreferencesGetAppBooleanValue(key, domain, &exists_and_is_valid);
354 // If the key doesn't exist or is invalid or is false, we will not log to
355 // stderr.
356 log_to_stderr = exists_and_is_valid && should_log;
357 }
358 if (key != NULL) {
359 CFRelease(key);
360 }
361#endif
362#if defined(WEBRTC_WIN)
363 // Always log to the debugger.
364 // Perhaps stderr should be controlled by a preference, as on Mac?
365 OutputDebugStringA(str.c_str());
366 if (log_to_stderr) {
367 // This handles dynamically allocated consoles, too.
368 if (HANDLE error_handle = ::GetStdHandle(STD_ERROR_HANDLE)) {
369 log_to_stderr = false;
370 DWORD written = 0;
371 ::WriteFile(error_handle, str.data(), static_cast<DWORD>(str.size()),
372 &written, 0);
373 }
374 }
Tommi0eefb4d2015-05-23 09:54:07 +0200375#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000376#if defined(WEBRTC_ANDROID)
377 // Android's logging facility uses severity to log messages but we
378 // need to map libjingle's severity levels to Android ones first.
379 // Also write to stderr which maybe available to executable started
380 // from the shell.
381 int prio;
382 switch (severity) {
383 case LS_SENSITIVE:
jiayl66f0da22015-09-14 15:06:39 -0700384 __android_log_write(ANDROID_LOG_INFO, tag.c_str(), "SENSITIVE");
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000385 if (log_to_stderr) {
386 fprintf(stderr, "SENSITIVE");
387 fflush(stderr);
388 }
389 return;
390 case LS_VERBOSE:
391 prio = ANDROID_LOG_VERBOSE;
392 break;
393 case LS_INFO:
394 prio = ANDROID_LOG_INFO;
395 break;
396 case LS_WARNING:
397 prio = ANDROID_LOG_WARN;
398 break;
399 case LS_ERROR:
400 prio = ANDROID_LOG_ERROR;
401 break;
402 default:
403 prio = ANDROID_LOG_UNKNOWN;
404 }
405
406 int size = str.size();
407 int line = 0;
408 int idx = 0;
409 const int max_lines = size / kMaxLogLineSize + 1;
410 if (max_lines == 1) {
jiayl66f0da22015-09-14 15:06:39 -0700411 __android_log_print(prio, tag.c_str(), "%.*s", size, str.c_str());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000412 } else {
413 while (size > 0) {
414 const int len = std::min(size, kMaxLogLineSize);
415 // Use the size of the string in the format (str may have \0 in the
416 // middle).
jiayl66f0da22015-09-14 15:06:39 -0700417 __android_log_print(prio, tag.c_str(), "[%d/%d] %.*s",
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000418 line + 1, max_lines,
419 len, str.c_str() + idx);
420 idx += len;
421 size -= len;
422 ++line;
423 }
424 }
425#endif // WEBRTC_ANDROID
426 if (log_to_stderr) {
427 fprintf(stderr, "%s", str.c_str());
428 fflush(stderr);
429 }
430}
431
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000432//////////////////////////////////////////////////////////////////////
433// Logging Helpers
434//////////////////////////////////////////////////////////////////////
435
436void LogMultiline(LoggingSeverity level, const char* label, bool input,
437 const void* data, size_t len, bool hex_mode,
438 LogMultilineState* state) {
439 if (!LOG_CHECK_LEVEL_V(level))
440 return;
441
442 const char * direction = (input ? " << " : " >> ");
443
444 // NULL data means to flush our count of unprintable characters.
445 if (!data) {
446 if (state && state->unprintable_count_[input]) {
447 LOG_V(level) << label << direction << "## "
448 << state->unprintable_count_[input]
449 << " consecutive unprintable ##";
450 state->unprintable_count_[input] = 0;
451 }
452 return;
453 }
454
455 // The ctype classification functions want unsigned chars.
456 const unsigned char* udata = static_cast<const unsigned char*>(data);
457
458 if (hex_mode) {
459 const size_t LINE_SIZE = 24;
460 char hex_line[LINE_SIZE * 9 / 4 + 2], asc_line[LINE_SIZE + 1];
461 while (len > 0) {
462 memset(asc_line, ' ', sizeof(asc_line));
463 memset(hex_line, ' ', sizeof(hex_line));
andresp@webrtc.orgff689be2015-02-12 11:54:26 +0000464 size_t line_len = std::min(len, LINE_SIZE);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000465 for (size_t i = 0; i < line_len; ++i) {
466 unsigned char ch = udata[i];
467 asc_line[i] = isprint(ch) ? ch : '.';
468 hex_line[i*2 + i/4] = hex_encode(ch >> 4);
469 hex_line[i*2 + i/4 + 1] = hex_encode(ch & 0xf);
470 }
471 asc_line[sizeof(asc_line)-1] = 0;
472 hex_line[sizeof(hex_line)-1] = 0;
473 LOG_V(level) << label << direction
474 << asc_line << " " << hex_line << " ";
475 udata += line_len;
476 len -= line_len;
477 }
478 return;
479 }
480
481 size_t consecutive_unprintable = state ? state->unprintable_count_[input] : 0;
482
483 const unsigned char* end = udata + len;
484 while (udata < end) {
485 const unsigned char* line = udata;
486 const unsigned char* end_of_line = strchrn<unsigned char>(udata,
487 end - udata,
488 '\n');
489 if (!end_of_line) {
490 udata = end_of_line = end;
491 } else {
492 udata = end_of_line + 1;
493 }
494
495 bool is_printable = true;
496
497 // If we are in unprintable mode, we need to see a line of at least
498 // kMinPrintableLine characters before we'll switch back.
499 const ptrdiff_t kMinPrintableLine = 4;
500 if (consecutive_unprintable && ((end_of_line - line) < kMinPrintableLine)) {
501 is_printable = false;
502 } else {
503 // Determine if the line contains only whitespace and printable
504 // characters.
505 bool is_entirely_whitespace = true;
506 for (const unsigned char* pos = line; pos < end_of_line; ++pos) {
507 if (isspace(*pos))
508 continue;
509 is_entirely_whitespace = false;
510 if (!isprint(*pos)) {
511 is_printable = false;
512 break;
513 }
514 }
515 // Treat an empty line following unprintable data as unprintable.
516 if (consecutive_unprintable && is_entirely_whitespace) {
517 is_printable = false;
518 }
519 }
520 if (!is_printable) {
521 consecutive_unprintable += (udata - line);
522 continue;
523 }
524 // Print out the current line, but prefix with a count of prior unprintable
525 // characters.
526 if (consecutive_unprintable) {
527 LOG_V(level) << label << direction << "## " << consecutive_unprintable
528 << " consecutive unprintable ##";
529 consecutive_unprintable = 0;
530 }
531 // Strip off trailing whitespace.
532 while ((end_of_line > line) && isspace(*(end_of_line-1))) {
533 --end_of_line;
534 }
535 // Filter out any private data
536 std::string substr(reinterpret_cast<const char*>(line), end_of_line - line);
537 std::string::size_type pos_private = substr.find("Email");
538 if (pos_private == std::string::npos) {
539 pos_private = substr.find("Passwd");
540 }
541 if (pos_private == std::string::npos) {
542 LOG_V(level) << label << direction << substr;
543 } else {
544 LOG_V(level) << label << direction << "## omitted for privacy ##";
545 }
546 }
547
548 if (state) {
549 state->unprintable_count_[input] = consecutive_unprintable;
550 }
551}
552
553//////////////////////////////////////////////////////////////////////
554
555} // namespace rtc