Chromium Code Reviews| Index: base/debug/trace_to_file.cc |
| diff --git a/base/debug/trace_to_file.cc b/base/debug/trace_to_file.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..8a7608a0e5ba2c6e0688bd340c8147ce27a0edac |
| --- /dev/null |
| +++ b/base/debug/trace_to_file.cc |
| @@ -0,0 +1,126 @@ |
| +// Copyright (c) 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/debug/trace_to_file.h" |
| + |
| +#include "base/base_switches.h" |
| +#include "base/command_line.h" |
| +#include "base/debug/trace_event_impl.h" |
| +#include "base/files/file_util.h" |
| +#include "base/message_loop/message_loop.h" |
| +#include "base/synchronization/waitable_event.h" |
| + |
| +namespace base { |
| +namespace debug { |
| + |
| +TraceToFile::TraceToFile() : started_(false) { |
| +} |
| + |
| +TraceToFile::~TraceToFile() { |
| + EndTracingIfNeeded(); |
| +} |
| + |
| +void TraceToFile::BeginTracingFromCommandLineOptions() { |
| + DCHECK(CommandLine::InitializedForCurrentProcess()); |
| + DCHECK(!started_); |
| + |
| + if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kTraceToFile)) |
| + return; |
| + |
| + // 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.
|
| + // TraceEventImpl |
| + std::string filter = CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| + switches::kTraceToFile); |
| + |
| + base::FilePath path; |
| + if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kTraceToFileName)) { |
| + path = base::FilePath(CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| + switches::kTraceToFileName)); |
| + } else { |
| + path = base::FilePath("trace.json"); |
| + } |
| + |
| + LOG(ERROR) << "Start " << switches::kTraceToFile << " with CategoryFilter '" |
| + << filter << "' to file '" << path.value() << "'."; |
| + |
| + BeginTracing(path, filter); |
| +} |
| + |
| +void TraceToFile::BeginTracing(const base::FilePath& path, |
| + const std::string& categories) { |
| + DCHECK(!started_); |
| + started_ = true; |
| + path_ = path; |
| + WriteFileHeader(); |
| + |
| + base::debug::TraceLog::GetInstance()->SetEnabled( |
| + CategoryFilter(categories), |
| + TraceLog::RECORDING_MODE, |
| + TraceOptions(RECORD_UNTIL_FULL)); |
| +} |
| + |
| +void TraceToFile::WriteFileHeader() { |
| + const char* str = "{\"traceEvents\": ["; |
| + WriteFile(path_, str, strlen(str)); |
| +} |
| + |
| +void TraceToFile::AppendFileFooter() { |
| + const char* str = "]}"; |
| + AppendToFile(path_, str, strlen(str)); |
| +} |
| + |
| +void TraceToFile::TraceOutputCallback(const std::string& data) { |
| + int ret = base::AppendToFile(path_, data.c_str(), data.size()); |
| + DCHECK_NE(-1, ret); |
| +} |
| + |
| +static void OnTraceDataCollected( |
| + bool* all_data_collected, |
| + base::debug::TraceResultBuffer* buffer, |
| + const scoped_refptr<base::RefCountedString>& json_events_str, |
| + bool has_more_events) { |
| + buffer->AddFragment(json_events_str->data()); |
| + if (!has_more_events) |
| + *all_data_collected = true; |
| +} |
| + |
| +static void RepostUntilAllDataCollected(base::MessageLoop* loop, |
| + bool* all_data_collected) { |
| + if (*all_data_collected) |
| + return; |
| + loop->PostDelayedTask(FROM_HERE, |
| + base::Bind(&RepostUntilAllDataCollected, |
| + base::Unretained(loop), |
| + base::Unretained(all_data_collected)), |
| + base::TimeDelta::FromMilliseconds(1)); |
| +} |
| + |
| +void TraceToFile::EndTracingIfNeeded() { |
| + if (!started_) |
| + return; |
| + started_ = false; |
| + |
| + base::debug::TraceLog::GetInstance()->SetDisabled(); |
| + |
| + base::debug::TraceResultBuffer buffer; |
| + buffer.SetOutputCallback( |
| + base::Bind(&TraceToFile::TraceOutputCallback, base::Unretained(this))); |
| + |
| + 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
|
| + bool all_data_collected = false; |
| + RepostUntilAllDataCollected(&loop, &all_data_collected); |
| + |
| + base::debug::TraceLog::GetInstance()->Flush( |
| + base::Bind(&OnTraceDataCollected, |
| + base::Unretained(&all_data_collected), |
| + base::Unretained(&buffer))); |
| + loop.RunUntilIdle(); |
| + |
| + DCHECK(all_data_collected); |
| + |
| + AppendFileFooter(); |
| +} |
| + |
| +} // namespace debug |
| +} // namespace base |