| 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" | |
| 13 #include "base/format_macros.h" | 12 #include "base/format_macros.h" |
| 14 #include "base/memory/ref_counted_memory.h" | 13 #include "base/memory/ref_counted_memory.h" |
| 15 #include "base/process_util.h" | 14 #include "base/process_util.h" |
| 16 #include "base/stringprintf.h" | 15 #include "base/stringprintf.h" |
| 17 #include "base/threading/thread_local.h" | 16 #include "base/threading/thread_local.h" |
| 18 #include "base/utf_string_conversions.h" | 17 #include "base/utf_string_conversions.h" |
| 19 #include "base/stl_util.h" | 18 #include "base/stl_util.h" |
| 20 #include "base/time.h" | 19 #include "base/time.h" |
| 21 | 20 |
| 22 #define USE_UNRELIABLE_NOW | 21 #define USE_UNRELIABLE_NOW |
| (...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 218 } | 217 } |
| 219 } | 218 } |
| 220 | 219 |
| 221 TraceEvent::~TraceEvent() { | 220 TraceEvent::~TraceEvent() { |
| 222 } | 221 } |
| 223 | 222 |
| 224 void TraceEvent::AppendEventsAsJSON(const std::vector<TraceEvent>& events, | 223 void TraceEvent::AppendEventsAsJSON(const std::vector<TraceEvent>& events, |
| 225 size_t start, | 224 size_t start, |
| 226 size_t count, | 225 size_t count, |
| 227 std::string* out) { | 226 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 += "]"; |
| 233 } | 234 } |
| 234 | 235 |
| 235 void TraceEvent::AppendAsJSON(std::string* out) const { | 236 void TraceEvent::AppendAsJSON(std::string* out) const { |
| 236 const char* phase_str = GetPhaseStr(phase_); | 237 const char* phase_str = GetPhaseStr(phase_); |
| 237 int64 time_int64 = timestamp_.ToInternalValue(); | 238 int64 time_int64 = timestamp_.ToInternalValue(); |
| 238 // Category name checked at category creation time. | 239 // Category name checked at category creation time. |
| 239 DCHECK(!strchr(name_, '"')); | 240 DCHECK(!strchr(name_, '"')); |
| 240 StringAppendF(out, | 241 StringAppendF(out, |
| 241 "{\"cat\":\"%s\",\"pid\":%i,\"tid\":%i,\"ts\":%lld," | 242 "{\"cat\":\"%s\",\"pid\":%i,\"tid\":%i,\"ts\":%lld," |
| 242 "\"ph\":\"%s\",\"name\":\"%s\",\"args\":{", | 243 "\"ph\":\"%s\",\"name\":\"%s\",\"args\":{", |
| (...skipping 11 matching lines...) Expand all Loading... |
| 254 *out += "\""; | 255 *out += "\""; |
| 255 *out += arg_names_[i]; | 256 *out += arg_names_[i]; |
| 256 *out += "\":"; | 257 *out += "\":"; |
| 257 arg_values_[i].AppendAsJSON(out); | 258 arg_values_[i].AppendAsJSON(out); |
| 258 } | 259 } |
| 259 *out += "}}"; | 260 *out += "}}"; |
| 260 } | 261 } |
| 261 | 262 |
| 262 //////////////////////////////////////////////////////////////////////////////// | 263 //////////////////////////////////////////////////////////////////////////////// |
| 263 // | 264 // |
| 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 // | |
| 306 // TraceLog | 265 // TraceLog |
| 307 // | 266 // |
| 308 //////////////////////////////////////////////////////////////////////////////// | 267 //////////////////////////////////////////////////////////////////////////////// |
| 309 | 268 |
| 310 // static | 269 // static |
| 311 TraceLog* TraceLog::GetInstance() { | 270 TraceLog* TraceLog::GetInstance() { |
| 312 return Singleton<TraceLog, StaticMemorySingletonTraits<TraceLog> >::get(); | 271 return Singleton<TraceLog, StaticMemorySingletonTraits<TraceLog> >::get(); |
| 313 } | 272 } |
| 314 | 273 |
| 315 TraceLog::TraceLog() | 274 TraceLog::TraceLog() |
| (...skipping 335 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 651 NULL, 0, NULL, 0, | 610 NULL, 0, NULL, 0, |
| 652 p_data_->threshold_begin_id, p_data_->threshold, | 611 p_data_->threshold_begin_id, p_data_->threshold, |
| 653 TraceLog::EVENT_FLAG_NONE); | 612 TraceLog::EVENT_FLAG_NONE); |
| 654 } | 613 } |
| 655 } | 614 } |
| 656 | 615 |
| 657 } // namespace internal | 616 } // namespace internal |
| 658 | 617 |
| 659 } // namespace debug | 618 } // namespace debug |
| 660 } // namespace base | 619 } // namespace base |
| OLD | NEW |