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 // Trace events are for tracking application performance. | 5 // Trace events are for tracking application performance. |
6 // | 6 // |
7 // Events are issued against categories. Whereas LOG's | 7 // Events are issued against categories. Whereas LOG's |
8 // categories are statically defined, TRACE categories are created | 8 // categories are statically defined, TRACE categories are created |
9 // implicitly with a string. For example: | 9 // implicitly with a string. For example: |
10 // TRACE_EVENT_INSTANT0("MY_SUBSYSTEM", "SomeImportantEvent") | 10 // TRACE_EVENT_INSTANT0("MY_SUBSYSTEM", "SomeImportantEvent") |
(...skipping 497 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
508 TimeTicks timestamp_; | 508 TimeTicks timestamp_; |
509 TraceEventPhase phase_; | 509 TraceEventPhase phase_; |
510 const TraceCategory* category_; | 510 const TraceCategory* category_; |
511 const char* name_; | 511 const char* name_; |
512 const char* arg_names_[kTraceMaxNumArgs]; | 512 const char* arg_names_[kTraceMaxNumArgs]; |
513 TraceValue arg_values_[kTraceMaxNumArgs]; | 513 TraceValue arg_values_[kTraceMaxNumArgs]; |
514 scoped_refptr<base::RefCountedString> parameter_copy_storage_; | 514 scoped_refptr<base::RefCountedString> parameter_copy_storage_; |
515 }; | 515 }; |
516 | 516 |
517 | 517 |
| 518 // TraceResultBuffer collects and converts trace fragments returned by TraceLog |
| 519 // to JSON output. |
| 520 class BASE_EXPORT TraceResultBuffer { |
| 521 public: |
| 522 typedef base::Callback<void(const std::string&)> OutputCallback; |
| 523 |
| 524 // If you don't need to stream JSON chunks out efficiently, and just want to |
| 525 // get a complete JSON string after calling Finish, use this struct to collect |
| 526 // JSON trace output. |
| 527 struct BASE_EXPORT SimpleOutput { |
| 528 OutputCallback GetCallback(); |
| 529 void Append(const std::string& json_string); |
| 530 |
| 531 // Do what you want with the json_output_ string after calling |
| 532 // TraceResultBuffer::Finish. |
| 533 std::string json_output; |
| 534 }; |
| 535 |
| 536 TraceResultBuffer(); |
| 537 ~TraceResultBuffer(); |
| 538 |
| 539 // Set callback. The callback will be called during Start with the initial |
| 540 // JSON output and during AddFragment and Finish with following JSON output |
| 541 // chunks. The callback target must live past the last calls to |
| 542 // TraceResultBuffer::Start/AddFragment/Finish. |
| 543 void SetOutputCallback(OutputCallback json_chunk_callback); |
| 544 |
| 545 // Start JSON output. This resets all internal state, so you can reuse |
| 546 // the TraceResultBuffer by calling Start. |
| 547 void Start(); |
| 548 |
| 549 // Call AddFragment 0 or more times to add trace fragments from TraceLog. |
| 550 void AddFragment(const std::string& trace_fragment); |
| 551 |
| 552 // When all fragments have been added, call Finish to complete the JSON |
| 553 // formatted output. |
| 554 void Finish(); |
| 555 |
| 556 private: |
| 557 OutputCallback output_callback_; |
| 558 bool append_comma_; |
| 559 }; |
| 560 |
| 561 |
518 class BASE_EXPORT TraceLog { | 562 class BASE_EXPORT TraceLog { |
519 public: | 563 public: |
520 // Flags for passing to AddTraceEvent. | 564 // Flags for passing to AddTraceEvent. |
521 enum EventFlags { | 565 enum EventFlags { |
522 EVENT_FLAG_NONE = 0, | 566 EVENT_FLAG_NONE = 0, |
523 EVENT_FLAG_COPY = 1<<0 | 567 EVENT_FLAG_COPY = 1<<0 |
524 }; | 568 }; |
525 | 569 |
526 static TraceLog* GetInstance(); | 570 static TraceLog* GetInstance(); |
527 | 571 |
(...skipping 14 matching lines...) Expand all Loading... |
542 // Disable tracing for all categories. | 586 // Disable tracing for all categories. |
543 void SetDisabled(); | 587 void SetDisabled(); |
544 // Helper method to enable/disable tracing for all categories. | 588 // Helper method to enable/disable tracing for all categories. |
545 void SetEnabled(bool enabled); | 589 void SetEnabled(bool enabled); |
546 bool IsEnabled() { return enabled_; } | 590 bool IsEnabled() { return enabled_; } |
547 | 591 |
548 float GetBufferPercentFull() const; | 592 float GetBufferPercentFull() const; |
549 | 593 |
550 // When enough events are collected, they are handed (in bulk) to | 594 // When enough events are collected, they are handed (in bulk) to |
551 // the output callback. If no callback is set, the output will be | 595 // the output callback. If no callback is set, the output will be |
552 // silently dropped. The callback must be thread safe. | 596 // silently dropped. The callback must be thread safe. The string format is |
| 597 // undefined. Use TraceResultBuffer to convert one or more trace strings to |
| 598 // JSON. |
553 typedef RefCountedData<std::string> RefCountedString; | 599 typedef RefCountedData<std::string> RefCountedString; |
554 typedef base::Callback<void(scoped_refptr<RefCountedString>)> OutputCallback; | 600 typedef base::Callback<void(scoped_refptr<RefCountedString>)> OutputCallback; |
555 void SetOutputCallback(const OutputCallback& cb); | 601 void SetOutputCallback(const OutputCallback& cb); |
556 | 602 |
557 // The trace buffer does not flush dynamically, so when it fills up, | 603 // The trace buffer does not flush dynamically, so when it fills up, |
558 // subsequent trace events will be dropped. This callback is generated when | 604 // subsequent trace events will be dropped. This callback is generated when |
559 // the trace buffer is full. The callback must be thread safe. | 605 // the trace buffer is full. The callback must be thread safe. |
560 typedef base::Callback<void(void)> BufferFullCallback; | 606 typedef base::Callback<void(void)> BufferFullCallback; |
561 void SetBufferFullCallback(const BufferFullCallback& cb); | 607 void SetBufferFullCallback(const BufferFullCallback& cb); |
562 | 608 |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
698 Data* p_data_; | 744 Data* p_data_; |
699 Data data_; | 745 Data data_; |
700 }; | 746 }; |
701 | 747 |
702 } // namespace internal | 748 } // namespace internal |
703 | 749 |
704 } // namespace debug | 750 } // namespace debug |
705 } // namespace base | 751 } // namespace base |
706 | 752 |
707 #endif // BASE_DEBUG_TRACE_EVENT_H_ | 753 #endif // BASE_DEBUG_TRACE_EVENT_H_ |
OLD | NEW |