offline_event_logger.cc 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright 2016 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. #include "components/offline_pages/core/offline_event_logger.h"
  5. #include "base/logging.h"
  6. #include "base/strings/stringprintf.h"
  7. #include "base/time/time.h"
  8. #include "components/offline_pages/core/offline_clock.h"
  9. namespace offline_pages {
  10. OfflineEventLogger::OfflineEventLogger()
  11. : activities_(0), is_logging_(false), client_(nullptr) {}
  12. OfflineEventLogger::~OfflineEventLogger() {}
  13. void OfflineEventLogger::Clear() {
  14. activities_.clear();
  15. }
  16. void OfflineEventLogger::SetIsLogging(bool is_logging) {
  17. is_logging_ = is_logging;
  18. }
  19. bool OfflineEventLogger::GetIsLogging() {
  20. return is_logging_;
  21. }
  22. void OfflineEventLogger::GetLogs(std::vector<std::string>* records) {
  23. DCHECK(records);
  24. records->insert(records->end(), activities_.begin(), activities_.end());
  25. }
  26. void OfflineEventLogger::RecordActivity(const std::string& activity) {
  27. DVLOG(1) << activity;
  28. if (!is_logging_ || activity.empty())
  29. return;
  30. base::Time::Exploded current_time;
  31. OfflineTimeNow().LocalExplode(&current_time);
  32. std::string date_string = base::StringPrintf(
  33. "%d %02d %02d %02d:%02d:%02d", current_time.year, current_time.month,
  34. current_time.day_of_month, current_time.hour, current_time.minute,
  35. current_time.second);
  36. if (client_)
  37. client_->CustomLog(activity);
  38. if (activities_.size() == kMaxLogCount)
  39. activities_.pop_back();
  40. activities_.push_front(date_string + ": " + activity);
  41. }
  42. void OfflineEventLogger::SetClient(Client* client) {
  43. DCHECK(client);
  44. SetIsLogging(true);
  45. client_ = client;
  46. }
  47. } // namespace offline_pages