5 #include "src/tracing/traced-value.h" 7 #include "src/base/platform/platform.h" 8 #include "src/conversions.h" 15 #define DCHECK_CURRENT_CONTAINER_IS(x) DCHECK_EQ(x, nesting_stack_.back()) 16 #define DCHECK_CONTAINER_STACK_DEPTH_EQ(x) DCHECK_EQ(x, nesting_stack_.size()) 18 const bool kStackTypeDict =
false;
19 const bool kStackTypeArray =
true;
20 #define DEBUG_PUSH_CONTAINER(x) nesting_stack_.push_back(x) 21 #define DEBUG_POP_CONTAINER() nesting_stack_.pop_back() 23 #define DEBUG_PUSH_CONTAINER(x) ((void)0) 24 #define DEBUG_POP_CONTAINER() ((void)0) 27 void EscapeAndAppendString(
const char* value, std::string* result) {
30 unsigned char c = *value++;
54 if (c <
'\x20' || c ==
'\x7F') {
55 char number_buffer[8];
56 base::OS::SNPrintF(number_buffer, arraysize(number_buffer),
"\\u%04X",
57 static_cast<unsigned>(c));
58 *result += number_buffer;
69 std::unique_ptr<TracedValue> TracedValue::Create() {
70 return std::unique_ptr<TracedValue>(
new TracedValue());
73 TracedValue::TracedValue() : first_item_(true) {
74 DEBUG_PUSH_CONTAINER(kStackTypeDict);
77 TracedValue::~TracedValue() {
78 DCHECK_CURRENT_CONTAINER_IS(kStackTypeDict);
79 DEBUG_POP_CONTAINER();
80 DCHECK_CONTAINER_STACK_DEPTH_EQ(0u);
83 void TracedValue::SetInteger(
const char* name,
int value) {
84 DCHECK_CURRENT_CONTAINER_IS(kStackTypeDict);
86 data_ += std::to_string(value);
89 void TracedValue::SetDouble(
const char* name,
double value) {
90 DCHECK_CURRENT_CONTAINER_IS(kStackTypeDict);
93 data_ += DoubleToCString(value, buffer);
96 void TracedValue::SetBoolean(
const char* name,
bool value) {
97 DCHECK_CURRENT_CONTAINER_IS(kStackTypeDict);
99 data_ += value ?
"true" :
"false";
102 void TracedValue::SetString(
const char* name,
const char* value) {
103 DCHECK_CURRENT_CONTAINER_IS(kStackTypeDict);
105 EscapeAndAppendString(value, &data_);
108 void TracedValue::BeginDictionary(
const char* name) {
109 DCHECK_CURRENT_CONTAINER_IS(kStackTypeDict);
110 DEBUG_PUSH_CONTAINER(kStackTypeDict);
116 void TracedValue::BeginArray(
const char* name) {
117 DCHECK_CURRENT_CONTAINER_IS(kStackTypeDict);
118 DEBUG_PUSH_CONTAINER(kStackTypeArray);
124 void TracedValue::AppendInteger(
int value) {
125 DCHECK_CURRENT_CONTAINER_IS(kStackTypeArray);
127 data_ += std::to_string(value);
130 void TracedValue::AppendDouble(
double value) {
131 DCHECK_CURRENT_CONTAINER_IS(kStackTypeArray);
134 data_ += DoubleToCString(value, buffer);
137 void TracedValue::AppendBoolean(
bool value) {
138 DCHECK_CURRENT_CONTAINER_IS(kStackTypeArray);
140 data_ += value ?
"true" :
"false";
143 void TracedValue::AppendString(
const char* value) {
144 DCHECK_CURRENT_CONTAINER_IS(kStackTypeArray);
146 EscapeAndAppendString(value, &data_);
149 void TracedValue::BeginDictionary() {
150 DCHECK_CURRENT_CONTAINER_IS(kStackTypeArray);
151 DEBUG_PUSH_CONTAINER(kStackTypeDict);
157 void TracedValue::BeginArray() {
158 DCHECK_CURRENT_CONTAINER_IS(kStackTypeArray);
159 DEBUG_PUSH_CONTAINER(kStackTypeArray);
165 void TracedValue::EndDictionary() {
166 DCHECK_CURRENT_CONTAINER_IS(kStackTypeDict);
167 DEBUG_POP_CONTAINER();
172 void TracedValue::EndArray() {
173 DCHECK_CURRENT_CONTAINER_IS(kStackTypeArray);
174 DEBUG_POP_CONTAINER();
179 void TracedValue::WriteComma() {
187 void TracedValue::WriteName(
const char* name) {
void AppendAsTraceFormat(std::string *out) const override