| 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 #include "net/log/net_log_logger.h" | 5 #include "net/log/write_to_file_net_log_observer.h" |
| 6 | 6 |
| 7 #include <stdio.h> | 7 #include <stdio.h> |
| 8 | 8 |
| 9 #include <set> | 9 #include <set> |
| 10 | 10 |
| 11 #include "base/json/json_writer.h" | 11 #include "base/json/json_writer.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/values.h" | 14 #include "base/values.h" |
| 15 #include "net/log/net_log_util.h" | 15 #include "net/log/net_log_util.h" |
| 16 #include "net/url_request/url_request_context.h" | 16 #include "net/url_request/url_request_context.h" |
| 17 | 17 |
| 18 namespace net { | 18 namespace net { |
| 19 | 19 |
| 20 NetLogLogger::NetLogLogger() | 20 WriteToFileNetLogObserver::WriteToFileNetLogObserver() |
| 21 : log_level_(NetLog::LOG_STRIP_PRIVATE_DATA), added_events_(false) { | 21 : log_level_(NetLog::LOG_STRIP_PRIVATE_DATA), added_events_(false) { |
| 22 } | 22 } |
| 23 | 23 |
| 24 NetLogLogger::~NetLogLogger() { | 24 WriteToFileNetLogObserver::~WriteToFileNetLogObserver() { |
| 25 } | 25 } |
| 26 | 26 |
| 27 void NetLogLogger::set_log_level(net::NetLog::LogLevel log_level) { | 27 void WriteToFileNetLogObserver::set_log_level(net::NetLog::LogLevel log_level) { |
| 28 DCHECK(!net_log()); | 28 DCHECK(!net_log()); |
| 29 log_level_ = log_level; | 29 log_level_ = log_level; |
| 30 } | 30 } |
| 31 | 31 |
| 32 void NetLogLogger::StartObserving(net::NetLog* net_log, | 32 void WriteToFileNetLogObserver::StartObserving( |
| 33 base::ScopedFILE file, | 33 net::NetLog* net_log, |
| 34 base::Value* constants, | 34 base::ScopedFILE file, |
| 35 net::URLRequestContext* url_request_context) { | 35 base::Value* constants, |
| 36 net::URLRequestContext* url_request_context) { |
| 36 DCHECK(file.get()); | 37 DCHECK(file.get()); |
| 37 file_ = file.Pass(); | 38 file_ = file.Pass(); |
| 38 added_events_ = false; | 39 added_events_ = false; |
| 39 | 40 |
| 40 // Write constants to the output file. This allows loading files that have | 41 // Write constants to the output file. This allows loading files that have |
| 41 // different source and event types, as they may be added and removed | 42 // different source and event types, as they may be added and removed |
| 42 // between Chrome versions. | 43 // between Chrome versions. |
| 43 std::string json; | 44 std::string json; |
| 44 if (constants) { | 45 if (constants) { |
| 45 base::JSONWriter::Write(constants, &json); | 46 base::JSONWriter::Write(constants, &json); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 57 DCHECK(url_request_context->CalledOnValidThread()); | 58 DCHECK(url_request_context->CalledOnValidThread()); |
| 58 | 59 |
| 59 std::set<URLRequestContext*> contexts; | 60 std::set<URLRequestContext*> contexts; |
| 60 contexts.insert(url_request_context); | 61 contexts.insert(url_request_context); |
| 61 CreateNetLogEntriesForActiveObjects(contexts, this); | 62 CreateNetLogEntriesForActiveObjects(contexts, this); |
| 62 } | 63 } |
| 63 | 64 |
| 64 net_log->DeprecatedAddObserver(this, log_level_); | 65 net_log->DeprecatedAddObserver(this, log_level_); |
| 65 } | 66 } |
| 66 | 67 |
| 67 void NetLogLogger::StopObserving(net::URLRequestContext* url_request_context) { | 68 void WriteToFileNetLogObserver::StopObserving( |
| 69 net::URLRequestContext* url_request_context) { |
| 68 net_log()->DeprecatedRemoveObserver(this); | 70 net_log()->DeprecatedRemoveObserver(this); |
| 69 | 71 |
| 70 // End events array. | 72 // End events array. |
| 71 fprintf(file_.get(), "]"); | 73 fprintf(file_.get(), "]"); |
| 72 | 74 |
| 73 // Write state of the URLRequestContext when logging stopped. | 75 // Write state of the URLRequestContext when logging stopped. |
| 74 if (url_request_context) { | 76 if (url_request_context) { |
| 75 DCHECK(url_request_context->CalledOnValidThread()); | 77 DCHECK(url_request_context->CalledOnValidThread()); |
| 76 | 78 |
| 77 std::string json; | 79 std::string json; |
| 78 scoped_ptr<base::DictionaryValue> net_info = | 80 scoped_ptr<base::DictionaryValue> net_info = |
| 79 GetNetInfo(url_request_context, NET_INFO_ALL_SOURCES); | 81 GetNetInfo(url_request_context, NET_INFO_ALL_SOURCES); |
| 80 base::JSONWriter::Write(net_info.get(), &json); | 82 base::JSONWriter::Write(net_info.get(), &json); |
| 81 fprintf(file_.get(), ",\"tabInfo\": %s\n", json.c_str()); | 83 fprintf(file_.get(), ",\"tabInfo\": %s\n", json.c_str()); |
| 82 } | 84 } |
| 83 fprintf(file_.get(), "}"); | 85 fprintf(file_.get(), "}"); |
| 84 | 86 |
| 85 file_.reset(); | 87 file_.reset(); |
| 86 } | 88 } |
| 87 | 89 |
| 88 void NetLogLogger::OnAddEntry(const net::NetLog::Entry& entry) { | 90 void WriteToFileNetLogObserver::OnAddEntry(const net::NetLog::Entry& entry) { |
| 89 // Add a comma and newline for every event but the first. Newlines are needed | 91 // Add a comma and newline for every event but the first. Newlines are needed |
| 90 // so can load partial log files by just ignoring the last line. For this to | 92 // so can load partial log files by just ignoring the last line. For this to |
| 91 // work, lines cannot be pretty printed. | 93 // work, lines cannot be pretty printed. |
| 92 scoped_ptr<base::Value> value(entry.ToValue()); | 94 scoped_ptr<base::Value> value(entry.ToValue()); |
| 93 std::string json; | 95 std::string json; |
| 94 base::JSONWriter::Write(value.get(), &json); | 96 base::JSONWriter::Write(value.get(), &json); |
| 95 fprintf(file_.get(), "%s%s", (added_events_ ? ",\n" : ""), json.c_str()); | 97 fprintf(file_.get(), "%s%s", (added_events_ ? ",\n" : ""), json.c_str()); |
| 96 added_events_ = true; | 98 added_events_ = true; |
| 97 } | 99 } |
| 98 | 100 |
| 99 } // namespace net | 101 } // namespace net |
| OLD | NEW |