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" |
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 FilePath& path) |
20 : path_(path), | 20 : path_(path), |
21 file_(0) {} | 21 file_(0) {} |
22 | 22 |
23 ~TraceSubscriberStdioImpl() { | |
24 CloseFile(); | |
25 } | |
26 | |
27 void OnStart() { | 23 void OnStart() { |
28 DCHECK(!file_); | 24 DCHECK(!file_); |
29 file_ = file_util::OpenFile(path_, "w+"); | 25 file_ = file_util::OpenFile(path_, "w+"); |
30 if (IsValid()) { | 26 if (IsValid()) { |
31 LOG(INFO) << "Logging performance trace to file: " << path_.value(); | 27 LOG(INFO) << "Logging performance trace to file: " << path_.value(); |
32 trace_buffer_.SetOutputCallback( | 28 trace_buffer_.SetOutputCallback( |
33 base::Bind(&TraceSubscriberStdioImpl::Write, this)); | 29 base::Bind(&TraceSubscriberStdioImpl::Write, this)); |
34 trace_buffer_.Start(); | 30 trace_buffer_.Start(); |
35 } else { | 31 } else { |
36 LOG(ERROR) << "Failed to open performance trace file: " << path_.value(); | 32 LOG(ERROR) << "Failed to open performance trace file: " << path_.value(); |
37 } | 33 } |
38 } | 34 } |
39 | 35 |
40 void OnData(const scoped_refptr<base::RefCountedString>& data_ptr) { | 36 void OnData(const scoped_refptr<base::RefCountedString>& data_ptr) { |
41 trace_buffer_.AddFragment(data_ptr->data()); | 37 trace_buffer_.AddFragment(data_ptr->data()); |
42 } | 38 } |
43 | 39 |
44 void OnEnd() { | 40 void OnEnd() { |
45 trace_buffer_.Finish(); | 41 trace_buffer_.Finish(); |
46 CloseFile(); | 42 CloseFile(); |
47 } | 43 } |
48 | 44 |
49 private: | 45 private: |
| 46 friend class base::RefCountedThreadSafe<TraceSubscriberStdioImpl>; |
| 47 |
| 48 ~TraceSubscriberStdioImpl() { |
| 49 CloseFile(); |
| 50 } |
| 51 |
50 bool IsValid() { | 52 bool IsValid() { |
51 return file_ && (0 == ferror(file_)); | 53 return file_ && (0 == ferror(file_)); |
52 } | 54 } |
53 | 55 |
54 void CloseFile() { | 56 void CloseFile() { |
55 if (file_) { | 57 if (file_) { |
56 fclose(file_); | 58 fclose(file_); |
57 file_ = 0; | 59 file_ = 0; |
58 } | 60 } |
59 } | 61 } |
(...skipping 30 matching lines...) Expand all Loading... |
90 } | 92 } |
91 | 93 |
92 void TraceSubscriberStdio::OnTraceDataCollected( | 94 void TraceSubscriberStdio::OnTraceDataCollected( |
93 const scoped_refptr<base::RefCountedString>& data_ptr) { | 95 const scoped_refptr<base::RefCountedString>& data_ptr) { |
94 BrowserThread::PostBlockingPoolSequencedTask( | 96 BrowserThread::PostBlockingPoolSequencedTask( |
95 __FILE__, FROM_HERE, | 97 __FILE__, FROM_HERE, |
96 base::Bind(&TraceSubscriberStdioImpl::OnData, impl_.get(), data_ptr)); | 98 base::Bind(&TraceSubscriberStdioImpl::OnData, impl_.get(), data_ptr)); |
97 } | 99 } |
98 | 100 |
99 } // namespace content | 101 } // namespace content |
OLD | NEW |