| OLD | NEW |
| 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/test/trace_to_file.h" | 5 #include "base/test/trace_to_file.h" |
| 6 | 6 |
| 7 #include "base/base_switches.h" | 7 #include "base/base_switches.h" |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 BeginTracing(path, filter); | 44 BeginTracing(path, filter); |
| 45 } | 45 } |
| 46 | 46 |
| 47 void TraceToFile::BeginTracing(const FilePath& path, | 47 void TraceToFile::BeginTracing(const FilePath& path, |
| 48 const std::string& categories) { | 48 const std::string& categories) { |
| 49 DCHECK(!started_); | 49 DCHECK(!started_); |
| 50 started_ = true; | 50 started_ = true; |
| 51 path_ = path; | 51 path_ = path; |
| 52 WriteFileHeader(); | 52 WriteFileHeader(); |
| 53 | 53 |
| 54 debug::TraceLog::GetInstance()->SetEnabled( | 54 trace_event::TraceLog::GetInstance()->SetEnabled( |
| 55 debug::CategoryFilter(categories), | 55 trace_event::CategoryFilter(categories), |
| 56 debug::TraceLog::RECORDING_MODE, | 56 trace_event::TraceLog::RECORDING_MODE, |
| 57 debug::TraceOptions(debug::RECORD_UNTIL_FULL)); | 57 trace_event::TraceOptions(trace_event::RECORD_UNTIL_FULL)); |
| 58 } | 58 } |
| 59 | 59 |
| 60 void TraceToFile::WriteFileHeader() { | 60 void TraceToFile::WriteFileHeader() { |
| 61 const char str[] = "{\"traceEvents\": ["; | 61 const char str[] = "{\"traceEvents\": ["; |
| 62 WriteFile(path_, str, static_cast<int>(strlen(str))); | 62 WriteFile(path_, str, static_cast<int>(strlen(str))); |
| 63 } | 63 } |
| 64 | 64 |
| 65 void TraceToFile::AppendFileFooter() { | 65 void TraceToFile::AppendFileFooter() { |
| 66 const char str[] = "]}"; | 66 const char str[] = "]}"; |
| 67 AppendToFile(path_, str, static_cast<int>(strlen(str))); | 67 AppendToFile(path_, str, static_cast<int>(strlen(str))); |
| 68 } | 68 } |
| 69 | 69 |
| 70 void TraceToFile::TraceOutputCallback(const std::string& data) { | 70 void TraceToFile::TraceOutputCallback(const std::string& data) { |
| 71 bool ret = AppendToFile(path_, data.c_str(), static_cast<int>(data.size())); | 71 bool ret = AppendToFile(path_, data.c_str(), static_cast<int>(data.size())); |
| 72 DCHECK(ret); | 72 DCHECK(ret); |
| 73 } | 73 } |
| 74 | 74 |
| 75 static void OnTraceDataCollected( | 75 static void OnTraceDataCollected( |
| 76 Closure quit_closure, | 76 Closure quit_closure, |
| 77 debug::TraceResultBuffer* buffer, | 77 trace_event::TraceResultBuffer* buffer, |
| 78 const scoped_refptr<RefCountedString>& json_events_str, | 78 const scoped_refptr<RefCountedString>& json_events_str, |
| 79 bool has_more_events) { | 79 bool has_more_events) { |
| 80 buffer->AddFragment(json_events_str->data()); | 80 buffer->AddFragment(json_events_str->data()); |
| 81 if (!has_more_events) | 81 if (!has_more_events) |
| 82 quit_closure.Run(); | 82 quit_closure.Run(); |
| 83 } | 83 } |
| 84 | 84 |
| 85 void TraceToFile::EndTracingIfNeeded() { | 85 void TraceToFile::EndTracingIfNeeded() { |
| 86 if (!started_) | 86 if (!started_) |
| 87 return; | 87 return; |
| 88 started_ = false; | 88 started_ = false; |
| 89 | 89 |
| 90 debug::TraceLog::GetInstance()->SetDisabled(); | 90 trace_event::TraceLog::GetInstance()->SetDisabled(); |
| 91 | 91 |
| 92 debug::TraceResultBuffer buffer; | 92 trace_event::TraceResultBuffer buffer; |
| 93 buffer.SetOutputCallback( | 93 buffer.SetOutputCallback( |
| 94 Bind(&TraceToFile::TraceOutputCallback, Unretained(this))); | 94 Bind(&TraceToFile::TraceOutputCallback, Unretained(this))); |
| 95 | 95 |
| 96 RunLoop run_loop; | 96 RunLoop run_loop; |
| 97 debug::TraceLog::GetInstance()->Flush( | 97 trace_event::TraceLog::GetInstance()->Flush( |
| 98 Bind(&OnTraceDataCollected, run_loop.QuitClosure(), Unretained(&buffer))); | 98 Bind(&OnTraceDataCollected, run_loop.QuitClosure(), Unretained(&buffer))); |
| 99 run_loop.Run(); | 99 run_loop.Run(); |
| 100 | 100 |
| 101 AppendFileFooter(); | 101 AppendFileFooter(); |
| 102 } | 102 } |
| 103 | 103 |
| 104 } // namespace test | 104 } // namespace test |
| 105 } // namespace base | 105 } // namespace base |
| OLD | NEW |