Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(100)

Side by Side Diff: webrtc/stats/rtcstats.cc

Issue 2983243002: Make RTCStatsReport::ToString() return JSON-parseable string. (Closed)
Patch Set: Add test. Created 3 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2016 The WebRTC Project Authors. All rights reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #include "webrtc/api/stats/rtcstats.h" 11 #include "webrtc/api/stats/rtcstats.h"
12 12
13 #include <sstream> 13 #include <sstream>
14 14
15 #include "webrtc/rtc_base/stringencode.h" 15 #include "webrtc/rtc_base/stringencode.h"
16 16
17 namespace webrtc { 17 namespace webrtc {
18 18
19 namespace { 19 namespace {
20 20
21 // Produces "{ a, b, c }". Works for non-vector |RTCStatsMemberInterface::Type| 21 // Produces "[ a, b, c ]". Works for non-vector |RTCStatsMemberInterface::Type|
22 // types. 22 // types.
23 template<typename T> 23 template<typename T>
24 std::string VectorToString(const std::vector<T>& vector) { 24 std::string VectorToString(const std::vector<T>& vector) {
25 if (vector.empty()) 25 if (vector.empty())
26 return "{}"; 26 return "[]";
27 std::ostringstream oss; 27 std::ostringstream oss;
28 oss << "{ " << rtc::ToString<T>(vector[0]); 28 oss << "[ " << rtc::ToString<T>(vector[0]);
29 for (size_t i = 1; i < vector.size(); ++i) { 29 for (size_t i = 1; i < vector.size(); ++i) {
30 oss << ", " << rtc::ToString<T>(vector[i]); 30 oss << ", " << rtc::ToString<T>(vector[i]);
31 } 31 }
32 oss << " }"; 32 oss << " ]";
33 return oss.str(); 33 return oss.str();
34 } 34 }
35 35
36 // Produces "{ \"a\", \"b\", \"c\" }". Works for vectors of both const char* and 36 // Produces "[ \"a\", \"b\", \"c\" ]". Works for vectors of both const char* and
37 // std::string element types. 37 // std::string element types.
38 template<typename T> 38 template<typename T>
39 std::string VectorOfStringsToString(const std::vector<T>& strings) { 39 std::string VectorOfStringsToString(const std::vector<T>& strings) {
40 if (strings.empty()) 40 if (strings.empty())
41 return "{}"; 41 return "[]";
42 std::ostringstream oss; 42 std::ostringstream oss;
43 oss << "{ \"" << rtc::ToString<T>(strings[0]) << '\"'; 43 oss << "[ \"" << rtc::ToString<T>(strings[0]) << '\"';
44 for (size_t i = 1; i < strings.size(); ++i) { 44 for (size_t i = 1; i < strings.size(); ++i) {
45 oss << ", \"" << rtc::ToString<T>(strings[i]) << '\"'; 45 oss << ", \"" << rtc::ToString<T>(strings[i]) << '\"';
46 } 46 }
47 oss << " }"; 47 oss << " ]";
48 return oss.str(); 48 return oss.str();
49 } 49 }
50 50
51 } // namespace 51 } // namespace
52 52
53 bool RTCStats::operator==(const RTCStats& other) const { 53 bool RTCStats::operator==(const RTCStats& other) const {
54 if (type() != other.type() || id() != other.id()) 54 if (type() != other.type() || id() != other.id())
55 return false; 55 return false;
56 std::vector<const RTCStatsMemberInterface*> members = Members(); 56 std::vector<const RTCStatsMemberInterface*> members = Members();
57 std::vector<const RTCStatsMemberInterface*> other_members = other.Members(); 57 std::vector<const RTCStatsMemberInterface*> other_members = other.Members();
58 RTC_DCHECK_EQ(members.size(), other_members.size()); 58 RTC_DCHECK_EQ(members.size(), other_members.size());
59 for (size_t i = 0; i < members.size(); ++i) { 59 for (size_t i = 0; i < members.size(); ++i) {
60 const RTCStatsMemberInterface* member = members[i]; 60 const RTCStatsMemberInterface* member = members[i];
61 const RTCStatsMemberInterface* other_member = other_members[i]; 61 const RTCStatsMemberInterface* other_member = other_members[i];
62 RTC_DCHECK_EQ(member->type(), other_member->type()); 62 RTC_DCHECK_EQ(member->type(), other_member->type());
63 RTC_DCHECK_EQ(member->name(), other_member->name()); 63 RTC_DCHECK_EQ(member->name(), other_member->name());
64 if (*member != *other_member) 64 if (*member != *other_member)
65 return false; 65 return false;
66 } 66 }
67 return true; 67 return true;
68 } 68 }
69 69
70 bool RTCStats::operator!=(const RTCStats& other) const { 70 bool RTCStats::operator!=(const RTCStats& other) const {
71 return !(*this == other); 71 return !(*this == other);
72 } 72 }
73 73
74 std::string RTCStats::ToString() const { 74 std::string RTCStats::ToString() const {
75 std::ostringstream oss; 75 std::ostringstream oss;
76 oss << type() << " {\n id: \"" << id_ << "\"\n timestamp: " 76 oss << "{\n \"type\": \"" << type() << "\",\n \"id\": \"" << id_
77 << timestamp_us_ << '\n'; 77 << "\",\n \"timestamp\": " << timestamp_us_;
78 for (const RTCStatsMemberInterface* member : Members()) { 78 for (const RTCStatsMemberInterface* member : Members()) {
79 oss << " " << member->name() << ": ";
80 if (member->is_defined()) { 79 if (member->is_defined()) {
80 oss << ",\n \"" << member->name() << "\": ";
81 if (member->is_string()) 81 if (member->is_string())
82 oss << '"' << member->ValueToString() << "\"\n"; 82 oss << '"' << member->ValueToString() << '"';
83 else 83 else
84 oss << member->ValueToString() << '\n'; 84 oss << member->ValueToString();
85 } else {
86 oss << "undefined\n";
87 } 85 }
88 } 86 }
89 oss << '}'; 87 oss << "\n}";
90 return oss.str(); 88 return oss.str();
91 } 89 }
92 90
93 std::vector<const RTCStatsMemberInterface*> RTCStats::Members() const { 91 std::vector<const RTCStatsMemberInterface*> RTCStats::Members() const {
94 return MembersOfThisObjectAndAncestors(0); 92 return MembersOfThisObjectAndAncestors(0);
95 } 93 }
96 94
97 std::vector<const RTCStatsMemberInterface*> 95 std::vector<const RTCStatsMemberInterface*>
98 RTCStats::MembersOfThisObjectAndAncestors( 96 RTCStats::MembersOfThisObjectAndAncestors(
99 size_t additional_capacity) const { 97 size_t additional_capacity) const {
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
146 std::vector<uint64_t>, kSequenceUint64, true, false, 144 std::vector<uint64_t>, kSequenceUint64, true, false,
147 VectorToString(value_)); 145 VectorToString(value_));
148 WEBRTC_DEFINE_RTCSTATSMEMBER( 146 WEBRTC_DEFINE_RTCSTATSMEMBER(
149 std::vector<double>, kSequenceDouble, true, false, 147 std::vector<double>, kSequenceDouble, true, false,
150 VectorToString(value_)); 148 VectorToString(value_));
151 WEBRTC_DEFINE_RTCSTATSMEMBER( 149 WEBRTC_DEFINE_RTCSTATSMEMBER(
152 std::vector<std::string>, kSequenceString, true, false, 150 std::vector<std::string>, kSequenceString, true, false,
153 VectorOfStringsToString(value_)); 151 VectorOfStringsToString(value_));
154 152
155 } // namespace webrtc 153 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698