blob: b60a244429261ea1e823dcf58e1c6e876d86ed1f [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>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000024// Android has a 1024 limit on log inputs. We use 60 chars as an
25// approx for the header/tag portion.
26// See android/system/core/liblog/logd_write.c
27static const int kMaxLogLineSize = 1024 - 60;
28#endif // WEBRTC_MAC && !defined(WEBRTC_IOS) || WEBRTC_ANDROID
29
jiayl66f0da22015-09-14 15:06:39 -070030static const char kLibjingle[] = "libjingle";
31
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000032#include <time.h>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000033#include <limits.h>
andresp@webrtc.orgff689be2015-02-12 11:54:26 +000034
35#include <algorithm>
36#include <iomanip>
37#include <ostream>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000038#include <vector>
39
40#include "webrtc/base/logging.h"
henrikaba35d052015-07-14 17:04:08 +020041#include "webrtc/base/platform_thread.h"
Tommi0eefb4d2015-05-23 09:54:07 +020042#include "webrtc/base/scoped_ptr.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000043#include "webrtc/base/stringencode.h"
44#include "webrtc/base/stringutils.h"
45#include "webrtc/base/timeutils.h"
46
47namespace rtc {
andrew88703d72015-09-07 00:34:56 -070048namespace {
49
50// Return the filename portion of the string (that following the last slash).
51const char* FilenameFromPath(const char* file) {
52 const char* end1 = ::strrchr(file, '/');
53 const char* end2 = ::strrchr(file, '\\');
54 if (!end1 && !end2)
55 return file;
56 else
57 return (end1 > end2) ? end1 + 1 : end2 + 1;
58}
59
60} // namespace
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000061
62/////////////////////////////////////////////////////////////////////////////
63// Constant Labels
64/////////////////////////////////////////////////////////////////////////////
65
andrew88703d72015-09-07 00:34:56 -070066const char* FindLabel(int value, const ConstantLabel entries[]) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000067 for (int i = 0; entries[i].label; ++i) {
68 if (value == entries[i].value) {
69 return entries[i].label;
70 }
71 }
72 return 0;
73}
74
andrew88703d72015-09-07 00:34:56 -070075std::string ErrorName(int err, const ConstantLabel* err_table) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000076 if (err == 0)
77 return "No error";
78
79 if (err_table != 0) {
andrew88703d72015-09-07 00:34:56 -070080 if (const char* value = FindLabel(err, err_table))
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000081 return value;
82 }
83
84 char buffer[16];
85 snprintf(buffer, sizeof(buffer), "0x%08x", err);
86 return buffer;
87}
88
89/////////////////////////////////////////////////////////////////////////////
90// LogMessage
91/////////////////////////////////////////////////////////////////////////////
92
Tommi0eefb4d2015-05-23 09:54:07 +020093// By default, release builds don't log, debug builds at info level
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000094#if _DEBUG
Tommi0eefb4d2015-05-23 09:54:07 +020095LoggingSeverity LogMessage::min_sev_ = LS_INFO;
96LoggingSeverity LogMessage::dbg_sev_ = LS_INFO;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000097#else // !_DEBUG
Tommi0eefb4d2015-05-23 09:54:07 +020098LoggingSeverity LogMessage::min_sev_ = LS_NONE;
99LoggingSeverity LogMessage::dbg_sev_ = LS_NONE;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000100#endif // !_DEBUG
andrew88703d72015-09-07 00:34:56 -0700101bool LogMessage::log_to_stderr_ = true;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000102
103// Global lock for log subsystem, only needed to serialize access to streams_.
104CriticalSection LogMessage::crit_;
105
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000106// The list of logging streams currently configured.
107// Note: we explicitly do not clean this up, because of the uncertain ordering
108// of destructors at program exit. Let the person who sets the stream trigger
109// cleanup by setting to NULL, or let it leak (safe at program exit).
Tommi00aac5a2015-05-25 11:25:59 +0200110LogMessage::StreamList LogMessage::streams_ GUARDED_BY(LogMessage::crit_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000111
112// Boolean options default to false (0)
113bool LogMessage::thread_, LogMessage::timestamp_;
114
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000115LogMessage::LogMessage(const char* file, int line, LoggingSeverity sev,
116 LogErrorContext err_ctx, int err, const char* module)
117 : severity_(sev),
jiayl66f0da22015-09-14 15:06:39 -0700118 tag_(kLibjingle),
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000119 warn_slow_logs_delay_(WARN_SLOW_LOGS_DELAY) {
120 if (timestamp_) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200121 uint32_t time = TimeSince(LogStartTime());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000122 // Also ensure WallClockStartTime is initialized, so that it matches
123 // LogStartTime.
124 WallClockStartTime();
125 print_stream_ << "[" << std::setfill('0') << std::setw(3) << (time / 1000)
126 << ":" << std::setw(3) << (time % 1000) << std::setfill(' ')
127 << "] ";
128 }
129
130 if (thread_) {
henrikaba35d052015-07-14 17:04:08 +0200131 PlatformThreadId id = CurrentThreadId();
132 print_stream_ << "[" << std::dec << id << "] ";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000133 }
134
Alex Glaznevebed24d2015-09-15 11:05:24 -0700135 if (file != NULL)
136 print_stream_ << "(" << FilenameFromPath(file) << ":" << line << "): ";
andrew88703d72015-09-07 00:34:56 -0700137
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000138 if (err_ctx != ERRCTX_NONE) {
139 std::ostringstream tmp;
140 tmp << "[0x" << std::setfill('0') << std::hex << std::setw(8) << err << "]";
141 switch (err_ctx) {
142 case ERRCTX_ERRNO:
143 tmp << " " << strerror(err);
144 break;
145#if WEBRTC_WIN
146 case ERRCTX_HRESULT: {
147 char msgbuf[256];
148 DWORD flags = FORMAT_MESSAGE_FROM_SYSTEM;
149 HMODULE hmod = GetModuleHandleA(module);
150 if (hmod)
151 flags |= FORMAT_MESSAGE_FROM_HMODULE;
152 if (DWORD len = FormatMessageA(
153 flags, hmod, err,
154 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
155 msgbuf, sizeof(msgbuf) / sizeof(msgbuf[0]), NULL)) {
156 while ((len > 0) &&
157 isspace(static_cast<unsigned char>(msgbuf[len-1]))) {
158 msgbuf[--len] = 0;
159 }
160 tmp << " " << msgbuf;
161 }
162 break;
163 }
Tommi0eefb4d2015-05-23 09:54:07 +0200164#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000165#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS)
166 case ERRCTX_OSSTATUS: {
167 tmp << " " << nonnull(GetMacOSStatusErrorString(err), "Unknown error");
168 if (const char* desc = GetMacOSStatusCommentString(err)) {
169 tmp << ": " << desc;
170 }
171 break;
172 }
173#endif // WEBRTC_MAC && !defined(WEBRTC_IOS)
174 default:
175 break;
176 }
177 extra_ = tmp.str();
178 }
179}
180
jiayl66f0da22015-09-14 15:06:39 -0700181LogMessage::LogMessage(const char* file,
182 int line,
183 LoggingSeverity sev,
184 const std::string& tag)
185 : LogMessage(file, line, sev, ERRCTX_NONE, 0 /* err */, NULL /* module */) {
186 tag_ = tag;
Jiayang Liue4ba6ce92015-09-21 15:49:24 -0700187 print_stream_ << tag << ": ";
jiayl66f0da22015-09-14 15:06:39 -0700188}
189
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000190LogMessage::~LogMessage() {
191 if (!extra_.empty())
192 print_stream_ << " : " << extra_;
193 print_stream_ << std::endl;
194
195 const std::string& str = print_stream_.str();
196 if (severity_ >= dbg_sev_) {
jiayl66f0da22015-09-14 15:06:39 -0700197 OutputToDebug(str, severity_, tag_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000198 }
199
Peter Boström0c4e06b2015-10-07 12:23:21 +0200200 uint32_t before = Time();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000201 // Must lock streams_ before accessing
202 CritScope cs(&crit_);
203 for (StreamList::iterator it = streams_.begin(); it != streams_.end(); ++it) {
204 if (severity_ >= it->second) {
Tommi0eefb4d2015-05-23 09:54:07 +0200205 it->first->OnLogMessage(str);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000206 }
207 }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200208 uint32_t delay = TimeSince(before);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000209 if (delay >= warn_slow_logs_delay_) {
henrikg38419432015-09-16 06:33:20 -0700210 rtc::LogMessage slow_log_warning(__FILE__, __LINE__, LS_WARNING);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000211 // If our warning is slow, we don't want to warn about it, because
212 // that would lead to inifinite recursion. So, give a really big
213 // number for the delay threshold.
214 slow_log_warning.warn_slow_logs_delay_ = UINT_MAX;
215 slow_log_warning.stream() << "Slow log: took " << delay << "ms to write "
216 << str.size() << " bytes.";
217 }
218}
219
Peter Boström0c4e06b2015-10-07 12:23:21 +0200220uint32_t LogMessage::LogStartTime() {
221 static const uint32_t g_start = Time();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000222 return g_start;
223}
224
Peter Boström0c4e06b2015-10-07 12:23:21 +0200225uint32_t LogMessage::WallClockStartTime() {
226 static const uint32_t g_start_wallclock = time(NULL);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000227 return g_start_wallclock;
228}
229
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000230void LogMessage::LogThreads(bool on) {
231 thread_ = on;
232}
233
234void LogMessage::LogTimestamps(bool on) {
235 timestamp_ = on;
236}
237
Tommi0eefb4d2015-05-23 09:54:07 +0200238void LogMessage::LogToDebug(LoggingSeverity min_sev) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000239 dbg_sev_ = min_sev;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000240 CritScope cs(&crit_);
Tommi00aac5a2015-05-25 11:25:59 +0200241 UpdateMinLogSeverity();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000242}
243
andrew88703d72015-09-07 00:34:56 -0700244void LogMessage::SetLogToStderr(bool log_to_stderr) {
245 log_to_stderr_ = log_to_stderr;
246}
247
Tommi0eefb4d2015-05-23 09:54:07 +0200248int LogMessage::GetLogToStream(LogSink* stream) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000249 CritScope cs(&crit_);
Tommi0eefb4d2015-05-23 09:54:07 +0200250 LoggingSeverity sev = LS_NONE;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000251 for (StreamList::iterator it = streams_.begin(); it != streams_.end(); ++it) {
252 if (!stream || stream == it->first) {
andresp@webrtc.orgff689be2015-02-12 11:54:26 +0000253 sev = std::min(sev, it->second);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000254 }
255 }
256 return sev;
257}
258
Tommi0eefb4d2015-05-23 09:54:07 +0200259void LogMessage::AddLogToStream(LogSink* stream, LoggingSeverity min_sev) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000260 CritScope cs(&crit_);
261 streams_.push_back(std::make_pair(stream, min_sev));
262 UpdateMinLogSeverity();
263}
264
Tommi0eefb4d2015-05-23 09:54:07 +0200265void LogMessage::RemoveLogToStream(LogSink* stream) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000266 CritScope cs(&crit_);
267 for (StreamList::iterator it = streams_.begin(); it != streams_.end(); ++it) {
268 if (stream == it->first) {
269 streams_.erase(it);
270 break;
271 }
272 }
273 UpdateMinLogSeverity();
274}
275
Tommi0eefb4d2015-05-23 09:54:07 +0200276void LogMessage::ConfigureLogging(const char* params) {
277 LoggingSeverity current_level = LS_VERBOSE;
278 LoggingSeverity debug_level = GetLogToDebug();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000279
280 std::vector<std::string> tokens;
281 tokenize(params, ' ', &tokens);
282
Tommi0eefb4d2015-05-23 09:54:07 +0200283 for (const std::string& token : tokens) {
284 if (token.empty())
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000285 continue;
286
287 // Logging features
Tommi0eefb4d2015-05-23 09:54:07 +0200288 if (token == "tstamp") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000289 LogTimestamps();
Tommi0eefb4d2015-05-23 09:54:07 +0200290 } else if (token == "thread") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000291 LogThreads();
292
293 // Logging levels
Tommi0eefb4d2015-05-23 09:54:07 +0200294 } else if (token == "sensitive") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000295 current_level = LS_SENSITIVE;
Tommi0eefb4d2015-05-23 09:54:07 +0200296 } else if (token == "verbose") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000297 current_level = LS_VERBOSE;
Tommi0eefb4d2015-05-23 09:54:07 +0200298 } else if (token == "info") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000299 current_level = LS_INFO;
Tommi0eefb4d2015-05-23 09:54:07 +0200300 } else if (token == "warning") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000301 current_level = LS_WARNING;
Tommi0eefb4d2015-05-23 09:54:07 +0200302 } else if (token == "error") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000303 current_level = LS_ERROR;
Tommi0eefb4d2015-05-23 09:54:07 +0200304 } else if (token == "none") {
305 current_level = LS_NONE;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000306
307 // Logging targets
Tommi0eefb4d2015-05-23 09:54:07 +0200308 } else if (token == "debug") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000309 debug_level = current_level;
310 }
311 }
312
313#if defined(WEBRTC_WIN)
Tommi0eefb4d2015-05-23 09:54:07 +0200314 if ((LS_NONE != debug_level) && !::IsDebuggerPresent()) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000315 // First, attempt to attach to our parent's console... so if you invoke
316 // from the command line, we'll see the output there. Otherwise, create
317 // our own console window.
318 // Note: These methods fail if a console already exists, which is fine.
319 bool success = false;
320 typedef BOOL (WINAPI* PFN_AttachConsole)(DWORD);
321 if (HINSTANCE kernel32 = ::LoadLibrary(L"kernel32.dll")) {
322 // AttachConsole is defined on WinXP+.
323 if (PFN_AttachConsole attach_console = reinterpret_cast<PFN_AttachConsole>
324 (::GetProcAddress(kernel32, "AttachConsole"))) {
325 success = (FALSE != attach_console(ATTACH_PARENT_PROCESS));
326 }
327 ::FreeLibrary(kernel32);
328 }
329 if (!success) {
330 ::AllocConsole();
331 }
332 }
Tommi0eefb4d2015-05-23 09:54:07 +0200333#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000334
335 LogToDebug(debug_level);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000336}
337
Tommi00aac5a2015-05-25 11:25:59 +0200338void LogMessage::UpdateMinLogSeverity() EXCLUSIVE_LOCKS_REQUIRED(crit_) {
Tommi0eefb4d2015-05-23 09:54:07 +0200339 LoggingSeverity min_sev = dbg_sev_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000340 for (StreamList::iterator it = streams_.begin(); it != streams_.end(); ++it) {
andresp@webrtc.orgff689be2015-02-12 11:54:26 +0000341 min_sev = std::min(dbg_sev_, it->second);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000342 }
343 min_sev_ = min_sev;
344}
345
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000346void LogMessage::OutputToDebug(const std::string& str,
jiayl66f0da22015-09-14 15:06:39 -0700347 LoggingSeverity severity,
348 const std::string& tag) {
andrew88703d72015-09-07 00:34:56 -0700349 bool log_to_stderr = log_to_stderr_;
deadbeef59e72ab2015-09-24 10:42:43 -0700350#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) && (!defined(_DEBUG) || defined(NDEBUG))
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000351 // On the Mac, all stderr output goes to the Console log and causes clutter.
352 // So in opt builds, don't log to stderr unless the user specifically sets
353 // a preference to do so.
354 CFStringRef key = CFStringCreateWithCString(kCFAllocatorDefault,
355 "logToStdErr",
356 kCFStringEncodingUTF8);
357 CFStringRef domain = CFBundleGetIdentifier(CFBundleGetMainBundle());
358 if (key != NULL && domain != NULL) {
359 Boolean exists_and_is_valid;
360 Boolean should_log =
361 CFPreferencesGetAppBooleanValue(key, domain, &exists_and_is_valid);
362 // If the key doesn't exist or is invalid or is false, we will not log to
363 // stderr.
364 log_to_stderr = exists_and_is_valid && should_log;
365 }
366 if (key != NULL) {
367 CFRelease(key);
368 }
369#endif
370#if defined(WEBRTC_WIN)
371 // Always log to the debugger.
372 // Perhaps stderr should be controlled by a preference, as on Mac?
373 OutputDebugStringA(str.c_str());
374 if (log_to_stderr) {
375 // This handles dynamically allocated consoles, too.
376 if (HANDLE error_handle = ::GetStdHandle(STD_ERROR_HANDLE)) {
377 log_to_stderr = false;
378 DWORD written = 0;
379 ::WriteFile(error_handle, str.data(), static_cast<DWORD>(str.size()),
380 &written, 0);
381 }
382 }
Tommi0eefb4d2015-05-23 09:54:07 +0200383#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000384#if defined(WEBRTC_ANDROID)
385 // Android's logging facility uses severity to log messages but we
386 // need to map libjingle's severity levels to Android ones first.
387 // Also write to stderr which maybe available to executable started
388 // from the shell.
389 int prio;
390 switch (severity) {
391 case LS_SENSITIVE:
jiayl66f0da22015-09-14 15:06:39 -0700392 __android_log_write(ANDROID_LOG_INFO, tag.c_str(), "SENSITIVE");
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000393 if (log_to_stderr) {
394 fprintf(stderr, "SENSITIVE");
395 fflush(stderr);
396 }
397 return;
398 case LS_VERBOSE:
399 prio = ANDROID_LOG_VERBOSE;
400 break;
401 case LS_INFO:
402 prio = ANDROID_LOG_INFO;
403 break;
404 case LS_WARNING:
405 prio = ANDROID_LOG_WARN;
406 break;
407 case LS_ERROR:
408 prio = ANDROID_LOG_ERROR;
409 break;
410 default:
411 prio = ANDROID_LOG_UNKNOWN;
412 }
413
414 int size = str.size();
415 int line = 0;
416 int idx = 0;
417 const int max_lines = size / kMaxLogLineSize + 1;
418 if (max_lines == 1) {
jiayl66f0da22015-09-14 15:06:39 -0700419 __android_log_print(prio, tag.c_str(), "%.*s", size, str.c_str());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000420 } else {
421 while (size > 0) {
422 const int len = std::min(size, kMaxLogLineSize);
423 // Use the size of the string in the format (str may have \0 in the
424 // middle).
jiayl66f0da22015-09-14 15:06:39 -0700425 __android_log_print(prio, tag.c_str(), "[%d/%d] %.*s",
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000426 line + 1, max_lines,
427 len, str.c_str() + idx);
428 idx += len;
429 size -= len;
430 ++line;
431 }
432 }
433#endif // WEBRTC_ANDROID
434 if (log_to_stderr) {
435 fprintf(stderr, "%s", str.c_str());
436 fflush(stderr);
437 }
438}
439
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000440//////////////////////////////////////////////////////////////////////
441// Logging Helpers
442//////////////////////////////////////////////////////////////////////
443
444void LogMultiline(LoggingSeverity level, const char* label, bool input,
445 const void* data, size_t len, bool hex_mode,
446 LogMultilineState* state) {
447 if (!LOG_CHECK_LEVEL_V(level))
448 return;
449
450 const char * direction = (input ? " << " : " >> ");
451
452 // NULL data means to flush our count of unprintable characters.
453 if (!data) {
454 if (state && state->unprintable_count_[input]) {
455 LOG_V(level) << label << direction << "## "
456 << state->unprintable_count_[input]
457 << " consecutive unprintable ##";
458 state->unprintable_count_[input] = 0;
459 }
460 return;
461 }
462
463 // The ctype classification functions want unsigned chars.
464 const unsigned char* udata = static_cast<const unsigned char*>(data);
465
466 if (hex_mode) {
467 const size_t LINE_SIZE = 24;
468 char hex_line[LINE_SIZE * 9 / 4 + 2], asc_line[LINE_SIZE + 1];
469 while (len > 0) {
470 memset(asc_line, ' ', sizeof(asc_line));
471 memset(hex_line, ' ', sizeof(hex_line));
andresp@webrtc.orgff689be2015-02-12 11:54:26 +0000472 size_t line_len = std::min(len, LINE_SIZE);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000473 for (size_t i = 0; i < line_len; ++i) {
474 unsigned char ch = udata[i];
475 asc_line[i] = isprint(ch) ? ch : '.';
476 hex_line[i*2 + i/4] = hex_encode(ch >> 4);
477 hex_line[i*2 + i/4 + 1] = hex_encode(ch & 0xf);
478 }
479 asc_line[sizeof(asc_line)-1] = 0;
480 hex_line[sizeof(hex_line)-1] = 0;
481 LOG_V(level) << label << direction
482 << asc_line << " " << hex_line << " ";
483 udata += line_len;
484 len -= line_len;
485 }
486 return;
487 }
488
489 size_t consecutive_unprintable = state ? state->unprintable_count_[input] : 0;
490
491 const unsigned char* end = udata + len;
492 while (udata < end) {
493 const unsigned char* line = udata;
494 const unsigned char* end_of_line = strchrn<unsigned char>(udata,
495 end - udata,
496 '\n');
497 if (!end_of_line) {
498 udata = end_of_line = end;
499 } else {
500 udata = end_of_line + 1;
501 }
502
503 bool is_printable = true;
504
505 // If we are in unprintable mode, we need to see a line of at least
506 // kMinPrintableLine characters before we'll switch back.
507 const ptrdiff_t kMinPrintableLine = 4;
508 if (consecutive_unprintable && ((end_of_line - line) < kMinPrintableLine)) {
509 is_printable = false;
510 } else {
511 // Determine if the line contains only whitespace and printable
512 // characters.
513 bool is_entirely_whitespace = true;
514 for (const unsigned char* pos = line; pos < end_of_line; ++pos) {
515 if (isspace(*pos))
516 continue;
517 is_entirely_whitespace = false;
518 if (!isprint(*pos)) {
519 is_printable = false;
520 break;
521 }
522 }
523 // Treat an empty line following unprintable data as unprintable.
524 if (consecutive_unprintable && is_entirely_whitespace) {
525 is_printable = false;
526 }
527 }
528 if (!is_printable) {
529 consecutive_unprintable += (udata - line);
530 continue;
531 }
532 // Print out the current line, but prefix with a count of prior unprintable
533 // characters.
534 if (consecutive_unprintable) {
535 LOG_V(level) << label << direction << "## " << consecutive_unprintable
536 << " consecutive unprintable ##";
537 consecutive_unprintable = 0;
538 }
539 // Strip off trailing whitespace.
540 while ((end_of_line > line) && isspace(*(end_of_line-1))) {
541 --end_of_line;
542 }
543 // Filter out any private data
544 std::string substr(reinterpret_cast<const char*>(line), end_of_line - line);
545 std::string::size_type pos_private = substr.find("Email");
546 if (pos_private == std::string::npos) {
547 pos_private = substr.find("Passwd");
548 }
549 if (pos_private == std::string::npos) {
550 LOG_V(level) << label << direction << substr;
551 } else {
552 LOG_V(level) << label << direction << "## omitted for privacy ##";
553 }
554 }
555
556 if (state) {
557 state->unprintable_count_[input] = consecutive_unprintable;
558 }
559}
560
561//////////////////////////////////////////////////////////////////////
562
563} // namespace rtc