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/base/net_log_logger.h"
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"
17 NetLogLogger::NetLogLogger(FILE* file
, const base::Value
& constants
)
19 log_level_(NetLog::LOG_STRIP_PRIVATE_DATA
),
20 added_events_(false) {
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.
27 base::JSONWriter::Write(&constants
, &json
);
28 fprintf(file_
.get(), "{\"constants\": %s,\n", json
.c_str());
29 fprintf(file_
.get(), "\"events\": [\n");
32 NetLogLogger::~NetLogLogger() {
34 fprintf(file_
.get(), "]}");
37 void NetLogLogger::set_log_level(net::NetLog::LogLevel log_level
) {
39 log_level_
= log_level
;
42 void NetLogLogger::StartObserving(net::NetLog
* net_log
) {
43 net_log
->AddThreadSafeObserver(this, log_level_
);
46 void NetLogLogger::StopObserving() {
47 net_log()->RemoveThreadSafeObserver(this);
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());
56 base::JSONWriter::Write(value
.get(), &json
);
57 fprintf(file_
.get(), "%s%s",
58 (added_events_
? ",\n" : ""),
64 base::DictionaryValue
* NetLogLogger::GetConstants() {
65 return GetNetConstants().release();