| 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/tracing/trace_subscriber_stdio.h" | 5 #include "content/browser/tracing/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" |
| 11 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
| 12 | 12 |
| 13 namespace content { | 13 namespace content { |
| 14 | 14 |
| 15 // All method calls on this class are done on a SequencedWorkerPool thread. | 15 // All method calls on this class are done on a SequencedWorkerPool thread. |
| 16 class TraceSubscriberStdioImpl | 16 class TraceSubscriberStdioImpl |
| 17 : public base::RefCountedThreadSafe<TraceSubscriberStdioImpl> { | 17 : public base::RefCountedThreadSafe<TraceSubscriberStdioImpl> { |
| 18 public: | 18 public: |
| 19 explicit TraceSubscriberStdioImpl(const FilePath& path) | 19 explicit TraceSubscriberStdioImpl(const base::FilePath& path) |
| 20 : path_(path), | 20 : path_(path), |
| 21 file_(0) {} | 21 file_(0) {} |
| 22 | 22 |
| 23 void OnStart() { | 23 void OnStart() { |
| 24 DCHECK(!file_); | 24 DCHECK(!file_); |
| 25 trace_buffer_.SetOutputCallback( | 25 trace_buffer_.SetOutputCallback( |
| 26 base::Bind(&TraceSubscriberStdioImpl::Write, this)); | 26 base::Bind(&TraceSubscriberStdioImpl::Write, this)); |
| 27 file_ = file_util::OpenFile(path_, "w+"); | 27 file_ = file_util::OpenFile(path_, "w+"); |
| 28 if (IsValid()) { | 28 if (IsValid()) { |
| 29 LOG(INFO) << "Logging performance trace to file: " << path_.value(); | 29 LOG(INFO) << "Logging performance trace to file: " << path_.value(); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 void Write(const std::string& output_str) { | 66 void Write(const std::string& output_str) { |
| 67 if (IsValid()) { | 67 if (IsValid()) { |
| 68 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_); |
| 69 if (written != output_str.size()) { | 69 if (written != output_str.size()) { |
| 70 LOG(ERROR) << "Error " << ferror(file_) << " in fwrite() to trace file"; | 70 LOG(ERROR) << "Error " << ferror(file_) << " in fwrite() to trace file"; |
| 71 CloseFile(); | 71 CloseFile(); |
| 72 } | 72 } |
| 73 } | 73 } |
| 74 } | 74 } |
| 75 | 75 |
| 76 FilePath path_; | 76 base::FilePath path_; |
| 77 FILE* file_; | 77 FILE* file_; |
| 78 base::debug::TraceResultBuffer trace_buffer_; | 78 base::debug::TraceResultBuffer trace_buffer_; |
| 79 }; | 79 }; |
| 80 | 80 |
| 81 TraceSubscriberStdio::TraceSubscriberStdio(const FilePath& path) | 81 TraceSubscriberStdio::TraceSubscriberStdio(const base::FilePath& path) |
| 82 : impl_(new TraceSubscriberStdioImpl(path)) { | 82 : impl_(new TraceSubscriberStdioImpl(path)) { |
| 83 BrowserThread::PostBlockingPoolSequencedTask( | 83 BrowserThread::PostBlockingPoolSequencedTask( |
| 84 __FILE__, FROM_HERE, | 84 __FILE__, FROM_HERE, |
| 85 base::Bind(&TraceSubscriberStdioImpl::OnStart, impl_)); | 85 base::Bind(&TraceSubscriberStdioImpl::OnStart, impl_)); |
| 86 } | 86 } |
| 87 | 87 |
| 88 TraceSubscriberStdio::~TraceSubscriberStdio() { | 88 TraceSubscriberStdio::~TraceSubscriberStdio() { |
| 89 } | 89 } |
| 90 | 90 |
| 91 void TraceSubscriberStdio::OnEndTracingComplete() { | 91 void TraceSubscriberStdio::OnEndTracingComplete() { |
| 92 BrowserThread::PostBlockingPoolSequencedTask( | 92 BrowserThread::PostBlockingPoolSequencedTask( |
| 93 __FILE__, FROM_HERE, | 93 __FILE__, FROM_HERE, |
| 94 base::Bind(&TraceSubscriberStdioImpl::OnEnd, impl_)); | 94 base::Bind(&TraceSubscriberStdioImpl::OnEnd, impl_)); |
| 95 } | 95 } |
| 96 | 96 |
| 97 void TraceSubscriberStdio::OnTraceDataCollected( | 97 void TraceSubscriberStdio::OnTraceDataCollected( |
| 98 const scoped_refptr<base::RefCountedString>& data_ptr) { | 98 const scoped_refptr<base::RefCountedString>& data_ptr) { |
| 99 BrowserThread::PostBlockingPoolSequencedTask( | 99 BrowserThread::PostBlockingPoolSequencedTask( |
| 100 __FILE__, FROM_HERE, | 100 __FILE__, FROM_HERE, |
| 101 base::Bind(&TraceSubscriberStdioImpl::OnData, impl_, data_ptr)); | 101 base::Bind(&TraceSubscriberStdioImpl::OnData, impl_, data_ptr)); |
| 102 } | 102 } |
| 103 | 103 |
| 104 } // namespace content | 104 } // namespace content |
| OLD | NEW |