Explicitly add python-numpy dependency to install-build-deps.
[chromium-blink-merge.git] / net / base / net_log_logger.cc
blobbb0331badeff0560333a0afd8b6bb418ed858b4a
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"
7 #include <stdio.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"
15 namespace net {
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);
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");
32 NetLogLogger::~NetLogLogger() {
33 if (file_.get())
34 fprintf(file_.get(), "]}");
37 void NetLogLogger::set_log_level(net::NetLog::LogLevel log_level) {
38 DCHECK(!net_log());
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());
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;
63 // static
64 base::DictionaryValue* NetLogLogger::GetConstants() {
65 return GetNetConstants().release();
68 } // namespace net