Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(152)

Side by Side Diff: chrome/browser/net/chrome_net_log.cc

Issue 16137008: Refactor net::NetLog to provide implementation of observer pattern, not just the interface. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/string_util.h" 11 #include "base/string_util.h"
12 #include "base/strings/string_number_conversions.h" 12 #include "base/strings/string_number_conversions.h"
13 #include "base/values.h" 13 #include "base/values.h"
14 #include "chrome/browser/net/net_log_logger.h" 14 #include "chrome/browser/net/net_log_logger.h"
15 #include "chrome/browser/net/net_log_temp_file.h" 15 #include "chrome/browser/net/net_log_temp_file.h"
16 #include "chrome/common/chrome_switches.h" 16 #include "chrome/common/chrome_switches.h"
17 17
18 ChromeNetLog::ChromeNetLog() 18 namespace {
19 : last_id_(0), 19
20 base_log_level_(LOG_NONE), 20 net::NetLog::LogLevel GetBaseLogLevelFromCommandLine() {
21 effective_log_level_(LOG_NONE),
22 net_log_temp_file_(new NetLogTempFile(this)) {
23 const CommandLine* command_line = CommandLine::ForCurrentProcess(); 21 const CommandLine* command_line = CommandLine::ForCurrentProcess();
22
24 // Adjust base log level based on command line switch, if present. 23 // 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 24 // This is done before adding any observers so the call to UpdateLogLevel when
26 // an observers is added will set |effective_log_level_| correctly. 25 // an observers is added will set |effective_log_level_| correctly.
27 if (command_line->HasSwitch(switches::kNetLogLevel)) { 26 if (command_line->HasSwitch(switches::kNetLogLevel)) {
28 std::string log_level_string = 27 std::string log_level_string =
29 command_line->GetSwitchValueASCII(switches::kNetLogLevel); 28 command_line->GetSwitchValueASCII(switches::kNetLogLevel);
30 int command_line_log_level; 29 int command_line_log_level;
31 if (base::StringToInt(log_level_string, &command_line_log_level) && 30 if (base::StringToInt(log_level_string, &command_line_log_level) &&
32 command_line_log_level >= LOG_ALL && 31 command_line_log_level >= net::NetLog::LOG_ALL &&
33 command_line_log_level <= LOG_NONE) { 32 command_line_log_level <= net::NetLog::LOG_NONE) {
34 base_log_level_ = static_cast<LogLevel>(command_line_log_level); 33 return static_cast<net::NetLog::LogLevel>(command_line_log_level);
35 } 34 }
36 } 35 }
37 36
37 return net::NetLog::LOG_NONE;
38 }
39
40 } // namespace
41
42 ChromeNetLog::ChromeNetLog()
43 : NetLog(GetBaseLogLevelFromCommandLine()),
mmenke 2013/06/03 14:27:09 This constructor no longer exists, should use SetL
kouhei (in TOK) 2013/06/04 15:41:44 Done.
44 net_log_temp_file_(new NetLogTempFile(this)) {
45 const CommandLine* command_line = CommandLine::ForCurrentProcess();
46
38 if (command_line->HasSwitch(switches::kLogNetLog)) { 47 if (command_line->HasSwitch(switches::kLogNetLog)) {
39 base::FilePath log_path = 48 base::FilePath log_path =
40 command_line->GetSwitchValuePath(switches::kLogNetLog); 49 command_line->GetSwitchValuePath(switches::kLogNetLog);
41 // Much like logging.h, bypass threading restrictions by using fopen 50 // 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 51 // 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 52 // 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 53 // 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 54 // doing this on another thread. It's only used when debugging Chrome, so
46 // performance is not a big concern. 55 // performance is not a big concern.
47 FILE* file = NULL; 56 FILE* file = NULL;
(...skipping 13 matching lines...) Expand all
61 } 70 }
62 } 71 }
63 72
64 ChromeNetLog::~ChromeNetLog() { 73 ChromeNetLog::~ChromeNetLog() {
65 net_log_temp_file_.reset(); 74 net_log_temp_file_.reset();
66 // Remove the observers we own before we're destroyed. 75 // Remove the observers we own before we're destroyed.
67 if (net_log_logger_) 76 if (net_log_logger_)
68 RemoveThreadSafeObserver(net_log_logger_.get()); 77 RemoveThreadSafeObserver(net_log_logger_.get());
69 } 78 }
70 79
71 void ChromeNetLog::OnAddEntry(const net::NetLog::Entry& entry) {
72 base::AutoLock lock(lock_);
73
74 // Notify all of the log observers.
75 FOR_EACH_OBSERVER(ThreadSafeObserver, observers_, OnAddEntry(entry));
76 }
77
78 uint32 ChromeNetLog::NextID() {
79 return base::subtle::NoBarrier_AtomicIncrement(&last_id_, 1);
80 }
81
82 net::NetLog::LogLevel ChromeNetLog::GetLogLevel() const {
83 base::subtle::Atomic32 log_level =
84 base::subtle::NoBarrier_Load(&effective_log_level_);
85 return static_cast<net::NetLog::LogLevel>(log_level);
86 }
87
88 void ChromeNetLog::AddThreadSafeObserver(
89 net::NetLog::ThreadSafeObserver* observer,
90 LogLevel log_level) {
91 base::AutoLock lock(lock_);
92
93 observers_.AddObserver(observer);
94 OnAddObserver(observer, log_level);
95 UpdateLogLevel();
96 }
97
98 void ChromeNetLog::SetObserverLogLevel(
99 net::NetLog::ThreadSafeObserver* observer,
100 LogLevel log_level) {
101 base::AutoLock lock(lock_);
102
103 DCHECK(observers_.HasObserver(observer));
104 OnSetObserverLogLevel(observer, log_level);
105 UpdateLogLevel();
106 }
107
108 void ChromeNetLog::RemoveThreadSafeObserver(
109 net::NetLog::ThreadSafeObserver* observer) {
110 base::AutoLock lock(lock_);
111
112 DCHECK(observers_.HasObserver(observer));
113 observers_.RemoveObserver(observer);
114 OnRemoveObserver(observer);
115 UpdateLogLevel();
116 }
117
118 void ChromeNetLog::UpdateLogLevel() {
119 lock_.AssertAcquired();
120
121 // Look through all the observers and find the finest granularity
122 // log level (higher values of the enum imply *lower* log levels).
123 LogLevel new_effective_log_level = base_log_level_;
124 ObserverListBase<ThreadSafeObserver>::Iterator it(observers_);
125 ThreadSafeObserver* observer;
126 while ((observer = it.GetNext()) != NULL) {
127 new_effective_log_level =
128 std::min(new_effective_log_level, observer->log_level());
129 }
130 base::subtle::NoBarrier_Store(&effective_log_level_,
131 new_effective_log_level);
132 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698