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/values.h"
12 #include "content/public/common/content_switches.h"
13 #include "net/base/net_log_logger.h"
19 base::DictionaryValue
* GetShellConstants(const std::string
& app_name
) {
20 base::DictionaryValue
* constants_dict
= net::NetLogLogger::GetConstants();
22 // Add a dictionary with client information
23 base::DictionaryValue
* dict
= new base::DictionaryValue();
25 dict
->SetString("name", app_name
);
26 dict
->SetString("command_line",
27 CommandLine::ForCurrentProcess()->GetCommandLineString());
29 constants_dict
->Set("clientInfo", dict
);
31 return constants_dict
;
36 ShellNetLog::ShellNetLog(const std::string
& app_name
) {
37 const CommandLine
* command_line
= CommandLine::ForCurrentProcess();
39 if (command_line
->HasSwitch(switches::kLogNetLog
)) {
40 base::FilePath log_path
=
41 command_line
->GetSwitchValuePath(switches::kLogNetLog
);
42 // Much like logging.h, bypass threading restrictions by using fopen
43 // directly. Have to write on a thread that's shutdown to handle events on
44 // shutdown properly, and posting events to another thread as they occur
45 // would result in an unbounded buffer size, so not much can be gained by
46 // doing this on another thread. It's only used when debugging, so
47 // performance is not a big concern.
50 file
= _wfopen(log_path
.value().c_str(), L
"w");
51 #elif defined(OS_POSIX)
52 file
= fopen(log_path
.value().c_str(), "w");
56 LOG(ERROR
) << "Could not open file " << log_path
.value()
57 << " for net logging";
59 scoped_ptr
<base::Value
> constants(GetShellConstants(app_name
));
60 net_log_logger_
.reset(new net::NetLogLogger(file
, *constants
));
61 net_log_logger_
->StartObserving(this);
66 ShellNetLog::~ShellNetLog() {
67 // Remove the observer we own before we're destroyed.
69 RemoveThreadSafeObserver(net_log_logger_
.get());
72 } // namespace content