Chromium Code Reviews| 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/files/scoped_file.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
| 14 #include "base/values.h" | 14 #include "base/values.h" |
| 15 #include "chrome/browser/net/net_log_temp_file.h" | 15 #include "chrome/browser/net/net_log_temp_file.h" |
| 16 #include "chrome/browser/ui/webui/net_internals/net_internals_ui.h" | 16 #include "chrome/browser/ui/webui/net_internals/net_internals_ui.h" |
| 17 #include "chrome/common/chrome_switches.h" | 17 #include "chrome/common/chrome_switches.h" |
| 18 #include "content/public/common/content_switches.h" | 18 #include "content/public/common/content_switches.h" |
| 19 #include "net/log/trace_net_log_observer.h" | 19 #include "net/log/trace_net_log_observer.h" |
| 20 #include "net/log/write_to_file_net_log_observer.h" | 20 #include "net/log/write_to_file_net_log_observer.h" |
| 21 | 21 |
| 22 namespace { | |
| 23 | |
| 24 bool GetCaptureModeFromCommandLine(const base::CommandLine* command_line, | |
|
mmenke
2015/04/22 18:52:17
nit: Think const base::CommandLine& command_line
eroman
2015/04/22 20:01:46
Done.
| |
| 25 net::NetLogCaptureMode* mode) { | |
|
mmenke
2015/04/22 18:52:17
optional: Could just make this return a net::NetL
eroman
2015/04/22 20:01:46
Done.
| |
| 26 // TODO(eroman): The NetLog "capture mode" used to be called the "log | |
| 27 // level". To preserve backwards compatibility the log level of old is | |
| 28 // converted into a capture mode. | |
| 29 if (!command_line->HasSwitch(switches::kNetLogLevel)) | |
| 30 return false; | |
| 31 | |
| 32 std::string log_level_string = | |
| 33 command_line->GetSwitchValueASCII(switches::kNetLogLevel); | |
| 34 | |
| 35 int level; | |
| 36 if (!base::StringToInt(log_level_string, &level)) | |
| 37 return false; | |
| 38 | |
| 39 switch (level) { | |
| 40 case 0: | |
| 41 *mode = net::NetLogCaptureMode::IncludeSocketBytes(); | |
| 42 return true; | |
| 43 case 1: | |
| 44 *mode = net::NetLogCaptureMode::IncludeCookiesAndCredentials(); | |
| 45 return true; | |
| 46 case 2: | |
| 47 *mode = net::NetLogCaptureMode::Default(); | |
| 48 return true; | |
| 49 case 3: | |
| 50 *mode = net::NetLogCaptureMode::None(); | |
| 51 return true; | |
| 52 default: | |
| 53 LOG(ERROR) << "Unrecognized --" << switches::kNetLogLevel; | |
| 54 break; | |
| 55 } | |
| 56 | |
| 57 return false; | |
| 58 } | |
| 59 | |
| 60 } // namespace | |
| 61 | |
| 22 ChromeNetLog::ChromeNetLog() | 62 ChromeNetLog::ChromeNetLog() |
| 23 : net_log_temp_file_(new NetLogTempFile(this)) { | 63 : net_log_temp_file_(new NetLogTempFile(this)) { |
| 24 const base::CommandLine* command_line = | 64 const base::CommandLine* command_line = |
| 25 base::CommandLine::ForCurrentProcess(); | 65 base::CommandLine::ForCurrentProcess(); |
| 26 | 66 |
| 27 if (command_line->HasSwitch(switches::kLogNetLog)) { | 67 if (command_line->HasSwitch(switches::kLogNetLog)) { |
| 28 base::FilePath log_path = | 68 base::FilePath log_path = |
| 29 command_line->GetSwitchValuePath(switches::kLogNetLog); | 69 command_line->GetSwitchValuePath(switches::kLogNetLog); |
| 30 // Much like logging.h, bypass threading restrictions by using fopen | 70 // Much like logging.h, bypass threading restrictions by using fopen |
| 31 // directly. Have to write on a thread that's shutdown to handle events on | 71 // directly. Have to write on a thread that's shutdown to handle events on |
| 32 // shutdown properly, and posting events to another thread as they occur | 72 // shutdown properly, and posting events to another thread as they occur |
| 33 // would result in an unbounded buffer size, so not much can be gained by | 73 // would result in an unbounded buffer size, so not much can be gained by |
| 34 // doing this on another thread. It's only used when debugging Chrome, so | 74 // doing this on another thread. It's only used when debugging Chrome, so |
| 35 // performance is not a big concern. | 75 // performance is not a big concern. |
| 36 base::ScopedFILE file; | 76 base::ScopedFILE file; |
| 37 #if defined(OS_WIN) | 77 #if defined(OS_WIN) |
| 38 file.reset(_wfopen(log_path.value().c_str(), L"w")); | 78 file.reset(_wfopen(log_path.value().c_str(), L"w")); |
| 39 #elif defined(OS_POSIX) | 79 #elif defined(OS_POSIX) |
| 40 file.reset(fopen(log_path.value().c_str(), "w")); | 80 file.reset(fopen(log_path.value().c_str(), "w")); |
| 41 #endif | 81 #endif |
| 42 | 82 |
| 43 if (!file) { | 83 if (!file) { |
| 44 LOG(ERROR) << "Could not open file " << log_path.value() | 84 LOG(ERROR) << "Could not open file " << log_path.value() |
| 45 << " for net logging"; | 85 << " for net logging"; |
| 46 } else { | 86 } else { |
| 47 scoped_ptr<base::Value> constants(NetInternalsUI::GetConstants()); | 87 scoped_ptr<base::Value> constants(NetInternalsUI::GetConstants()); |
| 48 net_log_logger_.reset(new net::WriteToFileNetLogObserver()); | 88 net_log_logger_.reset(new net::WriteToFileNetLogObserver()); |
| 49 if (command_line->HasSwitch(switches::kNetLogLevel)) { | 89 |
| 50 std::string log_level_string = | 90 net::NetLogCaptureMode capture_mode; |
| 51 command_line->GetSwitchValueASCII(switches::kNetLogLevel); | 91 if (GetCaptureModeFromCommandLine(command_line, &capture_mode)) |
| 52 int command_line_log_level; | 92 net_log_logger_->set_capture_mode(capture_mode); |
| 53 if (base::StringToInt(log_level_string, &command_line_log_level) && | 93 |
| 54 command_line_log_level >= LOG_ALL && | |
| 55 command_line_log_level <= LOG_NONE) { | |
| 56 net_log_logger_->set_log_level( | |
| 57 static_cast<LogLevel>(command_line_log_level)); | |
| 58 } | |
| 59 } | |
| 60 net_log_logger_->StartObserving(this, file.Pass(), constants.get(), | 94 net_log_logger_->StartObserving(this, file.Pass(), constants.get(), |
| 61 nullptr); | 95 nullptr); |
| 62 } | 96 } |
| 63 } | 97 } |
| 64 | 98 |
| 65 trace_net_log_observer_.reset(new net::TraceNetLogObserver()); | 99 trace_net_log_observer_.reset(new net::TraceNetLogObserver()); |
| 66 trace_net_log_observer_->WatchForTraceStart(this); | 100 trace_net_log_observer_->WatchForTraceStart(this); |
| 67 } | 101 } |
| 68 | 102 |
| 69 ChromeNetLog::~ChromeNetLog() { | 103 ChromeNetLog::~ChromeNetLog() { |
| 70 net_log_temp_file_.reset(); | 104 net_log_temp_file_.reset(); |
| 71 // Remove the observers we own before we're destroyed. | 105 // Remove the observers we own before we're destroyed. |
| 72 if (net_log_logger_) | 106 if (net_log_logger_) |
| 73 net_log_logger_->StopObserving(nullptr); | 107 net_log_logger_->StopObserving(nullptr); |
| 74 if (trace_net_log_observer_) | 108 if (trace_net_log_observer_) |
| 75 trace_net_log_observer_->StopWatchForTraceStart(); | 109 trace_net_log_observer_->StopWatchForTraceStart(); |
| 76 } | 110 } |
| 77 | 111 |
| OLD | NEW |