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

Unified Diff: base/debug/trace_to_file.cc

Issue 542893002: Add trace-to-file support for base::TestSuite (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: CHECK -> DCHECK Created 6 years, 3 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
« no previous file with comments | « base/debug/trace_to_file.h ('k') | base/test/test_suite.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/debug/trace_to_file.cc
diff --git a/base/debug/trace_to_file.cc b/base/debug/trace_to_file.cc
new file mode 100644
index 0000000000000000000000000000000000000000..8a7608a0e5ba2c6e0688bd340c8147ce27a0edac
--- /dev/null
+++ b/base/debug/trace_to_file.cc
@@ -0,0 +1,126 @@
+// Copyright (c) 2014 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 "base/debug/trace_to_file.h"
+
+#include "base/base_switches.h"
+#include "base/command_line.h"
+#include "base/debug/trace_event_impl.h"
+#include "base/files/file_util.h"
+#include "base/message_loop/message_loop.h"
+#include "base/synchronization/waitable_event.h"
+
+namespace base {
+namespace debug {
+
+TraceToFile::TraceToFile() : started_(false) {
+}
+
+TraceToFile::~TraceToFile() {
+ EndTracingIfNeeded();
+}
+
+void TraceToFile::BeginTracingFromCommandLineOptions() {
+ DCHECK(CommandLine::InitializedForCurrentProcess());
+ DCHECK(!started_);
+
+ if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kTraceToFile))
+ return;
+
+ // empty filter (i.e. just --trace-to-file) turns into default categories in
awong 2014/09/17 16:03:52 nit: empty -> Empty
enne (OOO) 2014/09/24 23:01:58 Done.
+ // TraceEventImpl
+ std::string filter = CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
+ switches::kTraceToFile);
+
+ base::FilePath path;
+ if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kTraceToFileName)) {
+ path = base::FilePath(CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
+ switches::kTraceToFileName));
+ } else {
+ path = base::FilePath("trace.json");
+ }
+
+ LOG(ERROR) << "Start " << switches::kTraceToFile << " with CategoryFilter '"
+ << filter << "' to file '" << path.value() << "'.";
+
+ BeginTracing(path, filter);
+}
+
+void TraceToFile::BeginTracing(const base::FilePath& path,
+ const std::string& categories) {
+ DCHECK(!started_);
+ started_ = true;
+ path_ = path;
+ WriteFileHeader();
+
+ base::debug::TraceLog::GetInstance()->SetEnabled(
+ CategoryFilter(categories),
+ TraceLog::RECORDING_MODE,
+ TraceOptions(RECORD_UNTIL_FULL));
+}
+
+void TraceToFile::WriteFileHeader() {
+ const char* str = "{\"traceEvents\": [";
+ WriteFile(path_, str, strlen(str));
+}
+
+void TraceToFile::AppendFileFooter() {
+ const char* str = "]}";
+ AppendToFile(path_, str, strlen(str));
+}
+
+void TraceToFile::TraceOutputCallback(const std::string& data) {
+ int ret = base::AppendToFile(path_, data.c_str(), data.size());
+ DCHECK_NE(-1, ret);
+}
+
+static void OnTraceDataCollected(
+ bool* all_data_collected,
+ base::debug::TraceResultBuffer* buffer,
+ const scoped_refptr<base::RefCountedString>& json_events_str,
+ bool has_more_events) {
+ buffer->AddFragment(json_events_str->data());
+ if (!has_more_events)
+ *all_data_collected = true;
+}
+
+static void RepostUntilAllDataCollected(base::MessageLoop* loop,
+ bool* all_data_collected) {
+ if (*all_data_collected)
+ return;
+ loop->PostDelayedTask(FROM_HERE,
+ base::Bind(&RepostUntilAllDataCollected,
+ base::Unretained(loop),
+ base::Unretained(all_data_collected)),
+ base::TimeDelta::FromMilliseconds(1));
+}
+
+void TraceToFile::EndTracingIfNeeded() {
+ if (!started_)
+ return;
+ started_ = false;
+
+ base::debug::TraceLog::GetInstance()->SetDisabled();
+
+ base::debug::TraceResultBuffer buffer;
+ buffer.SetOutputCallback(
+ base::Bind(&TraceToFile::TraceOutputCallback, base::Unretained(this)));
+
+ base::MessageLoop loop;
awong 2014/09/17 16:03:52 This doesn't look quite right. (a) Creating Ru
enne (OOO) 2014/09/24 23:01:58 Done. Thanks for the pointers, that looks *way* b
+ bool all_data_collected = false;
+ RepostUntilAllDataCollected(&loop, &all_data_collected);
+
+ base::debug::TraceLog::GetInstance()->Flush(
+ base::Bind(&OnTraceDataCollected,
+ base::Unretained(&all_data_collected),
+ base::Unretained(&buffer)));
+ loop.RunUntilIdle();
+
+ DCHECK(all_data_collected);
+
+ AppendFileFooter();
+}
+
+} // namespace debug
+} // namespace base
« no previous file with comments | « base/debug/trace_to_file.h ('k') | base/test/test_suite.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698