blob: 661c83bd0d814f290110643f79add5e66968fba4 [file] [log] [blame]
Sebastian Jansson98b07e92018-09-27 13:47:01 +02001/*
2 * Copyright 2018 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10#include "test/scenario/column_printer.h"
11
12namespace webrtc {
13namespace test {
14
15ColumnPrinter::ColumnPrinter(const ColumnPrinter&) = default;
16ColumnPrinter::~ColumnPrinter() = default;
17
18ColumnPrinter::ColumnPrinter(
19 const char* headers,
20 std::function<void(rtc::SimpleStringBuilder&)> printer,
21 size_t max_length)
22 : headers_(headers), printer_(printer), max_length_(max_length) {}
23
24ColumnPrinter ColumnPrinter::Fixed(const char* headers, std::string fields) {
Jonas Olssona4d87372019-07-05 19:08:33 +020025 return ColumnPrinter(
26 headers, [fields](rtc::SimpleStringBuilder& sb) { sb << fields; },
27 fields.size());
Sebastian Jansson98b07e92018-09-27 13:47:01 +020028}
29
30ColumnPrinter ColumnPrinter::Lambda(
31 const char* headers,
32 std::function<void(rtc::SimpleStringBuilder&)> printer,
33 size_t max_length) {
34 return ColumnPrinter(headers, printer, max_length);
35}
36
Sebastian Jansson52de8b02019-01-16 17:25:44 +010037StatesPrinter::StatesPrinter(std::unique_ptr<RtcEventLogOutput> writer,
Sebastian Jansson98b07e92018-09-27 13:47:01 +020038 std::vector<ColumnPrinter> printers)
Sebastian Jansson52de8b02019-01-16 17:25:44 +010039 : writer_(std::move(writer)), printers_(printers) {
Sebastian Jansson98b07e92018-09-27 13:47:01 +020040 RTC_CHECK(!printers_.empty());
41 for (auto& printer : printers_)
42 buffer_size_ += printer.max_length_ + 1;
43 buffer_.resize(buffer_size_);
44}
45
Sebastian Jansson52de8b02019-01-16 17:25:44 +010046StatesPrinter::~StatesPrinter() = default;
Sebastian Jansson98b07e92018-09-27 13:47:01 +020047
48void StatesPrinter::PrintHeaders() {
Sebastian Jansson52de8b02019-01-16 17:25:44 +010049 if (!writer_)
Sebastian Jansson98b07e92018-09-27 13:47:01 +020050 return;
Sebastian Jansson52de8b02019-01-16 17:25:44 +010051 writer_->Write(printers_[0].headers_);
Sebastian Jansson98b07e92018-09-27 13:47:01 +020052 for (size_t i = 1; i < printers_.size(); ++i) {
Sebastian Jansson52de8b02019-01-16 17:25:44 +010053 writer_->Write(" ");
54 writer_->Write(printers_[i].headers_);
Sebastian Jansson98b07e92018-09-27 13:47:01 +020055 }
Sebastian Jansson52de8b02019-01-16 17:25:44 +010056 writer_->Write("\n");
Sebastian Jansson98b07e92018-09-27 13:47:01 +020057}
58
59void StatesPrinter::PrintRow() {
60 // Note that this is run for null output to preserve side effects, this allows
61 // setting break points etc.
62 rtc::SimpleStringBuilder sb(buffer_);
63 printers_[0].printer_(sb);
64 for (size_t i = 1; i < printers_.size(); ++i) {
65 sb << ' ';
66 printers_[i].printer_(sb);
67 }
Sebastian Jansson52de8b02019-01-16 17:25:44 +010068 sb << "\n";
69 if (writer_)
70 writer_->Write(std::string(sb.str(), sb.size()));
Sebastian Jansson98b07e92018-09-27 13:47:01 +020071}
72} // namespace test
73} // namespace webrtc