hbos | 615d301 | 2016-08-24 01:33:13 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 11 | #include "webrtc/api/stats/rtcstats.h" |
hbos | 615d301 | 2016-08-24 01:33:13 -0700 | [diff] [blame] | 12 | |
| 13 | #include "webrtc/base/stringencode.h" |
| 14 | |
| 15 | namespace webrtc { |
| 16 | |
| 17 | namespace { |
| 18 | |
| 19 | // Produces "{ a, b, c }". Works for non-vector |RTCStatsMemberInterface::Type| |
| 20 | // types. |
| 21 | template<typename T> |
| 22 | std::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. |
| 36 | template<typename T> |
| 37 | std::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 | |
| 51 | std::string RTCStats::ToString() const { |
| 52 | std::ostringstream oss; |
| 53 | oss << type() << " {\n id: \"" << id_ << "\"\n timestamp: " |
hbos | 0e6758d | 2016-08-31 07:57:36 -0700 | [diff] [blame] | 54 | << timestamp_us_ << '\n'; |
hbos | 615d301 | 2016-08-24 01:33:13 -0700 | [diff] [blame] | 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 | |
| 70 | std::vector<const RTCStatsMemberInterface*> RTCStats::Members() const { |
| 71 | return MembersOfThisObjectAndAncestors(0); |
| 72 | } |
| 73 | |
| 74 | std::vector<const RTCStatsMemberInterface*> |
| 75 | RTCStats::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 | |
hbos | b20f387 | 2016-10-04 14:37:11 -0700 | [diff] [blame] | 96 | WEBRTC_DEFINE_RTCSTATSMEMBER(bool, kBool, false, false, |
| 97 | rtc::ToString(value_)); |
hbos | 615d301 | 2016-08-24 01:33:13 -0700 | [diff] [blame] | 98 | WEBRTC_DEFINE_RTCSTATSMEMBER(int32_t, kInt32, false, false, |
| 99 | rtc::ToString(value_)); |
| 100 | WEBRTC_DEFINE_RTCSTATSMEMBER(uint32_t, kUint32, false, false, |
| 101 | rtc::ToString(value_)); |
| 102 | WEBRTC_DEFINE_RTCSTATSMEMBER(int64_t, kInt64, false, false, |
| 103 | rtc::ToString(value_)); |
| 104 | WEBRTC_DEFINE_RTCSTATSMEMBER(uint64_t, kUint64, false, false, |
| 105 | rtc::ToString(value_)); |
| 106 | WEBRTC_DEFINE_RTCSTATSMEMBER(double, kDouble, false, false, |
| 107 | rtc::ToString(value_)); |
hbos | 615d301 | 2016-08-24 01:33:13 -0700 | [diff] [blame] | 108 | WEBRTC_DEFINE_RTCSTATSMEMBER(std::string, kString, false, true, |
| 109 | value_); |
| 110 | WEBRTC_DEFINE_RTCSTATSMEMBER( |
hbos | b20f387 | 2016-10-04 14:37:11 -0700 | [diff] [blame] | 111 | std::vector<bool>, kSequenceBool, true, false, |
| 112 | VectorToString(value_)); |
| 113 | WEBRTC_DEFINE_RTCSTATSMEMBER( |
hbos | 615d301 | 2016-08-24 01:33:13 -0700 | [diff] [blame] | 114 | std::vector<int32_t>, kSequenceInt32, true, false, |
| 115 | VectorToString(value_)); |
| 116 | WEBRTC_DEFINE_RTCSTATSMEMBER( |
| 117 | std::vector<uint32_t>, kSequenceUint32, true, false, |
| 118 | VectorToString(value_)); |
| 119 | WEBRTC_DEFINE_RTCSTATSMEMBER( |
| 120 | std::vector<int64_t>, kSequenceInt64, true, false, |
| 121 | VectorToString(value_)); |
| 122 | WEBRTC_DEFINE_RTCSTATSMEMBER( |
| 123 | std::vector<uint64_t>, kSequenceUint64, true, false, |
| 124 | VectorToString(value_)); |
| 125 | WEBRTC_DEFINE_RTCSTATSMEMBER( |
| 126 | std::vector<double>, kSequenceDouble, true, false, |
| 127 | VectorToString(value_)); |
| 128 | WEBRTC_DEFINE_RTCSTATSMEMBER( |
hbos | 615d301 | 2016-08-24 01:33:13 -0700 | [diff] [blame] | 129 | std::vector<std::string>, kSequenceString, true, false, |
| 130 | VectorOfStringsToString(value_)); |
| 131 | |
| 132 | } // namespace webrtc |