• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
12 namespace webrtc {
13 namespace test {
14 
15 ColumnPrinter::ColumnPrinter(const ColumnPrinter&) = default;
16 ColumnPrinter::~ColumnPrinter() = default;
17 
ColumnPrinter(const char * headers,std::function<void (rtc::SimpleStringBuilder &)> printer,size_t max_length)18 ColumnPrinter::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 
Fixed(const char * headers,std::string fields)24 ColumnPrinter ColumnPrinter::Fixed(const char* headers, std::string fields) {
25   return ColumnPrinter(
26       headers, [fields](rtc::SimpleStringBuilder& sb) { sb << fields; },
27       fields.size());
28 }
29 
Lambda(const char * headers,std::function<void (rtc::SimpleStringBuilder &)> printer,size_t max_length)30 ColumnPrinter 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 
StatesPrinter(std::unique_ptr<RtcEventLogOutput> writer,std::vector<ColumnPrinter> printers)37 StatesPrinter::StatesPrinter(std::unique_ptr<RtcEventLogOutput> writer,
38                              std::vector<ColumnPrinter> printers)
39     : writer_(std::move(writer)), printers_(printers) {
40   RTC_CHECK(!printers_.empty());
41   for (auto& printer : printers_)
42     buffer_size_ += printer.max_length_ + 1;
43   buffer_.resize(buffer_size_);
44 }
45 
46 StatesPrinter::~StatesPrinter() = default;
47 
PrintHeaders()48 void StatesPrinter::PrintHeaders() {
49   if (!writer_)
50     return;
51   writer_->Write(printers_[0].headers_);
52   for (size_t i = 1; i < printers_.size(); ++i) {
53     writer_->Write(" ");
54     writer_->Write(printers_[i].headers_);
55   }
56   writer_->Write("\n");
57 }
58 
PrintRow()59 void 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   }
68   sb << "\n";
69   if (writer_)
70     writer_->Write(std::string(sb.str(), sb.size()));
71 }
72 }  // namespace test
73 }  // namespace webrtc
74