blob: c951e156d8dc3e3da81af108c13630b198851a52 [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_) {
Taylor Brandstetter4f0dfbd2016-06-15 17:15:23 -0700127 // Use SystemTimeMillis so that even if tests use fake clocks, the timestamp
128 // in log messages represents the real system time.
129 int64_t time = TimeDiff(SystemTimeMillis(), LogStartTime());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000130 // Also ensure WallClockStartTime is initialized, so that it matches
131 // LogStartTime.
132 WallClockStartTime();
133 print_stream_ << "[" << std::setfill('0') << std::setw(3) << (time / 1000)
134 << ":" << std::setw(3) << (time % 1000) << std::setfill(' ')
135 << "] ";
136 }
137
138 if (thread_) {
henrikaba35d052015-07-14 17:04:08 +0200139 PlatformThreadId id = CurrentThreadId();
140 print_stream_ << "[" << std::dec << id << "] ";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000141 }
142
Alex Glaznevebed24d2015-09-15 11:05:24 -0700143 if (file != NULL)
144 print_stream_ << "(" << FilenameFromPath(file) << ":" << line << "): ";
andrew88703d72015-09-07 00:34:56 -0700145
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000146 if (err_ctx != ERRCTX_NONE) {
147 std::ostringstream tmp;
148 tmp << "[0x" << std::setfill('0') << std::hex << std::setw(8) << err << "]";
149 switch (err_ctx) {
150 case ERRCTX_ERRNO:
151 tmp << " " << strerror(err);
152 break;
153#if WEBRTC_WIN
154 case ERRCTX_HRESULT: {
155 char msgbuf[256];
156 DWORD flags = FORMAT_MESSAGE_FROM_SYSTEM;
157 HMODULE hmod = GetModuleHandleA(module);
158 if (hmod)
159 flags |= FORMAT_MESSAGE_FROM_HMODULE;
160 if (DWORD len = FormatMessageA(
161 flags, hmod, err,
162 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
163 msgbuf, sizeof(msgbuf) / sizeof(msgbuf[0]), NULL)) {
164 while ((len > 0) &&
165 isspace(static_cast<unsigned char>(msgbuf[len-1]))) {
166 msgbuf[--len] = 0;
167 }
168 tmp << " " << msgbuf;
169 }
170 break;
171 }
Tommi0eefb4d2015-05-23 09:54:07 +0200172#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000173#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS)
174 case ERRCTX_OSSTATUS: {
Tommi09ca02e2016-04-24 17:32:48 +0200175 std::string desc(DescriptionFromOSStatus(err));
176 tmp << " " << (desc.empty() ? "Unknown error" : desc.c_str());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000177 break;
178 }
179#endif // WEBRTC_MAC && !defined(WEBRTC_IOS)
180 default:
181 break;
182 }
183 extra_ = tmp.str();
184 }
185}
186
jiayl66f0da22015-09-14 15:06:39 -0700187LogMessage::LogMessage(const char* file,
188 int line,
189 LoggingSeverity sev,
190 const std::string& tag)
191 : LogMessage(file, line, sev, ERRCTX_NONE, 0 /* err */, NULL /* module */) {
192 tag_ = tag;
Jiayang Liue4ba6ce92015-09-21 15:49:24 -0700193 print_stream_ << tag << ": ";
jiayl66f0da22015-09-14 15:06:39 -0700194}
195
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000196LogMessage::~LogMessage() {
197 if (!extra_.empty())
198 print_stream_ << " : " << extra_;
199 print_stream_ << std::endl;
200
201 const std::string& str = print_stream_.str();
202 if (severity_ >= dbg_sev_) {
jiayl66f0da22015-09-14 15:06:39 -0700203 OutputToDebug(str, severity_, tag_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000204 }
205
Peter Boström225789d2015-10-23 15:20:56 +0200206 CritScope cs(&g_log_crit);
207 for (auto& kv : streams_) {
208 if (severity_ >= kv.second) {
209 kv.first->OnLogMessage(str);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000210 }
211 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000212}
213
Honghai Zhang82d78622016-05-06 11:29:15 -0700214int64_t LogMessage::LogStartTime() {
Taylor Brandstetter4f0dfbd2016-06-15 17:15:23 -0700215 static const int64_t g_start = SystemTimeMillis();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000216 return g_start;
217}
218
Peter Boström0c4e06b2015-10-07 12:23:21 +0200219uint32_t LogMessage::WallClockStartTime() {
220 static const uint32_t g_start_wallclock = time(NULL);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000221 return g_start_wallclock;
222}
223
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000224void LogMessage::LogThreads(bool on) {
225 thread_ = on;
226}
227
228void LogMessage::LogTimestamps(bool on) {
229 timestamp_ = on;
230}
231
Tommi0eefb4d2015-05-23 09:54:07 +0200232void LogMessage::LogToDebug(LoggingSeverity min_sev) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000233 dbg_sev_ = min_sev;
Peter Boström225789d2015-10-23 15:20:56 +0200234 CritScope cs(&g_log_crit);
Tommi00aac5a2015-05-25 11:25:59 +0200235 UpdateMinLogSeverity();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000236}
237
andrew88703d72015-09-07 00:34:56 -0700238void LogMessage::SetLogToStderr(bool log_to_stderr) {
239 log_to_stderr_ = log_to_stderr;
240}
241
Tommi0eefb4d2015-05-23 09:54:07 +0200242int LogMessage::GetLogToStream(LogSink* stream) {
Peter Boström225789d2015-10-23 15:20:56 +0200243 CritScope cs(&g_log_crit);
Tommi0eefb4d2015-05-23 09:54:07 +0200244 LoggingSeverity sev = LS_NONE;
Peter Boström225789d2015-10-23 15:20:56 +0200245 for (auto& kv : streams_) {
246 if (!stream || stream == kv.first) {
247 sev = std::min(sev, kv.second);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000248 }
249 }
250 return sev;
251}
252
Tommi0eefb4d2015-05-23 09:54:07 +0200253void LogMessage::AddLogToStream(LogSink* stream, LoggingSeverity min_sev) {
Peter Boström225789d2015-10-23 15:20:56 +0200254 CritScope cs(&g_log_crit);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000255 streams_.push_back(std::make_pair(stream, min_sev));
256 UpdateMinLogSeverity();
257}
258
Tommi0eefb4d2015-05-23 09:54:07 +0200259void LogMessage::RemoveLogToStream(LogSink* stream) {
Peter Boström225789d2015-10-23 15:20:56 +0200260 CritScope cs(&g_log_crit);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000261 for (StreamList::iterator it = streams_.begin(); it != streams_.end(); ++it) {
262 if (stream == it->first) {
263 streams_.erase(it);
264 break;
265 }
266 }
267 UpdateMinLogSeverity();
268}
269
Tommi0eefb4d2015-05-23 09:54:07 +0200270void LogMessage::ConfigureLogging(const char* params) {
271 LoggingSeverity current_level = LS_VERBOSE;
272 LoggingSeverity debug_level = GetLogToDebug();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000273
274 std::vector<std::string> tokens;
275 tokenize(params, ' ', &tokens);
276
Tommi0eefb4d2015-05-23 09:54:07 +0200277 for (const std::string& token : tokens) {
278 if (token.empty())
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000279 continue;
280
281 // Logging features
Tommi0eefb4d2015-05-23 09:54:07 +0200282 if (token == "tstamp") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000283 LogTimestamps();
Tommi0eefb4d2015-05-23 09:54:07 +0200284 } else if (token == "thread") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000285 LogThreads();
286
287 // Logging levels
Tommi0eefb4d2015-05-23 09:54:07 +0200288 } else if (token == "sensitive") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000289 current_level = LS_SENSITIVE;
Tommi0eefb4d2015-05-23 09:54:07 +0200290 } else if (token == "verbose") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000291 current_level = LS_VERBOSE;
Tommi0eefb4d2015-05-23 09:54:07 +0200292 } else if (token == "info") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000293 current_level = LS_INFO;
Tommi0eefb4d2015-05-23 09:54:07 +0200294 } else if (token == "warning") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000295 current_level = LS_WARNING;
Tommi0eefb4d2015-05-23 09:54:07 +0200296 } else if (token == "error") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000297 current_level = LS_ERROR;
Tommi0eefb4d2015-05-23 09:54:07 +0200298 } else if (token == "none") {
299 current_level = LS_NONE;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000300
301 // Logging targets
Tommi0eefb4d2015-05-23 09:54:07 +0200302 } else if (token == "debug") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000303 debug_level = current_level;
304 }
305 }
306
307#if defined(WEBRTC_WIN)
Tommi0eefb4d2015-05-23 09:54:07 +0200308 if ((LS_NONE != debug_level) && !::IsDebuggerPresent()) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000309 // First, attempt to attach to our parent's console... so if you invoke
310 // from the command line, we'll see the output there. Otherwise, create
311 // our own console window.
312 // Note: These methods fail if a console already exists, which is fine.
313 bool success = false;
314 typedef BOOL (WINAPI* PFN_AttachConsole)(DWORD);
315 if (HINSTANCE kernel32 = ::LoadLibrary(L"kernel32.dll")) {
316 // AttachConsole is defined on WinXP+.
317 if (PFN_AttachConsole attach_console = reinterpret_cast<PFN_AttachConsole>
318 (::GetProcAddress(kernel32, "AttachConsole"))) {
319 success = (FALSE != attach_console(ATTACH_PARENT_PROCESS));
320 }
321 ::FreeLibrary(kernel32);
322 }
323 if (!success) {
324 ::AllocConsole();
325 }
326 }
Tommi0eefb4d2015-05-23 09:54:07 +0200327#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000328
329 LogToDebug(debug_level);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000330}
331
Peter Boström225789d2015-10-23 15:20:56 +0200332void LogMessage::UpdateMinLogSeverity() EXCLUSIVE_LOCKS_REQUIRED(g_log_crit) {
Tommi0eefb4d2015-05-23 09:54:07 +0200333 LoggingSeverity min_sev = dbg_sev_;
Peter Boström225789d2015-10-23 15:20:56 +0200334 for (auto& kv : streams_) {
335 min_sev = std::min(dbg_sev_, kv.second);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000336 }
337 min_sev_ = min_sev;
338}
339
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000340void LogMessage::OutputToDebug(const std::string& str,
jiayl66f0da22015-09-14 15:06:39 -0700341 LoggingSeverity severity,
342 const std::string& tag) {
andrew88703d72015-09-07 00:34:56 -0700343 bool log_to_stderr = log_to_stderr_;
tfarinaa41ab932015-10-30 16:08:48 -0700344#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) && defined(NDEBUG)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000345 // On the Mac, all stderr output goes to the Console log and causes clutter.
346 // So in opt builds, don't log to stderr unless the user specifically sets
347 // a preference to do so.
348 CFStringRef key = CFStringCreateWithCString(kCFAllocatorDefault,
349 "logToStdErr",
350 kCFStringEncodingUTF8);
351 CFStringRef domain = CFBundleGetIdentifier(CFBundleGetMainBundle());
352 if (key != NULL && domain != NULL) {
353 Boolean exists_and_is_valid;
354 Boolean should_log =
355 CFPreferencesGetAppBooleanValue(key, domain, &exists_and_is_valid);
356 // If the key doesn't exist or is invalid or is false, we will not log to
357 // stderr.
358 log_to_stderr = exists_and_is_valid && should_log;
359 }
360 if (key != NULL) {
361 CFRelease(key);
362 }
363#endif
364#if defined(WEBRTC_WIN)
365 // Always log to the debugger.
366 // Perhaps stderr should be controlled by a preference, as on Mac?
367 OutputDebugStringA(str.c_str());
368 if (log_to_stderr) {
369 // This handles dynamically allocated consoles, too.
370 if (HANDLE error_handle = ::GetStdHandle(STD_ERROR_HANDLE)) {
371 log_to_stderr = false;
372 DWORD written = 0;
373 ::WriteFile(error_handle, str.data(), static_cast<DWORD>(str.size()),
374 &written, 0);
375 }
376 }
Tommi0eefb4d2015-05-23 09:54:07 +0200377#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000378#if defined(WEBRTC_ANDROID)
379 // Android's logging facility uses severity to log messages but we
380 // need to map libjingle's severity levels to Android ones first.
381 // Also write to stderr which maybe available to executable started
382 // from the shell.
383 int prio;
384 switch (severity) {
385 case LS_SENSITIVE:
jiayl66f0da22015-09-14 15:06:39 -0700386 __android_log_write(ANDROID_LOG_INFO, tag.c_str(), "SENSITIVE");
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000387 if (log_to_stderr) {
388 fprintf(stderr, "SENSITIVE");
389 fflush(stderr);
390 }
391 return;
392 case LS_VERBOSE:
393 prio = ANDROID_LOG_VERBOSE;
394 break;
395 case LS_INFO:
396 prio = ANDROID_LOG_INFO;
397 break;
398 case LS_WARNING:
399 prio = ANDROID_LOG_WARN;
400 break;
401 case LS_ERROR:
402 prio = ANDROID_LOG_ERROR;
403 break;
404 default:
405 prio = ANDROID_LOG_UNKNOWN;
406 }
407
408 int size = str.size();
409 int line = 0;
410 int idx = 0;
411 const int max_lines = size / kMaxLogLineSize + 1;
412 if (max_lines == 1) {
jiayl66f0da22015-09-14 15:06:39 -0700413 __android_log_print(prio, tag.c_str(), "%.*s", size, str.c_str());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000414 } else {
415 while (size > 0) {
416 const int len = std::min(size, kMaxLogLineSize);
417 // Use the size of the string in the format (str may have \0 in the
418 // middle).
jiayl66f0da22015-09-14 15:06:39 -0700419 __android_log_print(prio, tag.c_str(), "[%d/%d] %.*s",
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000420 line + 1, max_lines,
421 len, str.c_str() + idx);
422 idx += len;
423 size -= len;
424 ++line;
425 }
426 }
427#endif // WEBRTC_ANDROID
428 if (log_to_stderr) {
429 fprintf(stderr, "%s", str.c_str());
430 fflush(stderr);
431 }
432}
433
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000434//////////////////////////////////////////////////////////////////////
435// Logging Helpers
436//////////////////////////////////////////////////////////////////////
437
438void LogMultiline(LoggingSeverity level, const char* label, bool input,
439 const void* data, size_t len, bool hex_mode,
440 LogMultilineState* state) {
441 if (!LOG_CHECK_LEVEL_V(level))
442 return;
443
444 const char * direction = (input ? " << " : " >> ");
445
446 // NULL data means to flush our count of unprintable characters.
447 if (!data) {
448 if (state && state->unprintable_count_[input]) {
449 LOG_V(level) << label << direction << "## "
450 << state->unprintable_count_[input]
451 << " consecutive unprintable ##";
452 state->unprintable_count_[input] = 0;
453 }
454 return;
455 }
456
457 // The ctype classification functions want unsigned chars.
458 const unsigned char* udata = static_cast<const unsigned char*>(data);
459
460 if (hex_mode) {
461 const size_t LINE_SIZE = 24;
462 char hex_line[LINE_SIZE * 9 / 4 + 2], asc_line[LINE_SIZE + 1];
463 while (len > 0) {
464 memset(asc_line, ' ', sizeof(asc_line));
465 memset(hex_line, ' ', sizeof(hex_line));
andresp@webrtc.orgff689be2015-02-12 11:54:26 +0000466 size_t line_len = std::min(len, LINE_SIZE);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000467 for (size_t i = 0; i < line_len; ++i) {
468 unsigned char ch = udata[i];
469 asc_line[i] = isprint(ch) ? ch : '.';
470 hex_line[i*2 + i/4] = hex_encode(ch >> 4);
471 hex_line[i*2 + i/4 + 1] = hex_encode(ch & 0xf);
472 }
473 asc_line[sizeof(asc_line)-1] = 0;
474 hex_line[sizeof(hex_line)-1] = 0;
475 LOG_V(level) << label << direction
476 << asc_line << " " << hex_line << " ";
477 udata += line_len;
478 len -= line_len;
479 }
480 return;
481 }
482
483 size_t consecutive_unprintable = state ? state->unprintable_count_[input] : 0;
484
485 const unsigned char* end = udata + len;
486 while (udata < end) {
487 const unsigned char* line = udata;
488 const unsigned char* end_of_line = strchrn<unsigned char>(udata,
489 end - udata,
490 '\n');
491 if (!end_of_line) {
492 udata = end_of_line = end;
493 } else {
494 udata = end_of_line + 1;
495 }
496
497 bool is_printable = true;
498
499 // If we are in unprintable mode, we need to see a line of at least
500 // kMinPrintableLine characters before we'll switch back.
501 const ptrdiff_t kMinPrintableLine = 4;
502 if (consecutive_unprintable && ((end_of_line - line) < kMinPrintableLine)) {
503 is_printable = false;
504 } else {
505 // Determine if the line contains only whitespace and printable
506 // characters.
507 bool is_entirely_whitespace = true;
508 for (const unsigned char* pos = line; pos < end_of_line; ++pos) {
509 if (isspace(*pos))
510 continue;
511 is_entirely_whitespace = false;
512 if (!isprint(*pos)) {
513 is_printable = false;
514 break;
515 }
516 }
517 // Treat an empty line following unprintable data as unprintable.
518 if (consecutive_unprintable && is_entirely_whitespace) {
519 is_printable = false;
520 }
521 }
522 if (!is_printable) {
523 consecutive_unprintable += (udata - line);
524 continue;
525 }
526 // Print out the current line, but prefix with a count of prior unprintable
527 // characters.
528 if (consecutive_unprintable) {
529 LOG_V(level) << label << direction << "## " << consecutive_unprintable
530 << " consecutive unprintable ##";
531 consecutive_unprintable = 0;
532 }
533 // Strip off trailing whitespace.
534 while ((end_of_line > line) && isspace(*(end_of_line-1))) {
535 --end_of_line;
536 }
537 // Filter out any private data
538 std::string substr(reinterpret_cast<const char*>(line), end_of_line - line);
539 std::string::size_type pos_private = substr.find("Email");
540 if (pos_private == std::string::npos) {
541 pos_private = substr.find("Passwd");
542 }
543 if (pos_private == std::string::npos) {
544 LOG_V(level) << label << direction << substr;
545 } else {
546 LOG_V(level) << label << direction << "## omitted for privacy ##";
547 }
548 }
549
550 if (state) {
551 state->unprintable_count_[input] = consecutive_unprintable;
552 }
553}
554
555//////////////////////////////////////////////////////////////////////
556
557} // namespace rtc