OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/logging.h" | 8 #include "base/logging.h" |
9 #include "base/threading/sequenced_worker_pool.h" | |
10 #include "content/public/browser/browser_thread.h" | |
11 | 9 |
12 namespace content { | 10 TraceSubscriberStdio::TraceSubscriberStdio() : file_(0) { |
| 11 } |
13 | 12 |
14 // All method calls on this class are done on a SequencedWorkerPool thread. | 13 TraceSubscriberStdio::TraceSubscriberStdio(const FilePath& path) : file_(0) { |
15 class TraceSubscriberStdioImpl | 14 OpenFile(path); |
16 : public base::RefCountedThreadSafe<TraceSubscriberStdioImpl> { | |
17 public: | |
18 explicit TraceSubscriberStdioImpl(const FilePath& path) | |
19 : path_(path), | |
20 file_(0) {} | |
21 | |
22 ~TraceSubscriberStdioImpl() { | |
23 CloseFile(); | |
24 } | |
25 | |
26 void OnStart() { | |
27 DCHECK(!file_); | |
28 file_ = file_util::OpenFile(path_, "w+"); | |
29 if (IsValid()) { | |
30 LOG(INFO) << "Logging performance trace to file: " << path_.value(); | |
31 trace_buffer_.SetOutputCallback( | |
32 base::Bind(&TraceSubscriberStdioImpl::Write, this)); | |
33 trace_buffer_.Start(); | |
34 } else { | |
35 LOG(ERROR) << "Failed to open performance trace file: " << path_.value(); | |
36 } | |
37 } | |
38 | |
39 void OnData(const scoped_refptr<base::RefCountedString>& data_ptr) { | |
40 trace_buffer_.AddFragment(data_ptr->data()); | |
41 } | |
42 | |
43 void OnEnd() { | |
44 trace_buffer_.Finish(); | |
45 CloseFile(); | |
46 } | |
47 | |
48 private: | |
49 bool IsValid() { | |
50 return file_ && (0 == ferror(file_)); | |
51 } | |
52 | |
53 void CloseFile() { | |
54 if (file_) { | |
55 fclose(file_); | |
56 file_ = 0; | |
57 } | |
58 } | |
59 | |
60 void Write(const std::string& output_str) { | |
61 if (IsValid()) { | |
62 size_t written = fwrite(output_str.data(), 1, output_str.size(), file_); | |
63 if (written != output_str.size()) { | |
64 LOG(ERROR) << "Error " << ferror(file_) << " in fwrite() to trace file"; | |
65 CloseFile(); | |
66 } | |
67 } | |
68 } | |
69 | |
70 FilePath path_; | |
71 FILE* file_; | |
72 base::debug::TraceResultBuffer trace_buffer_; | |
73 }; | |
74 | |
75 TraceSubscriberStdio::TraceSubscriberStdio(const FilePath& path) | |
76 : impl_(new TraceSubscriberStdioImpl(path)) { | |
77 BrowserThread::PostBlockingPoolSequencedTask( | |
78 __FILE__, FROM_HERE, | |
79 base::Bind(&TraceSubscriberStdioImpl::OnStart, impl_.get())); | |
80 } | 15 } |
81 | 16 |
82 TraceSubscriberStdio::~TraceSubscriberStdio() { | 17 TraceSubscriberStdio::~TraceSubscriberStdio() { |
| 18 CloseFile(); |
| 19 } |
| 20 |
| 21 bool TraceSubscriberStdio::OpenFile(const FilePath& path) { |
| 22 LOG(INFO) << "Logging performance trace to file: " << path.value(); |
| 23 CloseFile(); |
| 24 file_ = file_util::OpenFile(path, "w+"); |
| 25 if (IsValid()) { |
| 26 trace_buffer_.SetOutputCallback(base::Bind(&TraceSubscriberStdio::Write, |
| 27 base::Unretained(this))); |
| 28 trace_buffer_.Start(); |
| 29 return true; |
| 30 } else { |
| 31 LOG(ERROR) << "Failed to open performance trace file: " << path.value(); |
| 32 return false; |
| 33 } |
| 34 } |
| 35 |
| 36 void TraceSubscriberStdio::CloseFile() { |
| 37 if (file_) { |
| 38 fclose(file_); |
| 39 file_ = 0; |
| 40 } |
| 41 } |
| 42 |
| 43 bool TraceSubscriberStdio::IsValid() { |
| 44 return file_ && (0 == ferror(file_)); |
83 } | 45 } |
84 | 46 |
85 void TraceSubscriberStdio::OnEndTracingComplete() { | 47 void TraceSubscriberStdio::OnEndTracingComplete() { |
86 BrowserThread::PostBlockingPoolSequencedTask( | 48 trace_buffer_.Finish(); |
87 __FILE__, FROM_HERE, | 49 CloseFile(); |
88 base::Bind(&TraceSubscriberStdioImpl::OnEnd, impl_.get())); | |
89 } | 50 } |
90 | 51 |
91 void TraceSubscriberStdio::OnTraceDataCollected( | 52 void TraceSubscriberStdio::OnTraceDataCollected( |
92 const scoped_refptr<base::RefCountedString>& data_ptr) { | 53 const std::string& trace_fragment) { |
93 BrowserThread::PostBlockingPoolSequencedTask( | 54 trace_buffer_.AddFragment(trace_fragment); |
94 __FILE__, FROM_HERE, | |
95 base::Bind(&TraceSubscriberStdioImpl::OnData, impl_.get(), data_ptr)); | |
96 } | 55 } |
97 | 56 |
98 } // namespace content | 57 void TraceSubscriberStdio::Write(const std::string& output_str) { |
99 | 58 if (IsValid()) { |
| 59 size_t written = fwrite(output_str.data(), 1, output_str.size(), file_); |
| 60 if (written != output_str.size()) { |
| 61 LOG(ERROR) << "Error " << ferror(file_) << " when writing to trace file"; |
| 62 CloseFile(); |
| 63 } |
| 64 } |
| 65 } |
OLD | NEW |