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

Side by Side Diff: net/base/net_log.h

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 #ifndef NET_BASE_NET_LOG_H_ 5 #ifndef NET_BASE_NET_LOG_H_
6 #define NET_BASE_NET_LOG_H_ 6 #define NET_BASE_NET_LOG_H_
7 7
8 #include <string> 8 #include <string>
9 9
10 #include "base/atomicops.h"
10 #include "base/basictypes.h" 11 #include "base/basictypes.h"
11 #include "base/callback_forward.h" 12 #include "base/callback_forward.h"
12 #include "base/compiler_specific.h" 13 #include "base/compiler_specific.h"
14 #include "base/observer_list.h"
13 #include "base/string16.h" 15 #include "base/string16.h"
16 #include "base/synchronization/lock.h"
14 #include "base/time.h" 17 #include "base/time.h"
15 #include "net/base/net_export.h" 18 #include "net/base/net_export.h"
16 19
17 namespace base { 20 namespace base {
18 class DictionaryValue; 21 class DictionaryValue;
19 class Value; 22 class Value;
20 } 23 }
21 24
22 namespace net { 25 namespace net {
23 26
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after
191 private: 194 private:
192 friend class NetLog; 195 friend class NetLog;
193 196
194 // Both of these values are only modified by the NetLog. 197 // Both of these values are only modified by the NetLog.
195 LogLevel log_level_; 198 LogLevel log_level_;
196 NetLog* net_log_; 199 NetLog* net_log_;
197 200
198 DISALLOW_COPY_AND_ASSIGN(ThreadSafeObserver); 201 DISALLOW_COPY_AND_ASSIGN(ThreadSafeObserver);
199 }; 202 };
200 203
201 NetLog() {} 204 NetLog();
202 virtual ~NetLog() {} 205 virtual ~NetLog();
203 206
204 // Emits a global event to the log stream, with its own unique source ID. 207 // Emits a global event to the log stream, with its own unique source ID.
205 void AddGlobalEntry(EventType type); 208 void AddGlobalEntry(EventType type);
206 void AddGlobalEntry(EventType type, 209 void AddGlobalEntry(EventType type,
207 const NetLog::ParametersCallback& parameters_callback); 210 const NetLog::ParametersCallback& parameters_callback);
208 211
209 // Returns a unique ID which can be used as a source ID. All returned IDs 212 // Returns a unique ID which can be used as a source ID. All returned IDs
210 // will be unique and greater than 0. 213 // will be unique and greater than 0.
211 virtual uint32 NextID() = 0; 214 uint32 NextID();
215
216 // Set the lowest allowed log level, regardless of any Observers.
217 void SetBaseLogLevel(LogLevel log_level);
mmenke 2013/06/03 14:27:09 Suggest making this protected, to discourage exter
kouhei (in TOK) 2013/06/04 15:41:44 Done.
212 218
213 // Returns the logging level for this NetLog. This is used to avoid computing 219 // Returns the logging level for this NetLog. This is used to avoid computing
214 // and saving expensive log entries. 220 // and saving expensive log entries.
215 virtual LogLevel GetLogLevel() const = 0; 221 LogLevel GetLogLevel() const;
216 222
217 // Adds an observer and sets its log level. The observer must not be 223 // Adds an observer and sets its log level. The observer must not be
218 // watching any NetLog, including this one, when this is called. 224 // watching any NetLog, including this one, when this is called.
219 // 225 //
220 // Typical observers should specify LOG_BASIC. 226 // Typical observers should specify LOG_BASIC.
221 // 227 //
222 // Observers that need to see the full granularity of events can specify 228 // Observers that need to see the full granularity of events can specify
223 // LOG_ALL_BUT_BYTES. However, doing so will have performance consequences. 229 // LOG_ALL_BUT_BYTES. However, doing so will have performance consequences.
224 // 230 //
225 // NetLog implementations must call NetLog::OnAddObserver to update the 231 // NetLog implementations must call NetLog::OnAddObserver to update the
226 // observer's internal state. 232 // observer's internal state.
227 virtual void AddThreadSafeObserver(ThreadSafeObserver* observer, 233 void AddThreadSafeObserver(ThreadSafeObserver* observer, LogLevel log_level);
228 LogLevel log_level) = 0;
229 234
230 // Sets the log level of |observer| to |log_level|. |observer| must be 235 // Sets the log level of |observer| to |log_level|. |observer| must be
231 // watching |this|. NetLog implementations must call 236 // watching |this|. NetLog implementations must call
232 // NetLog::OnSetObserverLogLevel to update the observer's internal state. 237 // NetLog::OnSetObserverLogLevel to update the observer's internal state.
233 virtual void SetObserverLogLevel(ThreadSafeObserver* observer, 238 void SetObserverLogLevel(ThreadSafeObserver* observer, LogLevel log_level);
234 LogLevel log_level) = 0;
235 239
236 // Removes an observer. NetLog implementations must call 240 // Removes an observer. NetLog implementations must call
237 // NetLog::OnAddObserver to update the observer's internal state. 241 // NetLog::OnAddObserver to update the observer's internal state.
238 // 242 //
239 // For thread safety reasons, it is recommended that this not be called in 243 // For thread safety reasons, it is recommended that this not be called in
240 // an object's destructor. 244 // an object's destructor.
241 virtual void RemoveThreadSafeObserver(ThreadSafeObserver* observer) = 0; 245 void RemoveThreadSafeObserver(ThreadSafeObserver* observer);
242 246
243 // Converts a time to the string format that the NetLog uses to represent 247 // Converts a time to the string format that the NetLog uses to represent
244 // times. Strings are used since integers may overflow. 248 // times. Strings are used since integers may overflow.
245 static std::string TickCountToString(const base::TimeTicks& time); 249 static std::string TickCountToString(const base::TimeTicks& time);
246 250
247 // Returns a C-String symbolic name for |event_type|. 251 // Returns a C-String symbolic name for |event_type|.
248 static const char* EventTypeToString(EventType event_type); 252 static const char* EventTypeToString(EventType event_type);
249 253
250 // Returns a dictionary that maps event type symbolic names to their enum 254 // Returns a dictionary that maps event type symbolic names to their enum
251 // values. Caller takes ownership of the returned Value. 255 // values. Caller takes ownership of the returned Value.
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
286 // the life of the callback. |value| may not be NULL. 290 // the life of the callback. |value| may not be NULL.
287 // Warning: |name| and |value| must remain valid for the life of the callback. 291 // Warning: |name| and |value| must remain valid for the life of the callback.
288 static ParametersCallback StringCallback(const char* name, 292 static ParametersCallback StringCallback(const char* name,
289 const std::string* value); 293 const std::string* value);
290 294
291 // Same as above, but takes in a UTF16 string. 295 // Same as above, but takes in a UTF16 string.
292 static ParametersCallback StringCallback(const char* name, 296 static ParametersCallback StringCallback(const char* name,
293 const base::string16* value); 297 const base::string16* value);
294 298
295 protected: 299 protected:
296 // Child classes should respond to the new entry here. This includes
297 // creating the Entry object and alerting their observers.
298 virtual void OnAddEntry(const Entry& entry) = 0;
299
300 // Subclasses must call these in the corresponding functions to set an 300 // Subclasses must call these in the corresponding functions to set an
301 // observer's |net_log_| and |log_level_| values. 301 // observer's |net_log_| and |log_level_| values.
mmenke 2013/06/03 14:27:09 This comment is no longer accurate, though I think
kouhei (in TOK) 2013/06/04 15:41:44 Done.
302 void OnAddObserver(ThreadSafeObserver* observer, LogLevel log_level); 302 void OnAddObserver(ThreadSafeObserver* observer, LogLevel log_level);
303 void OnSetObserverLogLevel(ThreadSafeObserver* observer, 303 void OnSetObserverLogLevel(ThreadSafeObserver* observer,
304 LogLevel log_level); 304 LogLevel log_level);
305 void OnRemoveObserver(ThreadSafeObserver* observer); 305 void OnRemoveObserver(ThreadSafeObserver* observer);
306 306
307 private: 307 private:
308 friend class BoundNetLog; 308 friend class BoundNetLog;
309 309
310 void AddEntry(EventType type, 310 void AddEntry(EventType type,
311 const Source& source, 311 const Source& source,
312 EventPhase phase, 312 EventPhase phase,
313 const NetLog::ParametersCallback* parameters_callback); 313 const NetLog::ParametersCallback* parameters_callback);
314 314
315 // Called whenever an observer is added or removed, or has its log level
316 // changed. Must have acquired |lock_| prior to calling.
317 void UpdateLogLevel();
318
319 // |lock_| protects access to |observers_|.
320 base::Lock lock_;
321
322 // Last assigned source ID. Incremented to get the next one.
323 base::subtle::Atomic32 last_id_;
324
325 // The lowest allowed log level, regardless of any Observers.
326 // Normally defaults to LOG_NONE, but can be changed with command line flags
mmenke 2013/06/03 14:27:09 Fix comment: "but can be changed with SetLogLevel
kouhei (in TOK) 2013/06/04 15:41:44 Done.
327 LogLevel base_log_level_;
328
329 // The current log level.
330 base::subtle::Atomic32 effective_log_level_;
331
332 // |lock_| must be acquired whenever reading or writing to this.
333 ObserverList<ThreadSafeObserver, true> observers_;
334
315 DISALLOW_COPY_AND_ASSIGN(NetLog); 335 DISALLOW_COPY_AND_ASSIGN(NetLog);
316 }; 336 };
317 337
318 // Helper that binds a Source to a NetLog, and exposes convenience methods to 338 // Helper that binds a Source to a NetLog, and exposes convenience methods to
319 // output log messages without needing to pass in the source. 339 // output log messages without needing to pass in the source.
320 class NET_EXPORT BoundNetLog { 340 class NET_EXPORT BoundNetLog {
321 public: 341 public:
322 BoundNetLog() : net_log_(NULL) {} 342 BoundNetLog() : net_log_(NULL) {}
323 343
324 // Add a log entry to the NetLog for the bound source. 344 // Add a log entry to the NetLog for the bound source.
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
380 : source_(source), net_log_(net_log) { 400 : source_(source), net_log_(net_log) {
381 } 401 }
382 402
383 NetLog::Source source_; 403 NetLog::Source source_;
384 NetLog* net_log_; 404 NetLog* net_log_;
385 }; 405 };
386 406
387 } // namespace net 407 } // namespace net
388 408
389 #endif // NET_BASE_NET_LOG_H_ 409 #endif // NET_BASE_NET_LOG_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698