| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "content/browser/trace_subscriber_stdio.h" | 5 #include "content/browser/trace_subscriber_stdio.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/debug/trace_event.h" | 8 #include "base/debug/trace_event.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/threading/sequenced_worker_pool.h" | 10 #include "base/threading/sequenced_worker_pool.h" |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 | 51 |
| 52 bool IsValid() { | 52 bool IsValid() { |
| 53 return file_ && (0 == ferror(file_)); | 53 return file_ && (0 == ferror(file_)); |
| 54 } | 54 } |
| 55 | 55 |
| 56 void CloseFile() { | 56 void CloseFile() { |
| 57 if (file_) { | 57 if (file_) { |
| 58 fclose(file_); | 58 fclose(file_); |
| 59 file_ = 0; | 59 file_ = 0; |
| 60 } | 60 } |
| 61 // This is important, as it breaks a reference cycle. |
| 62 trace_buffer_.SetOutputCallback( |
| 63 base::debug::TraceResultBuffer::OutputCallback()); |
| 61 } | 64 } |
| 62 | 65 |
| 63 void Write(const std::string& output_str) { | 66 void Write(const std::string& output_str) { |
| 64 if (IsValid()) { | 67 if (IsValid()) { |
| 65 size_t written = fwrite(output_str.data(), 1, output_str.size(), file_); | 68 size_t written = fwrite(output_str.data(), 1, output_str.size(), file_); |
| 66 if (written != output_str.size()) { | 69 if (written != output_str.size()) { |
| 67 LOG(ERROR) << "Error " << ferror(file_) << " in fwrite() to trace file"; | 70 LOG(ERROR) << "Error " << ferror(file_) << " in fwrite() to trace file"; |
| 68 CloseFile(); | 71 CloseFile(); |
| 69 } | 72 } |
| 70 } | 73 } |
| 71 } | 74 } |
| 72 | 75 |
| 73 FilePath path_; | 76 FilePath path_; |
| 74 FILE* file_; | 77 FILE* file_; |
| 75 base::debug::TraceResultBuffer trace_buffer_; | 78 base::debug::TraceResultBuffer trace_buffer_; |
| 76 }; | 79 }; |
| 77 | 80 |
| 78 TraceSubscriberStdio::TraceSubscriberStdio(const FilePath& path) | 81 TraceSubscriberStdio::TraceSubscriberStdio(const FilePath& path) |
| 79 : impl_(new TraceSubscriberStdioImpl(path)) { | 82 : impl_(new TraceSubscriberStdioImpl(path)) { |
| 80 BrowserThread::PostBlockingPoolSequencedTask( | 83 BrowserThread::PostBlockingPoolSequencedTask( |
| 81 __FILE__, FROM_HERE, | 84 __FILE__, FROM_HERE, |
| 82 base::Bind(&TraceSubscriberStdioImpl::OnStart, impl_.get())); | 85 base::Bind(&TraceSubscriberStdioImpl::OnStart, impl_)); |
| 83 } | 86 } |
| 84 | 87 |
| 85 TraceSubscriberStdio::~TraceSubscriberStdio() { | 88 TraceSubscriberStdio::~TraceSubscriberStdio() { |
| 86 } | 89 } |
| 87 | 90 |
| 88 void TraceSubscriberStdio::OnEndTracingComplete() { | 91 void TraceSubscriberStdio::OnEndTracingComplete() { |
| 89 BrowserThread::PostBlockingPoolSequencedTask( | 92 BrowserThread::PostBlockingPoolSequencedTask( |
| 90 __FILE__, FROM_HERE, | 93 __FILE__, FROM_HERE, |
| 91 base::Bind(&TraceSubscriberStdioImpl::OnEnd, impl_.get())); | 94 base::Bind(&TraceSubscriberStdioImpl::OnEnd, impl_)); |
| 92 } | 95 } |
| 93 | 96 |
| 94 void TraceSubscriberStdio::OnTraceDataCollected( | 97 void TraceSubscriberStdio::OnTraceDataCollected( |
| 95 const scoped_refptr<base::RefCountedString>& data_ptr) { | 98 const scoped_refptr<base::RefCountedString>& data_ptr) { |
| 96 BrowserThread::PostBlockingPoolSequencedTask( | 99 BrowserThread::PostBlockingPoolSequencedTask( |
| 97 __FILE__, FROM_HERE, | 100 __FILE__, FROM_HERE, |
| 98 base::Bind(&TraceSubscriberStdioImpl::OnData, impl_.get(), data_ptr)); | 101 base::Bind(&TraceSubscriberStdioImpl::OnData, impl_, data_ptr)); |
| 99 } | 102 } |
| 100 | 103 |
| 101 } // namespace content | 104 } // namespace content |
| OLD | NEW |