147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org/*
247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org *  Copyright 2004 The WebRTC Project Authors. All rights reserved.
347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org *
447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org *  Use of this source code is governed by a BSD-style license
547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org *  that can be found in the LICENSE file in the root of the source
647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org *  tree. An additional intellectual property rights grant can be found
747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org *  in the file PATENTS.  All contributing project authors may
847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org *  be found in the AUTHORS file in the root of the source tree.
947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org */
1047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
1147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#if defined(WEBRTC_WIN)
1247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#define WIN32_LEAN_AND_MEAN
1347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include <windows.h>
1447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#define snprintf _snprintf
1547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#undef ERROR  // wingdi.h
1647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#endif
1747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
1847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS)
1947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include <CoreServices/CoreServices.h>
2047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#elif defined(WEBRTC_ANDROID)
2147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include <android/log.h>
2247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgstatic const char kLibjingle[] = "libjingle";
2347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// Android has a 1024 limit on log inputs. We use 60 chars as an
2447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// approx for the header/tag portion.
2547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// See android/system/core/liblog/logd_write.c
2647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgstatic const int kMaxLogLineSize = 1024 - 60;
2747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#endif  // WEBRTC_MAC && !defined(WEBRTC_IOS) || WEBRTC_ANDROID
2847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
2947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include <time.h>
3047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
3147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include <ostream>
3247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include <iomanip>
3347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include <limits.h>
3447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include <vector>
3547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
3647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include "webrtc/base/logging.h"
3747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include "webrtc/base/stream.h"
3847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include "webrtc/base/stringencode.h"
3947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include "webrtc/base/stringutils.h"
4047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include "webrtc/base/timeutils.h"
4147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
4247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgnamespace rtc {
4347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
4447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org/////////////////////////////////////////////////////////////////////////////
4547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// Constant Labels
4647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org/////////////////////////////////////////////////////////////////////////////
4747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
4847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgconst char * FindLabel(int value, const ConstantLabel entries[]) {
4947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  for (int i = 0; entries[i].label; ++i) {
5047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    if (value == entries[i].value) {
5147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      return entries[i].label;
5247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    }
5347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
5447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  return 0;
5547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
5647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
5747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgstd::string ErrorName(int err, const ConstantLabel * err_table) {
5847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  if (err == 0)
5947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    return "No error";
6047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
6147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  if (err_table != 0) {
6247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    if (const char * value = FindLabel(err, err_table))
6347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      return value;
6447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
6547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
6647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  char buffer[16];
6747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  snprintf(buffer, sizeof(buffer), "0x%08x", err);
6847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  return buffer;
6947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
7047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
7147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org/////////////////////////////////////////////////////////////////////////////
7247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// LogMessage
7347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org/////////////////////////////////////////////////////////////////////////////
7447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
7547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgconst int LogMessage::NO_LOGGING = LS_ERROR + 1;
7647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
7747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#if _DEBUG
7847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgstatic const int LOG_DEFAULT = LS_INFO;
7947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#else  // !_DEBUG
8047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgstatic const int LOG_DEFAULT = LogMessage::NO_LOGGING;
8147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#endif  // !_DEBUG
8247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
8347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// Global lock for log subsystem, only needed to serialize access to streams_.
8447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgCriticalSection LogMessage::crit_;
8547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
8647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// By default, release builds don't log, debug builds at info level
8747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgint LogMessage::min_sev_ = LOG_DEFAULT;
8847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgint LogMessage::dbg_sev_ = LOG_DEFAULT;
8947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
9047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// Don't bother printing context for the ubiquitous INFO log messages
9147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgint LogMessage::ctx_sev_ = LS_WARNING;
9247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
9347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// The list of logging streams currently configured.
9447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// Note: we explicitly do not clean this up, because of the uncertain ordering
9547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// of destructors at program exit.  Let the person who sets the stream trigger
9647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// cleanup by setting to NULL, or let it leak (safe at program exit).
9747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgLogMessage::StreamList LogMessage::streams_;
9847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
9947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// Boolean options default to false (0)
10047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgbool LogMessage::thread_, LogMessage::timestamp_;
10147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
10247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// If we're in diagnostic mode, we'll be explicitly set that way; default=false.
10347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgbool LogMessage::is_diagnostic_mode_ = false;
10447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
10547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgLogMessage::LogMessage(const char* file, int line, LoggingSeverity sev,
10647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org                       LogErrorContext err_ctx, int err, const char* module)
10747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    : severity_(sev),
10847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      warn_slow_logs_delay_(WARN_SLOW_LOGS_DELAY) {
10947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  if (timestamp_) {
11047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    uint32 time = TimeSince(LogStartTime());
11147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    // Also ensure WallClockStartTime is initialized, so that it matches
11247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    // LogStartTime.
11347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    WallClockStartTime();
11447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    print_stream_ << "[" << std::setfill('0') << std::setw(3) << (time / 1000)
11547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org                  << ":" << std::setw(3) << (time % 1000) << std::setfill(' ')
11647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org                  << "] ";
11747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
11847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
11947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  if (thread_) {
12047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#if defined(WEBRTC_WIN)
12147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    DWORD id = GetCurrentThreadId();
12247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    print_stream_ << "[" << std::hex << id << std::dec << "] ";
12347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#endif  // WEBRTC_WIN
12447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
12547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
12647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  if (severity_ >= ctx_sev_) {
12747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    print_stream_ << Describe(sev) << "(" << DescribeFile(file)
12847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org                  << ":" << line << "): ";
12947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
13047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
13147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  if (err_ctx != ERRCTX_NONE) {
13247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    std::ostringstream tmp;
13347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    tmp << "[0x" << std::setfill('0') << std::hex << std::setw(8) << err << "]";
13447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    switch (err_ctx) {
13547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      case ERRCTX_ERRNO:
13647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org        tmp << " " << strerror(err);
13747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org        break;
13847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#if WEBRTC_WIN
13947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      case ERRCTX_HRESULT: {
14047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org        char msgbuf[256];
14147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org        DWORD flags = FORMAT_MESSAGE_FROM_SYSTEM;
14247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org        HMODULE hmod = GetModuleHandleA(module);
14347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org        if (hmod)
14447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org          flags |= FORMAT_MESSAGE_FROM_HMODULE;
14547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org        if (DWORD len = FormatMessageA(
14647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org            flags, hmod, err,
14747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org            MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
14847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org            msgbuf, sizeof(msgbuf) / sizeof(msgbuf[0]), NULL)) {
14947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org          while ((len > 0) &&
15047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org              isspace(static_cast<unsigned char>(msgbuf[len-1]))) {
15147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org            msgbuf[--len] = 0;
15247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org          }
15347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org          tmp << " " << msgbuf;
15447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org        }
15547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org        break;
15647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      }
15747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#endif  // WEBRTC_WIN
15847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS)
15947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      case ERRCTX_OSSTATUS: {
16047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org        tmp << " " << nonnull(GetMacOSStatusErrorString(err), "Unknown error");
16147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org        if (const char* desc = GetMacOSStatusCommentString(err)) {
16247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org          tmp << ": " << desc;
16347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org        }
16447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org        break;
16547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      }
16647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#endif  // WEBRTC_MAC && !defined(WEBRTC_IOS)
16747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      default:
16847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org        break;
16947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    }
17047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    extra_ = tmp.str();
17147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
17247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
17347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
17447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgLogMessage::~LogMessage() {
17547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  if (!extra_.empty())
17647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    print_stream_ << " : " << extra_;
17747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  print_stream_ << std::endl;
17847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
17947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  const std::string& str = print_stream_.str();
18047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  if (severity_ >= dbg_sev_) {
18147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    OutputToDebug(str, severity_);
18247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
18347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
18447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  uint32 before = Time();
18547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // Must lock streams_ before accessing
18647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  CritScope cs(&crit_);
18747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  for (StreamList::iterator it = streams_.begin(); it != streams_.end(); ++it) {
18847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    if (severity_ >= it->second) {
18947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      OutputToStream(it->first, str);
19047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    }
19147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
19247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  uint32 delay = TimeSince(before);
19347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  if (delay >= warn_slow_logs_delay_) {
19447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    LogMessage slow_log_warning =
19547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org        rtc::LogMessage(__FILE__, __LINE__, LS_WARNING);
19647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    // If our warning is slow, we don't want to warn about it, because
19747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    // that would lead to inifinite recursion.  So, give a really big
19847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    // number for the delay threshold.
19947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    slow_log_warning.warn_slow_logs_delay_ = UINT_MAX;
20047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    slow_log_warning.stream() << "Slow log: took " << delay << "ms to write "
20147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org                              << str.size() << " bytes.";
20247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
20347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
20447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
20547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orguint32 LogMessage::LogStartTime() {
20647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  static const uint32 g_start = Time();
20747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  return g_start;
20847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
20947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
21047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orguint32 LogMessage::WallClockStartTime() {
21147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  static const uint32 g_start_wallclock = time(NULL);
21247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  return g_start_wallclock;
21347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
21447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
21547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgvoid LogMessage::LogContext(int min_sev) {
21647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  ctx_sev_ = min_sev;
21747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
21847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
21947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgvoid LogMessage::LogThreads(bool on) {
22047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  thread_ = on;
22147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
22247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
22347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgvoid LogMessage::LogTimestamps(bool on) {
22447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  timestamp_ = on;
22547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
22647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
22747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgvoid LogMessage::LogToDebug(int min_sev) {
22847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  dbg_sev_ = min_sev;
22947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  UpdateMinLogSeverity();
23047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
23147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
23247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgvoid LogMessage::LogToStream(StreamInterface* stream, int min_sev) {
23347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  CritScope cs(&crit_);
23447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // Discard and delete all previously installed streams
23547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  for (StreamList::iterator it = streams_.begin(); it != streams_.end(); ++it) {
23647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    delete it->first;
23747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
23847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  streams_.clear();
23947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // Install the new stream, if specified
24047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  if (stream) {
24147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    AddLogToStream(stream, min_sev);
24247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
24347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
24447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
24547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgint LogMessage::GetLogToStream(StreamInterface* stream) {
24647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  CritScope cs(&crit_);
24747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  int sev = NO_LOGGING;
24847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  for (StreamList::iterator it = streams_.begin(); it != streams_.end(); ++it) {
24947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    if (!stream || stream == it->first) {
25047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      sev = _min(sev, it->second);
25147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    }
25247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
25347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  return sev;
25447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
25547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
25647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgvoid LogMessage::AddLogToStream(StreamInterface* stream, int min_sev) {
25747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  CritScope cs(&crit_);
25847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  streams_.push_back(std::make_pair(stream, min_sev));
25947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  UpdateMinLogSeverity();
26047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
26147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
26247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgvoid LogMessage::RemoveLogToStream(StreamInterface* stream) {
26347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  CritScope cs(&crit_);
26447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  for (StreamList::iterator it = streams_.begin(); it != streams_.end(); ++it) {
26547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    if (stream == it->first) {
26647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      streams_.erase(it);
26747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      break;
26847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    }
26947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
27047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  UpdateMinLogSeverity();
27147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
27247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
27347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgvoid LogMessage::ConfigureLogging(const char* params, const char* filename) {
27447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  int current_level = LS_VERBOSE;
27547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  int debug_level = GetLogToDebug();
27647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  int file_level = GetLogToStream();
27747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
27847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  std::vector<std::string> tokens;
27947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  tokenize(params, ' ', &tokens);
28047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
28147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  for (size_t i = 0; i < tokens.size(); ++i) {
28247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    if (tokens[i].empty())
28347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      continue;
28447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
28547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    // Logging features
28647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    if (tokens[i] == "tstamp") {
28747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      LogTimestamps();
28847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    } else if (tokens[i] == "thread") {
28947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      LogThreads();
29047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
29147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    // Logging levels
29247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    } else if (tokens[i] == "sensitive") {
29347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      current_level = LS_SENSITIVE;
29447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    } else if (tokens[i] == "verbose") {
29547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      current_level = LS_VERBOSE;
29647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    } else if (tokens[i] == "info") {
29747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      current_level = LS_INFO;
29847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    } else if (tokens[i] == "warning") {
29947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      current_level = LS_WARNING;
30047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    } else if (tokens[i] == "error") {
30147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      current_level = LS_ERROR;
30247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    } else if (tokens[i] == "none") {
30347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      current_level = NO_LOGGING;
30447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
30547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    // Logging targets
30647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    } else if (tokens[i] == "file") {
30747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      file_level = current_level;
30847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    } else if (tokens[i] == "debug") {
30947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      debug_level = current_level;
31047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    }
31147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
31247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
31347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#if defined(WEBRTC_WIN)
31447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  if ((NO_LOGGING != debug_level) && !::IsDebuggerPresent()) {
31547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    // First, attempt to attach to our parent's console... so if you invoke
31647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    // from the command line, we'll see the output there.  Otherwise, create
31747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    // our own console window.
31847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    // Note: These methods fail if a console already exists, which is fine.
31947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    bool success = false;
32047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    typedef BOOL (WINAPI* PFN_AttachConsole)(DWORD);
32147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    if (HINSTANCE kernel32 = ::LoadLibrary(L"kernel32.dll")) {
32247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      // AttachConsole is defined on WinXP+.
32347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      if (PFN_AttachConsole attach_console = reinterpret_cast<PFN_AttachConsole>
32447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org            (::GetProcAddress(kernel32, "AttachConsole"))) {
32547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org        success = (FALSE != attach_console(ATTACH_PARENT_PROCESS));
32647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      }
32747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      ::FreeLibrary(kernel32);
32847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    }
32947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    if (!success) {
33047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      ::AllocConsole();
33147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    }
33247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
33347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#endif  // WEBRTC_WIN
33447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
33547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  LogToDebug(debug_level);
33647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
33747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#if !defined(__native_client__)  // No logging to file in NaCl.
33847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  scoped_ptr<FileStream> stream;
33947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  if (NO_LOGGING != file_level) {
34047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    stream.reset(new FileStream);
34147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    if (!stream->Open(filename, "wb", NULL) || !stream->DisableBuffering()) {
34247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      stream.reset();
34347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    }
34447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
34547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
34647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  LogToStream(stream.release(), file_level);
34747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#endif
34847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
34947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
35047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgint LogMessage::ParseLogSeverity(const std::string& value) {
35147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  int level = NO_LOGGING;
35247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  if (value == "LS_SENSITIVE") {
35347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    level = LS_SENSITIVE;
35447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  } else if (value == "LS_VERBOSE") {
35547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    level = LS_VERBOSE;
35647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  } else if (value == "LS_INFO") {
35747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    level = LS_INFO;
35847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  } else if (value == "LS_WARNING") {
35947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    level = LS_WARNING;
36047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  } else if (value == "LS_ERROR") {
36147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    level = LS_ERROR;
36247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  } else if (isdigit(value[0])) {
36347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    level = atoi(value.c_str());  // NOLINT
36447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
36547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  return level;
36647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
36747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
36847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgvoid LogMessage::UpdateMinLogSeverity() {
36947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  int min_sev = dbg_sev_;
37047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  for (StreamList::iterator it = streams_.begin(); it != streams_.end(); ++it) {
37147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    min_sev = _min(dbg_sev_, it->second);
37247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
37347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  min_sev_ = min_sev;
37447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
37547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
37647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgconst char* LogMessage::Describe(LoggingSeverity sev) {
37747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  switch (sev) {
37847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  case LS_SENSITIVE: return "Sensitive";
37947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  case LS_VERBOSE:   return "Verbose";
38047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  case LS_INFO:      return "Info";
38147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  case LS_WARNING:   return "Warning";
38247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  case LS_ERROR:     return "Error";
38347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  default:           return "<unknown>";
38447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
38547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
38647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
38747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgconst char* LogMessage::DescribeFile(const char* file) {
38847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  const char* end1 = ::strrchr(file, '/');
38947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  const char* end2 = ::strrchr(file, '\\');
39047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  if (!end1 && !end2)
39147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    return file;
39247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  else
39347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    return (end1 > end2) ? end1 + 1 : end2 + 1;
39447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
39547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
39647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgvoid LogMessage::OutputToDebug(const std::string& str,
39747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org                               LoggingSeverity severity) {
39847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  bool log_to_stderr = true;
39947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) && (!defined(DEBUG) || defined(NDEBUG))
40047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // On the Mac, all stderr output goes to the Console log and causes clutter.
40147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // So in opt builds, don't log to stderr unless the user specifically sets
40247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // a preference to do so.
40347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  CFStringRef key = CFStringCreateWithCString(kCFAllocatorDefault,
40447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org                                              "logToStdErr",
40547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org                                              kCFStringEncodingUTF8);
40647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  CFStringRef domain = CFBundleGetIdentifier(CFBundleGetMainBundle());
40747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  if (key != NULL && domain != NULL) {
40847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    Boolean exists_and_is_valid;
40947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    Boolean should_log =
41047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org        CFPreferencesGetAppBooleanValue(key, domain, &exists_and_is_valid);
41147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    // If the key doesn't exist or is invalid or is false, we will not log to
41247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    // stderr.
41347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    log_to_stderr = exists_and_is_valid && should_log;
41447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
41547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  if (key != NULL) {
41647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    CFRelease(key);
41747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
41847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#endif
41947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#if defined(WEBRTC_WIN)
42047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // Always log to the debugger.
42147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // Perhaps stderr should be controlled by a preference, as on Mac?
42247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  OutputDebugStringA(str.c_str());
42347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  if (log_to_stderr) {
42447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    // This handles dynamically allocated consoles, too.
42547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    if (HANDLE error_handle = ::GetStdHandle(STD_ERROR_HANDLE)) {
42647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      log_to_stderr = false;
42747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      DWORD written = 0;
42847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      ::WriteFile(error_handle, str.data(), static_cast<DWORD>(str.size()),
42947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org                  &written, 0);
43047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    }
43147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
43247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#endif  // WEBRTC_WIN
43347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#if defined(WEBRTC_ANDROID)
43447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // Android's logging facility uses severity to log messages but we
43547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // need to map libjingle's severity levels to Android ones first.
43647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // Also write to stderr which maybe available to executable started
43747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // from the shell.
43847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  int prio;
43947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  switch (severity) {
44047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    case LS_SENSITIVE:
44147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      __android_log_write(ANDROID_LOG_INFO, kLibjingle, "SENSITIVE");
44247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      if (log_to_stderr) {
44347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org        fprintf(stderr, "SENSITIVE");
44447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org        fflush(stderr);
44547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      }
44647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      return;
44747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    case LS_VERBOSE:
44847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      prio = ANDROID_LOG_VERBOSE;
44947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      break;
45047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    case LS_INFO:
45147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      prio = ANDROID_LOG_INFO;
45247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      break;
45347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    case LS_WARNING:
45447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      prio = ANDROID_LOG_WARN;
45547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      break;
45647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    case LS_ERROR:
45747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      prio = ANDROID_LOG_ERROR;
45847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      break;
45947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    default:
46047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      prio = ANDROID_LOG_UNKNOWN;
46147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
46247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
46347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  int size = str.size();
46447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  int line = 0;
46547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  int idx = 0;
46647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  const int max_lines = size / kMaxLogLineSize + 1;
46747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  if (max_lines == 1) {
46847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    __android_log_print(prio, kLibjingle, "%.*s", size, str.c_str());
46947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  } else {
47047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    while (size > 0) {
47147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      const int len = std::min(size, kMaxLogLineSize);
47247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      // Use the size of the string in the format (str may have \0 in the
47347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      // middle).
47447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      __android_log_print(prio, kLibjingle, "[%d/%d] %.*s",
47547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org                          line + 1, max_lines,
47647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org                          len, str.c_str() + idx);
47747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      idx += len;
47847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      size -= len;
47947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      ++line;
48047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    }
48147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
48247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#endif  // WEBRTC_ANDROID
48347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  if (log_to_stderr) {
48447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    fprintf(stderr, "%s", str.c_str());
48547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    fflush(stderr);
48647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
48747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
48847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
48947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgvoid LogMessage::OutputToStream(StreamInterface* stream,
49047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org                                const std::string& str) {
49147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // If write isn't fully successful, what are we going to do, log it? :)
49247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  stream->WriteAll(str.data(), str.size(), NULL, NULL);
49347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
49447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
49547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org//////////////////////////////////////////////////////////////////////
49647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// Logging Helpers
49747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org//////////////////////////////////////////////////////////////////////
49847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
49947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgvoid LogMultiline(LoggingSeverity level, const char* label, bool input,
50047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org                  const void* data, size_t len, bool hex_mode,
50147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org                  LogMultilineState* state) {
50247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  if (!LOG_CHECK_LEVEL_V(level))
50347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    return;
50447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
50547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  const char * direction = (input ? " << " : " >> ");
50647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
50747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // NULL data means to flush our count of unprintable characters.
50847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  if (!data) {
50947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    if (state && state->unprintable_count_[input]) {
51047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      LOG_V(level) << label << direction << "## "
51147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org                   << state->unprintable_count_[input]
51247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org                   << " consecutive unprintable ##";
51347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      state->unprintable_count_[input] = 0;
51447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    }
51547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    return;
51647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
51747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
51847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // The ctype classification functions want unsigned chars.
51947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  const unsigned char* udata = static_cast<const unsigned char*>(data);
52047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
52147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  if (hex_mode) {
52247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    const size_t LINE_SIZE = 24;
52347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    char hex_line[LINE_SIZE * 9 / 4 + 2], asc_line[LINE_SIZE + 1];
52447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    while (len > 0) {
52547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      memset(asc_line, ' ', sizeof(asc_line));
52647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      memset(hex_line, ' ', sizeof(hex_line));
52747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      size_t line_len = _min(len, LINE_SIZE);
52847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      for (size_t i = 0; i < line_len; ++i) {
52947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org        unsigned char ch = udata[i];
53047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org        asc_line[i] = isprint(ch) ? ch : '.';
53147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org        hex_line[i*2 + i/4] = hex_encode(ch >> 4);
53247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org        hex_line[i*2 + i/4 + 1] = hex_encode(ch & 0xf);
53347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      }
53447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      asc_line[sizeof(asc_line)-1] = 0;
53547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      hex_line[sizeof(hex_line)-1] = 0;
53647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      LOG_V(level) << label << direction
53747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org                   << asc_line << " " << hex_line << " ";
53847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      udata += line_len;
53947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      len -= line_len;
54047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    }
54147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    return;
54247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
54347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
54447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  size_t consecutive_unprintable = state ? state->unprintable_count_[input] : 0;
54547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
54647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  const unsigned char* end = udata + len;
54747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  while (udata < end) {
54847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    const unsigned char* line = udata;
54947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    const unsigned char* end_of_line = strchrn<unsigned char>(udata,
55047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org                                                              end - udata,
55147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org                                                              '\n');
55247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    if (!end_of_line) {
55347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      udata = end_of_line = end;
55447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    } else {
55547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      udata = end_of_line + 1;
55647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    }
55747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
55847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    bool is_printable = true;
55947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
56047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    // If we are in unprintable mode, we need to see a line of at least
56147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    // kMinPrintableLine characters before we'll switch back.
56247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    const ptrdiff_t kMinPrintableLine = 4;
56347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    if (consecutive_unprintable && ((end_of_line - line) < kMinPrintableLine)) {
56447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      is_printable = false;
56547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    } else {
56647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      // Determine if the line contains only whitespace and printable
56747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      // characters.
56847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      bool is_entirely_whitespace = true;
56947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      for (const unsigned char* pos = line; pos < end_of_line; ++pos) {
57047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org        if (isspace(*pos))
57147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org          continue;
57247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org        is_entirely_whitespace = false;
57347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org        if (!isprint(*pos)) {
57447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org          is_printable = false;
57547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org          break;
57647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org        }
57747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      }
57847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      // Treat an empty line following unprintable data as unprintable.
57947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      if (consecutive_unprintable && is_entirely_whitespace) {
58047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org        is_printable = false;
58147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      }
58247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    }
58347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    if (!is_printable) {
58447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      consecutive_unprintable += (udata - line);
58547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      continue;
58647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    }
58747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    // Print out the current line, but prefix with a count of prior unprintable
58847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    // characters.
58947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    if (consecutive_unprintable) {
59047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      LOG_V(level) << label << direction << "## " << consecutive_unprintable
59147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org                  << " consecutive unprintable ##";
59247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      consecutive_unprintable = 0;
59347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    }
59447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    // Strip off trailing whitespace.
59547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    while ((end_of_line > line) && isspace(*(end_of_line-1))) {
59647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      --end_of_line;
59747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    }
59847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    // Filter out any private data
59947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    std::string substr(reinterpret_cast<const char*>(line), end_of_line - line);
60047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    std::string::size_type pos_private = substr.find("Email");
60147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    if (pos_private == std::string::npos) {
60247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      pos_private = substr.find("Passwd");
60347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    }
60447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    if (pos_private == std::string::npos) {
60547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      LOG_V(level) << label << direction << substr;
60647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    } else {
60747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      LOG_V(level) << label << direction << "## omitted for privacy ##";
60847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    }
60947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
61047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
61147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  if (state) {
61247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    state->unprintable_count_[input] = consecutive_unprintable;
61347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
61447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
61547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
61647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org//////////////////////////////////////////////////////////////////////
61747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
61847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}  // namespace rtc
619