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