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