Use UintToString() for unsigned values.
[chromium-blink-merge.git] / base / test / trace_to_file.cc
blob85fff71e34a855230ded31f569a1225a78e3eaa4
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_buffer.h"
13 #include "base/trace_event/trace_log.h"
15 namespace base {
16 namespace test {
18 TraceToFile::TraceToFile() : started_(false) {
21 TraceToFile::~TraceToFile() {
22 EndTracingIfNeeded();
25 void TraceToFile::BeginTracingFromCommandLineOptions() {
26 DCHECK(CommandLine::InitializedForCurrentProcess());
27 DCHECK(!started_);
29 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kTraceToFile))
30 return;
32 // Empty filter (i.e. just --trace-to-file) turns into default categories in
33 // TraceEventImpl
34 std::string filter = CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
35 switches::kTraceToFile);
37 FilePath path;
38 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kTraceToFileName)) {
39 path = FilePath(CommandLine::ForCurrentProcess()
40 ->GetSwitchValuePath(switches::kTraceToFileName));
41 } else {
42 path = FilePath(FILE_PATH_LITERAL("trace.json"));
45 BeginTracing(path, filter);
48 void TraceToFile::BeginTracing(const FilePath& path,
49 const std::string& categories) {
50 DCHECK(!started_);
51 started_ = true;
52 path_ = path;
53 WriteFileHeader();
55 trace_event::TraceLog::GetInstance()->SetEnabled(
56 trace_event::TraceConfig(categories, trace_event::RECORD_UNTIL_FULL),
57 trace_event::TraceLog::RECORDING_MODE);
60 void TraceToFile::WriteFileHeader() {
61 const char str[] = "{\"traceEvents\": [";
62 WriteFile(path_, str, static_cast<int>(strlen(str)));
65 void TraceToFile::AppendFileFooter() {
66 const char str[] = "]}";
67 AppendToFile(path_, str, static_cast<int>(strlen(str)));
70 void TraceToFile::TraceOutputCallback(const std::string& data) {
71 bool ret = AppendToFile(path_, data.c_str(), static_cast<int>(data.size()));
72 DCHECK(ret);
75 static void OnTraceDataCollected(
76 Closure quit_closure,
77 trace_event::TraceResultBuffer* buffer,
78 const scoped_refptr<RefCountedString>& json_events_str,
79 bool has_more_events) {
80 buffer->AddFragment(json_events_str->data());
81 if (!has_more_events)
82 quit_closure.Run();
85 void TraceToFile::EndTracingIfNeeded() {
86 if (!started_)
87 return;
88 started_ = false;
90 trace_event::TraceLog::GetInstance()->SetDisabled();
92 trace_event::TraceResultBuffer buffer;
93 buffer.SetOutputCallback(
94 Bind(&TraceToFile::TraceOutputCallback, Unretained(this)));
96 RunLoop run_loop;
97 trace_event::TraceLog::GetInstance()->Flush(
98 Bind(&OnTraceDataCollected, run_loop.QuitClosure(), Unretained(&buffer)));
99 run_loop.Run();
101 AppendFileFooter();
104 } // namespace test
105 } // namespace base