Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_LOGGER_H_ | 5 #ifndef NET_BASE_NET_LOG_LOGGER_H_ |
| 6 #define NET_BASE_NET_LOG_LOGGER_H_ | 6 #define NET_BASE_NET_LOG_LOGGER_H_ |
| 7 | 7 |
| 8 #include <stdio.h> | 8 #include <stdio.h> |
| 9 | 9 |
| 10 #include "base/files/scoped_file.h" | 10 #include "base/files/scoped_file.h" |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/memory/ref_counted.h" | |
| 12 #include "net/base/net_log.h" | 13 #include "net/base/net_log.h" |
| 13 | 14 |
| 14 namespace base { | 15 namespace base { |
| 15 class DictionaryValue; | 16 class DictionaryValue; |
| 16 class FilePath; | 17 class FilePath; |
| 17 class Value; | 18 class Value; |
| 18 } | 19 } |
| 19 | 20 |
| 20 namespace net { | 21 namespace net { |
| 21 | 22 |
| 23 class URLRequestContext; | |
| 24 | |
| 22 // NetLogLogger watches the NetLog event stream, and sends all entries to | 25 // NetLogLogger watches the NetLog event stream, and sends all entries to |
| 23 // a file specified on creation. | 26 // a file specified on creation. |
| 24 // | 27 // |
| 25 // The text file will contain a single JSON object. | 28 // The text file will contain a single JSON object. |
| 26 class NET_EXPORT NetLogLogger : public NetLog::ThreadSafeObserver { | 29 class NET_EXPORT NetLogLogger : public NetLog::ThreadSafeObserver { |
| 27 public: | 30 public: |
| 28 // Takes ownership of |file| and will write network events to it once logging | 31 NetLogLogger(); |
| 29 // starts. |file| must be non-NULL handle and be open for writing. | |
| 30 // |constants| is a legend for decoding constant values used in the log. | |
| 31 NetLogLogger(FILE* file, const base::Value& constants); | |
| 32 ~NetLogLogger() override; | 32 ~NetLogLogger() override; |
| 33 | 33 |
| 34 // Sets the log level to log at. Must be called before StartObserving. | 34 // Sets the log level to log at. Must be called before StartObserving. |
| 35 void set_log_level(NetLog::LogLevel log_level); | 35 void set_log_level(NetLog::LogLevel log_level); |
| 36 | 36 |
| 37 // Starts observing specified NetLog. Must not already be watching a NetLog. | 37 // Starts observing |net_log| and writes output to |file|. Must not already |
| 38 // Separate from constructor to enforce thread safety. | 38 // be watching a NetLog. Separate from constructor to enforce thread safety. |
| 39 void StartObserving(NetLog* net_log); | 39 // |
| 40 // |file| must be a non-NULL empty file that's open for writing. | |
| 41 // | |
| 42 // |constants| is an optional legend for decoding constant values used in the | |
| 43 // log. It should generally be a modified version of GetNetConstants(). If | |
| 44 // not present, the output of GetNetConstants() will be used. | |
| 45 // | |
| 46 // |url_request_context| is an optional URLRequestContext to collect | |
| 47 // additional information from. When non-NULL, StartObserving and | |
| 48 // StopObserving must be called on the context's thread, while the context is | |
|
eroman
2015/03/05 19:25:58
I don't like this second requirement. How about in
mmenke
2015/03/05 22:04:17
Done. May make StartObserving take a list of conte
| |
| 49 // still valid. | |
| 50 void StartObserving(NetLog* net_log, | |
| 51 base::ScopedFILE file, | |
| 52 base::Value* constants, | |
| 53 net::URLRequestContext* url_request_context); | |
|
mmenke
2015/03/03 18:33:58
One downside of this approach is that we can't get
| |
| 40 | 54 |
| 41 // Stops observing net_log(). Must already be watching. | 55 // Stops observing net_log(). Must already be watching. Must be called |
| 56 // before destruction of the NetLogLogger and the NetLog. If a | |
| 57 // URLRequestContext was passed in to StartObserving, must be called on that | |
| 58 // context's thread. | |
| 42 void StopObserving(); | 59 void StopObserving(); |
| 43 | 60 |
| 44 // net::NetLog::ThreadSafeObserver implementation: | 61 // net::NetLog::ThreadSafeObserver implementation: |
| 45 void OnAddEntry(const NetLog::Entry& entry) override; | 62 void OnAddEntry(const NetLog::Entry& entry) override; |
| 46 | 63 |
| 47 // Create a dictionary containing legend for net/ constants. Caller takes | |
| 48 // ownership of returned value. | |
| 49 // TODO(mmenke): Get rid of this, and have embedders use GetNetConstants | |
| 50 // directly. Also maybe call that function by default, so only embedders | |
| 51 // that need more constants need to worry about it. | |
| 52 static base::DictionaryValue* GetConstants(); | |
| 53 | |
| 54 private: | 64 private: |
| 55 base::ScopedFILE file_; | 65 base::ScopedFILE file_; |
| 56 | 66 |
| 67 URLRequestContext* url_request_context_; | |
| 68 | |
| 57 // The LogLevel to log at. | 69 // The LogLevel to log at. |
| 58 NetLog::LogLevel log_level_; | 70 NetLog::LogLevel log_level_; |
| 59 | 71 |
| 60 // True if OnAddEntry() has been called at least once. | 72 // True if OnAddEntry() has been called at least once. |
| 61 bool added_events_; | 73 bool added_events_; |
| 62 | 74 |
| 63 DISALLOW_COPY_AND_ASSIGN(NetLogLogger); | 75 DISALLOW_COPY_AND_ASSIGN(NetLogLogger); |
| 64 }; | 76 }; |
| 65 | 77 |
| 66 } // namespace net | 78 } // namespace net |
| 67 | 79 |
| 68 #endif // NET_BASE_NET_LOG_LOGGER_H_ | 80 #endif // NET_BASE_NET_LOG_LOGGER_H_ |
| OLD | NEW |