OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 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/trace_net_log_observer.h" | |
6 | |
7 #include <stdio.h> | |
8 | |
9 #include "base/debug/trace_event.h" | |
mmenke
2014/08/21 15:35:40
nit: Probably safe to leave this out, since heade
xunjieli
2014/08/22 17:20:09
Done. Thanks!
| |
10 #include "base/json/json_writer.h" | |
11 #include "base/logging.h" | |
12 #include "base/memory/scoped_ptr.h" | |
13 | |
14 namespace net { | |
15 | |
16 namespace { | |
17 | |
18 class TracedValue : public base::debug::ConvertableToTraceFormat { | |
19 public: | |
20 explicit TracedValue(scoped_ptr<base::Value> value) : value_(value.Pass()) {} | |
21 | |
22 protected: | |
mmenke
2014/08/21 15:35:39
Suggest either making these private or public - pr
xunjieli
2014/08/22 17:20:09
Done. I see. Thank you!
On 2014/08/21 15:35:39,
| |
23 virtual ~TracedValue() {} | |
24 virtual void AppendAsTraceFormat(std::string* out) const OVERRIDE { | |
25 if (value_) { | |
26 std::string tmp; | |
27 base::JSONWriter::Write(value_.get(), &tmp); | |
28 *out += tmp; | |
29 } else { | |
30 *out += "\"\""; | |
31 } | |
32 } | |
33 | |
34 private: | |
35 scoped_ptr<base::Value> value_; | |
36 }; | |
37 } // namespace | |
mmenke
2014/08/21 15:35:39
nit: Blank line above end of namespace.
xunjieli
2014/08/22 17:20:10
Done. Thanks! I will keep this in mind next time!
| |
38 | |
39 TraceNetLogObserver::TraceNetLogObserver(): | |
40 log_level_(NetLog::LOG_STRIP_PRIVATE_DATA) { | |
mmenke
2014/08/21 15:35:40
Initialize net_log_to_watch_ to NULL.
xunjieli
2014/08/22 17:20:09
Done.
| |
41 } | |
42 | |
43 TraceNetLogObserver::~TraceNetLogObserver() { | |
mmenke
2014/08/21 15:35:39
Maybe:
DCHECK(!net_log_to_watch_);
DCHECK(!net_lo
xunjieli
2014/08/22 17:20:09
Done.
| |
44 } | |
45 | |
46 void TraceNetLogObserver::StartObserving(NetLog* net_log) { | |
47 net_log->AddThreadSafeObserver(this, log_level_); | |
48 } | |
49 | |
50 void TraceNetLogObserver::StopObserving() { | |
51 net_log()->RemoveThreadSafeObserver(this); | |
52 } | |
53 | |
54 void TraceNetLogObserver::OnAddEntry(const NetLog::Entry& entry) { | |
55 scoped_ptr<base::Value> value(entry.ToValue()); | |
56 switch (entry.phase()) { | |
57 case(NetLog::PHASE_BEGIN): | |
mmenke
2014/08/21 15:35:40
nit: case NetLog::PHASE_BEGIN:
(Same goes for th
xunjieli
2014/08/22 17:20:09
Done.Thanks!
| |
58 TRACE_EVENT_NET_LOG_BEGIN( | |
mmenke
2014/08/21 15:35:39
If this is only for NetLog events, then the macro
xunjieli
2014/08/22 17:20:09
Done.
| |
59 "netlog", NetLog::EventTypeToString(entry.type()), entry.source().id, | |
60 "value", scoped_refptr<base::debug::ConvertableToTraceFormat>( | |
61 new TracedValue(value.Pass()))); | |
mmenke
2014/08/21 15:35:39
Optional: This is a bit redundant. Value include
xunjieli
2014/08/22 17:20:10
Done. This is a good idea! Thanks!
On 2014/08/21 1
| |
62 break; | |
63 case(NetLog::PHASE_END): | |
64 TRACE_EVENT_NET_LOG_END( | |
65 "netlog", NetLog::EventTypeToString(entry.type()), entry.source().id, | |
66 "value", scoped_refptr<base::debug::ConvertableToTraceFormat>( | |
67 new TracedValue(value.Pass()))); | |
68 break; | |
69 case(NetLog::PHASE_NONE): | |
70 TRACE_EVENT_NET_LOG_INSTANT( | |
71 "netlog", NetLog::EventTypeToString(entry.type()), entry.source().id, | |
72 "value", scoped_refptr<base::debug::ConvertableToTraceFormat>( | |
73 new TracedValue(value.Pass()))); | |
74 break; | |
75 default: | |
76 NOTREACHED(); | |
mmenke
2014/08/21 15:35:39
Get rid of default path. Clang is configured to e
xunjieli
2014/08/22 17:20:09
Do you mean not doing anything in the default bran
| |
77 } | |
78 } | |
79 | |
80 void TraceNetLogObserver::WatchForTraceStart(NetLog* net_log) { | |
81 net_log_to_watch_ = net_log; | |
82 base::debug::TraceLog::GetInstance()->AddEnabledStateObserver(this); | |
83 } | |
84 | |
85 void TraceNetLogObserver::StopWatchForTraceStart() { | |
86 // Should only stop if is currently watching. | |
87 DCHECK(net_log_to_watch_); | |
88 base::debug::TraceLog::GetInstance()->RemoveEnabledStateObserver(this); | |
89 if (net_log()) | |
90 net_log()->RemoveThreadSafeObserver(this); | |
91 } | |
mmenke
2014/08/21 15:35:39
net_log_to_watch_ = NULL?
xunjieli
2014/08/22 17:20:09
Done.
| |
92 | |
93 void TraceNetLogObserver::OnTraceLogEnabled() { | |
94 StartObserving(net_log_to_watch_); | |
95 } | |
96 | |
97 void TraceNetLogObserver::OnTraceLogDisabled() { | |
98 if (net_log()) | |
99 StopObserving(); | |
100 } | |
101 | |
102 } // namespace net | |
OLD | NEW |