Chromium Code Reviews| 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/debug/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/message_loop/message_loop.h" | |
| 12 #include "base/synchronization/waitable_event.h" | |
| 13 | |
| 14 namespace base { | |
| 15 namespace debug { | |
| 16 | |
| 17 TraceToFile::TraceToFile() : started_(false) { | |
| 18 } | |
| 19 | |
| 20 TraceToFile::~TraceToFile() { | |
| 21 EndTracingIfNeeded(); | |
| 22 } | |
| 23 | |
| 24 void TraceToFile::BeginTracingFromCommandLineOptions() { | |
| 25 DCHECK(CommandLine::InitializedForCurrentProcess()); | |
| 26 DCHECK(!started_); | |
| 27 | |
| 28 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kTraceToFile)) | |
| 29 return; | |
| 30 | |
| 31 // 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.
| |
| 32 // TraceEventImpl | |
| 33 std::string filter = CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | |
| 34 switches::kTraceToFile); | |
| 35 | |
| 36 base::FilePath path; | |
| 37 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kTraceToFileName)) { | |
| 38 path = base::FilePath(CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | |
| 39 switches::kTraceToFileName)); | |
| 40 } else { | |
| 41 path = base::FilePath("trace.json"); | |
| 42 } | |
| 43 | |
| 44 LOG(ERROR) << "Start " << switches::kTraceToFile << " with CategoryFilter '" | |
| 45 << filter << "' to file '" << path.value() << "'."; | |
| 46 | |
| 47 BeginTracing(path, filter); | |
| 48 } | |
| 49 | |
| 50 void TraceToFile::BeginTracing(const base::FilePath& path, | |
| 51 const std::string& categories) { | |
| 52 DCHECK(!started_); | |
| 53 started_ = true; | |
| 54 path_ = path; | |
| 55 WriteFileHeader(); | |
| 56 | |
| 57 base::debug::TraceLog::GetInstance()->SetEnabled( | |
| 58 CategoryFilter(categories), | |
| 59 TraceLog::RECORDING_MODE, | |
| 60 TraceOptions(RECORD_UNTIL_FULL)); | |
| 61 } | |
| 62 | |
| 63 void TraceToFile::WriteFileHeader() { | |
| 64 const char* str = "{\"traceEvents\": ["; | |
| 65 WriteFile(path_, str, strlen(str)); | |
| 66 } | |
| 67 | |
| 68 void TraceToFile::AppendFileFooter() { | |
| 69 const char* str = "]}"; | |
| 70 AppendToFile(path_, str, strlen(str)); | |
| 71 } | |
| 72 | |
| 73 void TraceToFile::TraceOutputCallback(const std::string& data) { | |
| 74 int ret = base::AppendToFile(path_, data.c_str(), data.size()); | |
| 75 DCHECK_NE(-1, ret); | |
| 76 } | |
| 77 | |
| 78 static void OnTraceDataCollected( | |
| 79 bool* all_data_collected, | |
| 80 base::debug::TraceResultBuffer* buffer, | |
| 81 const scoped_refptr<base::RefCountedString>& json_events_str, | |
| 82 bool has_more_events) { | |
| 83 buffer->AddFragment(json_events_str->data()); | |
| 84 if (!has_more_events) | |
| 85 *all_data_collected = true; | |
| 86 } | |
| 87 | |
| 88 static void RepostUntilAllDataCollected(base::MessageLoop* loop, | |
| 89 bool* all_data_collected) { | |
| 90 if (*all_data_collected) | |
| 91 return; | |
| 92 loop->PostDelayedTask(FROM_HERE, | |
| 93 base::Bind(&RepostUntilAllDataCollected, | |
| 94 base::Unretained(loop), | |
| 95 base::Unretained(all_data_collected)), | |
| 96 base::TimeDelta::FromMilliseconds(1)); | |
| 97 } | |
| 98 | |
| 99 void TraceToFile::EndTracingIfNeeded() { | |
| 100 if (!started_) | |
| 101 return; | |
| 102 started_ = false; | |
| 103 | |
| 104 base::debug::TraceLog::GetInstance()->SetDisabled(); | |
| 105 | |
| 106 base::debug::TraceResultBuffer buffer; | |
| 107 buffer.SetOutputCallback( | |
| 108 base::Bind(&TraceToFile::TraceOutputCallback, base::Unretained(this))); | |
| 109 | |
| 110 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
| |
| 111 bool all_data_collected = false; | |
| 112 RepostUntilAllDataCollected(&loop, &all_data_collected); | |
| 113 | |
| 114 base::debug::TraceLog::GetInstance()->Flush( | |
| 115 base::Bind(&OnTraceDataCollected, | |
| 116 base::Unretained(&all_data_collected), | |
| 117 base::Unretained(&buffer))); | |
| 118 loop.RunUntilIdle(); | |
| 119 | |
| 120 DCHECK(all_data_collected); | |
| 121 | |
| 122 AppendFileFooter(); | |
| 123 } | |
| 124 | |
| 125 } // namespace debug | |
| 126 } // namespace base | |
| OLD | NEW |