webrtc_m130/rtc_base/logging_unittest.cc

417 lines
12 KiB
C++
Raw Normal View History

/*
* 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.
*/
#include "rtc_base/logging.h"
#if RTC_LOG_ENABLED()
#include <string>
#include "absl/strings/string_view.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/platform_thread.h"
#include "rtc_base/time_utils.h"
#include "test/gmock.h"
#include "test/gtest.h"
namespace rtc {
namespace {
using ::testing::HasSubstr;
#if defined(WEBRTC_WIN)
constexpr char kFakeFilePath[] = "some\\path\\myfile.cc";
#else
constexpr char kFakeFilePath[] = "some/path/myfile.cc";
#endif
} // namespace
class LogSinkImpl : public LogSink {
public:
explicit LogSinkImpl(std::string* log_data) : log_data_(log_data) {}
template <typename P>
explicit LogSinkImpl(P* p) {}
private:
void OnLogMessage(const std::string& message) override {
OnLogMessage(absl::string_view(message));
}
void OnLogMessage(absl::string_view message) override {
log_data_->append(message.begin(), message.end());
}
std::string* const log_data_;
};
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_; }
#if defined(WEBRTC_ANDROID)
const char* get_tag() const { return log_line_.tag().data(); }
#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();
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;
};
// 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) {
int sev = LogMessage::GetLogToStream(nullptr);
std::string str;
LogSinkImpl stream(&str);
LogMessage::AddLogToStream(&stream, LS_INFO);
EXPECT_EQ(LS_INFO, LogMessage::GetLogToStream(&stream));
RTC_LOG(LS_INFO) << "INFO";
RTC_LOG(LS_VERBOSE) << "VERBOSE";
EXPECT_THAT(str, HasSubstr("INFO"));
EXPECT_THAT(str, Not(HasSubstr("VERBOSE")));
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";
const char* null_string = nullptr;
void* p = reinterpret_cast<void*>(0xabcd);
// Log all supported 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 << "|" << null_string
<< "|";
// Signed integers
EXPECT_THAT(str, HasSubstr("|1|"));
EXPECT_THAT(str, HasSubstr("|2|"));
EXPECT_THAT(str, HasSubstr("|3|"));
// Unsigned integers
EXPECT_THAT(str, HasSubstr("|4|"));
EXPECT_THAT(str, HasSubstr("|5|"));
EXPECT_THAT(str, HasSubstr("|6|"));
// Strings
EXPECT_THAT(str, HasSubstr("|char*|"));
EXPECT_THAT(str, HasSubstr("|std::string|"));
EXPECT_THAT(str, HasSubstr("|absl::stringview|"));
// void*
EXPECT_THAT(str, HasSubstr("|abcd|"));
// null char*
EXPECT_THAT(str, HasSubstr("|(null)|"));
LogMessage::RemoveLogToStream(&stream);
EXPECT_EQ(LS_NONE, LogMessage::GetLogToStream(&stream));
EXPECT_EQ(sev, LogMessage::GetLogToStream(nullptr));
}
TEST(LogTest, LogIfLogIfConditionIsTrue) {
std::string str;
LogSinkImpl stream(&str);
LogMessage::AddLogToStream(&stream, LS_INFO);
RTC_LOG_IF(LS_INFO, true) << "Hello";
EXPECT_THAT(str, HasSubstr("Hello"));
LogMessage::RemoveLogToStream(&stream);
}
TEST(LogTest, LogIfDontLogIfConditionIsFalse) {
std::string str;
LogSinkImpl stream(&str);
LogMessage::AddLogToStream(&stream, LS_INFO);
RTC_LOG_IF(LS_INFO, false) << "Hello";
EXPECT_THAT(str, Not(HasSubstr("Hello")));
LogMessage::RemoveLogToStream(&stream);
}
TEST(LogTest, LogIfFLogIfConditionIsTrue) {
std::string str;
LogSinkImpl stream(&str);
LogMessage::AddLogToStream(&stream, LS_INFO);
RTC_LOG_IF_F(LS_INFO, true) << "Hello";
EXPECT_THAT(str, HasSubstr(__FUNCTION__));
EXPECT_THAT(str, HasSubstr("Hello"));
LogMessage::RemoveLogToStream(&stream);
}
TEST(LogTest, LogIfFDontLogIfConditionIsFalse) {
std::string str;
LogSinkImpl stream(&str);
LogMessage::AddLogToStream(&stream, LS_INFO);
RTC_LOG_IF_F(LS_INFO, false) << "Not";
EXPECT_THAT(str, Not(HasSubstr(__FUNCTION__)));
EXPECT_THAT(str, Not(HasSubstr("Not")));
LogMessage::RemoveLogToStream(&stream);
}
// 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) {
int sev = LogMessage::GetLogToStream(nullptr);
std::string str1, str2;
LogSinkImpl stream1(&str1), stream2(&str2);
LogMessage::AddLogToStream(&stream1, LS_INFO);
LogMessage::AddLogToStream(&stream2, LS_VERBOSE);
EXPECT_EQ(LS_INFO, LogMessage::GetLogToStream(&stream1));
EXPECT_EQ(LS_VERBOSE, LogMessage::GetLogToStream(&stream2));
RTC_LOG(LS_INFO) << "INFO";
RTC_LOG(LS_VERBOSE) << "VERBOSE";
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);
EXPECT_EQ(LS_NONE, LogMessage::GetLogToStream(&stream2));
EXPECT_EQ(LS_NONE, LogMessage::GetLogToStream(&stream1));
EXPECT_EQ(sev, LogMessage::GetLogToStream(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 LogThread {
public:
Reland "Refactor the PlatformThread API." This reverts commit 793bac569fdf1be16cbf24d7871d20d00bbec81b. Reason for revert: rare compilation error fixed Original change's description: > Revert "Refactor the PlatformThread API." > > This reverts commit c89fdd716c4c8af608017c76f75bf27e4c3d602e. > > Reason for revert: Causes rare compilation error on win-libfuzzer-asan trybot. > See https://ci.chromium.org/p/chromium/builders/try/win-libfuzzer-asan-rel/713745? > > Original change's description: > > Refactor the PlatformThread API. > > > > PlatformThread's API is using old style function pointers, causes > > casting, is unintuitive and forces artificial call sequences, and > > is additionally possible to misuse in release mode. > > > > Fix this by an API face lift: > > 1. The class is turned into a handle, which can be empty. > > 2. The only way of getting a non-empty PlatformThread is by calling > > SpawnJoinable or SpawnDetached, clearly conveying the semantics to the > > code reader. > > 3. Handles can be Finalized, which works differently for joinable and > > detached threads: > > a) Handles for detached threads are simply closed where applicable. > > b) Joinable threads are joined before handles are closed. > > 4. The destructor finalizes handles. No explicit call is needed. > > > > Fixed: webrtc:12727 > > Change-Id: Id00a0464edf4fc9e552b6a1fbb5d2e1280e88811 > > Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/215075 > > Commit-Queue: Markus Handell <handellm@webrtc.org> > > Reviewed-by: Harald Alvestrand <hta@webrtc.org> > > Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org> > > Reviewed-by: Tommi <tommi@webrtc.org> > > Cr-Commit-Position: refs/heads/master@{#33923} > > # Not skipping CQ checks because original CL landed > 1 day ago. > > TBR=handellm@webrtc.org > > Bug: webrtc:12727 > Change-Id: Ic0146be8866f6dd3ad9c364fb8646650b8e07419 > Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/217583 > Reviewed-by: Guido Urdaneta <guidou@webrtc.org> > Reviewed-by: Markus Handell <handellm@webrtc.org> > Commit-Queue: Guido Urdaneta <guidou@webrtc.org> > Cr-Commit-Position: refs/heads/master@{#33936} # Not skipping CQ checks because this is a reland. Bug: webrtc:12727 Change-Id: Ifd6f44eac72fed84474277a1be03eb84d2f4376e Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/217881 Commit-Queue: Mirko Bonadei <mbonadei@webrtc.org> Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org> Reviewed-by: Markus Handell <handellm@webrtc.org> Reviewed-by: Harald Alvestrand <hta@webrtc.org> Cr-Commit-Position: refs/heads/master@{#33950}
2021-05-07 15:02:36 +02:00
void Start() {
thread_ = PlatformThread::SpawnJoinable(
[] { RTC_LOG(LS_VERBOSE) << "RTC_LOG"; }, "LogThread");
}
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
PlatformThread thread_;
};
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.
TEST(LogTest, MultipleThreads) {
int sev = LogMessage::GetLogToStream(nullptr);
LogThread thread1, thread2, thread3;
thread1.Start();
thread2.Start();
thread3.Start();
std::string s1, s2, s3;
LogSinkImpl stream1(&s1), stream2(&s2), stream3(&s3);
for (int i = 0; i < 1000; ++i) {
LogMessage::AddLogToStream(&stream1, LS_WARNING);
LogMessage::AddLogToStream(&stream2, LS_INFO);
LogMessage::AddLogToStream(&stream3, LS_VERBOSE);
LogMessage::RemoveLogToStream(&stream1);
LogMessage::RemoveLogToStream(&stream2);
LogMessage::RemoveLogToStream(&stream3);
}
EXPECT_EQ(sev, LogMessage::GetLogToStream(nullptr));
}
TEST(LogTest, WallClockStartTime) {
uint32_t time = LogMessage::WallClockStartTime();
// 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(kFakeFilePath, 100, LS_WARNING, ERRCTX_ERRNO,
0xD);
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
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) {
std::string str;
LogSinkImpl stream(&str);
LogMessage::AddLogToStream(&stream, LS_INFO);
EXPECT_EQ(LS_INFO, LogMessage::GetLogToStream(&stream));
#if defined(WEBRTC_ANDROID)
const char* tag = nullptr;
#endif
{
LogMessageForTesting log_msg(kFakeFilePath, 100, LS_INFO);
log_msg.stream() << "<- Does this look right?";
#if defined(WEBRTC_ANDROID)
tag = log_msg.get_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
#if defined(WEBRTC_ANDROID)
EXPECT_NE(nullptr, strstr(tag, "myfile.cc"));
EXPECT_THAT(str, HasSubstr("100"));
#else
EXPECT_THAT(str, HasSubstr("(myfile.cc:100)"));
#endif
LogMessage::RemoveLogToStream(&stream);
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 defined(WEBRTC_ANDROID)
TEST(LogTest, CheckTagAddedToStringInDefaultOnLogMessageAndroid) {
std::string str;
LogSinkImpl stream(&str);
LogMessage::AddLogToStream(&stream, LS_INFO);
EXPECT_EQ(LS_INFO, LogMessage::GetLogToStream(&stream));
RTC_LOG_TAG(LS_INFO, "my_tag") << "INFO";
EXPECT_THAT(str, HasSubstr("INFO"));
EXPECT_THAT(str, HasSubstr("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 stream(&str);
LogMessage::AddLogToStream(&stream, LS_VERBOSE);
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_VERBOSE); }
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
// 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;
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_VERBOSE).stream() << message;
}
finish = TimeMillis();
LogMessage::RemoveLogToStream(&stream);
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();
}
TEST(LogTest, EnumsAreSupported) {
enum class TestEnum { kValue0 = 0, kValue1 = 1 };
std::string str;
LogSinkImpl stream(&str);
LogMessage::AddLogToStream(&stream, LS_INFO);
RTC_LOG(LS_INFO) << "[" << TestEnum::kValue0 << "]";
EXPECT_THAT(str, HasSubstr("[0]"));
EXPECT_THAT(str, Not(HasSubstr("[1]")));
RTC_LOG(LS_INFO) << "[" << TestEnum::kValue1 << "]";
EXPECT_THAT(str, HasSubstr("[1]"));
LogMessage::RemoveLogToStream(&stream);
}
TEST(LogTest, NoopSeverityDoesNotRunStringFormatting) {
if (!LogMessage::IsNoop(LS_VERBOSE)) {
RTC_LOG(LS_WARNING) << "Skipping test since verbose logging is turned on.";
return;
}
bool was_called = false;
auto cb = [&was_called]() {
was_called = true;
return "This could be an expensive callback.";
};
RTC_LOG(LS_VERBOSE) << "This should not be logged: " << cb();
EXPECT_FALSE(was_called);
}
struct StructWithStringfy {
template <typename Sink>
friend void AbslStringify(Sink& sink, const StructWithStringfy& /*self*/) {
sink.Append("absl-stringify");
}
};
TEST(LogTest, UseAbslStringForCustomTypes) {
std::string str;
LogSinkImpl stream(&str);
LogMessage::AddLogToStream(&stream, LS_INFO);
StructWithStringfy t;
RTC_LOG(LS_INFO) << t;
EXPECT_THAT(str, HasSubstr("absl-stringify"));
LogMessage::RemoveLogToStream(&stream);
}
enum class TestEnumStringify { kValue0 = 0, kValue1 = 1 };
template <typename Sink>
void AbslStringify(Sink& sink, TestEnumStringify value) {
switch (value) {
case TestEnumStringify::kValue0:
sink.Append("kValue0");
break;
case TestEnumStringify::kValue1:
sink.Append("kValue1");
break;
}
}
TEST(LogTest, EnumSupportsAbslStringify) {
std::string str;
LogSinkImpl stream(&str);
LogMessage::AddLogToStream(&stream, LS_INFO);
RTC_LOG(LS_INFO) << "[" << TestEnumStringify::kValue0 << "]";
EXPECT_THAT(str, HasSubstr("[kValue0]"));
EXPECT_THAT(str, Not(HasSubstr("[kValue1]")));
RTC_LOG(LS_INFO) << "[" << TestEnumStringify::kValue1 << "]";
EXPECT_THAT(str, HasSubstr("[kValue1]"));
LogMessage::RemoveLogToStream(&stream);
}
} // namespace rtc
#endif // RTC_LOG_ENABLED()