2014-05-13 18:00:26 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright 2004 The WebRTC Project Authors. All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#if defined(WEBRTC_WIN)
|
2015-05-25 10:45:43 +02:00
|
|
|
#if !defined(WIN32_LEAN_AND_MEAN)
|
2014-05-13 18:00:26 +00:00
|
|
|
#define WIN32_LEAN_AND_MEAN
|
2015-05-25 10:45:43 +02:00
|
|
|
#endif
|
2014-05-13 18:00:26 +00:00
|
|
|
#include <windows.h>
|
2016-01-30 14:40:44 -08:00
|
|
|
#if _MSC_VER < 1900
|
2014-05-13 18:00:26 +00:00
|
|
|
#define snprintf _snprintf
|
2016-01-30 14:40:44 -08:00
|
|
|
#endif
|
2014-05-13 18:00:26 +00:00
|
|
|
#undef ERROR // wingdi.h
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS)
|
|
|
|
|
#include <CoreServices/CoreServices.h>
|
|
|
|
|
#elif defined(WEBRTC_ANDROID)
|
|
|
|
|
#include <android/log.h>
|
|
|
|
|
// Android has a 1024 limit on log inputs. We use 60 chars as an
|
|
|
|
|
// approx for the header/tag portion.
|
|
|
|
|
// See android/system/core/liblog/logd_write.c
|
|
|
|
|
static const int kMaxLogLineSize = 1024 - 60;
|
|
|
|
|
#endif // WEBRTC_MAC && !defined(WEBRTC_IOS) || WEBRTC_ANDROID
|
|
|
|
|
|
|
|
|
|
#include <time.h>
|
2015-02-12 11:54:26 +00:00
|
|
|
#include <limits.h>
|
2014-05-13 18:00:26 +00:00
|
|
|
|
2015-02-12 11:54:26 +00:00
|
|
|
#include <algorithm>
|
2014-05-13 18:00:26 +00:00
|
|
|
#include <iomanip>
|
2015-02-12 11:54:26 +00:00
|
|
|
#include <ostream>
|
2014-05-13 18:00:26 +00:00
|
|
|
#include <vector>
|
|
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "rtc_base/criticalsection.h"
|
|
|
|
|
#include "rtc_base/logging.h"
|
2018-02-27 15:30:29 +01:00
|
|
|
#include "rtc_base/platform_thread_types.h"
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "rtc_base/stringencode.h"
|
Adding a new string utility class: SimpleStringBuilder.
This is a fairly minimalistic string building class that
can be used instead of stringstream, which is discouraged
but tempting to use due to its convenient interface and
familiarity for anyone using our logging macros.
As a starter, I'm changing the string building code in
ReceiveStatisticsProxy and SendStatisticsProxy from using
stringstream and using SimpleStringBuilder instead.
In the case of SimpleStringBuilder, there's a single allocation,
it's done on the stack (fast), and minimal code is required for
each concatenation. The developer is responsible for ensuring
that the buffer size is adequate but the class won't overflow
the buffer. In dcheck-enabled builds, a check will go off if
we run out of buffer space.
As part of using SimpleStringBuilder for a small part of
rtc::LogMessage, a few more changes were made:
- SimpleStringBuilder is used for formatting errors instead of ostringstream.
- A new 'noop' state has been introduced for log messages that will be dropped.
- Use a static (singleton) noop ostream object for noop logging messages
instead of building up an actual ostringstream object that will be dropped.
- Add a LogMessageForTest class for better state inspection/testing.
- Fix benign bug in LogTest.Perf, change the test to not use File IO and
always enable it.
- Ensure that minimal work is done for noop messages.
- Remove dependency on rtc::Thread.
- Add tests for the extra_ field, correctly parsed paths and noop handling.
Bug: webrtc:8529, webrtc:4364, webrtc:8933
Change-Id: Ifa258c135135945e4560d9e24315f7d96f784acb
Reviewed-on: https://webrtc-review.googlesource.com/55520
Commit-Queue: Tommi <tommi@webrtc.org>
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Reviewed-by: Erik Språng <sprang@webrtc.org>
Reviewed-by: Jonas Olsson <jonasolsson@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#22203}
2018-02-27 13:51:08 +01:00
|
|
|
#include "rtc_base/strings/string_builder.h"
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "rtc_base/stringutils.h"
|
|
|
|
|
#include "rtc_base/timeutils.h"
|
2014-05-13 18:00:26 +00:00
|
|
|
|
|
|
|
|
namespace rtc {
|
2015-09-07 00:34:56 -07:00
|
|
|
namespace {
|
2018-02-15 11:57:03 +01:00
|
|
|
// By default, release builds don't log, debug builds at info level
|
|
|
|
|
#if !defined(NDEBUG)
|
|
|
|
|
static LoggingSeverity g_min_sev = LS_INFO;
|
|
|
|
|
static LoggingSeverity g_dbg_sev = LS_INFO;
|
|
|
|
|
#else
|
|
|
|
|
static LoggingSeverity g_min_sev = LS_NONE;
|
|
|
|
|
static LoggingSeverity g_dbg_sev = LS_NONE;
|
|
|
|
|
#endif
|
2015-09-07 00:34:56 -07:00
|
|
|
|
|
|
|
|
// Return the filename portion of the string (that following the last slash).
|
|
|
|
|
const char* FilenameFromPath(const char* file) {
|
|
|
|
|
const char* end1 = ::strrchr(file, '/');
|
|
|
|
|
const char* end2 = ::strrchr(file, '\\');
|
|
|
|
|
if (!end1 && !end2)
|
|
|
|
|
return file;
|
|
|
|
|
else
|
|
|
|
|
return (end1 > end2) ? end1 + 1 : end2 + 1;
|
|
|
|
|
}
|
|
|
|
|
|
Adding a new string utility class: SimpleStringBuilder.
This is a fairly minimalistic string building class that
can be used instead of stringstream, which is discouraged
but tempting to use due to its convenient interface and
familiarity for anyone using our logging macros.
As a starter, I'm changing the string building code in
ReceiveStatisticsProxy and SendStatisticsProxy from using
stringstream and using SimpleStringBuilder instead.
In the case of SimpleStringBuilder, there's a single allocation,
it's done on the stack (fast), and minimal code is required for
each concatenation. The developer is responsible for ensuring
that the buffer size is adequate but the class won't overflow
the buffer. In dcheck-enabled builds, a check will go off if
we run out of buffer space.
As part of using SimpleStringBuilder for a small part of
rtc::LogMessage, a few more changes were made:
- SimpleStringBuilder is used for formatting errors instead of ostringstream.
- A new 'noop' state has been introduced for log messages that will be dropped.
- Use a static (singleton) noop ostream object for noop logging messages
instead of building up an actual ostringstream object that will be dropped.
- Add a LogMessageForTest class for better state inspection/testing.
- Fix benign bug in LogTest.Perf, change the test to not use File IO and
always enable it.
- Ensure that minimal work is done for noop messages.
- Remove dependency on rtc::Thread.
- Add tests for the extra_ field, correctly parsed paths and noop handling.
Bug: webrtc:8529, webrtc:4364, webrtc:8933
Change-Id: Ifa258c135135945e4560d9e24315f7d96f784acb
Reviewed-on: https://webrtc-review.googlesource.com/55520
Commit-Queue: Tommi <tommi@webrtc.org>
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Reviewed-by: Erik Språng <sprang@webrtc.org>
Reviewed-by: Jonas Olsson <jonasolsson@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#22203}
2018-02-27 13:51:08 +01:00
|
|
|
std::ostream& GetNoopStream() {
|
|
|
|
|
class NoopStreamBuf : public std::streambuf {
|
|
|
|
|
public:
|
|
|
|
|
int overflow(int c) override { return c; }
|
|
|
|
|
};
|
|
|
|
|
static NoopStreamBuf noop_buffer;
|
|
|
|
|
static std::ostream noop_stream(&noop_buffer);
|
|
|
|
|
return noop_stream;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-15 11:57:03 +01:00
|
|
|
// Global lock for log subsystem, only needed to serialize access to streams_.
|
|
|
|
|
CriticalSection g_log_crit;
|
2015-09-07 00:34:56 -07:00
|
|
|
} // namespace
|
2014-05-13 18:00:26 +00:00
|
|
|
|
2018-05-18 14:32:50 +02:00
|
|
|
void LogSink::OnLogMessage(const std::string& msg,
|
|
|
|
|
LoggingSeverity severity,
|
|
|
|
|
const char* tag) {
|
|
|
|
|
OnLogMessage(msg);
|
|
|
|
|
}
|
2014-05-13 18:00:26 +00:00
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// LogMessage
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2015-09-07 00:34:56 -07:00
|
|
|
bool LogMessage::log_to_stderr_ = true;
|
2014-05-13 18:00:26 +00:00
|
|
|
|
|
|
|
|
// The list of logging streams currently configured.
|
|
|
|
|
// Note: we explicitly do not clean this up, because of the uncertain ordering
|
|
|
|
|
// of destructors at program exit. Let the person who sets the stream trigger
|
2017-02-27 14:06:41 -08:00
|
|
|
// cleanup by setting to null, or let it leak (safe at program exit).
|
2017-09-06 05:46:29 -07:00
|
|
|
LogMessage::StreamList LogMessage::streams_ RTC_GUARDED_BY(g_log_crit);
|
2014-05-13 18:00:26 +00:00
|
|
|
|
|
|
|
|
// Boolean options default to false (0)
|
|
|
|
|
bool LogMessage::thread_, LogMessage::timestamp_;
|
|
|
|
|
|
2018-05-04 10:42:28 +02:00
|
|
|
LogMessage::LogMessage(const char* file, int line, LoggingSeverity sev)
|
|
|
|
|
: LogMessage(file, line, sev, ERRCTX_NONE, 0) {}
|
|
|
|
|
|
2015-10-23 15:20:56 +02:00
|
|
|
LogMessage::LogMessage(const char* file,
|
|
|
|
|
int line,
|
|
|
|
|
LoggingSeverity sev,
|
|
|
|
|
LogErrorContext err_ctx,
|
2018-02-27 15:30:29 +01:00
|
|
|
int err)
|
|
|
|
|
: severity_(sev), is_noop_(IsNoop(sev)) {
|
Adding a new string utility class: SimpleStringBuilder.
This is a fairly minimalistic string building class that
can be used instead of stringstream, which is discouraged
but tempting to use due to its convenient interface and
familiarity for anyone using our logging macros.
As a starter, I'm changing the string building code in
ReceiveStatisticsProxy and SendStatisticsProxy from using
stringstream and using SimpleStringBuilder instead.
In the case of SimpleStringBuilder, there's a single allocation,
it's done on the stack (fast), and minimal code is required for
each concatenation. The developer is responsible for ensuring
that the buffer size is adequate but the class won't overflow
the buffer. In dcheck-enabled builds, a check will go off if
we run out of buffer space.
As part of using SimpleStringBuilder for a small part of
rtc::LogMessage, a few more changes were made:
- SimpleStringBuilder is used for formatting errors instead of ostringstream.
- A new 'noop' state has been introduced for log messages that will be dropped.
- Use a static (singleton) noop ostream object for noop logging messages
instead of building up an actual ostringstream object that will be dropped.
- Add a LogMessageForTest class for better state inspection/testing.
- Fix benign bug in LogTest.Perf, change the test to not use File IO and
always enable it.
- Ensure that minimal work is done for noop messages.
- Remove dependency on rtc::Thread.
- Add tests for the extra_ field, correctly parsed paths and noop handling.
Bug: webrtc:8529, webrtc:4364, webrtc:8933
Change-Id: Ifa258c135135945e4560d9e24315f7d96f784acb
Reviewed-on: https://webrtc-review.googlesource.com/55520
Commit-Queue: Tommi <tommi@webrtc.org>
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Reviewed-by: Erik Språng <sprang@webrtc.org>
Reviewed-by: Jonas Olsson <jonasolsson@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#22203}
2018-02-27 13:51:08 +01:00
|
|
|
// If there's no need to do any work, let's not :)
|
|
|
|
|
if (is_noop_)
|
|
|
|
|
return;
|
|
|
|
|
|
2014-05-13 18:00:26 +00:00
|
|
|
if (timestamp_) {
|
2016-06-15 17:15:23 -07:00
|
|
|
// Use SystemTimeMillis so that even if tests use fake clocks, the timestamp
|
|
|
|
|
// in log messages represents the real system time.
|
|
|
|
|
int64_t time = TimeDiff(SystemTimeMillis(), LogStartTime());
|
2014-05-13 18:00:26 +00:00
|
|
|
// Also ensure WallClockStartTime is initialized, so that it matches
|
|
|
|
|
// LogStartTime.
|
|
|
|
|
WallClockStartTime();
|
|
|
|
|
print_stream_ << "[" << std::setfill('0') << std::setw(3) << (time / 1000)
|
|
|
|
|
<< ":" << std::setw(3) << (time % 1000) << std::setfill(' ')
|
|
|
|
|
<< "] ";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (thread_) {
|
2015-07-14 17:04:08 +02:00
|
|
|
PlatformThreadId id = CurrentThreadId();
|
|
|
|
|
print_stream_ << "[" << std::dec << id << "] ";
|
2014-05-13 18:00:26 +00:00
|
|
|
}
|
|
|
|
|
|
2018-05-18 14:32:50 +02:00
|
|
|
if (file != nullptr) {
|
|
|
|
|
#if defined(WEBRTC_ANDROID)
|
|
|
|
|
tag_ = FilenameFromPath(file);
|
|
|
|
|
print_stream_ << "(line " << line << "): ";
|
|
|
|
|
#else
|
2015-09-15 11:05:24 -07:00
|
|
|
print_stream_ << "(" << FilenameFromPath(file) << ":" << line << "): ";
|
2018-05-18 14:32:50 +02:00
|
|
|
#endif
|
|
|
|
|
}
|
2015-09-07 00:34:56 -07:00
|
|
|
|
2014-05-13 18:00:26 +00:00
|
|
|
if (err_ctx != ERRCTX_NONE) {
|
2018-03-08 15:03:23 +01:00
|
|
|
char tmp_buf[1024];
|
|
|
|
|
SimpleStringBuilder tmp(tmp_buf);
|
Adding a new string utility class: SimpleStringBuilder.
This is a fairly minimalistic string building class that
can be used instead of stringstream, which is discouraged
but tempting to use due to its convenient interface and
familiarity for anyone using our logging macros.
As a starter, I'm changing the string building code in
ReceiveStatisticsProxy and SendStatisticsProxy from using
stringstream and using SimpleStringBuilder instead.
In the case of SimpleStringBuilder, there's a single allocation,
it's done on the stack (fast), and minimal code is required for
each concatenation. The developer is responsible for ensuring
that the buffer size is adequate but the class won't overflow
the buffer. In dcheck-enabled builds, a check will go off if
we run out of buffer space.
As part of using SimpleStringBuilder for a small part of
rtc::LogMessage, a few more changes were made:
- SimpleStringBuilder is used for formatting errors instead of ostringstream.
- A new 'noop' state has been introduced for log messages that will be dropped.
- Use a static (singleton) noop ostream object for noop logging messages
instead of building up an actual ostringstream object that will be dropped.
- Add a LogMessageForTest class for better state inspection/testing.
- Fix benign bug in LogTest.Perf, change the test to not use File IO and
always enable it.
- Ensure that minimal work is done for noop messages.
- Remove dependency on rtc::Thread.
- Add tests for the extra_ field, correctly parsed paths and noop handling.
Bug: webrtc:8529, webrtc:4364, webrtc:8933
Change-Id: Ifa258c135135945e4560d9e24315f7d96f784acb
Reviewed-on: https://webrtc-review.googlesource.com/55520
Commit-Queue: Tommi <tommi@webrtc.org>
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Reviewed-by: Erik Språng <sprang@webrtc.org>
Reviewed-by: Jonas Olsson <jonasolsson@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#22203}
2018-02-27 13:51:08 +01:00
|
|
|
tmp.AppendFormat("[0x%08X]", err);
|
2014-05-13 18:00:26 +00:00
|
|
|
switch (err_ctx) {
|
|
|
|
|
case ERRCTX_ERRNO:
|
|
|
|
|
tmp << " " << strerror(err);
|
|
|
|
|
break;
|
2016-09-28 17:42:01 -07:00
|
|
|
#ifdef WEBRTC_WIN
|
2014-05-13 18:00:26 +00:00
|
|
|
case ERRCTX_HRESULT: {
|
|
|
|
|
char msgbuf[256];
|
2018-02-27 15:30:29 +01:00
|
|
|
DWORD flags = FORMAT_MESSAGE_FROM_SYSTEM |
|
|
|
|
|
FORMAT_MESSAGE_IGNORE_INSERTS;
|
2014-05-13 18:00:26 +00:00
|
|
|
if (DWORD len = FormatMessageA(
|
2018-02-27 15:30:29 +01:00
|
|
|
flags, nullptr, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
2017-02-27 14:06:41 -08:00
|
|
|
msgbuf, sizeof(msgbuf) / sizeof(msgbuf[0]), nullptr)) {
|
2014-05-13 18:00:26 +00:00
|
|
|
while ((len > 0) &&
|
|
|
|
|
isspace(static_cast<unsigned char>(msgbuf[len-1]))) {
|
|
|
|
|
msgbuf[--len] = 0;
|
|
|
|
|
}
|
|
|
|
|
tmp << " " << msgbuf;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2015-05-23 09:54:07 +02:00
|
|
|
#endif // WEBRTC_WIN
|
2014-05-13 18:00:26 +00:00
|
|
|
#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS)
|
|
|
|
|
case ERRCTX_OSSTATUS: {
|
2016-04-24 17:32:48 +02:00
|
|
|
std::string desc(DescriptionFromOSStatus(err));
|
|
|
|
|
tmp << " " << (desc.empty() ? "Unknown error" : desc.c_str());
|
2014-05-13 18:00:26 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
#endif // WEBRTC_MAC && !defined(WEBRTC_IOS)
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
extra_ = tmp.str();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-27 15:30:29 +01:00
|
|
|
#if defined(WEBRTC_ANDROID)
|
2015-09-14 15:06:39 -07:00
|
|
|
LogMessage::LogMessage(const char* file,
|
|
|
|
|
int line,
|
|
|
|
|
LoggingSeverity sev,
|
2018-02-27 15:30:29 +01:00
|
|
|
const char* tag)
|
2017-02-27 14:06:41 -08:00
|
|
|
: LogMessage(file,
|
|
|
|
|
line,
|
|
|
|
|
sev,
|
|
|
|
|
ERRCTX_NONE,
|
2018-02-27 15:30:29 +01:00
|
|
|
0 /* err */) {
|
Adding a new string utility class: SimpleStringBuilder.
This is a fairly minimalistic string building class that
can be used instead of stringstream, which is discouraged
but tempting to use due to its convenient interface and
familiarity for anyone using our logging macros.
As a starter, I'm changing the string building code in
ReceiveStatisticsProxy and SendStatisticsProxy from using
stringstream and using SimpleStringBuilder instead.
In the case of SimpleStringBuilder, there's a single allocation,
it's done on the stack (fast), and minimal code is required for
each concatenation. The developer is responsible for ensuring
that the buffer size is adequate but the class won't overflow
the buffer. In dcheck-enabled builds, a check will go off if
we run out of buffer space.
As part of using SimpleStringBuilder for a small part of
rtc::LogMessage, a few more changes were made:
- SimpleStringBuilder is used for formatting errors instead of ostringstream.
- A new 'noop' state has been introduced for log messages that will be dropped.
- Use a static (singleton) noop ostream object for noop logging messages
instead of building up an actual ostringstream object that will be dropped.
- Add a LogMessageForTest class for better state inspection/testing.
- Fix benign bug in LogTest.Perf, change the test to not use File IO and
always enable it.
- Ensure that minimal work is done for noop messages.
- Remove dependency on rtc::Thread.
- Add tests for the extra_ field, correctly parsed paths and noop handling.
Bug: webrtc:8529, webrtc:4364, webrtc:8933
Change-Id: Ifa258c135135945e4560d9e24315f7d96f784acb
Reviewed-on: https://webrtc-review.googlesource.com/55520
Commit-Queue: Tommi <tommi@webrtc.org>
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Reviewed-by: Erik Språng <sprang@webrtc.org>
Reviewed-by: Jonas Olsson <jonasolsson@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#22203}
2018-02-27 13:51:08 +01:00
|
|
|
if (!is_noop_) {
|
|
|
|
|
tag_ = tag;
|
|
|
|
|
}
|
2015-09-14 15:06:39 -07:00
|
|
|
}
|
2018-02-27 15:30:29 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
// DEPRECATED. Currently only used by downstream projects that use
|
|
|
|
|
// implementation details of logging.h. Work is ongoing to remove those
|
|
|
|
|
// dependencies.
|
|
|
|
|
LogMessage::LogMessage(const char* file, int line, LoggingSeverity sev,
|
|
|
|
|
const std::string& tag)
|
|
|
|
|
: LogMessage(file, line, sev) {
|
|
|
|
|
if (!is_noop_)
|
|
|
|
|
print_stream_ << tag << ": ";
|
|
|
|
|
}
|
2015-09-14 15:06:39 -07:00
|
|
|
|
2014-05-13 18:00:26 +00:00
|
|
|
LogMessage::~LogMessage() {
|
Adding a new string utility class: SimpleStringBuilder.
This is a fairly minimalistic string building class that
can be used instead of stringstream, which is discouraged
but tempting to use due to its convenient interface and
familiarity for anyone using our logging macros.
As a starter, I'm changing the string building code in
ReceiveStatisticsProxy and SendStatisticsProxy from using
stringstream and using SimpleStringBuilder instead.
In the case of SimpleStringBuilder, there's a single allocation,
it's done on the stack (fast), and minimal code is required for
each concatenation. The developer is responsible for ensuring
that the buffer size is adequate but the class won't overflow
the buffer. In dcheck-enabled builds, a check will go off if
we run out of buffer space.
As part of using SimpleStringBuilder for a small part of
rtc::LogMessage, a few more changes were made:
- SimpleStringBuilder is used for formatting errors instead of ostringstream.
- A new 'noop' state has been introduced for log messages that will be dropped.
- Use a static (singleton) noop ostream object for noop logging messages
instead of building up an actual ostringstream object that will be dropped.
- Add a LogMessageForTest class for better state inspection/testing.
- Fix benign bug in LogTest.Perf, change the test to not use File IO and
always enable it.
- Ensure that minimal work is done for noop messages.
- Remove dependency on rtc::Thread.
- Add tests for the extra_ field, correctly parsed paths and noop handling.
Bug: webrtc:8529, webrtc:4364, webrtc:8933
Change-Id: Ifa258c135135945e4560d9e24315f7d96f784acb
Reviewed-on: https://webrtc-review.googlesource.com/55520
Commit-Queue: Tommi <tommi@webrtc.org>
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Reviewed-by: Erik Språng <sprang@webrtc.org>
Reviewed-by: Jonas Olsson <jonasolsson@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#22203}
2018-02-27 13:51:08 +01:00
|
|
|
if (is_noop_)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
FinishPrintStream();
|
|
|
|
|
|
|
|
|
|
// TODO(tommi): Unfortunately |ostringstream::str()| always returns a copy
|
|
|
|
|
// of the constructed string. This means that we always end up creating
|
|
|
|
|
// two copies here (one owned by the stream, one by the return value of
|
|
|
|
|
// |str()|). It would be nice to switch to something else.
|
|
|
|
|
const std::string str = print_stream_.str();
|
2014-05-13 18:00:26 +00:00
|
|
|
|
2018-02-15 11:57:03 +01:00
|
|
|
if (severity_ >= g_dbg_sev) {
|
2018-02-27 15:30:29 +01:00
|
|
|
#if defined(WEBRTC_ANDROID)
|
2015-09-14 15:06:39 -07:00
|
|
|
OutputToDebug(str, severity_, tag_);
|
2018-02-27 15:30:29 +01:00
|
|
|
#else
|
|
|
|
|
OutputToDebug(str, severity_);
|
|
|
|
|
#endif
|
2014-05-13 18:00:26 +00:00
|
|
|
}
|
|
|
|
|
|
2015-10-23 15:20:56 +02:00
|
|
|
CritScope cs(&g_log_crit);
|
|
|
|
|
for (auto& kv : streams_) {
|
|
|
|
|
if (severity_ >= kv.second) {
|
2018-05-18 14:32:50 +02:00
|
|
|
#if defined(WEBRTC_ANDROID)
|
|
|
|
|
kv.first->OnLogMessage(str, severity_, tag_);
|
|
|
|
|
#else
|
2015-10-23 15:20:56 +02:00
|
|
|
kv.first->OnLogMessage(str);
|
2018-05-18 14:32:50 +02:00
|
|
|
#endif
|
2014-05-13 18:00:26 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
Adding a new string utility class: SimpleStringBuilder.
This is a fairly minimalistic string building class that
can be used instead of stringstream, which is discouraged
but tempting to use due to its convenient interface and
familiarity for anyone using our logging macros.
As a starter, I'm changing the string building code in
ReceiveStatisticsProxy and SendStatisticsProxy from using
stringstream and using SimpleStringBuilder instead.
In the case of SimpleStringBuilder, there's a single allocation,
it's done on the stack (fast), and minimal code is required for
each concatenation. The developer is responsible for ensuring
that the buffer size is adequate but the class won't overflow
the buffer. In dcheck-enabled builds, a check will go off if
we run out of buffer space.
As part of using SimpleStringBuilder for a small part of
rtc::LogMessage, a few more changes were made:
- SimpleStringBuilder is used for formatting errors instead of ostringstream.
- A new 'noop' state has been introduced for log messages that will be dropped.
- Use a static (singleton) noop ostream object for noop logging messages
instead of building up an actual ostringstream object that will be dropped.
- Add a LogMessageForTest class for better state inspection/testing.
- Fix benign bug in LogTest.Perf, change the test to not use File IO and
always enable it.
- Ensure that minimal work is done for noop messages.
- Remove dependency on rtc::Thread.
- Add tests for the extra_ field, correctly parsed paths and noop handling.
Bug: webrtc:8529, webrtc:4364, webrtc:8933
Change-Id: Ifa258c135135945e4560d9e24315f7d96f784acb
Reviewed-on: https://webrtc-review.googlesource.com/55520
Commit-Queue: Tommi <tommi@webrtc.org>
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Reviewed-by: Erik Språng <sprang@webrtc.org>
Reviewed-by: Jonas Olsson <jonasolsson@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#22203}
2018-02-27 13:51:08 +01:00
|
|
|
std::ostream& LogMessage::stream() {
|
|
|
|
|
return is_noop_ ? GetNoopStream() : print_stream_;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-15 11:57:03 +01:00
|
|
|
bool LogMessage::Loggable(LoggingSeverity sev) {
|
|
|
|
|
return sev >= g_min_sev;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int LogMessage::GetMinLogSeverity() {
|
|
|
|
|
return g_min_sev;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LoggingSeverity LogMessage::GetLogToDebug() {
|
|
|
|
|
return g_dbg_sev;
|
|
|
|
|
}
|
2016-05-06 11:29:15 -07:00
|
|
|
int64_t LogMessage::LogStartTime() {
|
2016-06-15 17:15:23 -07:00
|
|
|
static const int64_t g_start = SystemTimeMillis();
|
2014-05-13 18:00:26 +00:00
|
|
|
return g_start;
|
|
|
|
|
}
|
|
|
|
|
|
Use suffixed {uint,int}{8,16,32,64}_t types.
Removes the use of uint8, etc. in favor of uint8_t.
BUG=webrtc:5024
R=henrik.lundin@webrtc.org, henrikg@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1362503003 .
Cr-Commit-Position: refs/heads/master@{#10196}
2015-10-07 12:23:21 +02:00
|
|
|
uint32_t LogMessage::WallClockStartTime() {
|
2017-02-27 14:06:41 -08:00
|
|
|
static const uint32_t g_start_wallclock = time(nullptr);
|
2014-05-13 18:00:26 +00:00
|
|
|
return g_start_wallclock;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LogMessage::LogThreads(bool on) {
|
|
|
|
|
thread_ = on;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LogMessage::LogTimestamps(bool on) {
|
|
|
|
|
timestamp_ = on;
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-23 09:54:07 +02:00
|
|
|
void LogMessage::LogToDebug(LoggingSeverity min_sev) {
|
2018-02-15 11:57:03 +01:00
|
|
|
g_dbg_sev = min_sev;
|
2015-10-23 15:20:56 +02:00
|
|
|
CritScope cs(&g_log_crit);
|
2015-05-25 11:25:59 +02:00
|
|
|
UpdateMinLogSeverity();
|
2014-05-13 18:00:26 +00:00
|
|
|
}
|
|
|
|
|
|
2015-09-07 00:34:56 -07:00
|
|
|
void LogMessage::SetLogToStderr(bool log_to_stderr) {
|
|
|
|
|
log_to_stderr_ = log_to_stderr;
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-23 09:54:07 +02:00
|
|
|
int LogMessage::GetLogToStream(LogSink* stream) {
|
2015-10-23 15:20:56 +02:00
|
|
|
CritScope cs(&g_log_crit);
|
2015-05-23 09:54:07 +02:00
|
|
|
LoggingSeverity sev = LS_NONE;
|
2015-10-23 15:20:56 +02:00
|
|
|
for (auto& kv : streams_) {
|
|
|
|
|
if (!stream || stream == kv.first) {
|
|
|
|
|
sev = std::min(sev, kv.second);
|
2014-05-13 18:00:26 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return sev;
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-23 09:54:07 +02:00
|
|
|
void LogMessage::AddLogToStream(LogSink* stream, LoggingSeverity min_sev) {
|
2015-10-23 15:20:56 +02:00
|
|
|
CritScope cs(&g_log_crit);
|
2014-05-13 18:00:26 +00:00
|
|
|
streams_.push_back(std::make_pair(stream, min_sev));
|
|
|
|
|
UpdateMinLogSeverity();
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-23 09:54:07 +02:00
|
|
|
void LogMessage::RemoveLogToStream(LogSink* stream) {
|
2015-10-23 15:20:56 +02:00
|
|
|
CritScope cs(&g_log_crit);
|
2014-05-13 18:00:26 +00:00
|
|
|
for (StreamList::iterator it = streams_.begin(); it != streams_.end(); ++it) {
|
|
|
|
|
if (stream == it->first) {
|
|
|
|
|
streams_.erase(it);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
UpdateMinLogSeverity();
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-23 09:54:07 +02:00
|
|
|
void LogMessage::ConfigureLogging(const char* params) {
|
|
|
|
|
LoggingSeverity current_level = LS_VERBOSE;
|
|
|
|
|
LoggingSeverity debug_level = GetLogToDebug();
|
2014-05-13 18:00:26 +00:00
|
|
|
|
|
|
|
|
std::vector<std::string> tokens;
|
|
|
|
|
tokenize(params, ' ', &tokens);
|
|
|
|
|
|
2015-05-23 09:54:07 +02:00
|
|
|
for (const std::string& token : tokens) {
|
|
|
|
|
if (token.empty())
|
2014-05-13 18:00:26 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
// Logging features
|
2015-05-23 09:54:07 +02:00
|
|
|
if (token == "tstamp") {
|
2014-05-13 18:00:26 +00:00
|
|
|
LogTimestamps();
|
2015-05-23 09:54:07 +02:00
|
|
|
} else if (token == "thread") {
|
2014-05-13 18:00:26 +00:00
|
|
|
LogThreads();
|
|
|
|
|
|
|
|
|
|
// Logging levels
|
2015-05-23 09:54:07 +02:00
|
|
|
} else if (token == "sensitive") {
|
2014-05-13 18:00:26 +00:00
|
|
|
current_level = LS_SENSITIVE;
|
2015-05-23 09:54:07 +02:00
|
|
|
} else if (token == "verbose") {
|
2014-05-13 18:00:26 +00:00
|
|
|
current_level = LS_VERBOSE;
|
2015-05-23 09:54:07 +02:00
|
|
|
} else if (token == "info") {
|
2014-05-13 18:00:26 +00:00
|
|
|
current_level = LS_INFO;
|
2015-05-23 09:54:07 +02:00
|
|
|
} else if (token == "warning") {
|
2014-05-13 18:00:26 +00:00
|
|
|
current_level = LS_WARNING;
|
2015-05-23 09:54:07 +02:00
|
|
|
} else if (token == "error") {
|
2014-05-13 18:00:26 +00:00
|
|
|
current_level = LS_ERROR;
|
2015-05-23 09:54:07 +02:00
|
|
|
} else if (token == "none") {
|
|
|
|
|
current_level = LS_NONE;
|
2014-05-13 18:00:26 +00:00
|
|
|
|
|
|
|
|
// Logging targets
|
2015-05-23 09:54:07 +02:00
|
|
|
} else if (token == "debug") {
|
2014-05-13 18:00:26 +00:00
|
|
|
debug_level = current_level;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if defined(WEBRTC_WIN)
|
2015-05-23 09:54:07 +02:00
|
|
|
if ((LS_NONE != debug_level) && !::IsDebuggerPresent()) {
|
2014-05-13 18:00:26 +00:00
|
|
|
// First, attempt to attach to our parent's console... so if you invoke
|
|
|
|
|
// from the command line, we'll see the output there. Otherwise, create
|
|
|
|
|
// our own console window.
|
|
|
|
|
// Note: These methods fail if a console already exists, which is fine.
|
2018-02-27 15:30:29 +01:00
|
|
|
if (!AttachConsole(ATTACH_PARENT_PROCESS))
|
2014-05-13 18:00:26 +00:00
|
|
|
::AllocConsole();
|
|
|
|
|
}
|
2015-05-23 09:54:07 +02:00
|
|
|
#endif // WEBRTC_WIN
|
2014-05-13 18:00:26 +00:00
|
|
|
|
|
|
|
|
LogToDebug(debug_level);
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-06 05:46:29 -07:00
|
|
|
void LogMessage::UpdateMinLogSeverity()
|
|
|
|
|
RTC_EXCLUSIVE_LOCKS_REQUIRED(g_log_crit) {
|
2018-02-15 11:57:03 +01:00
|
|
|
LoggingSeverity min_sev = g_dbg_sev;
|
2015-10-23 15:20:56 +02:00
|
|
|
for (auto& kv : streams_) {
|
2018-02-15 11:57:03 +01:00
|
|
|
min_sev = std::min(g_dbg_sev, kv.second);
|
2014-05-13 18:00:26 +00:00
|
|
|
}
|
2018-02-15 11:57:03 +01:00
|
|
|
g_min_sev = min_sev;
|
2014-05-13 18:00:26 +00:00
|
|
|
}
|
|
|
|
|
|
2018-02-27 15:30:29 +01:00
|
|
|
#if defined(WEBRTC_ANDROID)
|
2014-05-13 18:00:26 +00:00
|
|
|
void LogMessage::OutputToDebug(const std::string& str,
|
2015-09-14 15:06:39 -07:00
|
|
|
LoggingSeverity severity,
|
2018-02-27 15:30:29 +01:00
|
|
|
const char* tag) {
|
|
|
|
|
#else
|
|
|
|
|
void LogMessage::OutputToDebug(const std::string& str,
|
|
|
|
|
LoggingSeverity severity) {
|
|
|
|
|
#endif
|
2015-09-07 00:34:56 -07:00
|
|
|
bool log_to_stderr = log_to_stderr_;
|
2015-10-30 16:08:48 -07:00
|
|
|
#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) && defined(NDEBUG)
|
2014-05-13 18:00:26 +00:00
|
|
|
// On the Mac, all stderr output goes to the Console log and causes clutter.
|
|
|
|
|
// So in opt builds, don't log to stderr unless the user specifically sets
|
|
|
|
|
// a preference to do so.
|
|
|
|
|
CFStringRef key = CFStringCreateWithCString(kCFAllocatorDefault,
|
|
|
|
|
"logToStdErr",
|
|
|
|
|
kCFStringEncodingUTF8);
|
|
|
|
|
CFStringRef domain = CFBundleGetIdentifier(CFBundleGetMainBundle());
|
2017-02-27 14:06:41 -08:00
|
|
|
if (key != nullptr && domain != nullptr) {
|
2014-05-13 18:00:26 +00:00
|
|
|
Boolean exists_and_is_valid;
|
|
|
|
|
Boolean should_log =
|
|
|
|
|
CFPreferencesGetAppBooleanValue(key, domain, &exists_and_is_valid);
|
|
|
|
|
// If the key doesn't exist or is invalid or is false, we will not log to
|
|
|
|
|
// stderr.
|
|
|
|
|
log_to_stderr = exists_and_is_valid && should_log;
|
|
|
|
|
}
|
2017-02-27 14:06:41 -08:00
|
|
|
if (key != nullptr) {
|
2014-05-13 18:00:26 +00:00
|
|
|
CFRelease(key);
|
|
|
|
|
}
|
2018-02-27 15:30:29 +01:00
|
|
|
#endif // defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) && defined(NDEBUG)
|
|
|
|
|
|
2014-05-13 18:00:26 +00:00
|
|
|
#if defined(WEBRTC_WIN)
|
|
|
|
|
// Always log to the debugger.
|
|
|
|
|
// Perhaps stderr should be controlled by a preference, as on Mac?
|
|
|
|
|
OutputDebugStringA(str.c_str());
|
|
|
|
|
if (log_to_stderr) {
|
|
|
|
|
// This handles dynamically allocated consoles, too.
|
|
|
|
|
if (HANDLE error_handle = ::GetStdHandle(STD_ERROR_HANDLE)) {
|
|
|
|
|
log_to_stderr = false;
|
|
|
|
|
DWORD written = 0;
|
|
|
|
|
::WriteFile(error_handle, str.data(), static_cast<DWORD>(str.size()),
|
|
|
|
|
&written, 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-05-23 09:54:07 +02:00
|
|
|
#endif // WEBRTC_WIN
|
2018-02-27 15:30:29 +01:00
|
|
|
|
2014-05-13 18:00:26 +00:00
|
|
|
#if defined(WEBRTC_ANDROID)
|
|
|
|
|
// Android's logging facility uses severity to log messages but we
|
|
|
|
|
// need to map libjingle's severity levels to Android ones first.
|
|
|
|
|
// Also write to stderr which maybe available to executable started
|
|
|
|
|
// from the shell.
|
|
|
|
|
int prio;
|
|
|
|
|
switch (severity) {
|
|
|
|
|
case LS_SENSITIVE:
|
2018-02-27 15:30:29 +01:00
|
|
|
__android_log_write(ANDROID_LOG_INFO, tag, "SENSITIVE");
|
2014-05-13 18:00:26 +00:00
|
|
|
if (log_to_stderr) {
|
|
|
|
|
fprintf(stderr, "SENSITIVE");
|
|
|
|
|
fflush(stderr);
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
case LS_VERBOSE:
|
|
|
|
|
prio = ANDROID_LOG_VERBOSE;
|
|
|
|
|
break;
|
|
|
|
|
case LS_INFO:
|
|
|
|
|
prio = ANDROID_LOG_INFO;
|
|
|
|
|
break;
|
|
|
|
|
case LS_WARNING:
|
|
|
|
|
prio = ANDROID_LOG_WARN;
|
|
|
|
|
break;
|
|
|
|
|
case LS_ERROR:
|
|
|
|
|
prio = ANDROID_LOG_ERROR;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
prio = ANDROID_LOG_UNKNOWN;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int size = str.size();
|
|
|
|
|
int line = 0;
|
|
|
|
|
int idx = 0;
|
|
|
|
|
const int max_lines = size / kMaxLogLineSize + 1;
|
|
|
|
|
if (max_lines == 1) {
|
2018-02-27 15:30:29 +01:00
|
|
|
__android_log_print(prio, tag, "%.*s", size, str.c_str());
|
2014-05-13 18:00:26 +00:00
|
|
|
} else {
|
|
|
|
|
while (size > 0) {
|
|
|
|
|
const int len = std::min(size, kMaxLogLineSize);
|
|
|
|
|
// Use the size of the string in the format (str may have \0 in the
|
|
|
|
|
// middle).
|
2018-02-27 15:30:29 +01:00
|
|
|
__android_log_print(prio, tag, "[%d/%d] %.*s", line + 1, max_lines, len,
|
|
|
|
|
str.c_str() + idx);
|
2014-05-13 18:00:26 +00:00
|
|
|
idx += len;
|
|
|
|
|
size -= len;
|
|
|
|
|
++line;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif // WEBRTC_ANDROID
|
|
|
|
|
if (log_to_stderr) {
|
|
|
|
|
fprintf(stderr, "%s", str.c_str());
|
|
|
|
|
fflush(stderr);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
Adding a new string utility class: SimpleStringBuilder.
This is a fairly minimalistic string building class that
can be used instead of stringstream, which is discouraged
but tempting to use due to its convenient interface and
familiarity for anyone using our logging macros.
As a starter, I'm changing the string building code in
ReceiveStatisticsProxy and SendStatisticsProxy from using
stringstream and using SimpleStringBuilder instead.
In the case of SimpleStringBuilder, there's a single allocation,
it's done on the stack (fast), and minimal code is required for
each concatenation. The developer is responsible for ensuring
that the buffer size is adequate but the class won't overflow
the buffer. In dcheck-enabled builds, a check will go off if
we run out of buffer space.
As part of using SimpleStringBuilder for a small part of
rtc::LogMessage, a few more changes were made:
- SimpleStringBuilder is used for formatting errors instead of ostringstream.
- A new 'noop' state has been introduced for log messages that will be dropped.
- Use a static (singleton) noop ostream object for noop logging messages
instead of building up an actual ostringstream object that will be dropped.
- Add a LogMessageForTest class for better state inspection/testing.
- Fix benign bug in LogTest.Perf, change the test to not use File IO and
always enable it.
- Ensure that minimal work is done for noop messages.
- Remove dependency on rtc::Thread.
- Add tests for the extra_ field, correctly parsed paths and noop handling.
Bug: webrtc:8529, webrtc:4364, webrtc:8933
Change-Id: Ifa258c135135945e4560d9e24315f7d96f784acb
Reviewed-on: https://webrtc-review.googlesource.com/55520
Commit-Queue: Tommi <tommi@webrtc.org>
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Reviewed-by: Erik Språng <sprang@webrtc.org>
Reviewed-by: Jonas Olsson <jonasolsson@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#22203}
2018-02-27 13:51:08 +01:00
|
|
|
// static
|
|
|
|
|
bool LogMessage::IsNoop(LoggingSeverity severity) {
|
|
|
|
|
if (severity >= g_dbg_sev)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// TODO(tommi): We're grabbing this lock for every LogMessage instance that
|
|
|
|
|
// is going to be logged. This introduces unnecessary synchronization for
|
|
|
|
|
// a feature that's mostly used for testing.
|
|
|
|
|
CritScope cs(&g_log_crit);
|
|
|
|
|
return streams_.size() == 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LogMessage::FinishPrintStream() {
|
|
|
|
|
if (is_noop_)
|
|
|
|
|
return;
|
|
|
|
|
if (!extra_.empty())
|
|
|
|
|
print_stream_ << " : " << extra_;
|
|
|
|
|
print_stream_ << std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-13 18:00:26 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
} // namespace rtc
|