blob: 019a31623be1333f48383362ed9fe74c21619fb9 [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"
Tommi0eefb4d2015-05-23 09:54:07 +020045#include "webrtc/base/scoped_ptr.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000046#include "webrtc/base/stringencode.h"
47#include "webrtc/base/stringutils.h"
48#include "webrtc/base/timeutils.h"
49
50namespace rtc {
andrew88703d72015-09-07 00:34:56 -070051namespace {
52
53// Return the filename portion of the string (that following the last slash).
54const char* FilenameFromPath(const char* file) {
55 const char* end1 = ::strrchr(file, '/');
56 const char* end2 = ::strrchr(file, '\\');
57 if (!end1 && !end2)
58 return file;
59 else
60 return (end1 > end2) ? end1 + 1 : end2 + 1;
61}
62
63} // namespace
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000064
65/////////////////////////////////////////////////////////////////////////////
66// Constant Labels
67/////////////////////////////////////////////////////////////////////////////
68
andrew88703d72015-09-07 00:34:56 -070069const char* FindLabel(int value, const ConstantLabel entries[]) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000070 for (int i = 0; entries[i].label; ++i) {
71 if (value == entries[i].value) {
72 return entries[i].label;
73 }
74 }
75 return 0;
76}
77
andrew88703d72015-09-07 00:34:56 -070078std::string ErrorName(int err, const ConstantLabel* err_table) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000079 if (err == 0)
80 return "No error";
81
82 if (err_table != 0) {
andrew88703d72015-09-07 00:34:56 -070083 if (const char* value = FindLabel(err, err_table))
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000084 return value;
85 }
86
87 char buffer[16];
88 snprintf(buffer, sizeof(buffer), "0x%08x", err);
89 return buffer;
90}
91
92/////////////////////////////////////////////////////////////////////////////
93// LogMessage
94/////////////////////////////////////////////////////////////////////////////
95
Tommi0eefb4d2015-05-23 09:54:07 +020096// By default, release builds don't log, debug builds at info level
tfarinaa41ab932015-10-30 16:08:48 -070097#if !defined(NDEBUG)
Tommi0eefb4d2015-05-23 09:54:07 +020098LoggingSeverity LogMessage::min_sev_ = LS_INFO;
99LoggingSeverity LogMessage::dbg_sev_ = LS_INFO;
tfarinaa41ab932015-10-30 16:08:48 -0700100#else
Tommi0eefb4d2015-05-23 09:54:07 +0200101LoggingSeverity LogMessage::min_sev_ = LS_NONE;
102LoggingSeverity LogMessage::dbg_sev_ = LS_NONE;
tfarinaa41ab932015-10-30 16:08:48 -0700103#endif
andrew88703d72015-09-07 00:34:56 -0700104bool LogMessage::log_to_stderr_ = true;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000105
Peter Boström225789d2015-10-23 15:20:56 +0200106namespace {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000107// Global lock for log subsystem, only needed to serialize access to streams_.
Peter Boström225789d2015-10-23 15:20:56 +0200108CriticalSection g_log_crit;
109} // namespace
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000110
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000111// The list of logging streams currently configured.
112// Note: we explicitly do not clean this up, because of the uncertain ordering
113// of destructors at program exit. Let the person who sets the stream trigger
114// cleanup by setting to NULL, or let it leak (safe at program exit).
Peter Boström225789d2015-10-23 15:20:56 +0200115LogMessage::StreamList LogMessage::streams_ GUARDED_BY(g_log_crit);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000116
117// Boolean options default to false (0)
118bool LogMessage::thread_, LogMessage::timestamp_;
119
Peter Boström225789d2015-10-23 15:20:56 +0200120LogMessage::LogMessage(const char* file,
121 int line,
122 LoggingSeverity sev,
123 LogErrorContext err_ctx,
124 int err,
125 const char* module)
126 : severity_(sev), tag_(kLibjingle) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000127 if (timestamp_) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200128 uint32_t time = TimeSince(LogStartTime());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000129 // Also ensure WallClockStartTime is initialized, so that it matches
130 // LogStartTime.
131 WallClockStartTime();
132 print_stream_ << "[" << std::setfill('0') << std::setw(3) << (time / 1000)
133 << ":" << std::setw(3) << (time % 1000) << std::setfill(' ')
134 << "] ";
135 }
136
137 if (thread_) {
henrikaba35d052015-07-14 17:04:08 +0200138 PlatformThreadId id = CurrentThreadId();
139 print_stream_ << "[" << std::dec << id << "] ";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000140 }
141
Alex Glaznevebed24d2015-09-15 11:05:24 -0700142 if (file != NULL)
143 print_stream_ << "(" << FilenameFromPath(file) << ":" << line << "): ";
andrew88703d72015-09-07 00:34:56 -0700144
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000145 if (err_ctx != ERRCTX_NONE) {
146 std::ostringstream tmp;
147 tmp << "[0x" << std::setfill('0') << std::hex << std::setw(8) << err << "]";
148 switch (err_ctx) {
149 case ERRCTX_ERRNO:
150 tmp << " " << strerror(err);
151 break;
152#if WEBRTC_WIN
153 case ERRCTX_HRESULT: {
154 char msgbuf[256];
155 DWORD flags = FORMAT_MESSAGE_FROM_SYSTEM;
156 HMODULE hmod = GetModuleHandleA(module);
157 if (hmod)
158 flags |= FORMAT_MESSAGE_FROM_HMODULE;
159 if (DWORD len = FormatMessageA(
160 flags, hmod, err,
161 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
162 msgbuf, sizeof(msgbuf) / sizeof(msgbuf[0]), NULL)) {
163 while ((len > 0) &&
164 isspace(static_cast<unsigned char>(msgbuf[len-1]))) {
165 msgbuf[--len] = 0;
166 }
167 tmp << " " << msgbuf;
168 }
169 break;
170 }
Tommi0eefb4d2015-05-23 09:54:07 +0200171#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000172#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS)
173 case ERRCTX_OSSTATUS: {
174 tmp << " " << nonnull(GetMacOSStatusErrorString(err), "Unknown error");
175 if (const char* desc = GetMacOSStatusCommentString(err)) {
176 tmp << ": " << desc;
177 }
178 break;
179 }
180#endif // WEBRTC_MAC && !defined(WEBRTC_IOS)
181 default:
182 break;
183 }
184 extra_ = tmp.str();
185 }
186}
187
jiayl66f0da22015-09-14 15:06:39 -0700188LogMessage::LogMessage(const char* file,
189 int line,
190 LoggingSeverity sev,
191 const std::string& tag)
192 : LogMessage(file, line, sev, ERRCTX_NONE, 0 /* err */, NULL /* module */) {
193 tag_ = tag;
Jiayang Liue4ba6ce92015-09-21 15:49:24 -0700194 print_stream_ << tag << ": ";
jiayl66f0da22015-09-14 15:06:39 -0700195}
196
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000197LogMessage::~LogMessage() {
198 if (!extra_.empty())
199 print_stream_ << " : " << extra_;
200 print_stream_ << std::endl;
201
202 const std::string& str = print_stream_.str();
203 if (severity_ >= dbg_sev_) {
jiayl66f0da22015-09-14 15:06:39 -0700204 OutputToDebug(str, severity_, tag_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000205 }
206
Peter Boström225789d2015-10-23 15:20:56 +0200207 CritScope cs(&g_log_crit);
208 for (auto& kv : streams_) {
209 if (severity_ >= kv.second) {
210 kv.first->OnLogMessage(str);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000211 }
212 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000213}
214
Peter Boström0c4e06b2015-10-07 12:23:21 +0200215uint32_t LogMessage::LogStartTime() {
216 static const uint32_t g_start = Time();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000217 return g_start;
218}
219
Peter Boström0c4e06b2015-10-07 12:23:21 +0200220uint32_t LogMessage::WallClockStartTime() {
221 static const uint32_t g_start_wallclock = time(NULL);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000222 return g_start_wallclock;
223}
224
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000225void LogMessage::LogThreads(bool on) {
226 thread_ = on;
227}
228
229void LogMessage::LogTimestamps(bool on) {
230 timestamp_ = on;
231}
232
Tommi0eefb4d2015-05-23 09:54:07 +0200233void LogMessage::LogToDebug(LoggingSeverity min_sev) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000234 dbg_sev_ = min_sev;
Peter Boström225789d2015-10-23 15:20:56 +0200235 CritScope cs(&g_log_crit);
Tommi00aac5a2015-05-25 11:25:59 +0200236 UpdateMinLogSeverity();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000237}
238
andrew88703d72015-09-07 00:34:56 -0700239void LogMessage::SetLogToStderr(bool log_to_stderr) {
240 log_to_stderr_ = log_to_stderr;
241}
242
Tommi0eefb4d2015-05-23 09:54:07 +0200243int LogMessage::GetLogToStream(LogSink* stream) {
Peter Boström225789d2015-10-23 15:20:56 +0200244 CritScope cs(&g_log_crit);
Tommi0eefb4d2015-05-23 09:54:07 +0200245 LoggingSeverity sev = LS_NONE;
Peter Boström225789d2015-10-23 15:20:56 +0200246 for (auto& kv : streams_) {
247 if (!stream || stream == kv.first) {
248 sev = std::min(sev, kv.second);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000249 }
250 }
251 return sev;
252}
253
Tommi0eefb4d2015-05-23 09:54:07 +0200254void LogMessage::AddLogToStream(LogSink* stream, LoggingSeverity min_sev) {
Peter Boström225789d2015-10-23 15:20:56 +0200255 CritScope cs(&g_log_crit);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000256 streams_.push_back(std::make_pair(stream, min_sev));
257 UpdateMinLogSeverity();
258}
259
Tommi0eefb4d2015-05-23 09:54:07 +0200260void LogMessage::RemoveLogToStream(LogSink* stream) {
Peter Boström225789d2015-10-23 15:20:56 +0200261 CritScope cs(&g_log_crit);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000262 for (StreamList::iterator it = streams_.begin(); it != streams_.end(); ++it) {
263 if (stream == it->first) {
264 streams_.erase(it);
265 break;
266 }
267 }
268 UpdateMinLogSeverity();
269}
270
Tommi0eefb4d2015-05-23 09:54:07 +0200271void LogMessage::ConfigureLogging(const char* params) {
272 LoggingSeverity current_level = LS_VERBOSE;
273 LoggingSeverity debug_level = GetLogToDebug();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000274
275 std::vector<std::string> tokens;
276 tokenize(params, ' ', &tokens);
277
Tommi0eefb4d2015-05-23 09:54:07 +0200278 for (const std::string& token : tokens) {
279 if (token.empty())
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000280 continue;
281
282 // Logging features
Tommi0eefb4d2015-05-23 09:54:07 +0200283 if (token == "tstamp") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000284 LogTimestamps();
Tommi0eefb4d2015-05-23 09:54:07 +0200285 } else if (token == "thread") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000286 LogThreads();
287
288 // Logging levels
Tommi0eefb4d2015-05-23 09:54:07 +0200289 } else if (token == "sensitive") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000290 current_level = LS_SENSITIVE;
Tommi0eefb4d2015-05-23 09:54:07 +0200291 } else if (token == "verbose") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000292 current_level = LS_VERBOSE;
Tommi0eefb4d2015-05-23 09:54:07 +0200293 } else if (token == "info") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000294 current_level = LS_INFO;
Tommi0eefb4d2015-05-23 09:54:07 +0200295 } else if (token == "warning") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000296 current_level = LS_WARNING;
Tommi0eefb4d2015-05-23 09:54:07 +0200297 } else if (token == "error") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000298 current_level = LS_ERROR;
Tommi0eefb4d2015-05-23 09:54:07 +0200299 } else if (token == "none") {
300 current_level = LS_NONE;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000301
302 // Logging targets
Tommi0eefb4d2015-05-23 09:54:07 +0200303 } else if (token == "debug") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000304 debug_level = current_level;
305 }
306 }
307
308#if defined(WEBRTC_WIN)
Tommi0eefb4d2015-05-23 09:54:07 +0200309 if ((LS_NONE != debug_level) && !::IsDebuggerPresent()) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000310 // First, attempt to attach to our parent's console... so if you invoke
311 // from the command line, we'll see the output there. Otherwise, create
312 // our own console window.
313 // Note: These methods fail if a console already exists, which is fine.
314 bool success = false;
315 typedef BOOL (WINAPI* PFN_AttachConsole)(DWORD);
316 if (HINSTANCE kernel32 = ::LoadLibrary(L"kernel32.dll")) {
317 // AttachConsole is defined on WinXP+.
318 if (PFN_AttachConsole attach_console = reinterpret_cast<PFN_AttachConsole>
319 (::GetProcAddress(kernel32, "AttachConsole"))) {
320 success = (FALSE != attach_console(ATTACH_PARENT_PROCESS));
321 }
322 ::FreeLibrary(kernel32);
323 }
324 if (!success) {
325 ::AllocConsole();
326 }
327 }
Tommi0eefb4d2015-05-23 09:54:07 +0200328#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000329
330 LogToDebug(debug_level);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000331}
332
Peter Boström225789d2015-10-23 15:20:56 +0200333void LogMessage::UpdateMinLogSeverity() EXCLUSIVE_LOCKS_REQUIRED(g_log_crit) {
Tommi0eefb4d2015-05-23 09:54:07 +0200334 LoggingSeverity min_sev = dbg_sev_;
Peter Boström225789d2015-10-23 15:20:56 +0200335 for (auto& kv : streams_) {
336 min_sev = std::min(dbg_sev_, kv.second);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000337 }
338 min_sev_ = min_sev;
339}
340
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000341void LogMessage::OutputToDebug(const std::string& str,
jiayl66f0da22015-09-14 15:06:39 -0700342 LoggingSeverity severity,
343 const std::string& tag) {
andrew88703d72015-09-07 00:34:56 -0700344 bool log_to_stderr = log_to_stderr_;
tfarinaa41ab932015-10-30 16:08:48 -0700345#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) && defined(NDEBUG)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000346 // On the Mac, all stderr output goes to the Console log and causes clutter.
347 // So in opt builds, don't log to stderr unless the user specifically sets
348 // a preference to do so.
349 CFStringRef key = CFStringCreateWithCString(kCFAllocatorDefault,
350 "logToStdErr",
351 kCFStringEncodingUTF8);
352 CFStringRef domain = CFBundleGetIdentifier(CFBundleGetMainBundle());
353 if (key != NULL && domain != NULL) {
354 Boolean exists_and_is_valid;
355 Boolean should_log =
356 CFPreferencesGetAppBooleanValue(key, domain, &exists_and_is_valid);
357 // If the key doesn't exist or is invalid or is false, we will not log to
358 // stderr.
359 log_to_stderr = exists_and_is_valid && should_log;
360 }
361 if (key != NULL) {
362 CFRelease(key);
363 }
364#endif
365#if defined(WEBRTC_WIN)
366 // Always log to the debugger.
367 // Perhaps stderr should be controlled by a preference, as on Mac?
368 OutputDebugStringA(str.c_str());
369 if (log_to_stderr) {
370 // This handles dynamically allocated consoles, too.
371 if (HANDLE error_handle = ::GetStdHandle(STD_ERROR_HANDLE)) {
372 log_to_stderr = false;
373 DWORD written = 0;
374 ::WriteFile(error_handle, str.data(), static_cast<DWORD>(str.size()),
375 &written, 0);
376 }
377 }
Tommi0eefb4d2015-05-23 09:54:07 +0200378#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000379#if defined(WEBRTC_ANDROID)
380 // Android's logging facility uses severity to log messages but we
381 // need to map libjingle's severity levels to Android ones first.
382 // Also write to stderr which maybe available to executable started
383 // from the shell.
384 int prio;
385 switch (severity) {
386 case LS_SENSITIVE:
jiayl66f0da22015-09-14 15:06:39 -0700387 __android_log_write(ANDROID_LOG_INFO, tag.c_str(), "SENSITIVE");
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000388 if (log_to_stderr) {
389 fprintf(stderr, "SENSITIVE");
390 fflush(stderr);
391 }
392 return;
393 case LS_VERBOSE:
394 prio = ANDROID_LOG_VERBOSE;
395 break;
396 case LS_INFO:
397 prio = ANDROID_LOG_INFO;
398 break;
399 case LS_WARNING:
400 prio = ANDROID_LOG_WARN;
401 break;
402 case LS_ERROR:
403 prio = ANDROID_LOG_ERROR;
404 break;
405 default:
406 prio = ANDROID_LOG_UNKNOWN;
407 }
408
409 int size = str.size();
410 int line = 0;
411 int idx = 0;
412 const int max_lines = size / kMaxLogLineSize + 1;
413 if (max_lines == 1) {
jiayl66f0da22015-09-14 15:06:39 -0700414 __android_log_print(prio, tag.c_str(), "%.*s", size, str.c_str());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000415 } else {
416 while (size > 0) {
417 const int len = std::min(size, kMaxLogLineSize);
418 // Use the size of the string in the format (str may have \0 in the
419 // middle).
jiayl66f0da22015-09-14 15:06:39 -0700420 __android_log_print(prio, tag.c_str(), "[%d/%d] %.*s",
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000421 line + 1, max_lines,
422 len, str.c_str() + idx);
423 idx += len;
424 size -= len;
425 ++line;
426 }
427 }
428#endif // WEBRTC_ANDROID
429 if (log_to_stderr) {
430 fprintf(stderr, "%s", str.c_str());
431 fflush(stderr);
432 }
433}
434
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000435//////////////////////////////////////////////////////////////////////
436// Logging Helpers
437//////////////////////////////////////////////////////////////////////
438
439void LogMultiline(LoggingSeverity level, const char* label, bool input,
440 const void* data, size_t len, bool hex_mode,
441 LogMultilineState* state) {
442 if (!LOG_CHECK_LEVEL_V(level))
443 return;
444
445 const char * direction = (input ? " << " : " >> ");
446
447 // NULL data means to flush our count of unprintable characters.
448 if (!data) {
449 if (state && state->unprintable_count_[input]) {
450 LOG_V(level) << label << direction << "## "
451 << state->unprintable_count_[input]
452 << " consecutive unprintable ##";
453 state->unprintable_count_[input] = 0;
454 }
455 return;
456 }
457
458 // The ctype classification functions want unsigned chars.
459 const unsigned char* udata = static_cast<const unsigned char*>(data);
460
461 if (hex_mode) {
462 const size_t LINE_SIZE = 24;
463 char hex_line[LINE_SIZE * 9 / 4 + 2], asc_line[LINE_SIZE + 1];
464 while (len > 0) {
465 memset(asc_line, ' ', sizeof(asc_line));
466 memset(hex_line, ' ', sizeof(hex_line));
andresp@webrtc.orgff689be2015-02-12 11:54:26 +0000467 size_t line_len = std::min(len, LINE_SIZE);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000468 for (size_t i = 0; i < line_len; ++i) {
469 unsigned char ch = udata[i];
470 asc_line[i] = isprint(ch) ? ch : '.';
471 hex_line[i*2 + i/4] = hex_encode(ch >> 4);
472 hex_line[i*2 + i/4 + 1] = hex_encode(ch & 0xf);
473 }
474 asc_line[sizeof(asc_line)-1] = 0;
475 hex_line[sizeof(hex_line)-1] = 0;
476 LOG_V(level) << label << direction
477 << asc_line << " " << hex_line << " ";
478 udata += line_len;
479 len -= line_len;
480 }
481 return;
482 }
483
484 size_t consecutive_unprintable = state ? state->unprintable_count_[input] : 0;
485
486 const unsigned char* end = udata + len;
487 while (udata < end) {
488 const unsigned char* line = udata;
489 const unsigned char* end_of_line = strchrn<unsigned char>(udata,
490 end - udata,
491 '\n');
492 if (!end_of_line) {
493 udata = end_of_line = end;
494 } else {
495 udata = end_of_line + 1;
496 }
497
498 bool is_printable = true;
499
500 // If we are in unprintable mode, we need to see a line of at least
501 // kMinPrintableLine characters before we'll switch back.
502 const ptrdiff_t kMinPrintableLine = 4;
503 if (consecutive_unprintable && ((end_of_line - line) < kMinPrintableLine)) {
504 is_printable = false;
505 } else {
506 // Determine if the line contains only whitespace and printable
507 // characters.
508 bool is_entirely_whitespace = true;
509 for (const unsigned char* pos = line; pos < end_of_line; ++pos) {
510 if (isspace(*pos))
511 continue;
512 is_entirely_whitespace = false;
513 if (!isprint(*pos)) {
514 is_printable = false;
515 break;
516 }
517 }
518 // Treat an empty line following unprintable data as unprintable.
519 if (consecutive_unprintable && is_entirely_whitespace) {
520 is_printable = false;
521 }
522 }
523 if (!is_printable) {
524 consecutive_unprintable += (udata - line);
525 continue;
526 }
527 // Print out the current line, but prefix with a count of prior unprintable
528 // characters.
529 if (consecutive_unprintable) {
530 LOG_V(level) << label << direction << "## " << consecutive_unprintable
531 << " consecutive unprintable ##";
532 consecutive_unprintable = 0;
533 }
534 // Strip off trailing whitespace.
535 while ((end_of_line > line) && isspace(*(end_of_line-1))) {
536 --end_of_line;
537 }
538 // Filter out any private data
539 std::string substr(reinterpret_cast<const char*>(line), end_of_line - line);
540 std::string::size_type pos_private = substr.find("Email");
541 if (pos_private == std::string::npos) {
542 pos_private = substr.find("Passwd");
543 }
544 if (pos_private == std::string::npos) {
545 LOG_V(level) << label << direction << substr;
546 } else {
547 LOG_V(level) << label << direction << "## omitted for privacy ##";
548 }
549 }
550
551 if (state) {
552 state->unprintable_count_[input] = consecutive_unprintable;
553 }
554}
555
556//////////////////////////////////////////////////////////////////////
557
558} // namespace rtc