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