| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chromeos/device_event_log_impl.h" | 5 #include "components/device_event_log/device_event_log_impl.h" |
| 6 | 6 |
| 7 #include <cmath> | 7 #include <cmath> |
| 8 #include <list> | 8 #include <list> |
| 9 #include <set> | 9 #include <set> |
| 10 | 10 |
| 11 #include "base/files/file_path.h" | 11 #include "base/containers/adapters.h" |
| 12 #include "base/json/json_string_value_serializer.h" | 12 #include "base/json/json_string_value_serializer.h" |
| 13 #include "base/json/json_writer.h" | 13 #include "base/json/json_writer.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
| 16 #include "base/strings/string_tokenizer.h" | 16 #include "base/strings/string_tokenizer.h" |
| 17 #include "base/strings/string_util.h" | 17 #include "base/strings/string_util.h" |
| 18 #include "base/strings/stringprintf.h" | 18 #include "base/strings/stringprintf.h" |
| 19 #include "base/strings/utf_string_conversions.h" | 19 #include "base/strings/utf_string_conversions.h" |
| 20 #include "base/values.h" | 20 #include "base/values.h" |
| 21 #include "net/base/escape.h" | 21 #include "net/base/escape.h" |
| 22 | 22 |
| 23 namespace chromeos { | |
| 24 | |
| 25 namespace device_event_log { | 23 namespace device_event_log { |
| 26 | 24 |
| 27 namespace { | 25 namespace { |
| 28 | 26 |
| 29 const char* kLogLevelName[] = {"Error", "User", "Event", "Debug"}; | 27 const char* kLogLevelName[] = {"Error", "User", "Event", "Debug"}; |
| 30 | 28 |
| 31 const char* kLogTypeNetworkDesc = "Network"; | 29 const char* kLogTypeNetworkDesc = "Network"; |
| 32 const char* kLogTypePowerDesc = "Power"; | 30 const char* kLogTypePowerDesc = "Power"; |
| 33 const char* kLogTypeLoginDesc = "Login"; | 31 const char* kLogTypeLoginDesc = "Login"; |
| 34 | 32 |
| (...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 318 | 316 |
| 319 std::string result; | 317 std::string result; |
| 320 base::ListValue log_entries; | 318 base::ListValue log_entries; |
| 321 if (order == OLDEST_FIRST) { | 319 if (order == OLDEST_FIRST) { |
| 322 size_t offset = 0; | 320 size_t offset = 0; |
| 323 if (max_events > 0 && max_events < entries_.size()) { | 321 if (max_events > 0 && max_events < entries_.size()) { |
| 324 // Iterate backwards through the list skipping uninteresting entries to | 322 // Iterate backwards through the list skipping uninteresting entries to |
| 325 // determine the first entry to include. | 323 // determine the first entry to include. |
| 326 size_t shown_events = 0; | 324 size_t shown_events = 0; |
| 327 size_t num_entries = 0; | 325 size_t num_entries = 0; |
| 328 for (LogEntryList::const_reverse_iterator riter = entries_.rbegin(); | 326 for (const LogEntry& entry : base::Reversed(entries_)) { |
| 329 riter != entries_.rend(); ++riter) { | |
| 330 ++num_entries; | 327 ++num_entries; |
| 331 if (!LogEntryMatchesTypes(*riter, include_types, exclude_types)) | 328 if (!LogEntryMatchesTypes(entry, include_types, exclude_types)) |
| 332 continue; | 329 continue; |
| 333 if (riter->log_level > max_level) | 330 if (entry.log_level > max_level) |
| 334 continue; | 331 continue; |
| 335 if (++shown_events >= max_events) | 332 if (++shown_events >= max_events) |
| 336 break; | 333 break; |
| 337 } | 334 } |
| 338 offset = entries_.size() - num_entries; | 335 offset = entries_.size() - num_entries; |
| 339 } | 336 } |
| 340 for (LogEntryList::const_iterator iter = entries_.begin(); | 337 for (const LogEntry& entry : entries_) { |
| 341 iter != entries_.end(); ++iter) { | |
| 342 if (offset > 0) { | 338 if (offset > 0) { |
| 343 --offset; | 339 --offset; |
| 344 continue; | 340 continue; |
| 345 } | 341 } |
| 346 if (!LogEntryMatchesTypes(*iter, include_types, exclude_types)) | 342 if (!LogEntryMatchesTypes(entry, include_types, exclude_types)) |
| 347 continue; | 343 continue; |
| 348 if (iter->log_level > max_level) | 344 if (entry.log_level > max_level) |
| 349 continue; | 345 continue; |
| 350 if (format_json) { | 346 if (format_json) { |
| 351 log_entries.AppendString(LogEntryAsJSON(*iter)); | 347 log_entries.AppendString(LogEntryAsJSON(entry)); |
| 352 } else { | 348 } else { |
| 353 result += LogEntryToString(*iter, show_time, show_file, show_type, | 349 result += LogEntryToString(entry, show_time, show_file, show_type, |
| 354 show_level, format_html); | 350 show_level, format_html); |
| 355 result += "\n"; | 351 result += "\n"; |
| 356 } | 352 } |
| 357 } | 353 } |
| 358 } else { | 354 } else { |
| 359 size_t nlines = 0; | 355 size_t nlines = 0; |
| 360 // Iterate backwards through the list to show the most recent entries first. | 356 // Iterate backwards through the list to show the most recent entries first. |
| 361 for (LogEntryList::const_reverse_iterator riter = entries_.rbegin(); | 357 for (const LogEntry& entry : base::Reversed(entries_)) { |
| 362 riter != entries_.rend(); ++riter) { | 358 if (!LogEntryMatchesTypes(entry, include_types, exclude_types)) |
| 363 if (!LogEntryMatchesTypes(*riter, include_types, exclude_types)) | |
| 364 continue; | 359 continue; |
| 365 if (riter->log_level > max_level) | 360 if (entry.log_level > max_level) |
| 366 continue; | 361 continue; |
| 367 if (format_json) { | 362 if (format_json) { |
| 368 log_entries.AppendString(LogEntryAsJSON(*riter)); | 363 log_entries.AppendString(LogEntryAsJSON(entry)); |
| 369 } else { | 364 } else { |
| 370 result += LogEntryToString(*riter, show_time, show_file, show_type, | 365 result += LogEntryToString(entry, show_time, show_file, show_type, |
| 371 show_level, format_html); | 366 show_level, format_html); |
| 372 result += "\n"; | 367 result += "\n"; |
| 373 } | 368 } |
| 374 if (max_events > 0 && ++nlines >= max_events) | 369 if (max_events > 0 && ++nlines >= max_events) |
| 375 break; | 370 break; |
| 376 } | 371 } |
| 377 } | 372 } |
| 378 if (format_json) { | 373 if (format_json) { |
| 379 JSONStringValueSerializer serializer(&result); | 374 JSONStringValueSerializer serializer(&result); |
| 380 serializer.Serialize(log_entries); | 375 serializer.Serialize(log_entries); |
| 381 } | 376 } |
| 382 | 377 |
| 383 return result; | 378 return result; |
| 384 } | 379 } |
| 385 | 380 |
| 386 DeviceEventLogImpl::LogEntry::LogEntry(const char* filedesc, | 381 DeviceEventLogImpl::LogEntry::LogEntry(const char* filedesc, |
| 387 int file_line, | 382 int file_line, |
| 388 LogType log_type, | 383 LogType log_type, |
| 389 LogLevel log_level, | 384 LogLevel log_level, |
| 390 const std::string& event) | 385 const std::string& event) |
| 391 : file_line(file_line), | 386 : file_line(file_line), |
| 392 log_type(log_type), | 387 log_type(log_type), |
| 393 log_level(log_level), | 388 log_level(log_level), |
| 394 event(event), | 389 event(event), |
| 395 time(base::Time::Now()), | 390 time(base::Time::Now()), |
| 396 count(1) { | 391 count(1) { |
| 397 if (filedesc) | 392 if (filedesc) { |
| 398 file = base::FilePath(std::string(filedesc)).BaseName().value(); | 393 file = filedesc; |
| 394 size_t last_slash_pos = file.find_last_of("\\/"); |
| 395 if (last_slash_pos != std::string::npos) { |
| 396 file.erase(0, last_slash_pos + 1); |
| 397 } |
| 398 } |
| 399 } | 399 } |
| 400 | 400 |
| 401 } // namespace device_event_log | 401 } // namespace device_event_log |
| 402 | |
| 403 } // namespace chromeos | |
| OLD | NEW |