OLD | NEW |
(Empty) | |
| 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 |
| 11 #include "webrtc/api/rtcstats.h" |
| 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 << "{ "; |
| 27 bool is_first = true; |
| 28 for (const T& element : vector) { |
| 29 if (!is_first) |
| 30 oss << ", "; |
| 31 oss << rtc::ToString<T>(element); |
| 32 is_first = false; |
| 33 } |
| 34 oss << " }"; |
| 35 return oss.str(); |
| 36 } |
| 37 |
| 38 // Produces "{ \"a\", \"b\", \"c\" }". Works for vectors of both const char* and |
| 39 // std::string element types. |
| 40 template<typename T> |
| 41 std::string VectorOfStringsToString(const std::vector<T>& strings) { |
| 42 if (strings.empty()) |
| 43 return "{}"; |
| 44 std::ostringstream oss; |
| 45 oss << "{ "; |
| 46 bool is_first = true; |
| 47 for (const T& string : strings) { |
| 48 if (!is_first) |
| 49 oss << ", "; |
| 50 oss << '"' << string << '"'; |
| 51 is_first = false; |
| 52 } |
| 53 oss << " }"; |
| 54 return oss.str(); |
| 55 } |
| 56 |
| 57 } // namespace |
| 58 |
| 59 std::string RTCStats::ToString() const { |
| 60 std::ostringstream oss; |
| 61 oss << type_name() << " {\n id: \"" << id_ << "\"\n timestamp: " |
| 62 << timestamp_ << '\n'; |
| 63 for (const RTCStatsMemberInterface* member : Members()) { |
| 64 oss << " " << member->name() << ": "; |
| 65 if (member->is_defined()) { |
| 66 if (member->is_string() && !member->is_sequence()) |
| 67 oss << '"' << member->ValueToString() << "\"\n"; |
| 68 else |
| 69 oss << member->ValueToString() << '\n'; |
| 70 } else { |
| 71 oss << "undefined\n"; |
| 72 } |
| 73 } |
| 74 oss << '}'; |
| 75 return oss.str(); |
| 76 } |
| 77 |
| 78 std::vector<const RTCStatsMemberInterface*> RTCStats::Members() const { |
| 79 return ThisMembers(0); |
| 80 } |
| 81 |
| 82 std::vector<const RTCStatsMemberInterface*> RTCStats::ThisMembers( |
| 83 size_t additional_capacity) const { |
| 84 std::vector<const RTCStatsMemberInterface*> members; |
| 85 members.reserve(additional_capacity); |
| 86 return members; |
| 87 } |
| 88 |
| 89 // int32_t (kInt32) |
| 90 template<> |
| 91 const RTCStatsMemberInterface::Type RTCStatsMember<int32_t>::kType = |
| 92 RTCStatsMemberInterface::kInt32; |
| 93 |
| 94 template<> |
| 95 bool RTCStatsMember<int32_t>::is_sequence() const { return false; } |
| 96 |
| 97 template<> |
| 98 bool RTCStatsMember<int32_t>::is_string() const { return false; } |
| 99 |
| 100 template<> |
| 101 std::string RTCStatsMember<int32_t>::ValueToString() const { |
| 102 RTC_DCHECK(is_defined_); |
| 103 return rtc::ToString<int32_t>(value_); |
| 104 } |
| 105 |
| 106 // uint32_t (kUint32) |
| 107 template<> |
| 108 const RTCStatsMemberInterface::Type RTCStatsMember<uint32_t>::kType = |
| 109 RTCStatsMemberInterface::kUint32; |
| 110 |
| 111 template<> |
| 112 bool RTCStatsMember<uint32_t>::is_sequence() const { return false; } |
| 113 |
| 114 template<> |
| 115 bool RTCStatsMember<uint32_t>::is_string() const { return false; } |
| 116 |
| 117 template<> |
| 118 std::string RTCStatsMember<uint32_t>::ValueToString() const { |
| 119 RTC_DCHECK(is_defined_); |
| 120 return rtc::ToString<uint32_t>(value_); |
| 121 } |
| 122 |
| 123 // int64_t (kInt64) |
| 124 template<> |
| 125 const RTCStatsMemberInterface::Type RTCStatsMember<int64_t>::kType = |
| 126 RTCStatsMemberInterface::kInt64; |
| 127 |
| 128 template<> |
| 129 bool RTCStatsMember<int64_t>::is_sequence() const { return false; } |
| 130 |
| 131 template<> |
| 132 bool RTCStatsMember<int64_t>::is_string() const { return false; } |
| 133 |
| 134 template<> |
| 135 std::string RTCStatsMember<int64_t>::ValueToString() const { |
| 136 RTC_DCHECK(is_defined_); |
| 137 return rtc::ToString<int64_t>(value_); |
| 138 } |
| 139 |
| 140 // uint64_t (kUint64) |
| 141 template<> |
| 142 const RTCStatsMemberInterface::Type RTCStatsMember<uint64_t>::kType = |
| 143 RTCStatsMemberInterface::kUint64; |
| 144 |
| 145 template<> |
| 146 bool RTCStatsMember<uint64_t>::is_sequence() const { return false; } |
| 147 |
| 148 template<> |
| 149 bool RTCStatsMember<uint64_t>::is_string() const { return false; } |
| 150 |
| 151 template<> |
| 152 std::string RTCStatsMember<uint64_t>::ValueToString() const { |
| 153 RTC_DCHECK(is_defined_); |
| 154 return rtc::ToString<uint64_t>(value_); |
| 155 } |
| 156 |
| 157 // double (kDouble) |
| 158 template<> |
| 159 const RTCStatsMemberInterface::Type RTCStatsMember<double>::kType = |
| 160 RTCStatsMemberInterface::kDouble; |
| 161 |
| 162 template<> |
| 163 bool RTCStatsMember<double>::is_sequence() const { return false; } |
| 164 |
| 165 template<> |
| 166 bool RTCStatsMember<double>::is_string() const { return false; } |
| 167 |
| 168 template<> |
| 169 std::string RTCStatsMember<double>::ValueToString() const { |
| 170 RTC_DCHECK(is_defined_); |
| 171 return rtc::ToString<double>(value_); |
| 172 } |
| 173 |
| 174 // const char* (kStaticString) |
| 175 template<> |
| 176 const RTCStatsMemberInterface::Type RTCStatsMember<const char*>::kType = |
| 177 RTCStatsMemberInterface::kStaticString; |
| 178 |
| 179 template<> |
| 180 bool RTCStatsMember<const char*>::is_sequence() const { return false; } |
| 181 |
| 182 template<> |
| 183 bool RTCStatsMember<const char*>::is_string() const { return true; } |
| 184 |
| 185 template<> |
| 186 std::string RTCStatsMember<const char*>::ValueToString() const { |
| 187 RTC_DCHECK(is_defined_); |
| 188 return value_; |
| 189 } |
| 190 |
| 191 // std::string (kString) |
| 192 template<> |
| 193 const RTCStatsMemberInterface::Type RTCStatsMember<std::string>::kType = |
| 194 RTCStatsMemberInterface::kString; |
| 195 |
| 196 template<> |
| 197 bool RTCStatsMember<std::string>::is_sequence() const { return false; } |
| 198 |
| 199 template<> |
| 200 bool RTCStatsMember<std::string>::is_string() const { return true; } |
| 201 |
| 202 template<> |
| 203 std::string RTCStatsMember<std::string>::ValueToString() const { |
| 204 RTC_DCHECK(is_defined_); |
| 205 return value_; |
| 206 } |
| 207 |
| 208 // std::vector<int32_t> (kSequenceInt32) |
| 209 template<> |
| 210 const RTCStatsMemberInterface::Type |
| 211 RTCStatsMember<std::vector<int32_t>>::kType = |
| 212 RTCStatsMemberInterface::kSequenceInt32; |
| 213 |
| 214 template<> |
| 215 bool RTCStatsMember<std::vector<int32_t>>::is_sequence() const { return true; } |
| 216 |
| 217 template<> |
| 218 bool RTCStatsMember<std::vector<int32_t>>::is_string() const { return false; } |
| 219 |
| 220 template<> |
| 221 std::string RTCStatsMember<std::vector<int32_t>>::ValueToString() const { |
| 222 RTC_DCHECK(is_defined_); |
| 223 return VectorToString<int32_t>(value_); |
| 224 } |
| 225 |
| 226 // std::vector<uint32_t> (kSequenceUint32) |
| 227 template<> |
| 228 const RTCStatsMemberInterface::Type |
| 229 RTCStatsMember<std::vector<uint32_t>>::kType = |
| 230 RTCStatsMemberInterface::kSequenceUint32; |
| 231 |
| 232 template<> |
| 233 bool RTCStatsMember<std::vector<uint32_t>>::is_sequence() const { return true; } |
| 234 |
| 235 template<> |
| 236 bool RTCStatsMember<std::vector<uint32_t>>::is_string() const { return false; } |
| 237 |
| 238 template<> |
| 239 std::string RTCStatsMember<std::vector<uint32_t>>::ValueToString() const { |
| 240 RTC_DCHECK(is_defined_); |
| 241 return VectorToString<uint32_t>(value_); |
| 242 } |
| 243 |
| 244 // std::vector<int64_t> (kSequenceInt64) |
| 245 template<> |
| 246 const RTCStatsMemberInterface::Type |
| 247 RTCStatsMember<std::vector<int64_t>>::kType = |
| 248 RTCStatsMemberInterface::kSequenceInt64; |
| 249 |
| 250 template<> |
| 251 bool RTCStatsMember<std::vector<int64_t>>::is_sequence() const { return true; } |
| 252 |
| 253 template<> |
| 254 bool RTCStatsMember<std::vector<int64_t>>::is_string() const { return false; } |
| 255 |
| 256 template<> |
| 257 std::string RTCStatsMember<std::vector<int64_t>>::ValueToString() const { |
| 258 RTC_DCHECK(is_defined_); |
| 259 return VectorToString<int64_t>(value_); |
| 260 } |
| 261 |
| 262 // std::vector<uint64_t> (kSequenceUint64) |
| 263 template<> |
| 264 const RTCStatsMemberInterface::Type |
| 265 RTCStatsMember<std::vector<uint64_t>>::kType = |
| 266 RTCStatsMemberInterface::kSequenceUint64; |
| 267 |
| 268 template<> |
| 269 bool RTCStatsMember<std::vector<uint64_t>>::is_sequence() const { return true; } |
| 270 |
| 271 template<> |
| 272 bool RTCStatsMember<std::vector<uint64_t>>::is_string() const { return false; } |
| 273 |
| 274 template<> |
| 275 std::string RTCStatsMember<std::vector<uint64_t>>::ValueToString() const { |
| 276 RTC_DCHECK(is_defined_); |
| 277 return VectorToString<uint64_t>(value_); |
| 278 } |
| 279 |
| 280 // std::vector<double> (kSequenceDouble) |
| 281 template<> |
| 282 const RTCStatsMemberInterface::Type |
| 283 RTCStatsMember<std::vector<double>>::kType = |
| 284 RTCStatsMemberInterface::kSequenceDouble; |
| 285 |
| 286 template<> |
| 287 bool RTCStatsMember<std::vector<double>>::is_sequence() const { return true; } |
| 288 |
| 289 template<> |
| 290 bool RTCStatsMember<std::vector<double>>::is_string() const { return false; } |
| 291 |
| 292 template<> |
| 293 std::string RTCStatsMember<std::vector<double>>::ValueToString() const { |
| 294 RTC_DCHECK(is_defined_); |
| 295 return VectorToString<double>(value_); |
| 296 } |
| 297 |
| 298 // std::vector<const char*> (kSequenceStaticString) |
| 299 template<> |
| 300 const RTCStatsMemberInterface::Type |
| 301 RTCStatsMember<std::vector<const char*>>::kType = |
| 302 RTCStatsMemberInterface::kSequenceStaticString; |
| 303 |
| 304 template<> |
| 305 bool RTCStatsMember<std::vector<const char*>>::is_sequence() const { |
| 306 return true; |
| 307 } |
| 308 |
| 309 template<> |
| 310 bool RTCStatsMember<std::vector<const char*>>::is_string() const { |
| 311 return true; |
| 312 } |
| 313 |
| 314 template<> |
| 315 std::string RTCStatsMember<std::vector<const char*>>::ValueToString() const { |
| 316 RTC_DCHECK(is_defined_); |
| 317 return VectorOfStringsToString<const char*>(value_); |
| 318 } |
| 319 |
| 320 // std::vector<std::string> (kSequenceString) |
| 321 template<> |
| 322 const RTCStatsMemberInterface::Type |
| 323 RTCStatsMember<std::vector<std::string>>::kType = |
| 324 RTCStatsMemberInterface::kSequenceString; |
| 325 |
| 326 template<> |
| 327 bool RTCStatsMember<std::vector<std::string>>::is_sequence() const { |
| 328 return true; |
| 329 } |
| 330 |
| 331 template<> |
| 332 bool RTCStatsMember<std::vector<std::string>>::is_string() const { |
| 333 return true; |
| 334 } |
| 335 |
| 336 template<> |
| 337 std::string RTCStatsMember<std::vector<std::string>>::ValueToString() const { |
| 338 RTC_DCHECK(is_defined_); |
| 339 return VectorOfStringsToString<std::string>(value_); |
| 340 } |
| 341 |
| 342 } // namespace webrtc |
OLD | NEW |