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
3 // found in the LICENSE file.
7 #include "base/json/json_string_value_serializer.h"
8 #include "base/logging.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/values.h"
11 #include "net/tools/gdig/file_net_log.h"
15 FileNetLogObserver::FileNetLogObserver(FILE* destination
)
16 : destination_(destination
) {
17 DCHECK(destination
!= NULL
);
20 FileNetLogObserver::~FileNetLogObserver() {
23 void FileNetLogObserver::OnAddEntry(const net::NetLog::Entry
& entry
) {
24 // Only BoundNetLogs without a NetLog should have an invalid source.
25 DCHECK(entry
.source().IsValid());
27 const char* source
= NetLog::SourceTypeToString(entry
.source().type
);
28 const char* type
= NetLog::EventTypeToString(entry
.type());
30 scoped_ptr
<base::Value
> param_value(entry
.ParametersToValue());
32 if (param_value
.get() != NULL
) {
33 JSONStringValueSerializer
serializer(¶ms
);
34 bool ret
= serializer
.Serialize(*param_value
);
37 base::Time now
= base::Time::NowFromSystemTime();
38 base::AutoLock
lock(lock_
);
39 if (first_event_time_
.is_null()) {
40 first_event_time_
= now
;
42 base::TimeDelta elapsed_time
= now
- first_event_time_
;
43 fprintf(destination_
, "%u\t%u\t%s\t%s\t%s\n",
44 static_cast<unsigned>(elapsed_time
.InMilliseconds()),
45 entry
.source().id
, source
, type
, params
.c_str());