device_event_log_impl.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. #ifndef COMPONENTS_DEVICE_EVENT_LOG_DEVICE_EVENT_LOG_IMPL_H_
  5. #define COMPONENTS_DEVICE_EVENT_LOG_DEVICE_EVENT_LOG_IMPL_H_
  6. #include <stddef.h>
  7. #include <list>
  8. #include <string>
  9. #include "base/memory/ref_counted.h"
  10. #include "base/memory/weak_ptr.h"
  11. #include "base/task/single_thread_task_runner.h"
  12. #include "base/time/time.h"
  13. #include "components/device_event_log/device_event_log.h"
  14. namespace device_event_log {
  15. // Implementation class for DEVICE_LOG.
  16. class DEVICE_EVENT_LOG_EXPORT DeviceEventLogImpl {
  17. public:
  18. struct LogEntry {
  19. LogEntry(const char* filedesc,
  20. int file_line,
  21. LogType log_type,
  22. LogLevel log_level,
  23. const std::string& event);
  24. LogEntry(const char* filedesc,
  25. int file_line,
  26. LogType log_type,
  27. LogLevel log_level,
  28. const std::string& event,
  29. base::Time time_for_testing);
  30. LogEntry(const LogEntry& other);
  31. std::string file;
  32. int file_line;
  33. LogType log_type;
  34. LogLevel log_level;
  35. std::string event;
  36. base::Time time;
  37. int count;
  38. };
  39. explicit DeviceEventLogImpl(
  40. scoped_refptr<base::SingleThreadTaskRunner> task_runner,
  41. size_t max_entries);
  42. DeviceEventLogImpl(const DeviceEventLogImpl&) = delete;
  43. DeviceEventLogImpl& operator=(const DeviceEventLogImpl&) = delete;
  44. ~DeviceEventLogImpl();
  45. // Implements device_event_log::AddEntry.
  46. void AddEntry(const char* file,
  47. int file_line,
  48. LogType type,
  49. LogLevel level,
  50. const std::string& event);
  51. void AddEntryWithTimestampForTesting(const char* file,
  52. int file_line,
  53. LogType log_type,
  54. LogLevel log_level,
  55. const std::string& event,
  56. base::Time time);
  57. // Implements device_event_log::GetAsString.
  58. std::string GetAsString(StringOrder order,
  59. const std::string& format,
  60. const std::string& types,
  61. LogLevel max_level,
  62. size_t max_events);
  63. // Implements device_event_log::Clear.
  64. void ClearAll();
  65. void Clear(const base::Time& begin, const base::Time& end);
  66. int GetCountByLevelForTesting(LogLevel level);
  67. // Called from device_event_log::AddEntry if the global instance has not been
  68. // created (or has already been destroyed). Logs to LOG(ERROR) or VLOG(1).
  69. static void SendToVLogOrErrorLog(const char* file,
  70. int file_line,
  71. LogType type,
  72. LogLevel log_level,
  73. const std::string& event);
  74. private:
  75. friend class DeviceEventLogTest;
  76. typedef std::list<LogEntry> LogEntryList;
  77. void AddLogEntry(const LogEntry& entry);
  78. void RemoveEntry();
  79. // For testing
  80. size_t max_entries() const { return max_entries_; }
  81. void set_max_entries_for_test(size_t entries) { max_entries_ = entries; }
  82. scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
  83. size_t max_entries_;
  84. LogEntryList entries_;
  85. base::WeakPtrFactory<DeviceEventLogImpl> weak_ptr_factory_{this};
  86. };
  87. } // namespace device_event_log
  88. #endif // COMPONENTS_DEVICE_EVENT_LOG_DEVICE_EVENT_LOG_IMPL_H_