OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/browser/trace_subscriber_stdio.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 |
| 9 TraceSubscriberStdio::TraceSubscriberStdio(const FilePath& path) { |
| 10 LOG(INFO) << "Logging performance trace to file: " << path.value(); |
| 11 m_file = file_util::OpenFile(path, "w+"); |
| 12 if (IsValid()) { |
| 13 // FIXME: the file format expects it to start with "[". |
| 14 fputc('[', m_file); |
| 15 } else { |
| 16 LOG(ERROR) << "Failed to open performance trace file: " << path.value(); |
| 17 } |
| 18 } |
| 19 |
| 20 TraceSubscriberStdio::~TraceSubscriberStdio() { |
| 21 OnEndTracingComplete(); |
| 22 } |
| 23 |
| 24 bool TraceSubscriberStdio::IsValid() { |
| 25 return m_file && (0 == ferror(m_file)); |
| 26 } |
| 27 |
| 28 void TraceSubscriberStdio::OnEndTracingComplete() { |
| 29 if (m_file) { |
| 30 // FIXME: the file format expects it to end with "]". |
| 31 fputc(']', m_file); |
| 32 fclose(m_file); |
| 33 m_file = 0; |
| 34 } |
| 35 } |
| 36 |
| 37 void TraceSubscriberStdio::OnTraceDataCollected( |
| 38 const std::string& json_events) { |
| 39 if (!IsValid()) { |
| 40 return; |
| 41 } |
| 42 |
| 43 // FIXME: "json_events" currently comes with "[" and "]". But the file doesn't |
| 44 // expect them. So remove them when writing to the file. |
| 45 CHECK_GE(json_events.size(), 2); |
| 46 const char* data = json_events.data() + 1; |
| 47 size_t size = json_events.size() - 2; |
| 48 |
| 49 size_t written = fwrite(data, 1, size, m_file); |
| 50 if (written != size) { |
| 51 LOG(ERROR) << "Error " << ferror(m_file) << " when writing to trace file"; |
| 52 fclose(m_file); |
| 53 m_file = 0; |
| 54 } |
| 55 fputc(',', m_file); |
| 56 } |
OLD | NEW |