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

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: fix 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
« no previous file with comments | « chrome/browser/net/chrome_net_log.h ('k') | chrome/browser/net/chrome_net_log_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 ChromeNetLog::ChromeNetLog()
19 : last_id_(0), 19 : net_log_temp_file_(new NetLogTempFile(this)) {
20 base_log_level_(LOG_NONE),
21 effective_log_level_(LOG_NONE),
22 net_log_temp_file_(new NetLogTempFile(this)) {
23 const CommandLine* command_line = CommandLine::ForCurrentProcess(); 20 const CommandLine* command_line = CommandLine::ForCurrentProcess();
24 // Adjust base log level based on command line switch, if present. 21 // 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 22 // This is done before adding any observers so the call to UpdateLogLevel when
26 // an observers is added will set |effective_log_level_| correctly. 23 // an observers is added will set |effective_log_level_| correctly.
27 if (command_line->HasSwitch(switches::kNetLogLevel)) { 24 if (command_line->HasSwitch(switches::kNetLogLevel)) {
28 std::string log_level_string = 25 std::string log_level_string =
29 command_line->GetSwitchValueASCII(switches::kNetLogLevel); 26 command_line->GetSwitchValueASCII(switches::kNetLogLevel);
30 int command_line_log_level; 27 int command_line_log_level;
31 if (base::StringToInt(log_level_string, &command_line_log_level) && 28 if (base::StringToInt(log_level_string, &command_line_log_level) &&
32 command_line_log_level >= LOG_ALL && 29 command_line_log_level >= LOG_ALL &&
33 command_line_log_level <= LOG_NONE) { 30 command_line_log_level <= LOG_NONE) {
34 base_log_level_ = static_cast<LogLevel>(command_line_log_level); 31 SetBaseLogLevel(static_cast<LogLevel>(command_line_log_level));
35 } 32 }
36 } 33 }
37 34
38 if (command_line->HasSwitch(switches::kLogNetLog)) { 35 if (command_line->HasSwitch(switches::kLogNetLog)) {
39 base::FilePath log_path = 36 base::FilePath log_path =
40 command_line->GetSwitchValuePath(switches::kLogNetLog); 37 command_line->GetSwitchValuePath(switches::kLogNetLog);
41 // Much like logging.h, bypass threading restrictions by using fopen 38 // 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 39 // 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 40 // 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 41 // would result in an unbounded buffer size, so not much can be gained by
(...skipping 16 matching lines...) Expand all
61 } 58 }
62 } 59 }
63 60
64 ChromeNetLog::~ChromeNetLog() { 61 ChromeNetLog::~ChromeNetLog() {
65 net_log_temp_file_.reset(); 62 net_log_temp_file_.reset();
66 // Remove the observers we own before we're destroyed. 63 // Remove the observers we own before we're destroyed.
67 if (net_log_logger_) 64 if (net_log_logger_)
68 RemoveThreadSafeObserver(net_log_logger_.get()); 65 RemoveThreadSafeObserver(net_log_logger_.get());
69 } 66 }
70 67
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
« no previous file with comments | « chrome/browser/net/chrome_net_log.h ('k') | chrome/browser/net/chrome_net_log_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698