Index: base/debug/in_process_tracing.cc |
diff --git a/base/debug/in_process_tracing.cc b/base/debug/in_process_tracing.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..555a2b604100f6d5be3f944ab8767ea8e39d9f2c |
--- /dev/null |
+++ b/base/debug/in_process_tracing.cc |
@@ -0,0 +1,76 @@ |
+// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "base/debug/in_process_tracing.h" |
+#include "base/debug/trace_event.h" |
+#include "base/memory/ref_counted.h" |
+#include "base/memory/singleton.h" |
+ |
+namespace { |
+ |
+class InProcessTraceController { |
+ public: |
+ static InProcessTraceController* GetInstance() { |
+ return Singleton<InProcessTraceController>::get(); |
+ } |
+ |
+ InProcessTraceController() {} |
+ virtual ~InProcessTraceController() {} |
+ |
+ bool BeginTracing(const std::string& category_patterns) { |
+ base::debug::TraceLog::GetInstance()->SetEnabled( |
+ base::debug::CategoryFilter(category_patterns), |
+ base::debug::TraceLog::RECORD_UNTIL_FULL); |
+ return true; |
+ } |
+ |
+ void OnTraceDataCollected( |
+ const scoped_refptr<base::RefCountedString>& events_str, |
+ bool has_more_events) { |
+ trace_buffer_.AddFragment(events_str->data()); |
+ } |
+ |
+ bool EndTracing(std::string* json_trace_output) { |
+ using namespace base::debug; |
+ |
+ base::debug::TraceLog::GetInstance()->SetDisabled(); |
+ |
+ TraceResultBuffer::SimpleOutput output; |
+ trace_buffer_.SetOutputCallback(output.GetCallback()); |
+ |
+ trace_buffer_.Start(); |
+ |
+ base::debug::TraceLog::GetInstance()->Flush( |
+ base::Bind(&InProcessTraceController::OnTraceDataCollected, |
+ base::Unretained(this))); |
+ |
+ trace_buffer_.Finish(); |
+ trace_buffer_.SetOutputCallback(TraceResultBuffer::OutputCallback()); |
+ *json_trace_output = output.json_output; |
+ return true; |
+ } |
+ |
+ private: |
+ friend struct DefaultSingletonTraits<InProcessTraceController>; |
+ |
+ // For collecting trace data asynchronously. |
+ base::debug::TraceResultBuffer trace_buffer_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(InProcessTraceController); |
+}; |
+ |
+} // namespace |
+ |
+namespace tracing { |
+ |
+bool BeginTracing(const std::string& category_patterns) { |
+ return InProcessTraceController::GetInstance()->BeginTracing( |
+ category_patterns); |
+} |
+ |
+bool EndTracing(std::string* json_trace_output) { |
+ return InProcessTraceController::GetInstance()->EndTracing(json_trace_output); |
+} |
+ |
+} // namespace tracing |