1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/browser/trace_subscriber_stdio.h"
8 #include "base/debug/trace_event.h"
9 #include "base/logging.h"
10 #include "base/threading/sequenced_worker_pool.h"
11 #include "content/public/browser/browser_thread.h"
15 // All method calls on this class are done on a SequencedWorkerPool thread.
16 class TraceSubscriberStdioImpl
17 : public base::RefCountedThreadSafe
<TraceSubscriberStdioImpl
> {
19 explicit TraceSubscriberStdioImpl(const FilePath
& path
)
25 trace_buffer_
.SetOutputCallback(
26 base::Bind(&TraceSubscriberStdioImpl::Write
, this));
27 file_
= file_util::OpenFile(path_
, "w+");
29 LOG(INFO
) << "Logging performance trace to file: " << path_
.value();
30 trace_buffer_
.Start();
32 LOG(ERROR
) << "Failed to open performance trace file: " << path_
.value();
36 void OnData(const scoped_refptr
<base::RefCountedString
>& data_ptr
) {
37 trace_buffer_
.AddFragment(data_ptr
->data());
41 trace_buffer_
.Finish();
46 friend class base::RefCountedThreadSafe
<TraceSubscriberStdioImpl
>;
48 ~TraceSubscriberStdioImpl() {
52 bool IsValid() const {
53 return file_
&& (0 == ferror(file_
));
61 // This is important, as it breaks a reference cycle.
62 trace_buffer_
.SetOutputCallback(
63 base::debug::TraceResultBuffer::OutputCallback());
66 void Write(const std::string
& output_str
) {
68 size_t written
= fwrite(output_str
.data(), 1, output_str
.size(), file_
);
69 if (written
!= output_str
.size()) {
70 LOG(ERROR
) << "Error " << ferror(file_
) << " in fwrite() to trace file";
78 base::debug::TraceResultBuffer trace_buffer_
;
81 TraceSubscriberStdio::TraceSubscriberStdio(const FilePath
& path
)
82 : impl_(new TraceSubscriberStdioImpl(path
)) {
83 BrowserThread::PostBlockingPoolSequencedTask(
85 base::Bind(&TraceSubscriberStdioImpl::OnStart
, impl_
));
88 TraceSubscriberStdio::~TraceSubscriberStdio() {
91 void TraceSubscriberStdio::OnEndTracingComplete() {
92 BrowserThread::PostBlockingPoolSequencedTask(
94 base::Bind(&TraceSubscriberStdioImpl::OnEnd
, impl_
));
97 void TraceSubscriberStdio::OnTraceDataCollected(
98 const scoped_refptr
<base::RefCountedString
>& data_ptr
) {
99 BrowserThread::PostBlockingPoolSequencedTask(
101 base::Bind(&TraceSubscriberStdioImpl::OnData
, impl_
, data_ptr
));
104 } // namespace content