summaryrefslogtreecommitdiffstats
path: root/third_party/libwebrtc/test/logging
diff options
context:
space:
mode:
Diffstat (limited to 'third_party/libwebrtc/test/logging')
-rw-r--r--third_party/libwebrtc/test/logging/BUILD.gn35
-rw-r--r--third_party/libwebrtc/test/logging/file_log_writer.cc65
-rw-r--r--third_party/libwebrtc/test/logging/file_log_writer.h50
-rw-r--r--third_party/libwebrtc/test/logging/log_writer.cc26
-rw-r--r--third_party/libwebrtc/test/logging/log_writer.h65
-rw-r--r--third_party/libwebrtc/test/logging/memory_log_writer.cc64
-rw-r--r--third_party/libwebrtc/test/logging/memory_log_writer.h40
7 files changed, 345 insertions, 0 deletions
diff --git a/third_party/libwebrtc/test/logging/BUILD.gn b/third_party/libwebrtc/test/logging/BUILD.gn
new file mode 100644
index 0000000000..301c0e59c0
--- /dev/null
+++ b/third_party/libwebrtc/test/logging/BUILD.gn
@@ -0,0 +1,35 @@
+# Copyright (c) 2019 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.
+
+import("../../webrtc.gni")
+
+rtc_library("log_writer") {
+ testonly = true
+ visibility = [ "*" ]
+ sources = [
+ "file_log_writer.cc",
+ "file_log_writer.h",
+ "log_writer.cc",
+ "log_writer.h",
+ "memory_log_writer.cc",
+ "memory_log_writer.h",
+ ]
+
+ deps = [
+ "../../api:libjingle_logging_api",
+ "../../rtc_base:checks",
+ "../../rtc_base:logging",
+ "../../rtc_base:rtc_base_tests_utils",
+ "../../rtc_base:stringutils",
+ "../../test:fileutils",
+ ]
+ absl_deps = [
+ "//third_party/abseil-cpp/absl/strings",
+ "//third_party/abseil-cpp/absl/types:optional",
+ ]
+}
diff --git a/third_party/libwebrtc/test/logging/file_log_writer.cc b/third_party/libwebrtc/test/logging/file_log_writer.cc
new file mode 100644
index 0000000000..9189e1630d
--- /dev/null
+++ b/third_party/libwebrtc/test/logging/file_log_writer.cc
@@ -0,0 +1,65 @@
+/*
+ * Copyright 2019 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 "test/logging/file_log_writer.h"
+
+#include <memory>
+
+#include "absl/strings/string_view.h"
+#include "rtc_base/checks.h"
+#include "rtc_base/logging.h"
+#include "test/testsupport/file_utils.h"
+
+namespace webrtc {
+namespace webrtc_impl {
+
+FileLogWriter::FileLogWriter(absl::string_view file_path)
+ : out_(std::fopen(std::string(file_path).c_str(), "wb")) {
+ RTC_CHECK(out_ != nullptr)
+ << "Failed to open file: '" << file_path << "' for writing.";
+}
+
+FileLogWriter::~FileLogWriter() {
+ std::fclose(out_);
+}
+
+bool FileLogWriter::IsActive() const {
+ return true;
+}
+
+bool FileLogWriter::Write(absl::string_view value) {
+ // We don't expect the write to fail. If it does, we don't want to risk
+ // silently ignoring it.
+ RTC_CHECK_EQ(std::fwrite(value.data(), 1, value.size(), out_), value.size())
+ << "fwrite failed unexpectedly: " << errno;
+ return true;
+}
+
+void FileLogWriter::Flush() {
+ RTC_CHECK_EQ(fflush(out_), 0) << "fflush failed unexpectedly: " << errno;
+}
+
+} // namespace webrtc_impl
+
+FileLogWriterFactory::FileLogWriterFactory(absl::string_view base_path)
+ : base_path_(base_path) {
+ for (size_t i = 0; i < base_path.size(); ++i) {
+ if (base_path[i] == '/')
+ test::CreateDir(base_path.substr(0, i));
+ }
+}
+
+FileLogWriterFactory::~FileLogWriterFactory() {}
+
+std::unique_ptr<RtcEventLogOutput> FileLogWriterFactory::Create(
+ absl::string_view filename) {
+ return std::make_unique<webrtc_impl::FileLogWriter>(base_path_ +
+ std::string(filename));
+}
+} // namespace webrtc
diff --git a/third_party/libwebrtc/test/logging/file_log_writer.h b/third_party/libwebrtc/test/logging/file_log_writer.h
new file mode 100644
index 0000000000..c49b96ceff
--- /dev/null
+++ b/third_party/libwebrtc/test/logging/file_log_writer.h
@@ -0,0 +1,50 @@
+/*
+ * Copyright 2019 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.
+ */
+#ifndef TEST_LOGGING_FILE_LOG_WRITER_H_
+#define TEST_LOGGING_FILE_LOG_WRITER_H_
+
+#include <cstdio>
+#include <memory>
+#include <string>
+#include <vector>
+
+#include "absl/strings/string_view.h"
+#include "test/logging/log_writer.h"
+
+namespace webrtc {
+namespace webrtc_impl {
+class FileLogWriter final : public RtcEventLogOutput {
+ public:
+ explicit FileLogWriter(absl::string_view file_path);
+ ~FileLogWriter() final;
+ bool IsActive() const override;
+ bool Write(absl::string_view value) override;
+ void Flush() override;
+
+ private:
+ std::FILE* const out_;
+};
+} // namespace webrtc_impl
+class FileLogWriterFactory final : public LogWriterFactoryInterface {
+ public:
+ explicit FileLogWriterFactory(absl::string_view base_path);
+ ~FileLogWriterFactory() final;
+
+ std::unique_ptr<RtcEventLogOutput> Create(
+ absl::string_view filename) override;
+
+ private:
+ const std::string base_path_;
+ std::vector<std::unique_ptr<webrtc_impl::FileLogWriter>> writers_;
+};
+
+} // namespace webrtc
+
+#endif // TEST_LOGGING_FILE_LOG_WRITER_H_
diff --git a/third_party/libwebrtc/test/logging/log_writer.cc b/third_party/libwebrtc/test/logging/log_writer.cc
new file mode 100644
index 0000000000..d9b8c1e68f
--- /dev/null
+++ b/third_party/libwebrtc/test/logging/log_writer.cc
@@ -0,0 +1,26 @@
+/*
+ * Copyright 2019 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 "test/logging/log_writer.h"
+
+#include "absl/strings/string_view.h"
+
+namespace webrtc {
+
+LogWriterFactoryAddPrefix::LogWriterFactoryAddPrefix(
+ LogWriterFactoryInterface* base,
+ absl::string_view prefix)
+ : base_factory_(base), prefix_(prefix) {}
+
+std::unique_ptr<RtcEventLogOutput> LogWriterFactoryAddPrefix::Create(
+ absl::string_view filename) {
+ return base_factory_->Create(prefix_ + std::string(filename));
+}
+
+} // namespace webrtc
diff --git a/third_party/libwebrtc/test/logging/log_writer.h b/third_party/libwebrtc/test/logging/log_writer.h
new file mode 100644
index 0000000000..335dab353f
--- /dev/null
+++ b/third_party/libwebrtc/test/logging/log_writer.h
@@ -0,0 +1,65 @@
+/*
+ * Copyright 2019 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.
+ */
+#ifndef TEST_LOGGING_LOG_WRITER_H_
+#define TEST_LOGGING_LOG_WRITER_H_
+
+#include <stdarg.h>
+
+#include <memory>
+#include <string>
+#include <utility>
+
+#include "absl/strings/string_view.h"
+#include "api/rtc_event_log_output.h"
+#include "rtc_base/strings/string_builder.h"
+
+namespace webrtc {
+template <class... Args>
+inline void LogWriteFormat(RtcEventLogOutput* out_, const char* fmt, ...) {
+ va_list args, copy;
+ va_start(args, fmt);
+ va_copy(copy, args);
+ const int predicted_length = std::vsnprintf(nullptr, 0, fmt, copy);
+ va_end(copy);
+
+ RTC_DCHECK_GE(predicted_length, 0);
+ std::string out_str(predicted_length, '\0');
+ if (predicted_length > 0) {
+ // Pass "+ 1" to vsnprintf to include space for the '\0'.
+ const int actual_length =
+ std::vsnprintf(&out_str.front(), predicted_length + 1, fmt, args);
+ RTC_DCHECK_GE(actual_length, 0);
+ }
+ va_end(args);
+ out_->Write(out_str);
+}
+
+class LogWriterFactoryInterface {
+ public:
+ virtual std::unique_ptr<RtcEventLogOutput> Create(
+ absl::string_view filename) = 0;
+ virtual ~LogWriterFactoryInterface() = default;
+};
+
+class LogWriterFactoryAddPrefix : public LogWriterFactoryInterface {
+ public:
+ LogWriterFactoryAddPrefix(LogWriterFactoryInterface* base,
+ absl::string_view prefix);
+ std::unique_ptr<RtcEventLogOutput> Create(
+ absl::string_view filename) override;
+
+ private:
+ LogWriterFactoryInterface* const base_factory_;
+ const std::string prefix_;
+};
+
+} // namespace webrtc
+
+#endif // TEST_LOGGING_LOG_WRITER_H_
diff --git a/third_party/libwebrtc/test/logging/memory_log_writer.cc b/third_party/libwebrtc/test/logging/memory_log_writer.cc
new file mode 100644
index 0000000000..eae9223c77
--- /dev/null
+++ b/third_party/libwebrtc/test/logging/memory_log_writer.cc
@@ -0,0 +1,64 @@
+/*
+ * Copyright 2019 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 "test/logging/memory_log_writer.h"
+
+#include <memory>
+
+#include "absl/strings/string_view.h"
+#include "rtc_base/checks.h"
+#include "rtc_base/logging.h"
+
+namespace webrtc {
+namespace {
+class MemoryLogWriter final : public RtcEventLogOutput {
+ public:
+ explicit MemoryLogWriter(std::map<std::string, std::string>* target,
+ absl::string_view filename)
+ : target_(target), filename_(filename) {}
+ ~MemoryLogWriter() final { target_->insert({filename_, std::move(buffer_)}); }
+ bool IsActive() const override { return true; }
+ bool Write(absl::string_view value) override {
+ buffer_.append(value.data(), value.size());
+ return true;
+ }
+ void Flush() override {}
+
+ private:
+ std::map<std::string, std::string>* const target_;
+ const std::string filename_;
+ std::string buffer_;
+};
+
+class MemoryLogWriterFactory final : public LogWriterFactoryInterface {
+ public:
+ explicit MemoryLogWriterFactory(std::map<std::string, std::string>* target)
+ : target_(target) {}
+ ~MemoryLogWriterFactory() override {}
+ std::unique_ptr<RtcEventLogOutput> Create(
+ absl::string_view filename) override {
+ return std::make_unique<MemoryLogWriter>(target_, filename);
+ }
+
+ private:
+ std::map<std::string, std::string>* const target_;
+};
+
+} // namespace
+
+MemoryLogStorage::MemoryLogStorage() {}
+
+MemoryLogStorage::~MemoryLogStorage() {}
+
+std::unique_ptr<LogWriterFactoryInterface> MemoryLogStorage::CreateFactory() {
+ return std::make_unique<MemoryLogWriterFactory>(&logs_);
+}
+
+// namespace webrtc_impl
+} // namespace webrtc
diff --git a/third_party/libwebrtc/test/logging/memory_log_writer.h b/third_party/libwebrtc/test/logging/memory_log_writer.h
new file mode 100644
index 0000000000..e795b2fd10
--- /dev/null
+++ b/third_party/libwebrtc/test/logging/memory_log_writer.h
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2019 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.
+ */
+#ifndef TEST_LOGGING_MEMORY_LOG_WRITER_H_
+#define TEST_LOGGING_MEMORY_LOG_WRITER_H_
+
+#include <map>
+#include <memory>
+#include <string>
+#include <vector>
+
+#include "test/logging/log_writer.h"
+
+namespace webrtc {
+
+// Allows creating log writer factories that creates log writers that saves
+// their content to memory. When the log writers are destroyed, their content is
+// saved to the logs_ member of this class. The intended usage is to keep this
+// class alive after the created factories and writers have been destroyed and
+// then use logs() to access all the saved logs.
+class MemoryLogStorage {
+ public:
+ MemoryLogStorage();
+ ~MemoryLogStorage();
+ std::unique_ptr<LogWriterFactoryInterface> CreateFactory();
+ const std::map<std::string, std::string>& logs() { return logs_; }
+
+ private:
+ std::map<std::string, std::string> logs_;
+};
+
+} // namespace webrtc
+
+#endif // TEST_LOGGING_MEMORY_LOG_WRITER_H_