blob: bf6a8fccd2adc84ad88e86535eaf5a7495d81ce7 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
// 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() : 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;
}
}
void TraceSubscriberStdio::CloseFile() {
if (file_) {
// FIXME: the file format expects it to end with "]".
fputc(']', file_);
fclose(file_);
file_ = 0;
}
}
bool TraceSubscriberStdio::IsValid() {
return file_ && (0 == ferror(file_));
}
void TraceSubscriberStdio::OnEndTracingComplete() {
CloseFile();
}
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, file_);
if (written != size) {
LOG(ERROR) << "Error " << ferror(file_) << " when writing to trace file";
fclose(file_);
file_ = 0;
} else {
fputc(',', file_);
}
}
|