Switch global error menu icon to vectorized MD asset
[chromium-blink-merge.git] / net / log / write_to_file_net_log_observer.cc
blob5c4f95142d174aff6b5b346644fa06551bc29dab
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
3 // found in the LICENSE file.
5 #include "net/log/write_to_file_net_log_observer.h"
7 #include <stdio.h>
9 #include <set>
11 #include "base/json/json_writer.h"
12 #include "base/logging.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/values.h"
15 #include "net/log/net_log_util.h"
16 #include "net/url_request/url_request_context.h"
18 namespace net {
20 WriteToFileNetLogObserver::WriteToFileNetLogObserver()
21 : capture_mode_(NetLogCaptureMode::Default()), added_events_(false) {
24 WriteToFileNetLogObserver::~WriteToFileNetLogObserver() {
27 void WriteToFileNetLogObserver::set_capture_mode(
28 NetLogCaptureMode capture_mode) {
29 DCHECK(!net_log());
30 capture_mode_ = capture_mode;
33 void WriteToFileNetLogObserver::StartObserving(
34 NetLog* net_log,
35 base::ScopedFILE file,
36 base::Value* constants,
37 URLRequestContext* url_request_context) {
38 DCHECK(file.get());
39 file_ = file.Pass();
40 added_events_ = false;
42 // Write constants to the output file. This allows loading files that have
43 // different source and event types, as they may be added and removed
44 // between Chrome versions.
45 std::string json;
46 if (constants)
47 base::JSONWriter::Write(*constants, &json);
48 else
49 base::JSONWriter::Write(*GetNetConstants(), &json);
51 fprintf(file_.get(), "{\"constants\": %s,\n", json.c_str());
53 // Start events array. It's closed in StopObserving().
54 fprintf(file_.get(), "\"events\": [\n");
56 // Add events for in progress requests if a context is given.
57 if (url_request_context) {
58 DCHECK(url_request_context->CalledOnValidThread());
60 std::set<URLRequestContext*> contexts;
61 contexts.insert(url_request_context);
62 CreateNetLogEntriesForActiveObjects(contexts, this);
65 net_log->DeprecatedAddObserver(this, capture_mode_);
68 void WriteToFileNetLogObserver::StopObserving(
69 URLRequestContext* url_request_context) {
70 net_log()->DeprecatedRemoveObserver(this);
72 // End events array.
73 fprintf(file_.get(), "]");
75 // Write state of the URLRequestContext when logging stopped.
76 if (url_request_context) {
77 DCHECK(url_request_context->CalledOnValidThread());
79 std::string json;
80 base::JSONWriter::Write(
81 *GetNetInfo(url_request_context, NET_INFO_ALL_SOURCES), &json);
82 fprintf(file_.get(), ",\"tabInfo\": %s\n", json.c_str());
84 fprintf(file_.get(), "}");
86 file_.reset();
89 void WriteToFileNetLogObserver::OnAddEntry(const NetLog::Entry& entry) {
90 // Add a comma and newline for every event but the first. Newlines are needed
91 // so can load partial log files by just ignoring the last line. For this to
92 // work, lines cannot be pretty printed.
93 scoped_ptr<base::Value> value(entry.ToValue());
94 std::string json;
95 base::JSONWriter::Write(*value, &json);
96 fprintf(file_.get(), "%s%s", (added_events_ ? ",\n" : ""), json.c_str());
97 added_events_ = true;
100 } // namespace net