You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

635 lines
20 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2011, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifdef WIN32
  28. #define WIN32_LEAN_AND_MEAN
  29. #include <windows.h>
  30. #define snprintf _snprintf
  31. #undef ERROR // wingdi.h
  32. #endif
  33. #ifdef OSX
  34. #include <CoreServices/CoreServices.h>
  35. #elif defined(ANDROID)
  36. #include <android/log.h>
  37. static const char kLibjingle[] = "libjingle";
  38. // Android has a 1024 limit on log inputs. We use 60 chars as an
  39. // approx for the header/tag portion.
  40. // See android/system/core/liblog/logd_write.c
  41. static const int kMaxLogLineSize = 1024 - 60;
  42. #endif // OSX || ANDROID
  43. #include <time.h>
  44. #include <ostream>
  45. #include <iomanip>
  46. #include <limits.h>
  47. #include <vector>
  48. #include "talk/base/logging.h"
  49. #include "talk/base/stream.h"
  50. #include "talk/base/stringencode.h"
  51. #include "talk/base/stringutils.h"
  52. #include "talk/base/timeutils.h"
  53. namespace talk_base {
  54. /////////////////////////////////////////////////////////////////////////////
  55. // Constant Labels
  56. /////////////////////////////////////////////////////////////////////////////
  57. const char * FindLabel(int value, const ConstantLabel entries[]) {
  58. for (int i = 0; entries[i].label; ++i) {
  59. if (value == entries[i].value) {
  60. return entries[i].label;
  61. }
  62. }
  63. return 0;
  64. }
  65. std::string ErrorName(int err, const ConstantLabel * err_table) {
  66. if (err == 0)
  67. return "No error";
  68. if (err_table != 0) {
  69. if (const char * value = FindLabel(err, err_table))
  70. return value;
  71. }
  72. char buffer[16];
  73. snprintf(buffer, sizeof(buffer), "0x%08x", err);
  74. return buffer;
  75. }
  76. /////////////////////////////////////////////////////////////////////////////
  77. // LogMessage
  78. /////////////////////////////////////////////////////////////////////////////
  79. const int LogMessage::NO_LOGGING = LS_ERROR + 1;
  80. #if _DEBUG
  81. static const int LOG_DEFAULT = LS_INFO;
  82. #else // !_DEBUG
  83. static const int LOG_DEFAULT = LogMessage::NO_LOGGING;
  84. #endif // !_DEBUG
  85. // Global lock for log subsystem, only needed to serialize access to streams_.
  86. CriticalSection LogMessage::crit_;
  87. // By default, release builds don't log, debug builds at info level
  88. int LogMessage::min_sev_ = LOG_DEFAULT;
  89. int LogMessage::dbg_sev_ = LOG_DEFAULT;
  90. // Don't bother printing context for the ubiquitous INFO log messages
  91. int LogMessage::ctx_sev_ = LS_WARNING;
  92. // The list of logging streams currently configured.
  93. // Note: we explicitly do not clean this up, because of the uncertain ordering
  94. // of destructors at program exit. Let the person who sets the stream trigger
  95. // cleanup by setting to NULL, or let it leak (safe at program exit).
  96. LogMessage::StreamList LogMessage::streams_;
  97. // Boolean options default to false (0)
  98. bool LogMessage::thread_, LogMessage::timestamp_;
  99. // If we're in diagnostic mode, we'll be explicitly set that way; default=false.
  100. bool LogMessage::is_diagnostic_mode_ = false;
  101. LogMessage::LogMessage(const char* file, int line, LoggingSeverity sev,
  102. LogErrorContext err_ctx, int err, const char* module)
  103. : severity_(sev),
  104. warn_slow_logs_delay_(WARN_SLOW_LOGS_DELAY) {
  105. // Android's logging facility keeps track of timestamp and thread.
  106. #ifndef ANDROID
  107. if (timestamp_) {
  108. uint32 time = TimeSince(LogStartTime());
  109. // Also ensure WallClockStartTime is initialized, so that it matches
  110. // LogStartTime.
  111. WallClockStartTime();
  112. print_stream_ << "[" << std::setfill('0') << std::setw(3) << (time / 1000)
  113. << ":" << std::setw(3) << (time % 1000) << std::setfill(' ')
  114. << "] ";
  115. }
  116. if (thread_) {
  117. #ifdef WIN32
  118. DWORD id = GetCurrentThreadId();
  119. print_stream_ << "[" << std::hex << id << std::dec << "] ";
  120. #endif // WIN32
  121. }
  122. #endif // !ANDROID
  123. if (severity_ >= ctx_sev_) {
  124. print_stream_ << Describe(sev) << "(" << DescribeFile(file)
  125. << ":" << line << "): ";
  126. }
  127. if (err_ctx != ERRCTX_NONE) {
  128. std::ostringstream tmp;
  129. tmp << "[0x" << std::setfill('0') << std::hex << std::setw(8) << err << "]";
  130. switch (err_ctx) {
  131. case ERRCTX_ERRNO:
  132. tmp << " " << strerror(err);
  133. break;
  134. #if WIN32
  135. case ERRCTX_HRESULT: {
  136. char msgbuf[256];
  137. DWORD flags = FORMAT_MESSAGE_FROM_SYSTEM;
  138. HMODULE hmod = GetModuleHandleA(module);
  139. if (hmod)
  140. flags |= FORMAT_MESSAGE_FROM_HMODULE;
  141. if (DWORD len = FormatMessageA(
  142. flags, hmod, err,
  143. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  144. msgbuf, sizeof(msgbuf) / sizeof(msgbuf[0]), NULL)) {
  145. while ((len > 0) &&
  146. isspace(static_cast<unsigned char>(msgbuf[len-1]))) {
  147. msgbuf[--len] = 0;
  148. }
  149. tmp << " " << msgbuf;
  150. }
  151. break;
  152. }
  153. #endif // WIN32
  154. #if OSX
  155. case ERRCTX_OSSTATUS: {
  156. tmp << " " << nonnull(GetMacOSStatusErrorString(err), "Unknown error");
  157. if (const char* desc = GetMacOSStatusCommentString(err)) {
  158. tmp << ": " << desc;
  159. }
  160. break;
  161. }
  162. #endif // OSX
  163. default:
  164. break;
  165. }
  166. extra_ = tmp.str();
  167. }
  168. }
  169. LogMessage::~LogMessage() {
  170. if (!extra_.empty())
  171. print_stream_ << " : " << extra_;
  172. print_stream_ << std::endl;
  173. const std::string& str = print_stream_.str();
  174. if (severity_ >= dbg_sev_) {
  175. OutputToDebug(str, severity_);
  176. }
  177. uint32 before = Time();
  178. // Must lock streams_ before accessing
  179. CritScope cs(&crit_);
  180. for (StreamList::iterator it = streams_.begin(); it != streams_.end(); ++it) {
  181. if (severity_ >= it->second) {
  182. OutputToStream(it->first, str);
  183. }
  184. }
  185. uint32 delay = TimeSince(before);
  186. if (delay >= warn_slow_logs_delay_) {
  187. LogMessage slow_log_warning =
  188. talk_base::LogMessage(__FILE__, __LINE__, LS_WARNING);
  189. // If our warning is slow, we don't want to warn about it, because
  190. // that would lead to inifinite recursion. So, give a really big
  191. // number for the delay threshold.
  192. slow_log_warning.warn_slow_logs_delay_ = UINT_MAX;
  193. slow_log_warning.stream() << "Slow log: took " << delay << "ms to write "
  194. << str.size() << " bytes.";
  195. }
  196. }
  197. uint32 LogMessage::LogStartTime() {
  198. static const uint32 g_start = Time();
  199. return g_start;
  200. }
  201. uint32 LogMessage::WallClockStartTime() {
  202. static const uint32 g_start_wallclock = time(NULL);
  203. return g_start_wallclock;
  204. }
  205. void LogMessage::LogContext(int min_sev) {
  206. ctx_sev_ = min_sev;
  207. }
  208. void LogMessage::LogThreads(bool on) {
  209. thread_ = on;
  210. }
  211. void LogMessage::LogTimestamps(bool on) {
  212. timestamp_ = on;
  213. }
  214. void LogMessage::LogToDebug(int min_sev) {
  215. dbg_sev_ = min_sev;
  216. UpdateMinLogSeverity();
  217. }
  218. void LogMessage::LogToStream(StreamInterface* stream, int min_sev) {
  219. CritScope cs(&crit_);
  220. // Discard and delete all previously installed streams
  221. for (StreamList::iterator it = streams_.begin(); it != streams_.end(); ++it) {
  222. delete it->first;
  223. }
  224. streams_.clear();
  225. // Install the new stream, if specified
  226. if (stream) {
  227. AddLogToStream(stream, min_sev);
  228. }
  229. }
  230. int LogMessage::GetLogToStream(StreamInterface* stream) {
  231. CritScope cs(&crit_);
  232. int sev = NO_LOGGING;
  233. for (StreamList::iterator it = streams_.begin(); it != streams_.end(); ++it) {
  234. if (!stream || stream == it->first) {
  235. sev = _min(sev, it->second);
  236. }
  237. }
  238. return sev;
  239. }
  240. void LogMessage::AddLogToStream(StreamInterface* stream, int min_sev) {
  241. CritScope cs(&crit_);
  242. streams_.push_back(std::make_pair(stream, min_sev));
  243. UpdateMinLogSeverity();
  244. }
  245. void LogMessage::RemoveLogToStream(StreamInterface* stream) {
  246. CritScope cs(&crit_);
  247. for (StreamList::iterator it = streams_.begin(); it != streams_.end(); ++it) {
  248. if (stream == it->first) {
  249. streams_.erase(it);
  250. break;
  251. }
  252. }
  253. UpdateMinLogSeverity();
  254. }
  255. void LogMessage::ConfigureLogging(const char* params, const char* filename) {
  256. int current_level = LS_VERBOSE;
  257. int debug_level = GetLogToDebug();
  258. int file_level = GetLogToStream();
  259. std::vector<std::string> tokens;
  260. tokenize(params, ' ', &tokens);
  261. for (size_t i = 0; i < tokens.size(); ++i) {
  262. if (tokens[i].empty())
  263. continue;
  264. // Logging features
  265. if (tokens[i] == "tstamp") {
  266. LogTimestamps();
  267. } else if (tokens[i] == "thread") {
  268. LogThreads();
  269. // Logging levels
  270. } else if (tokens[i] == "sensitive") {
  271. current_level = LS_SENSITIVE;
  272. } else if (tokens[i] == "verbose") {
  273. current_level = LS_VERBOSE;
  274. } else if (tokens[i] == "info") {
  275. current_level = LS_INFO;
  276. } else if (tokens[i] == "warning") {
  277. current_level = LS_WARNING;
  278. } else if (tokens[i] == "error") {
  279. current_level = LS_ERROR;
  280. } else if (tokens[i] == "none") {
  281. current_level = NO_LOGGING;
  282. // Logging targets
  283. } else if (tokens[i] == "file") {
  284. file_level = current_level;
  285. } else if (tokens[i] == "debug") {
  286. debug_level = current_level;
  287. }
  288. }
  289. #ifdef WIN32
  290. if ((NO_LOGGING != debug_level) && !::IsDebuggerPresent()) {
  291. // First, attempt to attach to our parent's console... so if you invoke
  292. // from the command line, we'll see the output there. Otherwise, create
  293. // our own console window.
  294. // Note: These methods fail if a console already exists, which is fine.
  295. bool success = false;
  296. typedef BOOL (WINAPI* PFN_AttachConsole)(DWORD);
  297. if (HINSTANCE kernel32 = ::LoadLibrary(L"kernel32.dll")) {
  298. // AttachConsole is defined on WinXP+.
  299. if (PFN_AttachConsole attach_console = reinterpret_cast<PFN_AttachConsole>
  300. (::GetProcAddress(kernel32, "AttachConsole"))) {
  301. success = (FALSE != attach_console(ATTACH_PARENT_PROCESS));
  302. }
  303. ::FreeLibrary(kernel32);
  304. }
  305. if (!success) {
  306. ::AllocConsole();
  307. }
  308. }
  309. #endif // WIN32
  310. scoped_ptr<FileStream> stream;
  311. if (NO_LOGGING != file_level) {
  312. stream.reset(new FileStream);
  313. if (!stream->Open(filename, "wb", NULL) || !stream->DisableBuffering()) {
  314. stream.reset();
  315. }
  316. }
  317. LogToDebug(debug_level);
  318. LogToStream(stream.release(), file_level);
  319. }
  320. int LogMessage::ParseLogSeverity(const std::string& value) {
  321. int level = NO_LOGGING;
  322. if (value == "LS_SENSITIVE") {
  323. level = LS_SENSITIVE;
  324. } else if (value == "LS_VERBOSE") {
  325. level = LS_VERBOSE;
  326. } else if (value == "LS_INFO") {
  327. level = LS_INFO;
  328. } else if (value == "LS_WARNING") {
  329. level = LS_WARNING;
  330. } else if (value == "LS_ERROR") {
  331. level = LS_ERROR;
  332. } else if (isdigit(value[0])) {
  333. level = atoi(value.c_str()); // NOLINT
  334. }
  335. return level;
  336. }
  337. void LogMessage::UpdateMinLogSeverity() {
  338. int min_sev = dbg_sev_;
  339. for (StreamList::iterator it = streams_.begin(); it != streams_.end(); ++it) {
  340. min_sev = _min(dbg_sev_, it->second);
  341. }
  342. min_sev_ = min_sev;
  343. }
  344. const char* LogMessage::Describe(LoggingSeverity sev) {
  345. switch (sev) {
  346. case LS_SENSITIVE: return "Sensitive";
  347. case LS_VERBOSE: return "Verbose";
  348. case LS_INFO: return "Info";
  349. case LS_WARNING: return "Warning";
  350. case LS_ERROR: return "Error";
  351. default: return "<unknown>";
  352. }
  353. }
  354. const char* LogMessage::DescribeFile(const char* file) {
  355. const char* end1 = ::strrchr(file, '/');
  356. const char* end2 = ::strrchr(file, '\\');
  357. if (!end1 && !end2)
  358. return file;
  359. else
  360. return (end1 > end2) ? end1 + 1 : end2 + 1;
  361. }
  362. void LogMessage::OutputToDebug(const std::string& str,
  363. LoggingSeverity severity) {
  364. bool log_to_stderr = true;
  365. #if defined(OSX) && (!defined(DEBUG) || defined(NDEBUG))
  366. // On the Mac, all stderr output goes to the Console log and causes clutter.
  367. // So in opt builds, don't log to stderr unless the user specifically sets
  368. // a preference to do so.
  369. CFStringRef key = CFStringCreateWithCString(kCFAllocatorDefault,
  370. "logToStdErr",
  371. kCFStringEncodingUTF8);
  372. CFStringRef domain = CFBundleGetIdentifier(CFBundleGetMainBundle());
  373. if (key != NULL && domain != NULL) {
  374. Boolean exists_and_is_valid;
  375. Boolean should_log =
  376. CFPreferencesGetAppBooleanValue(key, domain, &exists_and_is_valid);
  377. // If the key doesn't exist or is invalid or is false, we will not log to
  378. // stderr.
  379. log_to_stderr = exists_and_is_valid && should_log;
  380. }
  381. if (key != NULL) {
  382. CFRelease(key);
  383. }
  384. #endif
  385. #ifdef WIN32
  386. // Always log to the debugger.
  387. // Perhaps stderr should be controlled by a preference, as on Mac?
  388. OutputDebugStringA(str.c_str());
  389. if (log_to_stderr) {
  390. // This handles dynamically allocated consoles, too.
  391. if (HANDLE error_handle = ::GetStdHandle(STD_ERROR_HANDLE)) {
  392. log_to_stderr = false;
  393. DWORD written = 0;
  394. ::WriteFile(error_handle, str.data(), static_cast<DWORD>(str.size()),
  395. &written, 0);
  396. }
  397. }
  398. #endif // WIN32
  399. #ifdef ANDROID
  400. // Android's logging facility uses severity to log messages but we
  401. // need to map libjingle's severity levels to Android ones first.
  402. // Also write to stderr which maybe available to executable started
  403. // from the shell.
  404. int prio;
  405. switch (severity) {
  406. case LS_SENSITIVE:
  407. __android_log_write(ANDROID_LOG_INFO, kLibjingle, "SENSITIVE");
  408. if (log_to_stderr) {
  409. fprintf(stderr, "SENSITIVE");
  410. fflush(stderr);
  411. }
  412. return;
  413. case LS_VERBOSE:
  414. prio = ANDROID_LOG_VERBOSE;
  415. break;
  416. case LS_INFO:
  417. prio = ANDROID_LOG_INFO;
  418. break;
  419. case LS_WARNING:
  420. prio = ANDROID_LOG_WARN;
  421. break;
  422. case LS_ERROR:
  423. prio = ANDROID_LOG_ERROR;
  424. break;
  425. default:
  426. prio = ANDROID_LOG_UNKNOWN;
  427. }
  428. int size = str.size();
  429. int line = 0;
  430. int idx = 0;
  431. const int max_lines = size / kMaxLogLineSize + 1;
  432. if (max_lines == 1) {
  433. __android_log_print(prio, kLibjingle, "%.*s", size, str.c_str());
  434. } else {
  435. while (size > 0) {
  436. const int len = std::min(size, kMaxLogLineSize);
  437. // Use the size of the string in the format (str may have \0 in the
  438. // middle).
  439. __android_log_print(prio, kLibjingle, "[%d/%d] %.*s",
  440. line + 1, max_lines,
  441. len, str.c_str() + idx);
  442. idx += len;
  443. size -= len;
  444. ++line;
  445. }
  446. }
  447. #endif // ANDROID
  448. if (log_to_stderr) {
  449. fprintf(stderr, "%s", str.c_str());
  450. fflush(stderr);
  451. }
  452. }
  453. void LogMessage::OutputToStream(StreamInterface* stream,
  454. const std::string& str) {
  455. // If write isn't fully successful, what are we going to do, log it? :)
  456. stream->WriteAll(str.data(), str.size(), NULL, NULL);
  457. }
  458. //////////////////////////////////////////////////////////////////////
  459. // Logging Helpers
  460. //////////////////////////////////////////////////////////////////////
  461. void LogMultiline(LoggingSeverity level, const char* label, bool input,
  462. const void* data, size_t len, bool hex_mode,
  463. LogMultilineState* state) {
  464. if (!LOG_CHECK_LEVEL_V(level))
  465. return;
  466. const char * direction = (input ? " << " : " >> ");
  467. // NULL data means to flush our count of unprintable characters.
  468. if (!data) {
  469. if (state && state->unprintable_count_[input]) {
  470. LOG_V(level) << label << direction << "## "
  471. << state->unprintable_count_[input]
  472. << " consecutive unprintable ##";
  473. state->unprintable_count_[input] = 0;
  474. }
  475. return;
  476. }
  477. // The ctype classification functions want unsigned chars.
  478. const unsigned char* udata = static_cast<const unsigned char*>(data);
  479. if (hex_mode) {
  480. const size_t LINE_SIZE = 24;
  481. char hex_line[LINE_SIZE * 9 / 4 + 2], asc_line[LINE_SIZE + 1];
  482. while (len > 0) {
  483. memset(asc_line, ' ', sizeof(asc_line));
  484. memset(hex_line, ' ', sizeof(hex_line));
  485. size_t line_len = _min(len, LINE_SIZE);
  486. for (size_t i = 0; i < line_len; ++i) {
  487. unsigned char ch = udata[i];
  488. asc_line[i] = isprint(ch) ? ch : '.';
  489. hex_line[i*2 + i/4] = hex_encode(ch >> 4);
  490. hex_line[i*2 + i/4 + 1] = hex_encode(ch & 0xf);
  491. }
  492. asc_line[sizeof(asc_line)-1] = 0;
  493. hex_line[sizeof(hex_line)-1] = 0;
  494. LOG_V(level) << label << direction
  495. << asc_line << " " << hex_line << " ";
  496. udata += line_len;
  497. len -= line_len;
  498. }
  499. return;
  500. }
  501. size_t consecutive_unprintable = state ? state->unprintable_count_[input] : 0;
  502. const unsigned char* end = udata + len;
  503. while (udata < end) {
  504. const unsigned char* line = udata;
  505. const unsigned char* end_of_line = strchrn<unsigned char>(udata,
  506. end - udata,
  507. '\n');
  508. if (!end_of_line) {
  509. udata = end_of_line = end;
  510. } else {
  511. udata = end_of_line + 1;
  512. }
  513. bool is_printable = true;
  514. // If we are in unprintable mode, we need to see a line of at least
  515. // kMinPrintableLine characters before we'll switch back.
  516. const ptrdiff_t kMinPrintableLine = 4;
  517. if (consecutive_unprintable && ((end_of_line - line) < kMinPrintableLine)) {
  518. is_printable = false;
  519. } else {
  520. // Determine if the line contains only whitespace and printable
  521. // characters.
  522. bool is_entirely_whitespace = true;
  523. for (const unsigned char* pos = line; pos < end_of_line; ++pos) {
  524. if (isspace(*pos))
  525. continue;
  526. is_entirely_whitespace = false;
  527. if (!isprint(*pos)) {
  528. is_printable = false;
  529. break;
  530. }
  531. }
  532. // Treat an empty line following unprintable data as unprintable.
  533. if (consecutive_unprintable && is_entirely_whitespace) {
  534. is_printable = false;
  535. }
  536. }
  537. if (!is_printable) {
  538. consecutive_unprintable += (udata - line);
  539. continue;
  540. }
  541. // Print out the current line, but prefix with a count of prior unprintable
  542. // characters.
  543. if (consecutive_unprintable) {
  544. LOG_V(level) << label << direction << "## " << consecutive_unprintable
  545. << " consecutive unprintable ##";
  546. consecutive_unprintable = 0;
  547. }
  548. // Strip off trailing whitespace.
  549. while ((end_of_line > line) && isspace(*(end_of_line-1))) {
  550. --end_of_line;
  551. }
  552. // Filter out any private data
  553. std::string substr(reinterpret_cast<const char*>(line), end_of_line - line);
  554. std::string::size_type pos_private = substr.find("Email");
  555. if (pos_private == std::string::npos) {
  556. pos_private = substr.find("Passwd");
  557. }
  558. if (pos_private == std::string::npos) {
  559. LOG_V(level) << label << direction << substr;
  560. } else {
  561. LOG_V(level) << label << direction << "## omitted for privacy ##";
  562. }
  563. }
  564. if (state) {
  565. state->unprintable_count_[input] = consecutive_unprintable;
  566. }
  567. }
  568. //////////////////////////////////////////////////////////////////////
  569. } // namespace talk_base