Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(153)

Unified Diff: services/tracing/trace_data_sink.cc

Issue 853943005: Make tracing work on Android (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: nits Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: services/tracing/trace_data_sink.cc
diff --git a/services/tracing/trace_data_sink.cc b/services/tracing/trace_data_sink.cc
index aeae144888f1787041150b471bc230747ac17a70..eadeb570940578e57f5181c989933fe8bf14c957 100644
--- a/services/tracing/trace_data_sink.cc
+++ b/services/tracing/trace_data_sink.cc
@@ -4,38 +4,44 @@
#include "services/tracing/trace_data_sink.h"
-#include "base/files/file_util.h"
+#include "base/logging.h"
namespace tracing {
+namespace {
-TraceDataSink::TraceDataSink(base::FilePath base_name) : empty_(true) {
- file_ =
- base::OpenFile(base_name.AddExtension(FILE_PATH_LITERAL(".trace")), "w+");
- static const char start[] = "{\"traceEvents\":[";
- fwrite(start, 1, strlen(start), file_);
+const char kStart[] = "{\"traceEvents\":[";
+const char kEnd[] = "]}";
+
+void Write(const mojo::ScopedDataPipeProducerHandle& pipe,
+ const char* string,
+ uint32_t num_bytes) {
+ CHECK_EQ(MOJO_RESULT_OK,
+ mojo::WriteDataRaw(pipe.get(), string, &num_bytes,
+ MOJO_WRITE_DATA_FLAG_ALL_OR_NONE));
+}
+}
+
+TraceDataSink::TraceDataSink(mojo::ScopedDataPipeProducerHandle pipe)
+ : pipe_(pipe.Pass()), empty_(true) {
+ Write(pipe_, kStart, strlen(kStart));
}
TraceDataSink::~TraceDataSink() {
- if (file_)
+ if (pipe_.is_valid())
Flush();
- DCHECK(!file_);
+ DCHECK(!pipe_.is_valid());
}
void TraceDataSink::AddChunk(const std::string& json) {
- if (!empty_) {
- fwrite(",", 1, 1, file_);
- }
-
+ if (!empty_)
+ Write(pipe_, ",", 1);
empty_ = false;
-
- fwrite(json.data(), 1, json.length(), file_);
+ Write(pipe_, json.data(), json.length());
}
void TraceDataSink::Flush() {
- static const char end[] = "]}";
- fwrite(end, 1, strlen(end), file_);
- base::CloseFile(file_);
- file_ = nullptr;
+ Write(pipe_, kEnd, strlen(kEnd));
+ pipe_.reset();
}
} // namespace tracing

Powered by Google App Engine
This is Rietveld 408576698