blob: 7ceb2798dd149271c6f0eea91a64f3eda94a08f1 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020042#include "rtc_base/criticalsection.h"
43#include "rtc_base/logging.h"
44#include "rtc_base/platform_thread.h"
45#include "rtc_base/stringencode.h"
46#include "rtc_base/stringutils.h"
47#include "rtc_base/timeutils.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000048
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).
danilchap3c6abd22017-09-06 05:46:29 -0700114LogMessage::StreamList LogMessage::streams_ RTC_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 Liue4ba6ce2015-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
danilchap3c6abd22017-09-06 05:46:29 -0700336void LogMessage::UpdateMinLogSeverity()
337 RTC_EXCLUSIVE_LOCKS_REQUIRED(g_log_crit) {
Tommi0eefb4d2015-05-23 09:54:07 +0200338 LoggingSeverity min_sev = dbg_sev_;
Peter Boström225789d2015-10-23 15:20:56 +0200339 for (auto& kv : streams_) {
340 min_sev = std::min(dbg_sev_, kv.second);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000341 }
342 min_sev_ = min_sev;
343}
344
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000345void LogMessage::OutputToDebug(const std::string& str,
jiayl66f0da22015-09-14 15:06:39 -0700346 LoggingSeverity severity,
347 const std::string& tag) {
andrew88703d72015-09-07 00:34:56 -0700348 bool log_to_stderr = log_to_stderr_;
tfarinaa41ab932015-10-30 16:08:48 -0700349#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) && defined(NDEBUG)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000350 // On the Mac, all stderr output goes to the Console log and causes clutter.
351 // So in opt builds, don't log to stderr unless the user specifically sets
352 // a preference to do so.
353 CFStringRef key = CFStringCreateWithCString(kCFAllocatorDefault,
354 "logToStdErr",
355 kCFStringEncodingUTF8);
356 CFStringRef domain = CFBundleGetIdentifier(CFBundleGetMainBundle());
deadbeef37f5ecf2017-02-27 14:06:41 -0800357 if (key != nullptr && domain != nullptr) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000358 Boolean exists_and_is_valid;
359 Boolean should_log =
360 CFPreferencesGetAppBooleanValue(key, domain, &exists_and_is_valid);
361 // If the key doesn't exist or is invalid or is false, we will not log to
362 // stderr.
363 log_to_stderr = exists_and_is_valid && should_log;
364 }
deadbeef37f5ecf2017-02-27 14:06:41 -0800365 if (key != nullptr) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000366 CFRelease(key);
367 }
368#endif
369#if defined(WEBRTC_WIN)
370 // Always log to the debugger.
371 // Perhaps stderr should be controlled by a preference, as on Mac?
372 OutputDebugStringA(str.c_str());
373 if (log_to_stderr) {
374 // This handles dynamically allocated consoles, too.
375 if (HANDLE error_handle = ::GetStdHandle(STD_ERROR_HANDLE)) {
376 log_to_stderr = false;
377 DWORD written = 0;
378 ::WriteFile(error_handle, str.data(), static_cast<DWORD>(str.size()),
379 &written, 0);
380 }
381 }
Tommi0eefb4d2015-05-23 09:54:07 +0200382#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000383#if defined(WEBRTC_ANDROID)
384 // Android's logging facility uses severity to log messages but we
385 // need to map libjingle's severity levels to Android ones first.
386 // Also write to stderr which maybe available to executable started
387 // from the shell.
388 int prio;
389 switch (severity) {
390 case LS_SENSITIVE:
jiayl66f0da22015-09-14 15:06:39 -0700391 __android_log_write(ANDROID_LOG_INFO, tag.c_str(), "SENSITIVE");
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000392 if (log_to_stderr) {
393 fprintf(stderr, "SENSITIVE");
394 fflush(stderr);
395 }
396 return;
397 case LS_VERBOSE:
398 prio = ANDROID_LOG_VERBOSE;
399 break;
400 case LS_INFO:
401 prio = ANDROID_LOG_INFO;
402 break;
403 case LS_WARNING:
404 prio = ANDROID_LOG_WARN;
405 break;
406 case LS_ERROR:
407 prio = ANDROID_LOG_ERROR;
408 break;
409 default:
410 prio = ANDROID_LOG_UNKNOWN;
411 }
412
413 int size = str.size();
414 int line = 0;
415 int idx = 0;
416 const int max_lines = size / kMaxLogLineSize + 1;
417 if (max_lines == 1) {
jiayl66f0da22015-09-14 15:06:39 -0700418 __android_log_print(prio, tag.c_str(), "%.*s", size, str.c_str());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000419 } else {
420 while (size > 0) {
421 const int len = std::min(size, kMaxLogLineSize);
422 // Use the size of the string in the format (str may have \0 in the
423 // middle).
jiayl66f0da22015-09-14 15:06:39 -0700424 __android_log_print(prio, tag.c_str(), "[%d/%d] %.*s",
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000425 line + 1, max_lines,
426 len, str.c_str() + idx);
427 idx += len;
428 size -= len;
429 ++line;
430 }
431 }
432#endif // WEBRTC_ANDROID
433 if (log_to_stderr) {
434 fprintf(stderr, "%s", str.c_str());
435 fflush(stderr);
436 }
437}
438
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000439//////////////////////////////////////////////////////////////////////
440// Logging Helpers
441//////////////////////////////////////////////////////////////////////
442
443void LogMultiline(LoggingSeverity level, const char* label, bool input,
444 const void* data, size_t len, bool hex_mode,
445 LogMultilineState* state) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100446 if (!RTC_LOG_CHECK_LEVEL_V(level))
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000447 return;
448
449 const char * direction = (input ? " << " : " >> ");
450
deadbeef37f5ecf2017-02-27 14:06:41 -0800451 // null data means to flush our count of unprintable characters.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000452 if (!data) {
453 if (state && state->unprintable_count_[input]) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100454 RTC_LOG_V(level) << label << direction << "## "
455 << state->unprintable_count_[input]
456 << " consecutive unprintable ##";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000457 state->unprintable_count_[input] = 0;
458 }
459 return;
460 }
461
462 // The ctype classification functions want unsigned chars.
463 const unsigned char* udata = static_cast<const unsigned char*>(data);
464
465 if (hex_mode) {
466 const size_t LINE_SIZE = 24;
467 char hex_line[LINE_SIZE * 9 / 4 + 2], asc_line[LINE_SIZE + 1];
468 while (len > 0) {
469 memset(asc_line, ' ', sizeof(asc_line));
470 memset(hex_line, ' ', sizeof(hex_line));
andresp@webrtc.orgff689be2015-02-12 11:54:26 +0000471 size_t line_len = std::min(len, LINE_SIZE);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000472 for (size_t i = 0; i < line_len; ++i) {
473 unsigned char ch = udata[i];
474 asc_line[i] = isprint(ch) ? ch : '.';
475 hex_line[i*2 + i/4] = hex_encode(ch >> 4);
476 hex_line[i*2 + i/4 + 1] = hex_encode(ch & 0xf);
477 }
478 asc_line[sizeof(asc_line)-1] = 0;
479 hex_line[sizeof(hex_line)-1] = 0;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100480 RTC_LOG_V(level) << label << direction << asc_line << " " << hex_line
481 << " ";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000482 udata += line_len;
483 len -= line_len;
484 }
485 return;
486 }
487
488 size_t consecutive_unprintable = state ? state->unprintable_count_[input] : 0;
489
490 const unsigned char* end = udata + len;
491 while (udata < end) {
492 const unsigned char* line = udata;
493 const unsigned char* end_of_line = strchrn<unsigned char>(udata,
494 end - udata,
495 '\n');
496 if (!end_of_line) {
497 udata = end_of_line = end;
498 } else {
499 udata = end_of_line + 1;
500 }
501
502 bool is_printable = true;
503
504 // If we are in unprintable mode, we need to see a line of at least
505 // kMinPrintableLine characters before we'll switch back.
506 const ptrdiff_t kMinPrintableLine = 4;
507 if (consecutive_unprintable && ((end_of_line - line) < kMinPrintableLine)) {
508 is_printable = false;
509 } else {
510 // Determine if the line contains only whitespace and printable
511 // characters.
512 bool is_entirely_whitespace = true;
513 for (const unsigned char* pos = line; pos < end_of_line; ++pos) {
514 if (isspace(*pos))
515 continue;
516 is_entirely_whitespace = false;
517 if (!isprint(*pos)) {
518 is_printable = false;
519 break;
520 }
521 }
522 // Treat an empty line following unprintable data as unprintable.
523 if (consecutive_unprintable && is_entirely_whitespace) {
524 is_printable = false;
525 }
526 }
527 if (!is_printable) {
528 consecutive_unprintable += (udata - line);
529 continue;
530 }
531 // Print out the current line, but prefix with a count of prior unprintable
532 // characters.
533 if (consecutive_unprintable) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100534 RTC_LOG_V(level) << label << direction << "## " << consecutive_unprintable
535 << " consecutive unprintable ##";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000536 consecutive_unprintable = 0;
537 }
538 // Strip off trailing whitespace.
539 while ((end_of_line > line) && isspace(*(end_of_line-1))) {
540 --end_of_line;
541 }
542 // Filter out any private data
543 std::string substr(reinterpret_cast<const char*>(line), end_of_line - line);
544 std::string::size_type pos_private = substr.find("Email");
545 if (pos_private == std::string::npos) {
546 pos_private = substr.find("Passwd");
547 }
548 if (pos_private == std::string::npos) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100549 RTC_LOG_V(level) << label << direction << substr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000550 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100551 RTC_LOG_V(level) << label << direction << "## omitted for privacy ##";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000552 }
553 }
554
555 if (state) {
556 state->unprintable_count_[input] = consecutive_unprintable;
557 }
558}
559
560//////////////////////////////////////////////////////////////////////
561
562} // namespace rtc