ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / components / device_event_log / device_event_log_impl.cc
blob3d7b8862b49eb2480a09f6e9bc7da77946f27a5f
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.
5 #include "components/device_event_log/device_event_log_impl.h"
7 #include <cmath>
8 #include <list>
9 #include <set>
11 #include "base/containers/adapters.h"
12 #include "base/json/json_string_value_serializer.h"
13 #include "base/json/json_writer.h"
14 #include "base/logging.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/strings/string_tokenizer.h"
17 #include "base/strings/string_util.h"
18 #include "base/strings/stringprintf.h"
19 #include "base/strings/utf_string_conversions.h"
20 #include "base/values.h"
21 #include "net/base/escape.h"
23 namespace device_event_log {
25 namespace {
27 const char* kLogLevelName[] = {"Error", "User", "Event", "Debug"};
29 const char* kLogTypeNetworkDesc = "Network";
30 const char* kLogTypePowerDesc = "Power";
31 const char* kLogTypeLoginDesc = "Login";
32 const char* kLogTypeUsbDesc = "USB";
33 const char* kLogTypeHidDesc = "HID";
35 std::string GetLogTypeString(LogType type) {
36 switch (type) {
37 case LOG_TYPE_NETWORK:
38 return kLogTypeNetworkDesc;
39 case LOG_TYPE_POWER:
40 return kLogTypePowerDesc;
41 case LOG_TYPE_LOGIN:
42 return kLogTypeLoginDesc;
43 case LOG_TYPE_USB:
44 return kLogTypeUsbDesc;
45 case LOG_TYPE_HID:
46 return kLogTypeHidDesc;
47 default:
48 NOTREACHED();
49 return "Unknown";
53 std::string DateAndTimeWithMicroseconds(const base::Time& time) {
54 base::Time::Exploded exploded;
55 time.LocalExplode(&exploded);
56 // base::Time::Exploded does not include microseconds, but sometimes we need
57 // microseconds, so append '.' + usecs to the end of the formatted string.
58 int usecs = static_cast<int>(fmod(time.ToDoubleT() * 1000000, 1000000));
59 return base::StringPrintf("%04d/%02d/%02d %02d:%02d:%02d.%06d", exploded.year,
60 exploded.month, exploded.day_of_month,
61 exploded.hour, exploded.minute, exploded.second,
62 usecs);
65 std::string TimeWithSeconds(const base::Time& time) {
66 base::Time::Exploded exploded;
67 time.LocalExplode(&exploded);
68 return base::StringPrintf("%02d:%02d:%02d", exploded.hour, exploded.minute,
69 exploded.second);
72 std::string TimeWithMillieconds(const base::Time& time) {
73 base::Time::Exploded exploded;
74 time.LocalExplode(&exploded);
75 return base::StringPrintf("%02d:%02d:%02d.%03d", exploded.hour,
76 exploded.minute, exploded.second,
77 exploded.millisecond);
80 // Defined below for easier review. TODO(stevenjb): Move implementation here.
81 std::string GetHtmlText(LogLevel log_level, const std::string& event);
83 std::string LogEntryToString(const DeviceEventLogImpl::LogEntry& log_entry,
84 bool show_time,
85 bool show_file,
86 bool show_type,
87 bool show_level,
88 bool format_html) {
89 std::string line;
90 if (show_time)
91 line += "[" + TimeWithMillieconds(log_entry.time) + "] ";
92 if (show_type)
93 line += GetLogTypeString(log_entry.log_type) + ": ";
94 if (show_level) {
95 const char* kLevelDesc[] = {"ERROR", "USER", "EVENT", "DEBUG"};
96 line += base::StringPrintf("%s: ", kLevelDesc[log_entry.log_level]);
98 if (show_file) {
99 std::string filestr =
100 format_html ? net::EscapeForHTML(log_entry.file) : log_entry.file;
101 line += base::StringPrintf("%s:%d ", log_entry.file.c_str(),
102 log_entry.file_line);
104 line += format_html ? GetHtmlText(log_entry.log_level, log_entry.event)
105 : log_entry.event;
106 if (log_entry.count > 1)
107 line += base::StringPrintf(" (%d)", log_entry.count);
108 return line;
111 void LogEntryToDictionary(const DeviceEventLogImpl::LogEntry& log_entry,
112 base::DictionaryValue* output) {
113 output->SetString("timestamp", DateAndTimeWithMicroseconds(log_entry.time));
114 output->SetString("timestampshort", TimeWithSeconds(log_entry.time));
115 output->SetString("level", kLogLevelName[log_entry.log_level]);
116 output->SetString("type", GetLogTypeString(log_entry.log_type));
117 output->SetString("file", base::StringPrintf("%s:%d ", log_entry.file.c_str(),
118 log_entry.file_line));
119 output->SetString("event", log_entry.event);
122 std::string LogEntryAsJSON(const DeviceEventLogImpl::LogEntry& log_entry) {
123 base::DictionaryValue entry_dict;
124 LogEntryToDictionary(log_entry, &entry_dict);
125 std::string json;
126 JSONStringValueSerializer serializer(&json);
127 if (!serializer.Serialize(entry_dict)) {
128 LOG(ERROR) << "Failed to serialize to JSON";
130 return json;
133 std::string GetHtmlText(LogLevel log_level, const std::string& event) {
134 std::string text;
135 if (log_level == LOG_LEVEL_DEBUG)
136 text += "<i>";
137 else if (log_level == LOG_LEVEL_USER)
138 text += "<b>";
139 else if (log_level == LOG_LEVEL_ERROR)
140 text += "<b><i>";
142 text += net::EscapeForHTML(event);
144 if (log_level == LOG_LEVEL_DEBUG)
145 text += "</i>";
146 else if (log_level == LOG_LEVEL_USER)
147 text += "</b>";
148 else if (log_level == LOG_LEVEL_ERROR)
149 text += "</i></b>";
150 return text;
153 void SendLogEntryToVLogOrErrorLog(
154 const DeviceEventLogImpl::LogEntry& log_entry) {
155 if (log_entry.log_level != LOG_LEVEL_ERROR && !VLOG_IS_ON(1))
156 return;
157 const bool show_time = true;
158 const bool show_file = true;
159 const bool show_type = true;
160 const bool show_level = false;
161 const bool format_html = false;
162 std::string output = LogEntryToString(log_entry, show_time, show_file,
163 show_type, show_level, format_html);
164 if (log_entry.log_level == LOG_LEVEL_ERROR)
165 LOG(ERROR) << output;
166 else
167 VLOG(1) << output;
170 bool LogEntryMatches(const DeviceEventLogImpl::LogEntry& first,
171 const DeviceEventLogImpl::LogEntry& second) {
172 return first.file == second.file && first.file_line == second.file_line &&
173 first.log_level == second.log_level &&
174 first.log_type == second.log_type && first.event == second.event;
177 bool LogEntryMatchesTypes(const DeviceEventLogImpl::LogEntry& entry,
178 const std::set<LogType>& include_types,
179 const std::set<LogType>& exclude_types) {
180 if (include_types.empty() && exclude_types.empty())
181 return true;
182 if (!include_types.empty() && include_types.count(entry.log_type))
183 return true;
184 if (!exclude_types.empty() && !exclude_types.count(entry.log_type))
185 return true;
186 return false;
189 void GetFormat(const std::string& format_string,
190 bool* show_time,
191 bool* show_file,
192 bool* show_type,
193 bool* show_level,
194 bool* format_html,
195 bool* format_json) {
196 base::StringTokenizer tokens(format_string, ",");
197 *show_time = false;
198 *show_file = false;
199 *show_type = false;
200 *show_level = false;
201 *format_html = false;
202 *format_json = false;
203 while (tokens.GetNext()) {
204 std::string tok(tokens.token());
205 if (tok == "time")
206 *show_time = true;
207 if (tok == "file")
208 *show_file = true;
209 if (tok == "type")
210 *show_type = true;
211 if (tok == "level")
212 *show_level = true;
213 if (tok == "html")
214 *format_html = true;
215 if (tok == "json")
216 *format_json = true;
220 LogType LogTypeFromString(const std::string& desc) {
221 std::string desc_lc = base::StringToLowerASCII(desc);
222 if (desc_lc == "network")
223 return LOG_TYPE_NETWORK;
224 if (desc_lc == "power")
225 return LOG_TYPE_POWER;
226 if (desc_lc == "login")
227 return LOG_TYPE_LOGIN;
228 NOTREACHED() << "Unrecogized LogType: " << desc;
229 return LOG_TYPE_UNKNOWN;
232 void GetLogTypes(const std::string& types,
233 std::set<LogType>* include_types,
234 std::set<LogType>* exclude_types) {
235 base::StringTokenizer tokens(types, ",");
236 while (tokens.GetNext()) {
237 std::string tok(tokens.token());
238 if (tok.substr(0, 4) == "non-") {
239 LogType type = LogTypeFromString(tok.substr(4));
240 if (type != LOG_TYPE_UNKNOWN)
241 exclude_types->insert(type);
242 } else {
243 LogType type = LogTypeFromString(tok);
244 if (type != LOG_TYPE_UNKNOWN)
245 include_types->insert(type);
250 } // namespace
252 // static
253 void DeviceEventLogImpl::SendToVLogOrErrorLog(const char* file,
254 int file_line,
255 LogType log_type,
256 LogLevel log_level,
257 const std::string& event) {
258 LogEntry entry(file, file_line, log_type, log_level, event);
259 SendLogEntryToVLogOrErrorLog(entry);
262 DeviceEventLogImpl::DeviceEventLogImpl(size_t max_entries)
263 : max_entries_(max_entries) {
266 DeviceEventLogImpl::~DeviceEventLogImpl() {
269 void DeviceEventLogImpl::AddEntry(const char* file,
270 int file_line,
271 LogType log_type,
272 LogLevel log_level,
273 const std::string& event) {
274 LogEntry entry(file, file_line, log_type, log_level, event);
275 AddLogEntry(entry);
278 void DeviceEventLogImpl::AddLogEntry(const LogEntry& entry) {
279 if (!entries_.empty()) {
280 LogEntry& last = entries_.back();
281 if (LogEntryMatches(last, entry)) {
282 // Update count and time for identical events to avoid log spam.
283 ++last.count;
284 last.log_level = std::min(last.log_level, entry.log_level);
285 last.time = base::Time::Now();
286 return;
289 if (entries_.size() >= max_entries_) {
290 const size_t max_error_entries = max_entries_ / 2;
291 // Remove the first (oldest) non-error entry, or the oldest entry if more
292 // than half the entries are errors.
293 size_t error_count = 0;
294 for (LogEntryList::iterator iter = entries_.begin(); iter != entries_.end();
295 ++iter) {
296 if (iter->log_level != LOG_LEVEL_ERROR) {
297 entries_.erase(iter);
298 break;
300 if (++error_count > max_error_entries) {
301 // Too many error entries, remove the oldest entry.
302 entries_.pop_front();
303 break;
307 entries_.push_back(entry);
308 SendLogEntryToVLogOrErrorLog(entry);
311 std::string DeviceEventLogImpl::GetAsString(StringOrder order,
312 const std::string& format,
313 const std::string& types,
314 LogLevel max_level,
315 size_t max_events) {
316 if (entries_.empty())
317 return "No Log Entries.";
319 bool show_time, show_file, show_type, show_level, format_html, format_json;
320 GetFormat(format, &show_time, &show_file, &show_type, &show_level,
321 &format_html, &format_json);
323 std::set<LogType> include_types, exclude_types;
324 GetLogTypes(types, &include_types, &exclude_types);
326 std::string result;
327 base::ListValue log_entries;
328 if (order == OLDEST_FIRST) {
329 size_t offset = 0;
330 if (max_events > 0 && max_events < entries_.size()) {
331 // Iterate backwards through the list skipping uninteresting entries to
332 // determine the first entry to include.
333 size_t shown_events = 0;
334 size_t num_entries = 0;
335 for (const LogEntry& entry : base::Reversed(entries_)) {
336 ++num_entries;
337 if (!LogEntryMatchesTypes(entry, include_types, exclude_types))
338 continue;
339 if (entry.log_level > max_level)
340 continue;
341 if (++shown_events >= max_events)
342 break;
344 offset = entries_.size() - num_entries;
346 for (const LogEntry& entry : entries_) {
347 if (offset > 0) {
348 --offset;
349 continue;
351 if (!LogEntryMatchesTypes(entry, include_types, exclude_types))
352 continue;
353 if (entry.log_level > max_level)
354 continue;
355 if (format_json) {
356 log_entries.AppendString(LogEntryAsJSON(entry));
357 } else {
358 result += LogEntryToString(entry, show_time, show_file, show_type,
359 show_level, format_html);
360 result += "\n";
363 } else {
364 size_t nlines = 0;
365 // Iterate backwards through the list to show the most recent entries first.
366 for (const LogEntry& entry : base::Reversed(entries_)) {
367 if (!LogEntryMatchesTypes(entry, include_types, exclude_types))
368 continue;
369 if (entry.log_level > max_level)
370 continue;
371 if (format_json) {
372 log_entries.AppendString(LogEntryAsJSON(entry));
373 } else {
374 result += LogEntryToString(entry, show_time, show_file, show_type,
375 show_level, format_html);
376 result += "\n";
378 if (max_events > 0 && ++nlines >= max_events)
379 break;
382 if (format_json) {
383 JSONStringValueSerializer serializer(&result);
384 serializer.Serialize(log_entries);
387 return result;
390 DeviceEventLogImpl::LogEntry::LogEntry(const char* filedesc,
391 int file_line,
392 LogType log_type,
393 LogLevel log_level,
394 const std::string& event)
395 : file_line(file_line),
396 log_type(log_type),
397 log_level(log_level),
398 event(event),
399 time(base::Time::Now()),
400 count(1) {
401 if (filedesc) {
402 file = filedesc;
403 size_t last_slash_pos = file.find_last_of("\\/");
404 if (last_slash_pos != std::string::npos) {
405 file.erase(0, last_slash_pos + 1);
410 } // namespace device_event_log