blob: 25f85c96a7c03c7b2e406f4441bd0f3a2f3a7965 [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
deadbeef37f5ecf2017-02-27 14:06:41 -0800113// 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
deadbeef37f5ecf2017-02-27 14:06:41 -0800143 if (file != nullptr)
Alex Glaznevebed24d2015-09-15 11:05:24 -0700144 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;
kwiberg77eab702016-09-28 17:42:01 -0700153#ifdef WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000154 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(
deadbeef37f5ecf2017-02-27 14:06:41 -0800161 flags, hmod, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
162 msgbuf, sizeof(msgbuf) / sizeof(msgbuf[0]), nullptr)) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000163 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: {
Tommi09ca02e2016-04-24 17:32:48 +0200174 std::string desc(DescriptionFromOSStatus(err));
175 tmp << " " << (desc.empty() ? "Unknown error" : desc.c_str());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000176 break;
177 }
178#endif // WEBRTC_MAC && !defined(WEBRTC_IOS)
179 default:
180 break;
181 }
182 extra_ = tmp.str();
183 }
184}
185
jiayl66f0da22015-09-14 15:06:39 -0700186LogMessage::LogMessage(const char* file,
187 int line,
188 LoggingSeverity sev,
189 const std::string& tag)
deadbeef37f5ecf2017-02-27 14:06:41 -0800190 : LogMessage(file,
191 line,
192 sev,
193 ERRCTX_NONE,
194 0 /* err */,
195 nullptr /* module */) {
jiayl66f0da22015-09-14 15:06:39 -0700196 tag_ = tag;
Jiayang Liue4ba6ce92015-09-21 15:49:24 -0700197 print_stream_ << tag << ": ";
jiayl66f0da22015-09-14 15:06:39 -0700198}
199
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000200LogMessage::~LogMessage() {
201 if (!extra_.empty())
202 print_stream_ << " : " << extra_;
203 print_stream_ << std::endl;
204
205 const std::string& str = print_stream_.str();
206 if (severity_ >= dbg_sev_) {
jiayl66f0da22015-09-14 15:06:39 -0700207 OutputToDebug(str, severity_, tag_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000208 }
209
Peter Boström225789d2015-10-23 15:20:56 +0200210 CritScope cs(&g_log_crit);
211 for (auto& kv : streams_) {
212 if (severity_ >= kv.second) {
213 kv.first->OnLogMessage(str);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000214 }
215 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000216}
217
Honghai Zhang82d78622016-05-06 11:29:15 -0700218int64_t LogMessage::LogStartTime() {
Taylor Brandstetter4f0dfbd2016-06-15 17:15:23 -0700219 static const int64_t g_start = SystemTimeMillis();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000220 return g_start;
221}
222
Peter Boström0c4e06b2015-10-07 12:23:21 +0200223uint32_t LogMessage::WallClockStartTime() {
deadbeef37f5ecf2017-02-27 14:06:41 -0800224 static const uint32_t g_start_wallclock = time(nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000225 return g_start_wallclock;
226}
227
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000228void LogMessage::LogThreads(bool on) {
229 thread_ = on;
230}
231
232void LogMessage::LogTimestamps(bool on) {
233 timestamp_ = on;
234}
235
Tommi0eefb4d2015-05-23 09:54:07 +0200236void LogMessage::LogToDebug(LoggingSeverity min_sev) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000237 dbg_sev_ = min_sev;
Peter Boström225789d2015-10-23 15:20:56 +0200238 CritScope cs(&g_log_crit);
Tommi00aac5a2015-05-25 11:25:59 +0200239 UpdateMinLogSeverity();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000240}
241
andrew88703d72015-09-07 00:34:56 -0700242void LogMessage::SetLogToStderr(bool log_to_stderr) {
243 log_to_stderr_ = log_to_stderr;
244}
245
Tommi0eefb4d2015-05-23 09:54:07 +0200246int LogMessage::GetLogToStream(LogSink* stream) {
Peter Boström225789d2015-10-23 15:20:56 +0200247 CritScope cs(&g_log_crit);
Tommi0eefb4d2015-05-23 09:54:07 +0200248 LoggingSeverity sev = LS_NONE;
Peter Boström225789d2015-10-23 15:20:56 +0200249 for (auto& kv : streams_) {
250 if (!stream || stream == kv.first) {
251 sev = std::min(sev, kv.second);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000252 }
253 }
254 return sev;
255}
256
Tommi0eefb4d2015-05-23 09:54:07 +0200257void LogMessage::AddLogToStream(LogSink* stream, LoggingSeverity min_sev) {
Peter Boström225789d2015-10-23 15:20:56 +0200258 CritScope cs(&g_log_crit);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000259 streams_.push_back(std::make_pair(stream, min_sev));
260 UpdateMinLogSeverity();
261}
262
Tommi0eefb4d2015-05-23 09:54:07 +0200263void LogMessage::RemoveLogToStream(LogSink* stream) {
Peter Boström225789d2015-10-23 15:20:56 +0200264 CritScope cs(&g_log_crit);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000265 for (StreamList::iterator it = streams_.begin(); it != streams_.end(); ++it) {
266 if (stream == it->first) {
267 streams_.erase(it);
268 break;
269 }
270 }
271 UpdateMinLogSeverity();
272}
273
Tommi0eefb4d2015-05-23 09:54:07 +0200274void LogMessage::ConfigureLogging(const char* params) {
275 LoggingSeverity current_level = LS_VERBOSE;
276 LoggingSeverity debug_level = GetLogToDebug();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000277
278 std::vector<std::string> tokens;
279 tokenize(params, ' ', &tokens);
280
Tommi0eefb4d2015-05-23 09:54:07 +0200281 for (const std::string& token : tokens) {
282 if (token.empty())
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000283 continue;
284
285 // Logging features
Tommi0eefb4d2015-05-23 09:54:07 +0200286 if (token == "tstamp") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000287 LogTimestamps();
Tommi0eefb4d2015-05-23 09:54:07 +0200288 } else if (token == "thread") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000289 LogThreads();
290
291 // Logging levels
Tommi0eefb4d2015-05-23 09:54:07 +0200292 } else if (token == "sensitive") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000293 current_level = LS_SENSITIVE;
Tommi0eefb4d2015-05-23 09:54:07 +0200294 } else if (token == "verbose") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000295 current_level = LS_VERBOSE;
Tommi0eefb4d2015-05-23 09:54:07 +0200296 } else if (token == "info") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000297 current_level = LS_INFO;
Tommi0eefb4d2015-05-23 09:54:07 +0200298 } else if (token == "warning") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000299 current_level = LS_WARNING;
Tommi0eefb4d2015-05-23 09:54:07 +0200300 } else if (token == "error") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000301 current_level = LS_ERROR;
Tommi0eefb4d2015-05-23 09:54:07 +0200302 } else if (token == "none") {
303 current_level = LS_NONE;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000304
305 // Logging targets
Tommi0eefb4d2015-05-23 09:54:07 +0200306 } else if (token == "debug") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000307 debug_level = current_level;
308 }
309 }
310
311#if defined(WEBRTC_WIN)
Tommi0eefb4d2015-05-23 09:54:07 +0200312 if ((LS_NONE != debug_level) && !::IsDebuggerPresent()) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000313 // First, attempt to attach to our parent's console... so if you invoke
314 // from the command line, we'll see the output there. Otherwise, create
315 // our own console window.
316 // Note: These methods fail if a console already exists, which is fine.
317 bool success = false;
318 typedef BOOL (WINAPI* PFN_AttachConsole)(DWORD);
319 if (HINSTANCE kernel32 = ::LoadLibrary(L"kernel32.dll")) {
320 // AttachConsole is defined on WinXP+.
321 if (PFN_AttachConsole attach_console = reinterpret_cast<PFN_AttachConsole>
322 (::GetProcAddress(kernel32, "AttachConsole"))) {
323 success = (FALSE != attach_console(ATTACH_PARENT_PROCESS));
324 }
325 ::FreeLibrary(kernel32);
326 }
327 if (!success) {
328 ::AllocConsole();
329 }
330 }
Tommi0eefb4d2015-05-23 09:54:07 +0200331#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000332
333 LogToDebug(debug_level);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000334}
335
Peter Boström225789d2015-10-23 15:20:56 +0200336void LogMessage::UpdateMinLogSeverity() EXCLUSIVE_LOCKS_REQUIRED(g_log_crit) {
Tommi0eefb4d2015-05-23 09:54:07 +0200337 LoggingSeverity min_sev = dbg_sev_;
Peter Boström225789d2015-10-23 15:20:56 +0200338 for (auto& kv : streams_) {
339 min_sev = std::min(dbg_sev_, kv.second);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000340 }
341 min_sev_ = min_sev;
342}
343
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000344void LogMessage::OutputToDebug(const std::string& str,
jiayl66f0da22015-09-14 15:06:39 -0700345 LoggingSeverity severity,
346 const std::string& tag) {
andrew88703d72015-09-07 00:34:56 -0700347 bool log_to_stderr = log_to_stderr_;
tfarinaa41ab932015-10-30 16:08:48 -0700348#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) && defined(NDEBUG)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000349 // On the Mac, all stderr output goes to the Console log and causes clutter.
350 // So in opt builds, don't log to stderr unless the user specifically sets
351 // a preference to do so.
352 CFStringRef key = CFStringCreateWithCString(kCFAllocatorDefault,
353 "logToStdErr",
354 kCFStringEncodingUTF8);
355 CFStringRef domain = CFBundleGetIdentifier(CFBundleGetMainBundle());
deadbeef37f5ecf2017-02-27 14:06:41 -0800356 if (key != nullptr && domain != nullptr) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000357 Boolean exists_and_is_valid;
358 Boolean should_log =
359 CFPreferencesGetAppBooleanValue(key, domain, &exists_and_is_valid);
360 // If the key doesn't exist or is invalid or is false, we will not log to
361 // stderr.
362 log_to_stderr = exists_and_is_valid && should_log;
363 }
deadbeef37f5ecf2017-02-27 14:06:41 -0800364 if (key != nullptr) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000365 CFRelease(key);
366 }
367#endif
368#if defined(WEBRTC_WIN)
369 // Always log to the debugger.
370 // Perhaps stderr should be controlled by a preference, as on Mac?
371 OutputDebugStringA(str.c_str());
372 if (log_to_stderr) {
373 // This handles dynamically allocated consoles, too.
374 if (HANDLE error_handle = ::GetStdHandle(STD_ERROR_HANDLE)) {
375 log_to_stderr = false;
376 DWORD written = 0;
377 ::WriteFile(error_handle, str.data(), static_cast<DWORD>(str.size()),
378 &written, 0);
379 }
380 }
Tommi0eefb4d2015-05-23 09:54:07 +0200381#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000382#if defined(WEBRTC_ANDROID)
383 // Android's logging facility uses severity to log messages but we
384 // need to map libjingle's severity levels to Android ones first.
385 // Also write to stderr which maybe available to executable started
386 // from the shell.
387 int prio;
388 switch (severity) {
389 case LS_SENSITIVE:
jiayl66f0da22015-09-14 15:06:39 -0700390 __android_log_write(ANDROID_LOG_INFO, tag.c_str(), "SENSITIVE");
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000391 if (log_to_stderr) {
392 fprintf(stderr, "SENSITIVE");
393 fflush(stderr);
394 }
395 return;
396 case LS_VERBOSE:
397 prio = ANDROID_LOG_VERBOSE;
398 break;
399 case LS_INFO:
400 prio = ANDROID_LOG_INFO;
401 break;
402 case LS_WARNING:
403 prio = ANDROID_LOG_WARN;
404 break;
405 case LS_ERROR:
406 prio = ANDROID_LOG_ERROR;
407 break;
408 default:
409 prio = ANDROID_LOG_UNKNOWN;
410 }
411
412 int size = str.size();
413 int line = 0;
414 int idx = 0;
415 const int max_lines = size / kMaxLogLineSize + 1;
416 if (max_lines == 1) {
jiayl66f0da22015-09-14 15:06:39 -0700417 __android_log_print(prio, tag.c_str(), "%.*s", size, str.c_str());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000418 } else {
419 while (size > 0) {
420 const int len = std::min(size, kMaxLogLineSize);
421 // Use the size of the string in the format (str may have \0 in the
422 // middle).
jiayl66f0da22015-09-14 15:06:39 -0700423 __android_log_print(prio, tag.c_str(), "[%d/%d] %.*s",
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000424 line + 1, max_lines,
425 len, str.c_str() + idx);
426 idx += len;
427 size -= len;
428 ++line;
429 }
430 }
431#endif // WEBRTC_ANDROID
432 if (log_to_stderr) {
433 fprintf(stderr, "%s", str.c_str());
434 fflush(stderr);
435 }
436}
437
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000438//////////////////////////////////////////////////////////////////////
439// Logging Helpers
440//////////////////////////////////////////////////////////////////////
441
442void LogMultiline(LoggingSeverity level, const char* label, bool input,
443 const void* data, size_t len, bool hex_mode,
444 LogMultilineState* state) {
445 if (!LOG_CHECK_LEVEL_V(level))
446 return;
447
448 const char * direction = (input ? " << " : " >> ");
449
deadbeef37f5ecf2017-02-27 14:06:41 -0800450 // null data means to flush our count of unprintable characters.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000451 if (!data) {
452 if (state && state->unprintable_count_[input]) {
453 LOG_V(level) << label << direction << "## "
454 << state->unprintable_count_[input]
455 << " consecutive unprintable ##";
456 state->unprintable_count_[input] = 0;
457 }
458 return;
459 }
460
461 // The ctype classification functions want unsigned chars.
462 const unsigned char* udata = static_cast<const unsigned char*>(data);
463
464 if (hex_mode) {
465 const size_t LINE_SIZE = 24;
466 char hex_line[LINE_SIZE * 9 / 4 + 2], asc_line[LINE_SIZE + 1];
467 while (len > 0) {
468 memset(asc_line, ' ', sizeof(asc_line));
469 memset(hex_line, ' ', sizeof(hex_line));
andresp@webrtc.orgff689be2015-02-12 11:54:26 +0000470 size_t line_len = std::min(len, LINE_SIZE);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000471 for (size_t i = 0; i < line_len; ++i) {
472 unsigned char ch = udata[i];
473 asc_line[i] = isprint(ch) ? ch : '.';
474 hex_line[i*2 + i/4] = hex_encode(ch >> 4);
475 hex_line[i*2 + i/4 + 1] = hex_encode(ch & 0xf);
476 }
477 asc_line[sizeof(asc_line)-1] = 0;
478 hex_line[sizeof(hex_line)-1] = 0;
479 LOG_V(level) << label << direction
480 << asc_line << " " << hex_line << " ";
481 udata += line_len;
482 len -= line_len;
483 }
484 return;
485 }
486
487 size_t consecutive_unprintable = state ? state->unprintable_count_[input] : 0;
488
489 const unsigned char* end = udata + len;
490 while (udata < end) {
491 const unsigned char* line = udata;
492 const unsigned char* end_of_line = strchrn<unsigned char>(udata,
493 end - udata,
494 '\n');
495 if (!end_of_line) {
496 udata = end_of_line = end;
497 } else {
498 udata = end_of_line + 1;
499 }
500
501 bool is_printable = true;
502
503 // If we are in unprintable mode, we need to see a line of at least
504 // kMinPrintableLine characters before we'll switch back.
505 const ptrdiff_t kMinPrintableLine = 4;
506 if (consecutive_unprintable && ((end_of_line - line) < kMinPrintableLine)) {
507 is_printable = false;
508 } else {
509 // Determine if the line contains only whitespace and printable
510 // characters.
511 bool is_entirely_whitespace = true;
512 for (const unsigned char* pos = line; pos < end_of_line; ++pos) {
513 if (isspace(*pos))
514 continue;
515 is_entirely_whitespace = false;
516 if (!isprint(*pos)) {
517 is_printable = false;
518 break;
519 }
520 }
521 // Treat an empty line following unprintable data as unprintable.
522 if (consecutive_unprintable && is_entirely_whitespace) {
523 is_printable = false;
524 }
525 }
526 if (!is_printable) {
527 consecutive_unprintable += (udata - line);
528 continue;
529 }
530 // Print out the current line, but prefix with a count of prior unprintable
531 // characters.
532 if (consecutive_unprintable) {
533 LOG_V(level) << label << direction << "## " << consecutive_unprintable
534 << " consecutive unprintable ##";
535 consecutive_unprintable = 0;
536 }
537 // Strip off trailing whitespace.
538 while ((end_of_line > line) && isspace(*(end_of_line-1))) {
539 --end_of_line;
540 }
541 // Filter out any private data
542 std::string substr(reinterpret_cast<const char*>(line), end_of_line - line);
543 std::string::size_type pos_private = substr.find("Email");
544 if (pos_private == std::string::npos) {
545 pos_private = substr.find("Passwd");
546 }
547 if (pos_private == std::string::npos) {
548 LOG_V(level) << label << direction << substr;
549 } else {
550 LOG_V(level) << label << direction << "## omitted for privacy ##";
551 }
552 }
553
554 if (state) {
555 state->unprintable_count_[input] = consecutive_unprintable;
556 }
557}
558
559//////////////////////////////////////////////////////////////////////
560
561} // namespace rtc