Browse Source

create net log instance only when needed

deepak1556 10 years ago
parent
commit
15255944b6
2 changed files with 20 additions and 18 deletions
  1. 13 16
      brightray/browser/net_log.cc
  2. 7 2
      brightray/browser/url_request_context_getter.cc

+ 13 - 16
brightray/browser/net_log.cc

@@ -21,7 +21,6 @@ base::Value* GetConstants() {
   // Adding client information to constants dictionary.
   base::DictionaryValue* client_info = new base::DictionaryValue();
 
-  client_info->SetString("name", "Electron");
   client_info->SetString("command_line",
       base::CommandLine::ForCurrentProcess()->GetCommandLineString());
 
@@ -38,21 +37,19 @@ NetLog::NetLog(net::URLRequestContext* context)
     : added_events_(false),
       context_(context) {
   auto command_line = base::CommandLine::ForCurrentProcess();
-
-  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 (!log_file_)
-      LOG(ERROR) << "Could not open file: " << log_path.value()
-                 << "for net logging";
-
+  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";
+  } else {
     std::string json;
     base::JSONWriter::Write(GetConstants(), &json);
     fprintf(log_file_.get(), "{\"constants\": %s, \n", json.c_str());

+ 7 - 2
brightray/browser/url_request_context_getter.cc

@@ -148,8 +148,13 @@ net::URLRequestContext* URLRequestContextGetter::GetURLRequestContext() {
   auto& command_line = *base::CommandLine::ForCurrentProcess();
   if (!url_request_context_.get()) {
     url_request_context_.reset(new net::URLRequestContext);
-    net_log_.reset(new NetLog(url_request_context_.get()));
-    url_request_context_->set_net_log(net_log_.get());
+
+    // --log-net-log
+    if (command_line.HasSwitch(switches::kLogNetLog)) {
+      net_log_.reset(new NetLog(url_request_context_.get()));
+      url_request_context_->set_net_log(net_log_.get());
+    }
+
     network_delegate_.reset(delegate_->CreateNetworkDelegate());
     url_request_context_->set_network_delegate(network_delegate_.get());