| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium 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 "base/debug/trace_event.h" | 5 #include "base/debug/trace_event.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #if defined(OS_WIN) | 9 #if defined(OS_WIN) |
| 10 #include "base/debug/trace_event_win.h" | 10 #include "base/debug/trace_event_win.h" |
| 11 #endif | 11 #endif |
| 12 #include "base/bind.h" |
| 12 #include "base/format_macros.h" | 13 #include "base/format_macros.h" |
| 13 #include "base/memory/ref_counted_memory.h" | 14 #include "base/memory/ref_counted_memory.h" |
| 14 #include "base/process_util.h" | 15 #include "base/process_util.h" |
| 15 #include "base/stringprintf.h" | 16 #include "base/stringprintf.h" |
| 16 #include "base/threading/thread_local.h" | 17 #include "base/threading/thread_local.h" |
| 17 #include "base/utf_string_conversions.h" | 18 #include "base/utf_string_conversions.h" |
| 18 #include "base/stl_util.h" | 19 #include "base/stl_util.h" |
| 19 #include "base/time.h" | 20 #include "base/time.h" |
| 20 | 21 |
| 21 #define USE_UNRELIABLE_NOW | 22 #define USE_UNRELIABLE_NOW |
| (...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 } | 218 } |
| 218 } | 219 } |
| 219 | 220 |
| 220 TraceEvent::~TraceEvent() { | 221 TraceEvent::~TraceEvent() { |
| 221 } | 222 } |
| 222 | 223 |
| 223 void TraceEvent::AppendEventsAsJSON(const std::vector<TraceEvent>& events, | 224 void TraceEvent::AppendEventsAsJSON(const std::vector<TraceEvent>& events, |
| 224 size_t start, | 225 size_t start, |
| 225 size_t count, | 226 size_t count, |
| 226 std::string* out) { | 227 std::string* out) { |
| 227 *out += "["; | |
| 228 for (size_t i = 0; i < count && start + i < events.size(); ++i) { | 228 for (size_t i = 0; i < count && start + i < events.size(); ++i) { |
| 229 if (i > 0) | 229 if (i > 0) |
| 230 *out += ","; | 230 *out += ","; |
| 231 events[i + start].AppendAsJSON(out); | 231 events[i + start].AppendAsJSON(out); |
| 232 } | 232 } |
| 233 *out += "]"; | |
| 234 } | 233 } |
| 235 | 234 |
| 236 void TraceEvent::AppendAsJSON(std::string* out) const { | 235 void TraceEvent::AppendAsJSON(std::string* out) const { |
| 237 const char* phase_str = GetPhaseStr(phase_); | 236 const char* phase_str = GetPhaseStr(phase_); |
| 238 int64 time_int64 = timestamp_.ToInternalValue(); | 237 int64 time_int64 = timestamp_.ToInternalValue(); |
| 239 // Category name checked at category creation time. | 238 // Category name checked at category creation time. |
| 240 DCHECK(!strchr(name_, '"')); | 239 DCHECK(!strchr(name_, '"')); |
| 241 StringAppendF(out, | 240 StringAppendF(out, |
| 242 "{\"cat\":\"%s\",\"pid\":%i,\"tid\":%i,\"ts\":%lld," | 241 "{\"cat\":\"%s\",\"pid\":%i,\"tid\":%i,\"ts\":%lld," |
| 243 "\"ph\":\"%s\",\"name\":\"%s\",\"args\":{", | 242 "\"ph\":\"%s\",\"name\":\"%s\",\"args\":{", |
| (...skipping 11 matching lines...) Expand all Loading... |
| 255 *out += "\""; | 254 *out += "\""; |
| 256 *out += arg_names_[i]; | 255 *out += arg_names_[i]; |
| 257 *out += "\":"; | 256 *out += "\":"; |
| 258 arg_values_[i].AppendAsJSON(out); | 257 arg_values_[i].AppendAsJSON(out); |
| 259 } | 258 } |
| 260 *out += "}}"; | 259 *out += "}}"; |
| 261 } | 260 } |
| 262 | 261 |
| 263 //////////////////////////////////////////////////////////////////////////////// | 262 //////////////////////////////////////////////////////////////////////////////// |
| 264 // | 263 // |
| 264 // TraceResultBuffer |
| 265 // |
| 266 //////////////////////////////////////////////////////////////////////////////// |
| 267 |
| 268 TraceResultBuffer::OutputCallback |
| 269 TraceResultBuffer::SimpleOutput::GetCallback() { |
| 270 return base::Bind(&SimpleOutput::Append, base::Unretained(this)); |
| 271 } |
| 272 |
| 273 void TraceResultBuffer::SimpleOutput::Append( |
| 274 const std::string& json_trace_output) { |
| 275 json_output += json_trace_output; |
| 276 } |
| 277 |
| 278 TraceResultBuffer::TraceResultBuffer() : append_comma_(false) { |
| 279 } |
| 280 |
| 281 TraceResultBuffer::~TraceResultBuffer() { |
| 282 } |
| 283 |
| 284 void TraceResultBuffer::SetOutputCallback(OutputCallback json_chunk_callback) { |
| 285 output_callback_ = json_chunk_callback; |
| 286 } |
| 287 |
| 288 void TraceResultBuffer::Start() { |
| 289 append_comma_ = false; |
| 290 output_callback_.Run("["); |
| 291 } |
| 292 |
| 293 void TraceResultBuffer::AddFragment(const std::string& trace_fragment) { |
| 294 if (append_comma_) |
| 295 output_callback_.Run(","); |
| 296 append_comma_ = true; |
| 297 output_callback_.Run(trace_fragment); |
| 298 } |
| 299 |
| 300 void TraceResultBuffer::Finish() { |
| 301 output_callback_.Run("]"); |
| 302 } |
| 303 |
| 304 //////////////////////////////////////////////////////////////////////////////// |
| 305 // |
| 265 // TraceLog | 306 // TraceLog |
| 266 // | 307 // |
| 267 //////////////////////////////////////////////////////////////////////////////// | 308 //////////////////////////////////////////////////////////////////////////////// |
| 268 | 309 |
| 269 // static | 310 // static |
| 270 TraceLog* TraceLog::GetInstance() { | 311 TraceLog* TraceLog::GetInstance() { |
| 271 return Singleton<TraceLog, StaticMemorySingletonTraits<TraceLog> >::get(); | 312 return Singleton<TraceLog, StaticMemorySingletonTraits<TraceLog> >::get(); |
| 272 } | 313 } |
| 273 | 314 |
| 274 TraceLog::TraceLog() | 315 TraceLog::TraceLog() |
| (...skipping 335 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 610 NULL, 0, NULL, 0, | 651 NULL, 0, NULL, 0, |
| 611 p_data_->threshold_begin_id, p_data_->threshold, | 652 p_data_->threshold_begin_id, p_data_->threshold, |
| 612 TraceLog::EVENT_FLAG_NONE); | 653 TraceLog::EVENT_FLAG_NONE); |
| 613 } | 654 } |
| 614 } | 655 } |
| 615 | 656 |
| 616 } // namespace internal | 657 } // namespace internal |
| 617 | 658 |
| 618 } // namespace debug | 659 } // namespace debug |
| 619 } // namespace base | 660 } // namespace base |
| OLD | NEW |