diff options
-rw-r--r-- | content/browser/trace_subscriber_stdio.cc | 32 | ||||
-rw-r--r-- | content/browser/trace_subscriber_stdio.h | 6 |
2 files changed, 29 insertions, 9 deletions
diff --git a/content/browser/trace_subscriber_stdio.cc b/content/browser/trace_subscriber_stdio.cc index b1b5daa..bf6a8fc 100644 --- a/content/browser/trace_subscriber_stdio.cc +++ b/content/browser/trace_subscriber_stdio.cc @@ -6,19 +6,38 @@ #include "base/logging.h" -TraceSubscriberStdio::TraceSubscriberStdio(const FilePath& path) { +TraceSubscriberStdio::TraceSubscriberStdio() : file_(0) { +} + +TraceSubscriberStdio::TraceSubscriberStdio(const FilePath& path) : file_(0) { + OpenFile(path); +} + +TraceSubscriberStdio::~TraceSubscriberStdio() { + CloseFile(); +} + +bool TraceSubscriberStdio::OpenFile(const FilePath& path) { LOG(INFO) << "Logging performance trace to file: " << path.value(); + CloseFile(); file_ = file_util::OpenFile(path, "w+"); if (IsValid()) { // FIXME: the file format expects it to start with "[". fputc('[', file_); + return true; } else { LOG(ERROR) << "Failed to open performance trace file: " << path.value(); + return false; } } -TraceSubscriberStdio::~TraceSubscriberStdio() { - OnEndTracingComplete(); +void TraceSubscriberStdio::CloseFile() { + if (file_) { + // FIXME: the file format expects it to end with "]". + fputc(']', file_); + fclose(file_); + file_ = 0; + } } bool TraceSubscriberStdio::IsValid() { @@ -26,12 +45,7 @@ bool TraceSubscriberStdio::IsValid() { } void TraceSubscriberStdio::OnEndTracingComplete() { - if (file_) { - // FIXME: the file format expects it to end with "]". - fputc(']', file_); - fclose(file_); - file_ = 0; - } + CloseFile(); } void TraceSubscriberStdio::OnTraceDataCollected( diff --git a/content/browser/trace_subscriber_stdio.h b/content/browser/trace_subscriber_stdio.h index 709196b..9220380 100644 --- a/content/browser/trace_subscriber_stdio.h +++ b/content/browser/trace_subscriber_stdio.h @@ -14,9 +14,15 @@ // Stdio implementation of TraceSubscriber. Use this to write traces to a file. class TraceSubscriberStdio : public TraceSubscriber { public: + TraceSubscriberStdio(); // Creates or overwrites the specified file. Check IsValid() for success. explicit TraceSubscriberStdio(const FilePath& path); + // Creates or overwrites the specified file. Returns true on success. + bool OpenFile(const FilePath& path); + // Finishes json output and closes file. + void CloseFile(); + // Returns TRUE if we're currently writing data to a file. bool IsValid(); |