5 #include "src/libplatform/tracing/trace-writer.h" 9 #include "base/trace_event/common/trace_event_common.h" 10 #include "include/v8-platform.h" 11 #include "src/base/platform/platform.h" 19 V8_INLINE
static void WriteJSONStringToStream(
const char* str,
20 std::ostream& stream) {
21 size_t len = strlen(str);
23 for (
size_t i = 0;
i < len; ++
i) {
58 void JSONTraceWriter::AppendArgValue(uint8_t type,
59 TraceObject::ArgValue value) {
61 case TRACE_VALUE_TYPE_BOOL:
62 stream_ << (value.as_bool ?
"true" :
"false");
64 case TRACE_VALUE_TYPE_UINT:
65 stream_ << value.as_uint;
67 case TRACE_VALUE_TYPE_INT:
68 stream_ << value.as_int;
70 case TRACE_VALUE_TYPE_DOUBLE: {
72 double val = value.as_double;
73 if (std::isfinite(val)) {
74 std::ostringstream convert_stream;
75 convert_stream << val;
76 real = convert_stream.str();
80 if (real.find(
'.') == std::string::npos &&
81 real.find(
'e') == std::string::npos &&
82 real.find(
'E') == std::string::npos) {
85 }
else if (std::isnan(val)) {
90 real =
"\"-Infinity\"";
92 real =
"\"Infinity\"";
97 case TRACE_VALUE_TYPE_POINTER:
100 stream_ <<
"\"" << value.as_pointer <<
"\"";
102 case TRACE_VALUE_TYPE_STRING:
103 case TRACE_VALUE_TYPE_COPY_STRING:
104 if (value.as_string ==
nullptr) {
105 stream_ <<
"\"nullptr\"";
107 WriteJSONStringToStream(value.as_string, stream_);
116 void JSONTraceWriter::AppendArgValue(ConvertableToTraceFormat* value) {
117 std::string arg_stringified;
118 value->AppendAsTraceFormat(&arg_stringified);
119 stream_ << arg_stringified;
122 JSONTraceWriter::JSONTraceWriter(std::ostream& stream)
123 : JSONTraceWriter(stream,
"traceEvents") {}
125 JSONTraceWriter::JSONTraceWriter(std::ostream& stream,
const std::string& tag)
127 stream_ <<
"{\"" << tag <<
"\":[";
130 JSONTraceWriter::~JSONTraceWriter() { stream_ <<
"]}"; }
132 void JSONTraceWriter::AppendTraceEvent(TraceObject* trace_event) {
133 if (append_comma_) stream_ <<
",";
134 append_comma_ =
true;
135 stream_ <<
"{\"pid\":" << trace_event->pid()
136 <<
",\"tid\":" << trace_event->tid()
137 <<
",\"ts\":" << trace_event->ts()
138 <<
",\"tts\":" << trace_event->tts() <<
",\"ph\":\"" 139 << trace_event->phase() <<
"\",\"cat\":\"" 140 << TracingController::GetCategoryGroupName(
141 trace_event->category_enabled_flag())
142 <<
"\",\"name\":\"" << trace_event->name()
143 <<
"\",\"dur\":" << trace_event->duration()
144 <<
",\"tdur\":" << trace_event->cpu_duration();
145 if (trace_event->flags() & TRACE_EVENT_FLAG_HAS_ID) {
146 if (trace_event->scope() !=
nullptr) {
147 stream_ <<
",\"scope\":\"" << trace_event->scope() <<
"\"";
150 stream_ <<
",\"id\":\"0x" << std::hex << trace_event->id() <<
"\"" 153 stream_ <<
",\"args\":{";
154 const char** arg_names = trace_event->arg_names();
155 const uint8_t* arg_types = trace_event->arg_types();
156 TraceObject::ArgValue* arg_values = trace_event->arg_values();
157 std::unique_ptr<v8::ConvertableToTraceFormat>* arg_convertables =
158 trace_event->arg_convertables();
159 for (
int i = 0;
i < trace_event->num_args(); ++
i) {
160 if (
i > 0) stream_ <<
",";
161 stream_ <<
"\"" << arg_names[
i] <<
"\":";
162 if (arg_types[
i] == TRACE_VALUE_TYPE_CONVERTABLE) {
163 AppendArgValue(arg_convertables[
i].
get());
165 AppendArgValue(arg_types[
i], arg_values[
i]);
172 void JSONTraceWriter::Flush() {}
174 TraceWriter* TraceWriter::CreateJSONTraceWriter(std::ostream& stream) {
175 return new JSONTraceWriter(stream);
178 TraceWriter* TraceWriter::CreateJSONTraceWriter(std::ostream& stream,
179 const std::string& tag) {
180 return new JSONTraceWriter(stream, tag);