1 // Copyright 2013 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 "content/shell/browser/shell_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"
21 base::DictionaryValue
* GetShellConstants(const std::string
& app_name
) {
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", app_name
);
30 base::CommandLine::ForCurrentProcess()->GetCommandLineString());
32 constants_dict
->Set("clientInfo", dict
);
34 return constants_dict
.release();
39 ShellNetLog::ShellNetLog(const std::string
& app_name
) {
40 // TODO(mmenke): Other than a different set of constants, this code is
41 // 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
;
56 file
.reset(_wfopen(log_path
.value().c_str(), L
"w"));
57 #elif defined(OS_POSIX)
58 file
.reset(fopen(log_path
.value().c_str(), "w"));
62 LOG(ERROR
) << "Could not open file " << log_path
.value()
63 << " for net logging";
65 scoped_ptr
<base::Value
> constants(GetShellConstants(app_name
));
66 write_to_file_observer_
.reset(new net::WriteToFileNetLogObserver());
67 write_to_file_observer_
->StartObserving(this, file
.Pass(),
68 constants
.get(), nullptr);
73 ShellNetLog::~ShellNetLog() {
74 // Remove the observer we own before we're destroyed.
75 if (write_to_file_observer_
)
76 write_to_file_observer_
->StopObserving(nullptr);
79 } // namespace content