OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "base/test/trace_to_file.h" | |
6 | |
7 #include "base/base_switches.h" | |
8 #include "base/command_line.h" | |
9 #include "base/debug/trace_event_impl.h" | |
10 #include "base/files/file_util.h" | |
11 #include "base/run_loop.h" | |
12 | |
13 namespace base { | |
14 namespace test { | |
15 | |
16 TraceToFile::TraceToFile() : started_(false) { | |
17 } | |
18 | |
19 TraceToFile::~TraceToFile() { | |
20 EndTracingIfNeeded(); | |
21 } | |
22 | |
23 void TraceToFile::BeginTracingFromCommandLineOptions() { | |
24 DCHECK(CommandLine::InitializedForCurrentProcess()); | |
25 DCHECK(!started_); | |
26 | |
27 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kTraceToFile)) | |
28 return; | |
29 | |
30 // Empty filter (i.e. just --trace-to-file) turns into default categories in | |
31 // TraceEventImpl | |
32 std::string filter = CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | |
33 switches::kTraceToFile); | |
34 | |
35 FilePath path; | |
36 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kTraceToFileName)) { | |
37 path = FilePath(CommandLine::ForCurrentProcess() | |
38 ->GetSwitchValueASCII(switches::kTraceToFileName)); | |
39 } else { | |
40 path = FilePath("trace.json"); | |
41 } | |
42 | |
43 LOG(ERROR) << "Start " << switches::kTraceToFile << " with CategoryFilter '" | |
Nico
2014/09/24 23:47:30
This is not an error. Don't log unless something's
enne (OOO)
2014/09/24 23:58:57
Done.
| |
44 << filter << "' to file '" << path.value() << "'."; | |
45 | |
46 BeginTracing(path, filter); | |
47 } | |
48 | |
49 void TraceToFile::BeginTracing(const FilePath& path, | |
50 const std::string& categories) { | |
51 DCHECK(!started_); | |
52 started_ = true; | |
53 path_ = path; | |
54 WriteFileHeader(); | |
55 | |
56 debug::TraceLog::GetInstance()->SetEnabled( | |
57 debug::CategoryFilter(categories), | |
58 debug::TraceLog::RECORDING_MODE, | |
59 debug::TraceOptions(debug::RECORD_UNTIL_FULL)); | |
60 } | |
61 | |
62 void TraceToFile::WriteFileHeader() { | |
63 const char* str = "{\"traceEvents\": ["; | |
Nico
2014/09/24 23:47:30
const char str[]
enne (OOO)
2014/09/24 23:58:56
Done.
| |
64 WriteFile(path_, str, strlen(str)); | |
65 } | |
66 | |
67 void TraceToFile::AppendFileFooter() { | |
68 const char* str = "]}"; | |
Nico
2014/09/24 23:47:30
ditto
enne (OOO)
2014/09/24 23:58:56
Done.
| |
69 AppendToFile(path_, str, strlen(str)); | |
70 } | |
71 | |
72 void TraceToFile::TraceOutputCallback(const std::string& data) { | |
73 int ret = AppendToFile(path_, data.c_str(), data.size()); | |
74 DCHECK_NE(-1, ret); | |
75 } | |
76 | |
77 static void OnTraceDataCollected( | |
78 Closure quit_closure, | |
79 debug::TraceResultBuffer* buffer, | |
80 const scoped_refptr<RefCountedString>& json_events_str, | |
81 bool has_more_events) { | |
82 buffer->AddFragment(json_events_str->data()); | |
83 if (!has_more_events) | |
84 quit_closure.Run(); | |
85 } | |
86 | |
87 void TraceToFile::EndTracingIfNeeded() { | |
88 if (!started_) | |
89 return; | |
90 started_ = false; | |
91 | |
92 debug::TraceLog::GetInstance()->SetDisabled(); | |
93 | |
94 debug::TraceResultBuffer buffer; | |
95 buffer.SetOutputCallback( | |
96 Bind(&TraceToFile::TraceOutputCallback, Unretained(this))); | |
97 | |
98 RunLoop run_loop; | |
99 debug::TraceLog::GetInstance()->Flush( | |
100 Bind(&OnTraceDataCollected, run_loop.QuitClosure(), Unretained(&buffer))); | |
101 run_loop.Run(); | |
102 | |
103 AppendFileFooter(); | |
104 } | |
105 | |
106 } // namespace test | |
107 } // namespace base | |
OLD | NEW |