blob: 1a35739c61758b9cb72d4f14b9ff3edb2f916cf2 [file] [log] [blame]
hbos615d3012016-08-24 01:33:13 -07001/*
2 * Copyright 2016 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
11#include "webrtc/api/rtcstats.h"
12
13#include "webrtc/base/stringencode.h"
14
15namespace webrtc {
16
17namespace {
18
19// Produces "{ a, b, c }". Works for non-vector |RTCStatsMemberInterface::Type|
20// types.
21template<typename T>
22std::string VectorToString(const std::vector<T>& vector) {
23 if (vector.empty())
24 return "{}";
25 std::ostringstream oss;
26 oss << "{ " << rtc::ToString<T>(vector[0]);
27 for (size_t i = 1; i < vector.size(); ++i) {
28 oss << ", " << rtc::ToString<T>(vector[i]);
29 }
30 oss << " }";
31 return oss.str();
32}
33
34// Produces "{ \"a\", \"b\", \"c\" }". Works for vectors of both const char* and
35// std::string element types.
36template<typename T>
37std::string VectorOfStringsToString(const std::vector<T>& strings) {
38 if (strings.empty())
39 return "{}";
40 std::ostringstream oss;
41 oss << "{ \"" << rtc::ToString<T>(strings[0]) << '\"';
42 for (size_t i = 1; i < strings.size(); ++i) {
43 oss << ", \"" << rtc::ToString<T>(strings[i]) << '\"';
44 }
45 oss << " }";
46 return oss.str();
47}
48
49} // namespace
50
51std::string RTCStats::ToString() const {
52 std::ostringstream oss;
53 oss << type() << " {\n id: \"" << id_ << "\"\n timestamp: "
54 << timestamp_ << '\n';
55 for (const RTCStatsMemberInterface* member : Members()) {
56 oss << " " << member->name() << ": ";
57 if (member->is_defined()) {
58 if (member->is_string())
59 oss << '"' << member->ValueToString() << "\"\n";
60 else
61 oss << member->ValueToString() << '\n';
62 } else {
63 oss << "undefined\n";
64 }
65 }
66 oss << '}';
67 return oss.str();
68}
69
70std::vector<const RTCStatsMemberInterface*> RTCStats::Members() const {
71 return MembersOfThisObjectAndAncestors(0);
72}
73
74std::vector<const RTCStatsMemberInterface*>
75RTCStats::MembersOfThisObjectAndAncestors(
76 size_t additional_capacity) const {
77 std::vector<const RTCStatsMemberInterface*> members;
78 members.reserve(additional_capacity);
79 return members;
80}
81
82#define WEBRTC_DEFINE_RTCSTATSMEMBER(T, type, is_seq, is_str, to_str) \
83 template<> \
84 const RTCStatsMemberInterface::Type RTCStatsMember<T>::kType = \
85 RTCStatsMemberInterface::type; \
86 template<> \
87 bool RTCStatsMember<T>::is_sequence() const { return is_seq; } \
88 template<> \
89 bool RTCStatsMember<T>::is_string() const { return is_str; } \
90 template<> \
91 std::string RTCStatsMember<T>::ValueToString() const { \
92 RTC_DCHECK(is_defined_); \
93 return to_str; \
94 }
95
96WEBRTC_DEFINE_RTCSTATSMEMBER(int32_t, kInt32, false, false,
97 rtc::ToString(value_));
98WEBRTC_DEFINE_RTCSTATSMEMBER(uint32_t, kUint32, false, false,
99 rtc::ToString(value_));
100WEBRTC_DEFINE_RTCSTATSMEMBER(int64_t, kInt64, false, false,
101 rtc::ToString(value_));
102WEBRTC_DEFINE_RTCSTATSMEMBER(uint64_t, kUint64, false, false,
103 rtc::ToString(value_));
104WEBRTC_DEFINE_RTCSTATSMEMBER(double, kDouble, false, false,
105 rtc::ToString(value_));
106WEBRTC_DEFINE_RTCSTATSMEMBER(const char*, kStaticString, false, true,
107 value_);
108WEBRTC_DEFINE_RTCSTATSMEMBER(std::string, kString, false, true,
109 value_);
110WEBRTC_DEFINE_RTCSTATSMEMBER(
111 std::vector<int32_t>, kSequenceInt32, true, false,
112 VectorToString(value_));
113WEBRTC_DEFINE_RTCSTATSMEMBER(
114 std::vector<uint32_t>, kSequenceUint32, true, false,
115 VectorToString(value_));
116WEBRTC_DEFINE_RTCSTATSMEMBER(
117 std::vector<int64_t>, kSequenceInt64, true, false,
118 VectorToString(value_));
119WEBRTC_DEFINE_RTCSTATSMEMBER(
120 std::vector<uint64_t>, kSequenceUint64, true, false,
121 VectorToString(value_));
122WEBRTC_DEFINE_RTCSTATSMEMBER(
123 std::vector<double>, kSequenceDouble, true, false,
124 VectorToString(value_));
125WEBRTC_DEFINE_RTCSTATSMEMBER(
126 std::vector<const char*>, kSequenceStaticString, true, false,
127 VectorOfStringsToString(value_));
128WEBRTC_DEFINE_RTCSTATSMEMBER(
129 std::vector<std::string>, kSequenceString, true, false,
130 VectorOfStringsToString(value_));
131
132} // namespace webrtc