1 // Copyright 2015 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 "chromecast/browser/cast_net_log.h"
9 #include "base/command_line.h"
10 #include "base/files/file_path.h"
11 #include "base/files/scoped_file.h"
12 #include "base/values.h"
13 #include "content/public/common/content_switches.h"
14 #include "net/log/net_log_util.h"
15 #include "net/log/write_to_file_net_log_observer.h"
17 namespace chromecast
{
21 base::DictionaryValue
* GetShellConstants() {
22 scoped_ptr
<base::DictionaryValue
> constants_dict
= net::GetNetConstants();
24 // Add a dictionary with client information
25 base::DictionaryValue
* dict
= new base::DictionaryValue();
27 dict
->SetString("name", "cast_shell");
30 base::CommandLine::ForCurrentProcess()->GetCommandLineString());
32 constants_dict
->Set("clientInfo", dict
);
34 return constants_dict
.release();
39 CastNetLog::CastNetLog() {
40 // TODO(derekjchow): This code is virtually identical to ShellNetLog which is
41 // nearly identical to code in ChromeNetLog. Consider merging the code.
42 const base::CommandLine
* command_line
=
43 base::CommandLine::ForCurrentProcess();
45 if (command_line
->HasSwitch(switches::kLogNetLog
)) {
46 base::FilePath log_path
=
47 command_line
->GetSwitchValuePath(switches::kLogNetLog
);
48 // Much like logging.h, bypass threading restrictions by using fopen
49 // directly. Have to write on a thread that's shutdown to handle events on
50 // shutdown properly, and posting events to another thread as they occur
51 // would result in an unbounded buffer size, so not much can be gained by
52 // doing this on another thread. It's only used when debugging, so
53 // performance is not a big concern.
54 base::ScopedFILE file
;
55 file
.reset(fopen(log_path
.value().c_str(), "w"));
58 LOG(ERROR
) << "Could not open file " << log_path
.value()
59 << " for net logging";
61 scoped_ptr
<base::Value
> constants(GetShellConstants());
62 write_to_file_observer_
.reset(new net::WriteToFileNetLogObserver());
63 write_to_file_observer_
->StartObserving(this, file
.Pass(),
64 constants
.get(), nullptr);
69 CastNetLog::~CastNetLog() {
70 // Remove the observer we own before we're destroyed.
71 if (write_to_file_observer_
)
72 write_to_file_observer_
->StopObserving(nullptr);
75 } // namespace chromecast