OLD | NEW |
1 // Copyright (c) 2011 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/logging.h" | 8 #include "base/logging.h" |
8 | 9 |
9 TraceSubscriberStdio::TraceSubscriberStdio() : file_(0) { | 10 TraceSubscriberStdio::TraceSubscriberStdio() : file_(0) { |
10 } | 11 } |
11 | 12 |
12 TraceSubscriberStdio::TraceSubscriberStdio(const FilePath& path) : file_(0) { | 13 TraceSubscriberStdio::TraceSubscriberStdio(const FilePath& path) : file_(0) { |
13 OpenFile(path); | 14 OpenFile(path); |
14 } | 15 } |
15 | 16 |
16 TraceSubscriberStdio::~TraceSubscriberStdio() { | 17 TraceSubscriberStdio::~TraceSubscriberStdio() { |
17 CloseFile(); | 18 CloseFile(); |
18 } | 19 } |
19 | 20 |
20 bool TraceSubscriberStdio::OpenFile(const FilePath& path) { | 21 bool TraceSubscriberStdio::OpenFile(const FilePath& path) { |
21 LOG(INFO) << "Logging performance trace to file: " << path.value(); | 22 LOG(INFO) << "Logging performance trace to file: " << path.value(); |
22 CloseFile(); | 23 CloseFile(); |
23 file_ = file_util::OpenFile(path, "w+"); | 24 file_ = file_util::OpenFile(path, "w+"); |
24 if (IsValid()) { | 25 if (IsValid()) { |
25 // FIXME: the file format expects it to start with "[". | 26 trace_buffer_.SetOutputCallback(base::Bind(&TraceSubscriberStdio::Write, |
26 fputc('[', file_); | 27 base::Unretained(this))); |
| 28 trace_buffer_.Start(); |
27 return true; | 29 return true; |
28 } else { | 30 } else { |
29 LOG(ERROR) << "Failed to open performance trace file: " << path.value(); | 31 LOG(ERROR) << "Failed to open performance trace file: " << path.value(); |
30 return false; | 32 return false; |
31 } | 33 } |
32 } | 34 } |
33 | 35 |
34 void TraceSubscriberStdio::CloseFile() { | 36 void TraceSubscriberStdio::CloseFile() { |
35 if (file_) { | 37 if (file_) { |
36 // FIXME: the file format expects it to end with "]". | |
37 fputc(']', file_); | |
38 fclose(file_); | 38 fclose(file_); |
39 file_ = 0; | 39 file_ = 0; |
40 } | 40 } |
41 } | 41 } |
42 | 42 |
43 bool TraceSubscriberStdio::IsValid() { | 43 bool TraceSubscriberStdio::IsValid() { |
44 return file_ && (0 == ferror(file_)); | 44 return file_ && (0 == ferror(file_)); |
45 } | 45 } |
46 | 46 |
47 void TraceSubscriberStdio::OnEndTracingComplete() { | 47 void TraceSubscriberStdio::OnEndTracingComplete() { |
| 48 trace_buffer_.Finish(); |
48 CloseFile(); | 49 CloseFile(); |
49 } | 50 } |
50 | 51 |
51 void TraceSubscriberStdio::OnTraceDataCollected( | 52 void TraceSubscriberStdio::OnTraceDataCollected( |
52 const std::string& json_events) { | 53 const std::string& trace_fragment) { |
53 if (!IsValid()) { | 54 trace_buffer_.AddFragment(trace_fragment); |
54 return; | 55 } |
55 } | |
56 | 56 |
57 // FIXME: "json_events" currently comes with "[" and "]". But the file doesn't | 57 void TraceSubscriberStdio::Write(const std::string& output_str) { |
58 // expect them. So remove them when writing to the file. | 58 if (IsValid()) { |
59 CHECK_GE(json_events.size(), 2u); | 59 size_t written = fwrite(output_str.c_str(), 1, output_str.size(), file_); |
60 const char* data = json_events.data() + 1; | 60 if (written != output_str.size()) { |
61 size_t size = json_events.size() - 2; | 61 LOG(ERROR) << "Error " << ferror(file_) << " when writing to trace file"; |
62 | 62 CloseFile(); |
63 size_t written = fwrite(data, 1, size, file_); | 63 } |
64 if (written != size) { | |
65 LOG(ERROR) << "Error " << ferror(file_) << " when writing to trace file"; | |
66 fclose(file_); | |
67 file_ = 0; | |
68 } else { | |
69 fputc(',', file_); | |
70 } | 64 } |
71 } | 65 } |
OLD | NEW |