OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "net/base/net_log_logger.h" | |
6 | |
7 #include <stdio.h> | |
8 | |
9 #include "base/json/json_writer.h" | |
10 #include "base/logging.h" | |
11 #include "base/memory/scoped_ptr.h" | |
12 #include "base/values.h" | |
13 #include "net/base/net_log_util.h" | |
14 | |
15 namespace net { | |
16 | |
17 NetLogLogger::NetLogLogger(FILE* file, const base::Value& constants) | |
18 : file_(file), | |
19 log_level_(NetLog::LOG_STRIP_PRIVATE_DATA), | |
20 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 } | |
31 | |
32 NetLogLogger::~NetLogLogger() { | |
33 if (file_.get()) | |
34 fprintf(file_.get(), "]}"); | |
35 } | |
36 | |
37 void NetLogLogger::set_log_level(net::NetLog::LogLevel log_level) { | |
38 DCHECK(!net_log()); | |
39 log_level_ = log_level; | |
40 } | |
41 | |
42 void NetLogLogger::StartObserving(net::NetLog* net_log) { | |
43 net_log->AddThreadSafeObserver(this, log_level_); | |
44 } | |
45 | |
46 void NetLogLogger::StopObserving() { | |
47 net_log()->RemoveThreadSafeObserver(this); | |
48 } | |
49 | |
50 void NetLogLogger::OnAddEntry(const net::NetLog::Entry& entry) { | |
51 // 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 | |
53 // work, lines cannot be pretty printed. | |
54 scoped_ptr<base::Value> value(entry.ToValue()); | |
55 std::string json; | |
56 base::JSONWriter::Write(value.get(), &json); | |
57 fprintf(file_.get(), "%s%s", | |
58 (added_events_ ? ",\n" : ""), | |
59 json.c_str()); | |
60 added_events_ = true; | |
61 } | |
62 | |
63 // static | |
64 base::DictionaryValue* NetLogLogger::GetConstants() { | |
65 return GetNetConstants().release(); | |
66 } | |
67 | |
68 } // namespace net | |
OLD | NEW |