OLD | NEW |
1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/tracing/traced-value.h" | 5 #include "src/tracing/traced-value.h" |
6 | 6 |
7 #include "src/base/platform/platform.h" | 7 #include "src/base/platform/platform.h" |
8 | 8 |
9 namespace v8 { | 9 namespace v8 { |
10 namespace tracing { | 10 namespace tracing { |
11 | 11 |
12 namespace { | 12 namespace { |
13 | 13 |
14 #define DCHECK_CURRENT_CONTAINER_IS(x) DCHECK_EQ(x, nesting_stack_.back()) | 14 #define DCHECK_CURRENT_CONTAINER_IS(x) DCHECK_EQ(x, nesting_stack_.back()) |
15 #define DCHECK_CONTAINER_STACK_DEPTH_EQ(x) DCHECK_EQ(x, nesting_stack_.size()) | 15 #define DCHECK_CONTAINER_STACK_DEPTH_EQ(x) DCHECK_EQ(x, nesting_stack_.size()) |
16 #ifdef DEBUG | 16 #ifdef DEBUG |
17 const bool kStackTypeDict = false; | 17 const bool kStackTypeDict = false; |
18 const bool kStackTypeArray = true; | 18 const bool kStackTypeArray = true; |
19 #define DEBUG_PUSH_CONTAINER(x) nesting_stack_.push_back(x) | 19 #define DEBUG_PUSH_CONTAINER(x) nesting_stack_.push_back(x) |
20 #define DEBUG_POP_CONTAINER() nesting_stack_.pop_back() | 20 #define DEBUG_POP_CONTAINER() nesting_stack_.pop_back() |
21 #else | 21 #else |
22 #define DEBUG_PUSH_CONTAINER(x) ((void)0) | 22 #define DEBUG_PUSH_CONTAINER(x) ((void)0) |
23 #define DEBUG_POP_CONTAINER() ((void)0) | 23 #define DEBUG_POP_CONTAINER() ((void)0) |
24 #endif | 24 #endif |
25 | 25 |
26 std::string EscapeString(const std::string& value) { | 26 void EscapeAndAppendString(const char* value, std::string* result) { |
27 std::string result; | 27 *result += '"'; |
28 result.reserve(value.length() + 2); | |
29 result += '"'; | |
30 size_t length = value.length(); | |
31 char number_buffer[10]; | 28 char number_buffer[10]; |
32 for (size_t src = 0; src < length; ++src) { | 29 while (*value) { |
33 char c = value[src]; | 30 char c = *value++; |
34 switch (c) { | 31 switch (c) { |
35 case '\t': | 32 case '\t': |
36 result += "\\t"; | 33 *result += "\\t"; |
37 break; | 34 break; |
38 case '\n': | 35 case '\n': |
39 result += "\\n"; | 36 *result += "\\n"; |
40 break; | 37 break; |
41 case '\"': | 38 case '\"': |
42 result += "\\\""; | 39 *result += "\\\""; |
43 break; | 40 break; |
44 case '\\': | 41 case '\\': |
45 result += "\\\\"; | 42 *result += "\\\\"; |
46 break; | 43 break; |
47 default: | 44 default: |
48 if (c < '\040') { | 45 if (c < '\040') { |
49 base::OS::SNPrintF( | 46 base::OS::SNPrintF( |
50 number_buffer, arraysize(number_buffer), "\\u%04X", | 47 number_buffer, arraysize(number_buffer), "\\u%04X", |
51 static_cast<unsigned>(static_cast<unsigned char>(c))); | 48 static_cast<unsigned>(static_cast<unsigned char>(c))); |
52 result += number_buffer; | 49 *result += number_buffer; |
53 } else { | 50 } else { |
54 result += c; | 51 *result += c; |
55 } | 52 } |
56 } | 53 } |
57 } | 54 } |
58 result += '"'; | 55 *result += '"'; |
59 return result; | |
60 } | 56 } |
61 | 57 |
62 } // namespace | 58 } // namespace |
63 | 59 |
64 std::unique_ptr<TracedValue> TracedValue::Create() { | 60 std::unique_ptr<TracedValue> TracedValue::Create() { |
65 return std::unique_ptr<TracedValue>(new TracedValue()); | 61 return std::unique_ptr<TracedValue>(new TracedValue()); |
66 } | 62 } |
67 | 63 |
68 TracedValue::TracedValue() : first_item_(true) { | 64 TracedValue::TracedValue() : first_item_(true) { |
69 DEBUG_PUSH_CONTAINER(kStackTypeDict); | 65 DEBUG_PUSH_CONTAINER(kStackTypeDict); |
(...skipping 16 matching lines...) Expand all Loading... |
86 WriteName(name); | 82 WriteName(name); |
87 data_ += std::to_string(value); | 83 data_ += std::to_string(value); |
88 } | 84 } |
89 | 85 |
90 void TracedValue::SetBoolean(const char* name, bool value) { | 86 void TracedValue::SetBoolean(const char* name, bool value) { |
91 DCHECK_CURRENT_CONTAINER_IS(kStackTypeDict); | 87 DCHECK_CURRENT_CONTAINER_IS(kStackTypeDict); |
92 WriteName(name); | 88 WriteName(name); |
93 data_ += value ? "true" : "false"; | 89 data_ += value ? "true" : "false"; |
94 } | 90 } |
95 | 91 |
96 void TracedValue::SetString(const char* name, const std::string& value) { | 92 void TracedValue::SetString(const char* name, const char* value) { |
97 DCHECK_CURRENT_CONTAINER_IS(kStackTypeDict); | 93 DCHECK_CURRENT_CONTAINER_IS(kStackTypeDict); |
98 WriteName(name); | 94 WriteName(name); |
99 data_ += EscapeString(value); | 95 EscapeAndAppendString(value, &data_); |
100 } | 96 } |
101 | 97 |
102 void TracedValue::BeginDictionary(const char* name) { | 98 void TracedValue::BeginDictionary(const char* name) { |
103 DCHECK_CURRENT_CONTAINER_IS(kStackTypeDict); | 99 DCHECK_CURRENT_CONTAINER_IS(kStackTypeDict); |
104 DEBUG_PUSH_CONTAINER(kStackTypeDict); | 100 DEBUG_PUSH_CONTAINER(kStackTypeDict); |
105 WriteName(name); | 101 WriteName(name); |
106 data_ += '{'; | 102 data_ += '{'; |
107 first_item_ = true; | 103 first_item_ = true; |
108 } | 104 } |
109 | 105 |
(...skipping 16 matching lines...) Expand all Loading... |
126 WriteComma(); | 122 WriteComma(); |
127 data_ += std::to_string(value); | 123 data_ += std::to_string(value); |
128 } | 124 } |
129 | 125 |
130 void TracedValue::AppendBoolean(bool value) { | 126 void TracedValue::AppendBoolean(bool value) { |
131 DCHECK_CURRENT_CONTAINER_IS(kStackTypeArray); | 127 DCHECK_CURRENT_CONTAINER_IS(kStackTypeArray); |
132 WriteComma(); | 128 WriteComma(); |
133 data_ += value ? "true" : "false"; | 129 data_ += value ? "true" : "false"; |
134 } | 130 } |
135 | 131 |
136 void TracedValue::AppendString(const std::string& value) { | 132 void TracedValue::AppendString(const char* value) { |
137 DCHECK_CURRENT_CONTAINER_IS(kStackTypeArray); | 133 DCHECK_CURRENT_CONTAINER_IS(kStackTypeArray); |
138 WriteComma(); | 134 WriteComma(); |
139 data_ += EscapeString(value); | 135 EscapeAndAppendString(value, &data_); |
140 } | 136 } |
141 | 137 |
142 void TracedValue::BeginDictionary() { | 138 void TracedValue::BeginDictionary() { |
143 DCHECK_CURRENT_CONTAINER_IS(kStackTypeArray); | 139 DCHECK_CURRENT_CONTAINER_IS(kStackTypeArray); |
144 DEBUG_PUSH_CONTAINER(kStackTypeDict); | 140 DEBUG_PUSH_CONTAINER(kStackTypeDict); |
145 WriteComma(); | 141 WriteComma(); |
146 data_ += '{'; | 142 data_ += '{'; |
147 first_item_ = true; | 143 first_item_ = true; |
148 } | 144 } |
149 | 145 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 } | 181 } |
186 | 182 |
187 void TracedValue::AppendAsTraceFormat(std::string* out) const { | 183 void TracedValue::AppendAsTraceFormat(std::string* out) const { |
188 *out += '{'; | 184 *out += '{'; |
189 *out += data_; | 185 *out += data_; |
190 *out += '}'; | 186 *out += '}'; |
191 } | 187 } |
192 | 188 |
193 } // namespace tracing | 189 } // namespace tracing |
194 } // namespace v8 | 190 } // namespace v8 |
OLD | NEW |