| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/net/chrome_net_log.h" | 5 #include "chrome/browser/net/chrome_net_log.h" |
| 6 | 6 |
| 7 #include <stdio.h> | 7 #include <stdio.h> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/files/scoped_file.h" |
| 10 #include "base/logging.h" | 11 #include "base/logging.h" |
| 11 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 12 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
| 13 #include "base/values.h" | 14 #include "base/values.h" |
| 14 #include "chrome/browser/net/net_log_temp_file.h" | 15 #include "chrome/browser/net/net_log_temp_file.h" |
| 15 #include "chrome/browser/ui/webui/net_internals/net_internals_ui.h" | 16 #include "chrome/browser/ui/webui/net_internals/net_internals_ui.h" |
| 16 #include "chrome/common/chrome_switches.h" | 17 #include "chrome/common/chrome_switches.h" |
| 17 #include "content/public/common/content_switches.h" | 18 #include "content/public/common/content_switches.h" |
| 18 #include "net/base/net_log_logger.h" | 19 #include "net/base/net_log_logger.h" |
| 19 #include "net/base/trace_net_log_observer.h" | 20 #include "net/base/trace_net_log_observer.h" |
| 20 | 21 |
| 21 ChromeNetLog::ChromeNetLog() | 22 ChromeNetLog::ChromeNetLog() |
| 22 : net_log_temp_file_(new NetLogTempFile(this)) { | 23 : net_log_temp_file_(new NetLogTempFile(this)) { |
| 23 const base::CommandLine* command_line = | 24 const base::CommandLine* command_line = |
| 24 base::CommandLine::ForCurrentProcess(); | 25 base::CommandLine::ForCurrentProcess(); |
| 25 | 26 |
| 26 if (command_line->HasSwitch(switches::kLogNetLog)) { | 27 if (command_line->HasSwitch(switches::kLogNetLog)) { |
| 27 base::FilePath log_path = | 28 base::FilePath log_path = |
| 28 command_line->GetSwitchValuePath(switches::kLogNetLog); | 29 command_line->GetSwitchValuePath(switches::kLogNetLog); |
| 29 // Much like logging.h, bypass threading restrictions by using fopen | 30 // Much like logging.h, bypass threading restrictions by using fopen |
| 30 // directly. Have to write on a thread that's shutdown to handle events on | 31 // directly. Have to write on a thread that's shutdown to handle events on |
| 31 // shutdown properly, and posting events to another thread as they occur | 32 // shutdown properly, and posting events to another thread as they occur |
| 32 // would result in an unbounded buffer size, so not much can be gained by | 33 // would result in an unbounded buffer size, so not much can be gained by |
| 33 // doing this on another thread. It's only used when debugging Chrome, so | 34 // doing this on another thread. It's only used when debugging Chrome, so |
| 34 // performance is not a big concern. | 35 // performance is not a big concern. |
| 35 FILE* file = NULL; | 36 base::ScopedFILE file; |
| 36 #if defined(OS_WIN) | 37 #if defined(OS_WIN) |
| 37 file = _wfopen(log_path.value().c_str(), L"w"); | 38 file.reset(_wfopen(log_path.value().c_str(), L"w")); |
| 38 #elif defined(OS_POSIX) | 39 #elif defined(OS_POSIX) |
| 39 file = fopen(log_path.value().c_str(), "w"); | 40 file.reset(fopen(log_path.value().c_str(), "w")); |
| 40 #endif | 41 #endif |
| 41 | 42 |
| 42 if (file == NULL) { | 43 if (!file) { |
| 43 LOG(ERROR) << "Could not open file " << log_path.value() | 44 LOG(ERROR) << "Could not open file " << log_path.value() |
| 44 << " for net logging"; | 45 << " for net logging"; |
| 45 } else { | 46 } else { |
| 46 scoped_ptr<base::Value> constants(NetInternalsUI::GetConstants()); | 47 scoped_ptr<base::Value> constants(NetInternalsUI::GetConstants()); |
| 47 net_log_logger_.reset(new net::NetLogLogger(file, *constants)); | 48 net_log_logger_.reset(new net::NetLogLogger()); |
| 48 if (command_line->HasSwitch(switches::kNetLogLevel)) { | 49 if (command_line->HasSwitch(switches::kNetLogLevel)) { |
| 49 std::string log_level_string = | 50 std::string log_level_string = |
| 50 command_line->GetSwitchValueASCII(switches::kNetLogLevel); | 51 command_line->GetSwitchValueASCII(switches::kNetLogLevel); |
| 51 int command_line_log_level; | 52 int command_line_log_level; |
| 52 if (base::StringToInt(log_level_string, &command_line_log_level) && | 53 if (base::StringToInt(log_level_string, &command_line_log_level) && |
| 53 command_line_log_level >= LOG_ALL && | 54 command_line_log_level >= LOG_ALL && |
| 54 command_line_log_level <= LOG_NONE) { | 55 command_line_log_level <= LOG_NONE) { |
| 55 net_log_logger_->set_log_level( | 56 net_log_logger_->set_log_level( |
| 56 static_cast<LogLevel>(command_line_log_level)); | 57 static_cast<LogLevel>(command_line_log_level)); |
| 57 } | 58 } |
| 58 } | 59 } |
| 59 net_log_logger_->StartObserving(this); | 60 net_log_logger_->StartObserving(this, file.Pass(), constants.get(), |
| 61 nullptr); |
| 60 } | 62 } |
| 61 } | 63 } |
| 62 | 64 |
| 63 trace_net_log_observer_.reset(new net::TraceNetLogObserver()); | 65 trace_net_log_observer_.reset(new net::TraceNetLogObserver()); |
| 64 trace_net_log_observer_->WatchForTraceStart(this); | 66 trace_net_log_observer_->WatchForTraceStart(this); |
| 65 } | 67 } |
| 66 | 68 |
| 67 ChromeNetLog::~ChromeNetLog() { | 69 ChromeNetLog::~ChromeNetLog() { |
| 68 net_log_temp_file_.reset(); | 70 net_log_temp_file_.reset(); |
| 69 // Remove the observers we own before we're destroyed. | 71 // Remove the observers we own before we're destroyed. |
| 70 if (net_log_logger_) | 72 if (net_log_logger_) |
| 71 RemoveThreadSafeObserver(net_log_logger_.get()); | 73 net_log_logger_->StopObserving(nullptr); |
| 72 if (trace_net_log_observer_) | 74 if (trace_net_log_observer_) |
| 73 trace_net_log_observer_->StopWatchForTraceStart(); | 75 trace_net_log_observer_->StopWatchForTraceStart(); |
| 74 } | 76 } |
| 75 | 77 |
| OLD | NEW |