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.
5 #include "chromeos/network/network_event_log.h"
7 #include "base/i18n/time_formatting.h"
8 #include "base/logging.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/stringprintf.h"
11 #include "base/utf_string_conversions.h"
15 namespace network_event_log
{
20 LogEntry(const std::string
& module
,
21 const std::string
& event
,
22 const std::string
& description
);
24 std::string
ToString() const;
26 bool ContentEquals(const LogEntry
& other
) const;
30 std::string description
;
35 LogEntry::LogEntry(const std::string
& module
,
36 const std::string
& event
,
37 const std::string
& description
)
40 description(description
),
41 time(base::Time::Now()),
45 std::string
LogEntry::ToString() const {
47 line
+= "[" + UTF16ToUTF8(base::TimeFormatShortDateAndTime(time
)) + "]";
48 line
+= " " + module
+ ":" + event
;
49 if (!description
.empty())
50 line
+= ": " + description
;
52 line
+= base::StringPrintf(" (%d)", count
);
57 bool LogEntry::ContentEquals(const LogEntry
& other
) const {
58 return module
== other
.module
&&
59 event
== other
.event
&&
60 description
== other
.description
;
63 typedef std::deque
<LogEntry
> LogEntryList
;
65 class NetworkEventLog
{
70 void AddEntry(const LogEntry
& entry
);
72 std::string
GetAsString(StringOrder order
,
76 LogEntryList entries_
;
78 DISALLOW_COPY_AND_ASSIGN(NetworkEventLog
);
81 void NetworkEventLog::AddEntry(const LogEntry
& entry
) {
82 if (!entries_
.empty()) {
83 LogEntry
& last
= entries_
.back();
84 if (last
.ContentEquals(entry
)) {
85 // Update count and time for identical events to avoid log spam.
87 last
.time
= base::Time::Now();
91 if (entries_
.size() >= kMaxNetworkEventLogEntries
)
93 entries_
.push_back(entry
);
94 VLOG(1) << entry
.ToString();
97 std::string
NetworkEventLog::GetAsString(StringOrder order
,
100 return "No Log Entries.";
103 if (order
== OLDEST_FIRST
) {
105 if (max_events
> 0 && max_events
< entries_
.size())
106 offset
= entries_
.size() - max_events
;
107 for (LogEntryList::const_iterator iter
= entries_
.begin() + offset
;
108 iter
!= entries_
.end(); ++iter
) {
109 result
+= (*iter
).ToString();
113 // Iterate backwards through the list to show the most recent entries first.
114 for (LogEntryList::const_reverse_iterator riter
= entries_
.rbegin();
115 riter
!= entries_
.rend(); ++riter
) {
116 result
+= (*riter
).ToString();
117 if (max_events
> 0 && ++nlines
>= max_events
)
126 NetworkEventLog
* g_network_event_log
= NULL
;
127 const size_t kMaxNetworkEventLogEntries
= 1000;
130 if (g_network_event_log
)
131 delete g_network_event_log
; // reset log
132 g_network_event_log
= new NetworkEventLog();
136 delete g_network_event_log
;
137 g_network_event_log
= NULL
;
140 bool IsInitialized() {
141 return g_network_event_log
!= NULL
;
144 void AddEntry(const std::string
& module
,
145 const std::string
& event
,
146 const std::string
& description
) {
147 LogEntry
entry(module
, event
, description
);
148 if (!g_network_event_log
) {
149 VLOG(1) << entry
.ToString();
152 g_network_event_log
->AddEntry(entry
);
155 std::string
GetAsString(StringOrder order
, size_t max_events
) {
156 if (!g_network_event_log
)
157 return "NetworkEventLog not intitialized.";
158 return g_network_event_log
->GetAsString(order
, max_events
);
161 } // namespace network_event_log
163 } // namespace chromeos