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/logging.h" | 10 #include "base/logging.h" |
11 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
13 #include "base/values.h" | 13 #include "base/values.h" |
14 #include "chrome/browser/net/net_log_temp_file.h" | 14 #include "chrome/browser/net/net_log_temp_file.h" |
15 #include "chrome/browser/ui/webui/net_internals/net_internals_ui.h" | 15 #include "chrome/browser/ui/webui/net_internals/net_internals_ui.h" |
16 #include "chrome/common/chrome_switches.h" | 16 #include "chrome/common/chrome_switches.h" |
17 #include "content/public/common/content_switches.h" | 17 #include "content/public/common/content_switches.h" |
18 #include "net/base/net_log_logger.h" | 18 #include "net/base/net_log_logger.h" |
19 #include "net/base/trace_net_log_observer.h" | 19 #include "net/base/trace_net_log_observer.h" |
20 | 20 |
21 ChromeNetLog::ChromeNetLog() | 21 ChromeNetLog::ChromeNetLog() |
22 : net_log_temp_file_(new NetLogTempFile(this)) { | 22 : net_log_temp_file_(new NetLogTempFile(this)) { |
23 const CommandLine* command_line = CommandLine::ForCurrentProcess(); | 23 const CommandLine* command_line = CommandLine::ForCurrentProcess(); |
24 // Adjust base log level based on command line switch, if present. | |
25 // This is done before adding any observers so the call to UpdateLogLevel when | |
26 // an observers is added will set |effective_log_level_| correctly. | |
27 if (command_line->HasSwitch(switches::kNetLogLevel)) { | |
28 std::string log_level_string = | |
29 command_line->GetSwitchValueASCII(switches::kNetLogLevel); | |
30 int command_line_log_level; | |
31 if (base::StringToInt(log_level_string, &command_line_log_level) && | |
32 command_line_log_level >= LOG_ALL && | |
33 command_line_log_level <= LOG_NONE) { | |
34 SetBaseLogLevel(static_cast<LogLevel>(command_line_log_level)); | |
35 } | |
36 } | |
37 | 24 |
38 if (command_line->HasSwitch(switches::kLogNetLog)) { | 25 if (command_line->HasSwitch(switches::kLogNetLog)) { |
39 base::FilePath log_path = | 26 base::FilePath log_path = |
40 command_line->GetSwitchValuePath(switches::kLogNetLog); | 27 command_line->GetSwitchValuePath(switches::kLogNetLog); |
41 // Much like logging.h, bypass threading restrictions by using fopen | 28 // Much like logging.h, bypass threading restrictions by using fopen |
42 // directly. Have to write on a thread that's shutdown to handle events on | 29 // directly. Have to write on a thread that's shutdown to handle events on |
43 // shutdown properly, and posting events to another thread as they occur | 30 // shutdown properly, and posting events to another thread as they occur |
44 // would result in an unbounded buffer size, so not much can be gained by | 31 // would result in an unbounded buffer size, so not much can be gained by |
45 // doing this on another thread. It's only used when debugging Chrome, so | 32 // doing this on another thread. It's only used when debugging Chrome, so |
46 // performance is not a big concern. | 33 // performance is not a big concern. |
47 FILE* file = NULL; | 34 FILE* file = NULL; |
48 #if defined(OS_WIN) | 35 #if defined(OS_WIN) |
49 file = _wfopen(log_path.value().c_str(), L"w"); | 36 file = _wfopen(log_path.value().c_str(), L"w"); |
50 #elif defined(OS_POSIX) | 37 #elif defined(OS_POSIX) |
51 file = fopen(log_path.value().c_str(), "w"); | 38 file = fopen(log_path.value().c_str(), "w"); |
52 #endif | 39 #endif |
53 | 40 |
54 if (file == NULL) { | 41 if (file == NULL) { |
55 LOG(ERROR) << "Could not open file " << log_path.value() | 42 LOG(ERROR) << "Could not open file " << log_path.value() |
56 << " for net logging"; | 43 << " for net logging"; |
57 } else { | 44 } else { |
58 scoped_ptr<base::Value> constants(NetInternalsUI::GetConstants()); | 45 scoped_ptr<base::Value> constants(NetInternalsUI::GetConstants()); |
59 net_log_logger_.reset(new net::NetLogLogger(file, *constants)); | 46 net_log_logger_.reset(new net::NetLogLogger(file, *constants)); |
| 47 if (command_line->HasSwitch(switches::kNetLogLevel)) { |
| 48 std::string log_level_string = |
| 49 command_line->GetSwitchValueASCII(switches::kNetLogLevel); |
| 50 int command_line_log_level; |
| 51 if (base::StringToInt(log_level_string, &command_line_log_level) && |
| 52 command_line_log_level >= LOG_ALL && |
| 53 command_line_log_level <= LOG_NONE) { |
| 54 net_log_logger_->set_log_level( |
| 55 static_cast<LogLevel>(command_line_log_level)); |
| 56 } |
| 57 } |
60 net_log_logger_->StartObserving(this); | 58 net_log_logger_->StartObserving(this); |
61 } | 59 } |
62 } | 60 } |
63 | 61 |
64 trace_net_log_observer_.reset(new net::TraceNetLogObserver()); | 62 trace_net_log_observer_.reset(new net::TraceNetLogObserver()); |
65 trace_net_log_observer_->WatchForTraceStart(this); | 63 trace_net_log_observer_->WatchForTraceStart(this); |
66 } | 64 } |
67 | 65 |
68 ChromeNetLog::~ChromeNetLog() { | 66 ChromeNetLog::~ChromeNetLog() { |
69 net_log_temp_file_.reset(); | 67 net_log_temp_file_.reset(); |
70 // Remove the observers we own before we're destroyed. | 68 // Remove the observers we own before we're destroyed. |
71 if (net_log_logger_) | 69 if (net_log_logger_) |
72 RemoveThreadSafeObserver(net_log_logger_.get()); | 70 RemoveThreadSafeObserver(net_log_logger_.get()); |
73 if (trace_net_log_observer_) | 71 if (trace_net_log_observer_) |
74 trace_net_log_observer_->StopWatchForTraceStart(); | 72 trace_net_log_observer_->StopWatchForTraceStart(); |
75 } | 73 } |
76 | 74 |
OLD | NEW |