| OLD | NEW |
| 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/net_log_logger.h" | 5 #include "chrome/browser/net/net_log_logger.h" |
| 6 | 6 |
| 7 #include <stdio.h> | 7 #include <stdio.h> |
| 8 | 8 |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/threading/thread_restrictions.h" | 13 #include "base/threading/thread_restrictions.h" |
| 14 #include "base/values.h" | 14 #include "base/values.h" |
| 15 #include "chrome/browser/ui/webui/net_internals/net_internals_ui.h" | 15 #include "chrome/browser/ui/webui/net_internals/net_internals_ui.h" |
| 16 | 16 |
| 17 NetLogLogger::NetLogLogger(const FilePath &log_path) { | 17 NetLogLogger::NetLogLogger(const FilePath &log_path) { |
| 18 if (!log_path.empty()) { | 18 if (!log_path.empty()) { |
| 19 base::ThreadRestrictions::ScopedAllowIO allow_io; | 19 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 20 file_.Set(file_util::OpenFile(log_path, "w")); | 20 file_.Set(file_util::OpenFile(log_path, "w")); |
| 21 | 21 |
| 22 // Write constants to the output file. This allows loading files that have | 22 // Write constants to the output file. This allows loading files that have |
| 23 // different source and event types, as they may be added and removed | 23 // different source and event types, as they may be added and removed |
| 24 // between Chrome versions. | 24 // between Chrome versions. |
| 25 scoped_ptr<Value> value(NetInternalsUI::GetConstants()); | 25 scoped_ptr<Value> value(NetInternalsUI::GetConstants()); |
| 26 std::string json; | 26 std::string json; |
| 27 base::JSONWriter::Write(value.get(), false, &json); | 27 base::JSONWriter::Write(value.get(), &json); |
| 28 fprintf(file_.get(), "{\"constants\": %s,\n", json.c_str()); | 28 fprintf(file_.get(), "{\"constants\": %s,\n", json.c_str()); |
| 29 fprintf(file_.get(), "\"events\": [\n"); | 29 fprintf(file_.get(), "\"events\": [\n"); |
| 30 } | 30 } |
| 31 } | 31 } |
| 32 | 32 |
| 33 NetLogLogger::~NetLogLogger() { | 33 NetLogLogger::~NetLogLogger() { |
| 34 } | 34 } |
| 35 | 35 |
| 36 void NetLogLogger::StartObserving(net::NetLog* net_log) { | 36 void NetLogLogger::StartObserving(net::NetLog* net_log) { |
| 37 net_log->AddThreadSafeObserver(this, net::NetLog::LOG_ALL_BUT_BYTES); | 37 net_log->AddThreadSafeObserver(this, net::NetLog::LOG_ALL_BUT_BYTES); |
| 38 } | 38 } |
| 39 | 39 |
| 40 void NetLogLogger::OnAddEntry(net::NetLog::EventType type, | 40 void NetLogLogger::OnAddEntry(net::NetLog::EventType type, |
| 41 const base::TimeTicks& time, | 41 const base::TimeTicks& time, |
| 42 const net::NetLog::Source& source, | 42 const net::NetLog::Source& source, |
| 43 net::NetLog::EventPhase phase, | 43 net::NetLog::EventPhase phase, |
| 44 net::NetLog::EventParameters* params) { | 44 net::NetLog::EventParameters* params) { |
| 45 scoped_ptr<Value> value( | 45 scoped_ptr<Value> value( |
| 46 net::NetLog::EntryToDictionaryValue( | 46 net::NetLog::EntryToDictionaryValue( |
| 47 type, time, source, phase, params, false)); | 47 type, time, source, phase, params, false)); |
| 48 // Don't pretty print, so each JSON value occupies a single line, with no | 48 // Don't pretty print, so each JSON value occupies a single line, with no |
| 49 // breaks (Line breaks in any text field will be escaped). Using strings | 49 // breaks (Line breaks in any text field will be escaped). Using strings |
| 50 // instead of integer identifiers allows logs from older versions to be | 50 // instead of integer identifiers allows logs from older versions to be |
| 51 // loaded, though a little extra parsing has to be done when loading a log. | 51 // loaded, though a little extra parsing has to be done when loading a log. |
| 52 std::string json; | 52 std::string json; |
| 53 base::JSONWriter::Write(value.get(), false, &json); | 53 base::JSONWriter::Write(value.get(), &json); |
| 54 if (!file_.get()) { | 54 if (!file_.get()) { |
| 55 VLOG(1) << json; | 55 VLOG(1) << json; |
| 56 } else { | 56 } else { |
| 57 fprintf(file_.get(), "%s,\n", json.c_str()); | 57 fprintf(file_.get(), "%s,\n", json.c_str()); |
| 58 } | 58 } |
| 59 } | 59 } |
| OLD | NEW |