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.
|
|
|
|
|
*/
|
|
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "rtc_base/logging.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/arraysize.h"
|
|
|
|
|
#include "rtc_base/checks.h"
|
|
|
|
|
#include "rtc_base/event.h"
|
|
|
|
|
#include "rtc_base/gunit.h"
|
|
|
|
|
#include "rtc_base/platform_thread.h"
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "rtc_base/stream.h"
|
|
|
|
|
#include "test/testsupport/fileutils.h"
|
2014-05-13 18:00:26 +00:00
|
|
|
|
|
|
|
|
namespace rtc {
|
|
|
|
|
|
2018-06-20 10:27:53 +02:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
class StringStream : public StreamInterface {
|
|
|
|
|
public:
|
|
|
|
|
explicit StringStream(std::string* str);
|
|
|
|
|
explicit StringStream(const std::string& str);
|
|
|
|
|
|
|
|
|
|
StreamState GetState() const override;
|
|
|
|
|
StreamResult Read(void* buffer,
|
|
|
|
|
size_t buffer_len,
|
|
|
|
|
size_t* read,
|
|
|
|
|
int* error) override;
|
|
|
|
|
StreamResult Write(const void* data,
|
|
|
|
|
size_t data_len,
|
|
|
|
|
size_t* written,
|
|
|
|
|
int* error) override;
|
|
|
|
|
void Close() override;
|
|
|
|
|
bool SetPosition(size_t position) override;
|
|
|
|
|
bool GetPosition(size_t* position) const override;
|
|
|
|
|
bool GetSize(size_t* size) const override;
|
|
|
|
|
bool ReserveSize(size_t size) override;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
std::string& str_;
|
|
|
|
|
size_t read_pos_;
|
|
|
|
|
bool read_only_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
StringStream::StringStream(std::string* str)
|
|
|
|
|
: str_(*str), read_pos_(0), read_only_(false) {}
|
|
|
|
|
|
|
|
|
|
StringStream::StringStream(const std::string& str)
|
|
|
|
|
: str_(const_cast<std::string&>(str)), read_pos_(0), read_only_(true) {}
|
|
|
|
|
|
|
|
|
|
StreamState StringStream::GetState() const {
|
|
|
|
|
return SS_OPEN;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
StreamResult StringStream::Read(void* buffer,
|
|
|
|
|
size_t buffer_len,
|
|
|
|
|
size_t* read,
|
|
|
|
|
int* error) {
|
|
|
|
|
size_t available = std::min(buffer_len, str_.size() - read_pos_);
|
|
|
|
|
if (!available)
|
|
|
|
|
return SR_EOS;
|
|
|
|
|
memcpy(buffer, str_.data() + read_pos_, available);
|
|
|
|
|
read_pos_ += available;
|
|
|
|
|
if (read)
|
|
|
|
|
*read = available;
|
|
|
|
|
return SR_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
StreamResult StringStream::Write(const void* data,
|
|
|
|
|
size_t data_len,
|
|
|
|
|
size_t* written,
|
|
|
|
|
int* error) {
|
|
|
|
|
if (read_only_) {
|
|
|
|
|
if (error) {
|
|
|
|
|
*error = -1;
|
|
|
|
|
}
|
|
|
|
|
return SR_ERROR;
|
|
|
|
|
}
|
|
|
|
|
str_.append(static_cast<const char*>(data),
|
|
|
|
|
static_cast<const char*>(data) + data_len);
|
|
|
|
|
if (written)
|
|
|
|
|
*written = data_len;
|
|
|
|
|
return SR_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void StringStream::Close() {}
|
|
|
|
|
|
|
|
|
|
bool StringStream::SetPosition(size_t position) {
|
|
|
|
|
if (position > str_.size())
|
|
|
|
|
return false;
|
|
|
|
|
read_pos_ = position;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool StringStream::GetPosition(size_t* position) const {
|
|
|
|
|
if (position)
|
|
|
|
|
*position = read_pos_;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool StringStream::GetSize(size_t* size) const {
|
|
|
|
|
if (size)
|
|
|
|
|
*size = str_.size();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool StringStream::ReserveSize(size_t size) {
|
|
|
|
|
if (read_only_)
|
|
|
|
|
return false;
|
|
|
|
|
str_.reserve(size);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
2015-05-23 09:54:07 +02:00
|
|
|
template <typename Base>
|
|
|
|
|
class LogSinkImpl : public LogSink, public Base {
|
|
|
|
|
public:
|
|
|
|
|
LogSinkImpl() {}
|
|
|
|
|
|
|
|
|
|
template <typename P>
|
2015-05-25 11:25:59 +02:00
|
|
|
explicit LogSinkImpl(P* p) : Base(p) {}
|
2015-05-23 09:54:07 +02:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
void OnLogMessage(const std::string& message) override {
|
|
|
|
|
static_cast<Base*>(this)->WriteAll(message.data(), message.size(), nullptr,
|
|
|
|
|
nullptr);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
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
|
|
|
class LogMessageForTesting : public LogMessage {
|
|
|
|
|
public:
|
|
|
|
|
LogMessageForTesting(const char* file,
|
|
|
|
|
int line,
|
|
|
|
|
LoggingSeverity sev,
|
|
|
|
|
LogErrorContext err_ctx = ERRCTX_NONE,
|
|
|
|
|
int err = 0)
|
|
|
|
|
: LogMessage(file, line, sev, err_ctx, err) {}
|
|
|
|
|
|
|
|
|
|
const std::string& get_extra() const { return extra_; }
|
2018-06-20 14:07:05 +02:00
|
|
|
#if defined(WEBRTC_ANDROID)
|
|
|
|
|
const char* get_tag() const { return tag_; }
|
|
|
|
|
#endif
|
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
|
|
|
|
|
|
|
|
// Returns the contents of the internal log stream.
|
|
|
|
|
// Note that parts of the stream won't (as is) be available until *after* the
|
|
|
|
|
// dtor of the parent class has run. So, as is, this only represents a
|
|
|
|
|
// partially built stream.
|
|
|
|
|
std::string GetPrintStream() {
|
|
|
|
|
RTC_DCHECK(!is_finished_);
|
|
|
|
|
is_finished_ = true;
|
|
|
|
|
FinishPrintStream();
|
2018-09-07 11:21:28 +02:00
|
|
|
return print_stream_.Release();
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
bool is_finished_ = false;
|
|
|
|
|
};
|
|
|
|
|
|
2014-05-13 18:00:26 +00:00
|
|
|
// Test basic logging operation. We should get the INFO log but not the VERBOSE.
|
|
|
|
|
// We should restore the correct global state at the end.
|
|
|
|
|
TEST(LogTest, SingleStream) {
|
2017-02-27 14:06:41 -08:00
|
|
|
int sev = LogMessage::GetLogToStream(nullptr);
|
2014-05-13 18:00:26 +00:00
|
|
|
|
|
|
|
|
std::string str;
|
2015-05-25 11:25:59 +02:00
|
|
|
LogSinkImpl<StringStream> stream(&str);
|
2014-05-13 18:00:26 +00:00
|
|
|
LogMessage::AddLogToStream(&stream, LS_INFO);
|
|
|
|
|
EXPECT_EQ(LS_INFO, LogMessage::GetLogToStream(&stream));
|
|
|
|
|
|
2017-11-09 11:09:25 +01:00
|
|
|
RTC_LOG(LS_INFO) << "INFO";
|
|
|
|
|
RTC_LOG(LS_VERBOSE) << "VERBOSE";
|
2014-05-13 18:00:26 +00:00
|
|
|
EXPECT_NE(std::string::npos, str.find("INFO"));
|
|
|
|
|
EXPECT_EQ(std::string::npos, str.find("VERBOSE"));
|
|
|
|
|
|
2018-09-12 15:32:47 +02:00
|
|
|
int i = 1;
|
|
|
|
|
long l = 2l;
|
|
|
|
|
long long ll = 3ll;
|
|
|
|
|
|
|
|
|
|
unsigned int u = 4u;
|
|
|
|
|
unsigned long ul = 5ul;
|
|
|
|
|
unsigned long long ull = 6ull;
|
|
|
|
|
|
|
|
|
|
std::string s1 = "char*";
|
|
|
|
|
std::string s2 = "std::string";
|
|
|
|
|
std::string s3 = "absl::stringview";
|
|
|
|
|
|
|
|
|
|
void* p = reinterpret_cast<void*>(0xabcd);
|
|
|
|
|
|
|
|
|
|
// Log all suported types(except doubles/floats) as a sanity-check.
|
|
|
|
|
RTC_LOG(LS_INFO) << "|" << i << "|" << l << "|" << ll << "|" << u << "|" << ul
|
|
|
|
|
<< "|" << ull << "|" << s1.c_str() << "|" << s2 << "|"
|
|
|
|
|
<< absl::string_view(s3) << "|" << p << "|";
|
|
|
|
|
|
|
|
|
|
// Signed integers
|
|
|
|
|
EXPECT_NE(std::string::npos, str.find("|1|"));
|
|
|
|
|
EXPECT_NE(std::string::npos, str.find("|2|"));
|
|
|
|
|
EXPECT_NE(std::string::npos, str.find("|3|"));
|
|
|
|
|
|
|
|
|
|
// Unsigned integers
|
|
|
|
|
EXPECT_NE(std::string::npos, str.find("|4|"));
|
|
|
|
|
EXPECT_NE(std::string::npos, str.find("|5|"));
|
|
|
|
|
EXPECT_NE(std::string::npos, str.find("|6|"));
|
|
|
|
|
|
|
|
|
|
// Strings
|
|
|
|
|
EXPECT_NE(std::string::npos, str.find("|char*|"));
|
|
|
|
|
EXPECT_NE(std::string::npos, str.find("|std::string|"));
|
|
|
|
|
EXPECT_NE(std::string::npos, str.find("|absl::stringview|"));
|
|
|
|
|
|
|
|
|
|
// void*
|
|
|
|
|
EXPECT_NE(std::string::npos, str.find("|abcd|"));
|
|
|
|
|
|
2014-05-13 18:00:26 +00:00
|
|
|
LogMessage::RemoveLogToStream(&stream);
|
2015-05-23 09:54:07 +02:00
|
|
|
EXPECT_EQ(LS_NONE, LogMessage::GetLogToStream(&stream));
|
2017-02-27 14:06:41 -08:00
|
|
|
EXPECT_EQ(sev, LogMessage::GetLogToStream(nullptr));
|
2014-05-13 18:00:26 +00:00
|
|
|
}
|
|
|
|
|
|
2018-06-14 13:14:22 +02:00
|
|
|
#if GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
|
|
|
|
|
TEST(LogTest, Checks) {
|
|
|
|
|
EXPECT_DEATH(FATAL() << "message",
|
|
|
|
|
"\n\n#\n"
|
2018-07-03 12:07:53 +02:00
|
|
|
"# Fatal error in: \\S+, line \\w+\n"
|
|
|
|
|
"# last system error: \\w+\n"
|
2018-06-14 13:14:22 +02:00
|
|
|
"# Check failed: FATAL\\(\\)\n"
|
2018-07-03 12:07:53 +02:00
|
|
|
"# message");
|
2018-06-14 13:14:22 +02:00
|
|
|
|
|
|
|
|
int a = 1, b = 2;
|
|
|
|
|
EXPECT_DEATH(RTC_CHECK_EQ(a, b) << 1 << 2u,
|
|
|
|
|
"\n\n#\n"
|
2018-07-03 12:07:53 +02:00
|
|
|
"# Fatal error in: \\S+, line \\w+\n"
|
|
|
|
|
"# last system error: \\w+\n"
|
2018-06-14 13:14:22 +02:00
|
|
|
"# Check failed: a == b \\(1 vs. 2\\)\n"
|
2018-07-03 12:07:53 +02:00
|
|
|
"# 12");
|
2018-06-14 13:14:22 +02:00
|
|
|
RTC_CHECK_EQ(5, 5);
|
|
|
|
|
|
|
|
|
|
RTC_CHECK(true) << "Shouldn't crash" << 1;
|
|
|
|
|
EXPECT_DEATH(RTC_CHECK(false) << "Hi there!",
|
|
|
|
|
"\n\n#\n"
|
2018-07-03 12:07:53 +02:00
|
|
|
"# Fatal error in: \\S+, line \\w+\n"
|
|
|
|
|
"# last system error: \\w+\n"
|
2018-06-14 13:14:22 +02:00
|
|
|
"# Check failed: false\n"
|
2018-07-03 12:07:53 +02:00
|
|
|
"# Hi there!");
|
2018-06-14 13:14:22 +02:00
|
|
|
}
|
|
|
|
|
#endif
|
2018-07-03 12:07:53 +02:00
|
|
|
|
2014-05-13 18:00:26 +00:00
|
|
|
// Test using multiple log streams. The INFO stream should get the INFO message,
|
|
|
|
|
// the VERBOSE stream should get the INFO and the VERBOSE.
|
|
|
|
|
// We should restore the correct global state at the end.
|
|
|
|
|
TEST(LogTest, MultipleStreams) {
|
2017-02-27 14:06:41 -08:00
|
|
|
int sev = LogMessage::GetLogToStream(nullptr);
|
2014-05-13 18:00:26 +00:00
|
|
|
|
|
|
|
|
std::string str1, str2;
|
2015-05-25 11:25:59 +02:00
|
|
|
LogSinkImpl<StringStream> stream1(&str1), stream2(&str2);
|
2014-05-13 18:00:26 +00:00
|
|
|
LogMessage::AddLogToStream(&stream1, LS_INFO);
|
|
|
|
|
LogMessage::AddLogToStream(&stream2, LS_VERBOSE);
|
|
|
|
|
EXPECT_EQ(LS_INFO, LogMessage::GetLogToStream(&stream1));
|
|
|
|
|
EXPECT_EQ(LS_VERBOSE, LogMessage::GetLogToStream(&stream2));
|
|
|
|
|
|
2017-11-09 11:09:25 +01:00
|
|
|
RTC_LOG(LS_INFO) << "INFO";
|
|
|
|
|
RTC_LOG(LS_VERBOSE) << "VERBOSE";
|
2014-05-13 18:00:26 +00:00
|
|
|
|
|
|
|
|
EXPECT_NE(std::string::npos, str1.find("INFO"));
|
|
|
|
|
EXPECT_EQ(std::string::npos, str1.find("VERBOSE"));
|
|
|
|
|
EXPECT_NE(std::string::npos, str2.find("INFO"));
|
|
|
|
|
EXPECT_NE(std::string::npos, str2.find("VERBOSE"));
|
|
|
|
|
|
|
|
|
|
LogMessage::RemoveLogToStream(&stream2);
|
|
|
|
|
LogMessage::RemoveLogToStream(&stream1);
|
2015-05-23 09:54:07 +02:00
|
|
|
EXPECT_EQ(LS_NONE, LogMessage::GetLogToStream(&stream2));
|
|
|
|
|
EXPECT_EQ(LS_NONE, LogMessage::GetLogToStream(&stream1));
|
2014-05-13 18:00:26 +00:00
|
|
|
|
2017-02-27 14:06:41 -08:00
|
|
|
EXPECT_EQ(sev, LogMessage::GetLogToStream(nullptr));
|
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
|
|
|
class LogThread {
|
2014-05-13 18:00:26 +00:00
|
|
|
public:
|
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
|
|
|
LogThread() : thread_(&ThreadEntry, this, "LogThread") {}
|
|
|
|
|
~LogThread() { thread_.Stop(); }
|
2017-07-14 14:44:46 -07: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
|
|
|
void Start() { thread_.Start(); }
|
2014-05-13 18:00:26 +00:00
|
|
|
|
|
|
|
|
private:
|
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
|
|
|
void Run() {
|
|
|
|
|
// LS_SENSITIVE by default to avoid cluttering up any real logging going on.
|
2017-11-09 11:09:25 +01:00
|
|
|
RTC_LOG(LS_SENSITIVE) << "RTC_LOG";
|
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
|
|
|
|
|
|
|
|
static void ThreadEntry(void* p) { static_cast<LogThread*>(p)->Run(); }
|
|
|
|
|
|
|
|
|
|
PlatformThread thread_;
|
2018-11-07 08:43:50 +01:00
|
|
|
Event event_;
|
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
|
|
|
// Ensure we don't crash when adding/removing streams while threads are going.
|
|
|
|
|
// We should restore the correct global state at the end.
|
2014-10-09 22:08:15 +00:00
|
|
|
TEST(LogTest, MultipleThreads) {
|
2017-02-27 14:06:41 -08:00
|
|
|
int sev = LogMessage::GetLogToStream(nullptr);
|
2014-05-13 18:00:26 +00:00
|
|
|
|
|
|
|
|
LogThread thread1, thread2, thread3;
|
|
|
|
|
thread1.Start();
|
|
|
|
|
thread2.Start();
|
|
|
|
|
thread3.Start();
|
|
|
|
|
|
2018-06-20 10:27:53 +02:00
|
|
|
std::string s1, s2, s3;
|
|
|
|
|
LogSinkImpl<StringStream> stream1(&s1), stream2(&s2), stream3(&s3);
|
2014-05-13 18:00:26 +00:00
|
|
|
for (int i = 0; i < 1000; ++i) {
|
|
|
|
|
LogMessage::AddLogToStream(&stream1, LS_INFO);
|
|
|
|
|
LogMessage::AddLogToStream(&stream2, LS_VERBOSE);
|
|
|
|
|
LogMessage::AddLogToStream(&stream3, LS_SENSITIVE);
|
|
|
|
|
LogMessage::RemoveLogToStream(&stream1);
|
|
|
|
|
LogMessage::RemoveLogToStream(&stream2);
|
|
|
|
|
LogMessage::RemoveLogToStream(&stream3);
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-27 14:06:41 -08:00
|
|
|
EXPECT_EQ(sev, LogMessage::GetLogToStream(nullptr));
|
2014-05-13 18:00:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(LogTest, WallClockStartTime) {
|
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 time = LogMessage::WallClockStartTime();
|
2014-05-13 18:00:26 +00:00
|
|
|
// Expect the time to be in a sensible range, e.g. > 2012-01-01.
|
|
|
|
|
EXPECT_GT(time, 1325376000u);
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
TEST(LogTest, CheckExtraErrorField) {
|
|
|
|
|
LogMessageForTesting log_msg("some/path/myfile.cc", 100, LS_WARNING,
|
|
|
|
|
ERRCTX_ERRNO, 0xD);
|
|
|
|
|
log_msg.stream() << "This gets added at dtor time";
|
|
|
|
|
|
|
|
|
|
const std::string& extra = log_msg.get_extra();
|
|
|
|
|
const size_t length_to_check = arraysize("[0x12345678]") - 1;
|
|
|
|
|
ASSERT_GE(extra.length(), length_to_check);
|
|
|
|
|
EXPECT_EQ(std::string("[0x0000000D]"), extra.substr(0, length_to_check));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(LogTest, CheckFilePathParsed) {
|
|
|
|
|
LogMessageForTesting log_msg("some/path/myfile.cc", 100, LS_INFO);
|
|
|
|
|
log_msg.stream() << "<- Does this look right?";
|
|
|
|
|
|
|
|
|
|
const std::string stream = log_msg.GetPrintStream();
|
2018-06-20 14:07:05 +02:00
|
|
|
#if defined(WEBRTC_ANDROID)
|
|
|
|
|
const char* tag = log_msg.get_tag();
|
|
|
|
|
EXPECT_NE(nullptr, strstr(tag, "myfile.cc"));
|
|
|
|
|
EXPECT_NE(std::string::npos, stream.find("100"));
|
|
|
|
|
#else
|
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
|
|
|
EXPECT_NE(std::string::npos, stream.find("(myfile.cc:100)"));
|
2018-06-20 14:07:05 +02:00
|
|
|
#endif
|
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
|
|
|
}
|
|
|
|
|
|
2018-06-20 14:07:05 +02:00
|
|
|
#if defined(WEBRTC_ANDROID)
|
|
|
|
|
TEST(LogTest, CheckTagAddedToStringInDefaultOnLogMessageAndroid) {
|
|
|
|
|
std::string str;
|
|
|
|
|
LogSinkImpl<StringStream> stream(&str);
|
|
|
|
|
LogMessage::AddLogToStream(&stream, LS_INFO);
|
|
|
|
|
EXPECT_EQ(LS_INFO, LogMessage::GetLogToStream(&stream));
|
|
|
|
|
|
|
|
|
|
RTC_LOG_TAG(LS_INFO, "my_tag") << "INFO";
|
|
|
|
|
EXPECT_NE(std::string::npos, str.find("INFO"));
|
|
|
|
|
EXPECT_NE(std::string::npos, str.find("my_tag"));
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
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
|
|
|
// Test the time required to write 1000 80-character logs to a string.
|
|
|
|
|
TEST(LogTest, Perf) {
|
|
|
|
|
std::string str;
|
|
|
|
|
LogSinkImpl<StringStream> stream(&str);
|
2014-05-13 18:00:26 +00:00
|
|
|
LogMessage::AddLogToStream(&stream, LS_SENSITIVE);
|
|
|
|
|
|
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
|
|
|
const std::string message(80, 'X');
|
|
|
|
|
{
|
|
|
|
|
LogMessageForTesting sanity_check_msg(__FILE__, __LINE__, LS_SENSITIVE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// We now know how many bytes the logging framework will tag onto every msg.
|
|
|
|
|
const size_t logging_overhead = str.size();
|
|
|
|
|
// Reset the stream to 0 size.
|
|
|
|
|
str.clear();
|
|
|
|
|
str.reserve(120000);
|
|
|
|
|
static const int kRepetitions = 1000;
|
|
|
|
|
|
2016-05-06 11:29:15 -07:00
|
|
|
int64_t start = TimeMillis(), finish;
|
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
|
|
|
for (int i = 0; i < kRepetitions; ++i) {
|
|
|
|
|
LogMessageForTesting(__FILE__, __LINE__, LS_SENSITIVE).stream() << message;
|
2014-05-13 18:00:26 +00:00
|
|
|
}
|
2016-05-06 11:29:15 -07:00
|
|
|
finish = TimeMillis();
|
2014-05-13 18:00:26 +00:00
|
|
|
|
|
|
|
|
LogMessage::RemoveLogToStream(&stream);
|
|
|
|
|
stream.Close();
|
|
|
|
|
|
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
|
|
|
EXPECT_EQ(str.size(), (message.size() + logging_overhead) * kRepetitions);
|
|
|
|
|
RTC_LOG(LS_INFO) << "Total log time: " << TimeDiff(finish, start) << " ms "
|
|
|
|
|
<< " total bytes logged: " << str.size();
|
2014-05-13 18:00:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace rtc
|