create netlog for devtoolsnetlogobserver

This commit is contained in:
Robo 2015-07-14 23:12:52 +05:30
parent 681e868a5b
commit 7496cb29fe
7 changed files with 44 additions and 31 deletions

View file

@ -36,44 +36,48 @@ NetLog::NetLog(net::URLRequestContext* context)
: added_events_(false),
context_(context) {
auto command_line = base::CommandLine::ForCurrentProcess();
base::FilePath log_path =
command_line->GetSwitchValuePath(switches::kLogNetLog);
if (command_line->HasSwitch(switches::kLogNetLog)) {
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 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";
} else {
std::string json;
scoped_ptr<base::Value> constants(GetConstants().Pass());
base::JSONWriter::Write(constants.get(), &json);
fprintf(log_file_.get(), "{\"constants\": %s, \n", json.c_str());
fprintf(log_file_.get(), "\"events\": [\n");
if (!log_file_) {
LOG(ERROR) << "Could not open file: " << log_path.value()
<< "for net logging";
} else {
std::string json;
scoped_ptr<base::Value> constants(GetConstants().Pass());
base::JSONWriter::Write(constants.get(), &json);
fprintf(log_file_.get(), "{\"constants\": %s, \n", json.c_str());
fprintf(log_file_.get(), "\"events\": [\n");
if (context_) {
DCHECK(context_->CalledOnValidThread());
if (context_) {
DCHECK(context_->CalledOnValidThread());
std::set<net::URLRequestContext*> contexts;
contexts.insert(context_);
std::set<net::URLRequestContext*> contexts;
contexts.insert(context_);
net::CreateNetLogEntriesForActiveObjects(contexts, this);
net::CreateNetLogEntriesForActiveObjects(contexts, this);
}
DeprecatedAddObserver(this, net::NetLog::LogLevel::LOG_STRIP_PRIVATE_DATA);
}
DeprecatedAddObserver(this, net::NetLog::LogLevel::LOG_STRIP_PRIVATE_DATA);
}
}
NetLog::~NetLog() {
DeprecatedRemoveObserver(this);
if (log_file_) {
DeprecatedRemoveObserver(this);
// Ending events array.
fprintf(log_file_.get(), "]}");
log_file_.reset();
// Ending events array.
fprintf(log_file_.get(), "]}");
log_file_.reset();
}
}
void NetLog::OnAddEntry(const net::NetLog::Entry& entry) {