OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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 "services/tracing/trace_data_sink.h" |
| 6 |
| 7 #include "base/files/file_util.h" |
| 8 |
| 9 namespace tracing { |
| 10 |
| 11 TraceDataSink::TraceDataSink(base::FilePath base_name) : empty_(true) { |
| 12 file_ = |
| 13 base::OpenFile(base_name.AddExtension(FILE_PATH_LITERAL(".trace")), "w+"); |
| 14 static const char start[] = "{\"traceEvents\":["; |
| 15 fwrite(start, 1, strlen(start), file_); |
| 16 } |
| 17 |
| 18 TraceDataSink::~TraceDataSink() { |
| 19 if (file_) |
| 20 Flush(); |
| 21 DCHECK(!file_); |
| 22 } |
| 23 |
| 24 void TraceDataSink::AddChunk(const std::string& json) { |
| 25 if (!empty_) { |
| 26 fwrite(",", 1, 1, file_); |
| 27 } |
| 28 |
| 29 empty_ = false; |
| 30 |
| 31 fwrite(json.data(), 1, json.length(), file_); |
| 32 } |
| 33 |
| 34 void TraceDataSink::Flush() { |
| 35 static const char end[] = "]}"; |
| 36 fwrite(end, 1, strlen(end), file_); |
| 37 base::CloseFile(file_); |
| 38 file_ = nullptr; |
| 39 } |
| 40 |
| 41 } // namespace tracing |
OLD | NEW |