diff options
author | husky@google.com <husky@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-06-01 12:13:26 +0000 |
---|---|---|
committer | husky@google.com <husky@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-06-01 12:13:26 +0000 |
commit | 58a4d14583935f77c0aafa9c2491ae7ae4a35d9d (patch) | |
tree | 41ffe8de7c53fbadc4913b646fea4bf3490cda6d /content/browser/trace_subscriber_stdio.cc | |
parent | 0c11dea7fb3740a3fe8862e2e5c0c9d5f8f11971 (diff) | |
download | chromium_src-58a4d14583935f77c0aafa9c2491ae7ae4a35d9d.zip chromium_src-58a4d14583935f77c0aafa9c2491ae7ae4a35d9d.tar.gz chromium_src-58a4d14583935f77c0aafa9c2491ae7ae4a35d9d.tar.bz2 |
TraceSubscriber implementation that writes to a file.
Review URL: http://codereview.chromium.org/7044010
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@87459 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'content/browser/trace_subscriber_stdio.cc')
-rw-r--r-- | content/browser/trace_subscriber_stdio.cc | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/content/browser/trace_subscriber_stdio.cc b/content/browser/trace_subscriber_stdio.cc new file mode 100644 index 0000000..6d4e23b --- /dev/null +++ b/content/browser/trace_subscriber_stdio.cc @@ -0,0 +1,56 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "content/browser/trace_subscriber_stdio.h" + +#include "base/logging.h" + +TraceSubscriberStdio::TraceSubscriberStdio(const FilePath& path) { + LOG(INFO) << "Logging performance trace to file: " << path.value(); + m_file = file_util::OpenFile(path, "w+"); + if (IsValid()) { + // FIXME: the file format expects it to start with "[". + fputc('[', m_file); + } else { + LOG(ERROR) << "Failed to open performance trace file: " << path.value(); + } +} + +TraceSubscriberStdio::~TraceSubscriberStdio() { + OnEndTracingComplete(); +} + +bool TraceSubscriberStdio::IsValid() { + return m_file && (0 == ferror(m_file)); +} + +void TraceSubscriberStdio::OnEndTracingComplete() { + if (m_file) { + // FIXME: the file format expects it to end with "]". + fputc(']', m_file); + fclose(m_file); + m_file = 0; + } +} + +void TraceSubscriberStdio::OnTraceDataCollected( + const std::string& json_events) { + if (!IsValid()) { + return; + } + + // FIXME: "json_events" currently comes with "[" and "]". But the file doesn't + // expect them. So remove them when writing to the file. + CHECK_GE(json_events.size(), 2u); + const char* data = json_events.data() + 1; + size_t size = json_events.size() - 2; + + size_t written = fwrite(data, 1, size, m_file); + if (written != size) { + LOG(ERROR) << "Error " << ferror(m_file) << " when writing to trace file"; + fclose(m_file); + m_file = 0; + } + fputc(',', m_file); +} |