Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / base / test / trace_to_file.cc
blobe00b58ae03eb522b4a31afc2f287032f13c957e6
1 // Copyright (c) 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.
5 #include "base/test/trace_to_file.h"
7 #include "base/base_switches.h"
8 #include "base/bind.h"
9 #include "base/command_line.h"
10 #include "base/files/file_util.h"
11 #include "base/run_loop.h"
12 #include "base/trace_event/trace_event_impl.h"
14 namespace base {
15 namespace test {
17 TraceToFile::TraceToFile() : started_(false) {
20 TraceToFile::~TraceToFile() {
21 EndTracingIfNeeded();
24 void TraceToFile::BeginTracingFromCommandLineOptions() {
25 DCHECK(CommandLine::InitializedForCurrentProcess());
26 DCHECK(!started_);
28 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kTraceToFile))
29 return;
31 // Empty filter (i.e. just --trace-to-file) turns into default categories in
32 // TraceEventImpl
33 std::string filter = CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
34 switches::kTraceToFile);
36 FilePath path;
37 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kTraceToFileName)) {
38 path = FilePath(CommandLine::ForCurrentProcess()
39 ->GetSwitchValuePath(switches::kTraceToFileName));
40 } else {
41 path = FilePath(FILE_PATH_LITERAL("trace.json"));
44 BeginTracing(path, filter);
47 void TraceToFile::BeginTracing(const FilePath& path,
48 const std::string& categories) {
49 DCHECK(!started_);
50 started_ = true;
51 path_ = path;
52 WriteFileHeader();
54 trace_event::TraceLog::GetInstance()->SetEnabled(
55 trace_event::TraceConfig(categories, trace_event::RECORD_UNTIL_FULL),
56 trace_event::TraceLog::RECORDING_MODE);
59 void TraceToFile::WriteFileHeader() {
60 const char str[] = "{\"traceEvents\": [";
61 WriteFile(path_, str, static_cast<int>(strlen(str)));
64 void TraceToFile::AppendFileFooter() {
65 const char str[] = "]}";
66 AppendToFile(path_, str, static_cast<int>(strlen(str)));
69 void TraceToFile::TraceOutputCallback(const std::string& data) {
70 bool ret = AppendToFile(path_, data.c_str(), static_cast<int>(data.size()));
71 DCHECK(ret);
74 static void OnTraceDataCollected(
75 Closure quit_closure,
76 trace_event::TraceResultBuffer* buffer,
77 const scoped_refptr<RefCountedString>& json_events_str,
78 bool has_more_events) {
79 buffer->AddFragment(json_events_str->data());
80 if (!has_more_events)
81 quit_closure.Run();
84 void TraceToFile::EndTracingIfNeeded() {
85 if (!started_)
86 return;
87 started_ = false;
89 trace_event::TraceLog::GetInstance()->SetDisabled();
91 trace_event::TraceResultBuffer buffer;
92 buffer.SetOutputCallback(
93 Bind(&TraceToFile::TraceOutputCallback, Unretained(this)));
95 RunLoop run_loop;
96 trace_event::TraceLog::GetInstance()->Flush(
97 Bind(&OnTraceDataCollected, run_loop.QuitClosure(), Unretained(&buffer)));
98 run_loop.Run();
100 AppendFileFooter();
103 } // namespace test
104 } // namespace base