electron/brightray/browser/net_log.cc

64 lines
1.8 KiB
C++
Raw Normal View History

2015-06-05 14:54:38 +00:00
// Copyright (c) 2015 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "browser/net_log.h"
#include "base/command_line.h"
2015-08-11 10:29:55 +00:00
#include "base/files/file_path.h"
#include "base/values.h"
2015-06-05 14:54:38 +00:00
#include "content/public/common/content_switches.h"
#include "net/log/net_log_util.h"
2015-08-11 10:29:55 +00:00
namespace brightray {
2015-06-05 14:54:38 +00:00
2015-06-06 09:03:07 +00:00
namespace {
std::unique_ptr<base::DictionaryValue> GetConstants() {
std::unique_ptr<base::DictionaryValue> constants = net::GetNetConstants();
2015-06-06 09:03:07 +00:00
// Adding client information to constants dictionary.
base::DictionaryValue* client_info = new base::DictionaryValue();
2015-06-09 01:51:38 +00:00
client_info->SetString(
"command_line",
2015-06-06 09:03:07 +00:00
base::CommandLine::ForCurrentProcess()->GetCommandLineString());
constants->Set("clientInfo", client_info);
2015-06-09 01:51:38 +00:00
return constants;
2015-06-06 09:03:07 +00:00
}
} // namespace
2015-08-11 10:29:55 +00:00
NetLog::NetLog() {
}
NetLog::~NetLog() {
}
void NetLog::StartLogging(net::URLRequestContext* url_request_context) {
2015-06-05 14:54:38 +00:00
auto command_line = base::CommandLine::ForCurrentProcess();
2015-08-11 10:29:55 +00:00
if (!command_line->HasSwitch(switches::kLogNetLog))
return;
base::FilePath log_path = command_line->GetSwitchValuePath(switches::kLogNetLog);
#if defined(OS_WIN)
log_file_.reset(_wfopen(log_path.value().c_str(), L"w"));
#elif defined(OS_POSIX)
log_file_.reset(fopen(log_path.value().c_str(), "w"));
#endif
if (!log_file_) {
LOG(ERROR) << "Could not open file: " << log_path.value()
<< "for net logging";
return;
2015-06-05 14:54:38 +00:00
}
std::unique_ptr<base::Value> constants(GetConstants());
write_to_file_observer_.StartObserving(this,
2016-03-08 11:59:29 +00:00
std::move(log_file_),
constants.get(),
url_request_context);
2015-06-05 14:54:38 +00:00
}
} // namespace brightray