1// Copyright (c) 2010 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5#include "chrome/browser/net/net_log_logger.h" 6 7#include <stdio.h> 8 9#include "base/file_util.h" 10#include "base/json/json_writer.h" 11#include "base/threading/thread_restrictions.h" 12#include "base/values.h" 13 14NetLogLogger::NetLogLogger(const FilePath &log_path) 15 : ThreadSafeObserver(net::NetLog::LOG_ALL_BUT_BYTES) { 16 if (!log_path.empty()) { 17 base::ThreadRestrictions::ScopedAllowIO allow_io; 18 file_.Set(file_util::OpenFile(log_path, "w")); 19 } 20} 21 22NetLogLogger::~NetLogLogger() {} 23 24void NetLogLogger::OnAddEntry(net::NetLog::EventType type, 25 const base::TimeTicks& time, 26 const net::NetLog::Source& source, 27 net::NetLog::EventPhase phase, 28 net::NetLog::EventParameters* params) { 29 scoped_ptr<Value> value(net::NetLog::EntryToDictionaryValue(type, time, 30 source, phase, 31 params, true)); 32 // Don't pretty print, so each JSON value occupies a single line, with no 33 // breaks (Line breaks in any text field will be escaped). Using strings 34 // instead of integer identifiers allows logs from older versions to be 35 // loaded, though a little extra parsing has to be done when loading a log. 36 std::string json; 37 base::JSONWriter::Write(value.get(), false, &json); 38 if (!file_.get()) { 39 VLOG(1) << json; 40 } else { 41 fprintf(file_.get(), "%s\n", json.c_str()); 42 } 43} 44 45