Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(123)

Side by Side Diff: chrome/browser/net/net_log_logger.cc

Issue 11635023: First cut at UI for saving net_logs data into a temporary file on (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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) : net_log_(NULL) {
18 if (!log_path.empty()) { 18 if (!log_path.empty()) {
19 base::ThreadRestrictions::ScopedAllowIO allow_io; 19 base::ThreadRestrictions::ScopedAllowIO allow_io;
20 FILE* fp = file_util::OpenFile(log_path, "w"); 20 FILE* fp = file_util::OpenFile(log_path, "w");
21 if (!fp) { 21 if (!fp) {
22 LOG(ERROR) << "Could not open file " << log_path.value() 22 LOG(ERROR) << "Could not open file " << log_path.value()
23 << " for net logging"; 23 << " for net logging";
24 return; 24 return;
25 } 25 }
26 file_.Set(fp); 26 file_.Set(fp);
27 27
28 // Write constants to the output file. This allows loading files that have 28 // Write constants to the output file. This allows loading files that have
29 // different source and event types, as they may be added and removed 29 // different source and event types, as they may be added and removed
30 // between Chrome versions. 30 // between Chrome versions.
31 scoped_ptr<Value> value(NetInternalsUI::GetConstants()); 31 scoped_ptr<Value> value(NetInternalsUI::GetConstants());
32 std::string json; 32 std::string json;
33 base::JSONWriter::Write(value.get(), &json); 33 base::JSONWriter::Write(value.get(), &json);
34 fprintf(file_.get(), "{\"constants\": %s,\n", json.c_str()); 34 fprintf(file_.get(), "{\"constants\": %s,\n", json.c_str());
35 fprintf(file_.get(), "\"events\": [\n"); 35 fprintf(file_.get(), "\"events\": [\n");
36 } 36 }
37 } 37 }
38 38
39 NetLogLogger::~NetLogLogger() { 39 NetLogLogger::~NetLogLogger() {
40 } 40 }
41 41
42 void NetLogLogger::StartObserving(net::NetLog* net_log) { 42 void NetLogLogger::StartObserving(net::NetLog* net_log) {
43 net_log->AddThreadSafeObserver(this, net::NetLog::LOG_ALL_BUT_BYTES); 43 net_log->AddThreadSafeObserver(this, net::NetLog::LOG_ALL_BUT_BYTES);
44 net_log_ = net_log;
45 }
46
47 void NetLogLogger::StopObserving() {
48 DCHECK(net_log_);
49 net_log_->RemoveThreadSafeObserver(this);
50 net_log_ = NULL;
44 } 51 }
45 52
46 void NetLogLogger::OnAddEntry(const net::NetLog::Entry& entry) { 53 void NetLogLogger::OnAddEntry(const net::NetLog::Entry& entry) {
47 scoped_ptr<Value> value(entry.ToValue()); 54 scoped_ptr<Value> value(entry.ToValue());
48 // Don't pretty print, so each JSON value occupies a single line, with no 55 // 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 56 // breaks (Line breaks in any text field will be escaped). Using strings
50 // instead of integer identifiers allows logs from older versions to be 57 // 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. 58 // loaded, though a little extra parsing has to be done when loading a log.
52 std::string json; 59 std::string json;
53 base::JSONWriter::Write(value.get(), &json); 60 base::JSONWriter::Write(value.get(), &json);
54 if (!file_.get()) { 61 if (!file_.get()) {
55 VLOG(1) << json; 62 VLOG(1) << json;
56 } else { 63 } else {
57 fprintf(file_.get(), "%s,\n", json.c_str()); 64 fprintf(file_.get(), "%s,\n", json.c_str());
58 } 65 }
59 } 66 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698