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

Unified Diff: webrtc/stats/rtcstats.cc

Issue 2983243002: Make RTCStatsReport::ToString() return JSON-parseable string. (Closed)
Patch Set: Display Int64 values as doubles. Created 3 years, 5 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 side-by-side diff with in-line comments
Download patch
Index: webrtc/stats/rtcstats.cc
diff --git a/webrtc/stats/rtcstats.cc b/webrtc/stats/rtcstats.cc
index 5d0a937e6f31c12abadc55fa95be9adfb4fac6da..53aa8a0b7a574a7604ebe9e45bd7ab3eabd2184a 100644
--- a/webrtc/stats/rtcstats.cc
+++ b/webrtc/stats/rtcstats.cc
@@ -10,6 +10,8 @@
#include "webrtc/api/stats/rtcstats.h"
+#include <cfloat>
+#include <iomanip>
#include <sstream>
#include "webrtc/rtc_base/stringencode.h"
@@ -18,33 +20,53 @@ namespace webrtc {
namespace {
-// Produces "{ a, b, c }". Works for non-vector |RTCStatsMemberInterface::Type|
+// Produces "[ a, b, c ]". Works for non-vector |RTCStatsMemberInterface::Type|
// types.
template<typename T>
std::string VectorToString(const std::vector<T>& vector) {
if (vector.empty())
- return "{}";
+ return "[]";
std::ostringstream oss;
- oss << "{ " << rtc::ToString<T>(vector[0]);
+ oss << "[ " << rtc::ToString<T>(vector[0]);
for (size_t i = 1; i < vector.size(); ++i) {
oss << ", " << rtc::ToString<T>(vector[i]);
}
- oss << " }";
+ oss << " ]";
return oss.str();
}
-// Produces "{ \"a\", \"b\", \"c\" }". Works for vectors of both const char* and
+// Produces "[ \"a\", \"b\", \"c\" ]". Works for vectors of both const char* and
// std::string element types.
template<typename T>
std::string VectorOfStringsToString(const std::vector<T>& strings) {
if (strings.empty())
- return "{}";
+ return "[]";
std::ostringstream oss;
- oss << "{ \"" << rtc::ToString<T>(strings[0]) << '\"';
+ oss << "[ \"" << rtc::ToString<T>(strings[0]) << '\"';
for (size_t i = 1; i < strings.size(); ++i) {
oss << ", \"" << rtc::ToString<T>(strings[i]) << '\"';
}
- oss << " }";
+ oss << " ]";
+ return oss.str();
+}
+
+template <typename T>
+std::string ToStringAsDouble(const T value) {
+ std::ostringstream oss;
+ oss << std::setprecision(DBL_DIG + 1) << static_cast<double>(value);
hbos 2017/07/27 16:27:40 nit: Hm, maybe prefer std::numeric_limits<double>:
+ return oss.str();
+}
+
+template <typename T>
+std::string VectorToStringAsDouble(const std::vector<T>& vector) {
+ if (vector.empty())
+ return "[]";
+ std::ostringstream oss;
+ oss << "[ " << ToStringAsDouble<T>(vector[0]);
+ for (size_t i = 1; i < vector.size(); ++i) {
+ oss << ", " << ToStringAsDouble<T>(vector[i]);
+ }
+ oss << " ]";
return oss.str();
}
@@ -71,22 +93,20 @@ bool RTCStats::operator!=(const RTCStats& other) const {
return !(*this == other);
}
-std::string RTCStats::ToString() const {
+std::string RTCStats::ToJson() const {
std::ostringstream oss;
- oss << type() << " {\n id: \"" << id_ << "\"\n timestamp: "
- << timestamp_us_ << '\n';
+ oss << "{\n \"type\": \"" << type() << "\",\n \"id\": \"" << id_
+ << "\",\n \"timestamp\": " << timestamp_us_;
for (const RTCStatsMemberInterface* member : Members()) {
- oss << " " << member->name() << ": ";
if (member->is_defined()) {
+ oss << ",\n \"" << member->name() << "\": ";
if (member->is_string())
- oss << '"' << member->ValueToString() << "\"\n";
+ oss << '"' << member->ValueToJson() << '"';
else
- oss << member->ValueToString() << '\n';
- } else {
- oss << "undefined\n";
+ oss << member->ValueToJson();
}
}
- oss << '}';
+ oss << "\n}";
return oss.str();
}
@@ -102,54 +122,107 @@ RTCStats::MembersOfThisObjectAndAncestors(
return members;
}
-#define WEBRTC_DEFINE_RTCSTATSMEMBER(T, type, is_seq, is_str, to_str) \
- template<> \
+#define WEBRTC_DEFINE_RTCSTATSMEMBER(T, type, is_seq, is_str, to_str, to_json) \
+ template <> \
const RTCStatsMemberInterface::Type RTCStatsMember<T>::kType = \
RTCStatsMemberInterface::type; \
- template<> \
- bool RTCStatsMember<T>::is_sequence() const { return is_seq; } \
- template<> \
- bool RTCStatsMember<T>::is_string() const { return is_str; } \
- template<> \
+ template <> \
+ bool RTCStatsMember<T>::is_sequence() const { \
+ return is_seq; \
+ } \
+ template <> \
+ bool RTCStatsMember<T>::is_string() const { \
+ return is_str; \
+ } \
+ template <> \
std::string RTCStatsMember<T>::ValueToString() const { \
RTC_DCHECK(is_defined_); \
return to_str; \
+ } \
+ template <> \
+ std::string RTCStatsMember<T>::ValueToJson() const { \
+ RTC_DCHECK(is_defined_); \
+ return to_json; \
}
-WEBRTC_DEFINE_RTCSTATSMEMBER(bool, kBool, false, false,
- rtc::ToString(value_));
-WEBRTC_DEFINE_RTCSTATSMEMBER(int32_t, kInt32, false, false,
- rtc::ToString(value_));
-WEBRTC_DEFINE_RTCSTATSMEMBER(uint32_t, kUint32, false, false,
- rtc::ToString(value_));
-WEBRTC_DEFINE_RTCSTATSMEMBER(int64_t, kInt64, false, false,
- rtc::ToString(value_));
-WEBRTC_DEFINE_RTCSTATSMEMBER(uint64_t, kUint64, false, false,
- rtc::ToString(value_));
-WEBRTC_DEFINE_RTCSTATSMEMBER(double, kDouble, false, false,
- rtc::ToString(value_));
-WEBRTC_DEFINE_RTCSTATSMEMBER(std::string, kString, false, true,
- value_);
-WEBRTC_DEFINE_RTCSTATSMEMBER(
- std::vector<bool>, kSequenceBool, true, false,
- VectorToString(value_));
-WEBRTC_DEFINE_RTCSTATSMEMBER(
- std::vector<int32_t>, kSequenceInt32, true, false,
- VectorToString(value_));
-WEBRTC_DEFINE_RTCSTATSMEMBER(
- std::vector<uint32_t>, kSequenceUint32, true, false,
- VectorToString(value_));
-WEBRTC_DEFINE_RTCSTATSMEMBER(
- std::vector<int64_t>, kSequenceInt64, true, false,
- VectorToString(value_));
-WEBRTC_DEFINE_RTCSTATSMEMBER(
- std::vector<uint64_t>, kSequenceUint64, true, false,
- VectorToString(value_));
-WEBRTC_DEFINE_RTCSTATSMEMBER(
- std::vector<double>, kSequenceDouble, true, false,
- VectorToString(value_));
-WEBRTC_DEFINE_RTCSTATSMEMBER(
- std::vector<std::string>, kSequenceString, true, false,
- VectorOfStringsToString(value_));
+#define WEBRTC_DEFINE_RTCSTATSMEMBER_DEFAULT_JSON(T, type, is_seq, is_str, \
ehmaldonado_webrtc 2017/07/27 08:56:29 I ran "git cl format" and got this. Should I rever
hbos 2017/07/27 16:27:40 Let's keep it, it looks readable and people might
+ to_str) \
+ WEBRTC_DEFINE_RTCSTATSMEMBER(T, type, is_seq, is_str, to_str, to_str)
+
+WEBRTC_DEFINE_RTCSTATSMEMBER_DEFAULT_JSON(bool,
+ kBool,
+ false,
+ false,
+ rtc::ToString(value_));
+WEBRTC_DEFINE_RTCSTATSMEMBER_DEFAULT_JSON(int32_t,
+ kInt32,
+ false,
+ false,
+ rtc::ToString(value_));
+WEBRTC_DEFINE_RTCSTATSMEMBER_DEFAULT_JSON(uint32_t,
+ kUint32,
+ false,
+ false,
+ rtc::ToString(value_));
+WEBRTC_DEFINE_RTCSTATSMEMBER_DEFAULT_JSON(double,
+ kDouble,
+ false,
+ false,
+ rtc::ToString(value_));
+WEBRTC_DEFINE_RTCSTATSMEMBER_DEFAULT_JSON(std::string,
+ kString,
+ false,
+ true,
+ value_);
+WEBRTC_DEFINE_RTCSTATSMEMBER_DEFAULT_JSON(std::vector<bool>,
+ kSequenceBool,
+ true,
+ false,
+ VectorToString(value_));
+WEBRTC_DEFINE_RTCSTATSMEMBER_DEFAULT_JSON(std::vector<int32_t>,
+ kSequenceInt32,
+ true,
+ false,
+ VectorToString(value_));
+WEBRTC_DEFINE_RTCSTATSMEMBER_DEFAULT_JSON(std::vector<uint32_t>,
+ kSequenceUint32,
+ true,
+ false,
+ VectorToString(value_));
+WEBRTC_DEFINE_RTCSTATSMEMBER_DEFAULT_JSON(std::vector<double>,
+ kSequenceDouble,
+ true,
+ false,
+ VectorToString(value_));
+WEBRTC_DEFINE_RTCSTATSMEMBER_DEFAULT_JSON(std::vector<std::string>,
+ kSequenceString,
+ true,
+ false,
+ VectorOfStringsToString(value_));
+
+WEBRTC_DEFINE_RTCSTATSMEMBER(int64_t,
+ kInt64,
+ false,
+ false,
+ rtc::ToString(value_),
+ ToStringAsDouble(value_));
+WEBRTC_DEFINE_RTCSTATSMEMBER(uint64_t,
+ kUint64,
+ false,
+ false,
+ rtc::ToString(value_),
+ ToStringAsDouble(value_));
+WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<int64_t>,
+ kSequenceInt64,
+ true,
+ false,
+ VectorToString(value_),
+ VectorToStringAsDouble(value_));
+WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<uint64_t>,
+ kSequenceUint64,
+ true,
+ false,
+ VectorToString(value_),
+ VectorToStringAsDouble(value_));
hbos 2017/07/27 16:27:40 Can you change back the order (..., kInt32, kUint3
} // namespace webrtc

Powered by Google App Engine
This is Rietveld 408576698