| 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 "components/metrics/metrics_log_manager.h" | 5 #include "components/metrics/metrics_log_manager.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| 11 #include "components/metrics/metrics_log.h" | 11 #include "components/metrics/metrics_log.h" |
| 12 #include "components/metrics/metrics_log_store.h" |
| 12 #include "components/metrics/metrics_pref_names.h" | 13 #include "components/metrics/metrics_pref_names.h" |
| 13 #include "components/metrics/persisted_logs_metrics_impl.h" | |
| 14 | 14 |
| 15 namespace metrics { | 15 namespace metrics { |
| 16 | 16 |
| 17 namespace { | 17 MetricsLogManager::MetricsLogManager() {} |
| 18 | |
| 19 // The number of "initial" logs to save, and hope to send during a future Chrome | |
| 20 // session. Initial logs contain crash stats, and are pretty small. | |
| 21 const size_t kInitialLogsPersistLimit = 20; | |
| 22 | |
| 23 // The number of ongoing logs to save persistently, and hope to | |
| 24 // send during a this or future sessions. Note that each log may be pretty | |
| 25 // large, as presumably the related "initial" log wasn't sent (probably nothing | |
| 26 // was, as the user was probably off-line). As a result, the log probably kept | |
| 27 // accumulating while the "initial" log was stalled, and couldn't be sent. As a | |
| 28 // result, we don't want to save too many of these mega-logs. | |
| 29 // A "standard shutdown" will create a small log, including just the data that | |
| 30 // was not yet been transmitted, and that is normal (to have exactly one | |
| 31 // ongoing_log_ at startup). | |
| 32 const size_t kOngoingLogsPersistLimit = 8; | |
| 33 | |
| 34 // The number of bytes each of initial and ongoing logs that must be stored. | |
| 35 // This ensures that a reasonable amount of history will be stored even if there | |
| 36 // is a long series of very small logs. | |
| 37 const size_t kStorageByteLimitPerLogType = 300000; | |
| 38 | |
| 39 } // namespace | |
| 40 | |
| 41 MetricsLogManager::MetricsLogManager(PrefService* local_state, | |
| 42 size_t max_ongoing_log_size) | |
| 43 : unsent_logs_loaded_(false), | |
| 44 initial_log_queue_(std::unique_ptr<PersistedLogsMetricsImpl>( | |
| 45 new PersistedLogsMetricsImpl()), | |
| 46 local_state, | |
| 47 prefs::kMetricsInitialLogs, | |
| 48 kInitialLogsPersistLimit, | |
| 49 kStorageByteLimitPerLogType, | |
| 50 0), | |
| 51 ongoing_log_queue_(std::unique_ptr<PersistedLogsMetricsImpl>( | |
| 52 new PersistedLogsMetricsImpl()), | |
| 53 local_state, | |
| 54 prefs::kMetricsOngoingLogs, | |
| 55 kOngoingLogsPersistLimit, | |
| 56 kStorageByteLimitPerLogType, | |
| 57 max_ongoing_log_size) {} | |
| 58 | 18 |
| 59 MetricsLogManager::~MetricsLogManager() {} | 19 MetricsLogManager::~MetricsLogManager() {} |
| 60 | 20 |
| 61 void MetricsLogManager::BeginLoggingWithLog(std::unique_ptr<MetricsLog> log) { | 21 void MetricsLogManager::BeginLoggingWithLog(std::unique_ptr<MetricsLog> log) { |
| 62 DCHECK(!current_log_); | 22 DCHECK(!current_log_); |
| 63 current_log_ = std::move(log); | 23 current_log_ = std::move(log); |
| 64 } | 24 } |
| 65 | 25 |
| 66 void MetricsLogManager::FinishCurrentLog() { | 26 void MetricsLogManager::FinishCurrentLog(MetricsLogStore* log_store) { |
| 67 DCHECK(current_log_.get()); | 27 DCHECK(current_log_.get()); |
| 68 current_log_->CloseLog(); | 28 current_log_->CloseLog(); |
| 69 std::string log_data; | 29 std::string log_data; |
| 70 current_log_->GetEncodedLog(&log_data); | 30 current_log_->GetEncodedLog(&log_data); |
| 71 if (!log_data.empty()) | 31 if (!log_data.empty()) |
| 72 StoreLog(log_data, current_log_->log_type()); | 32 log_store->StoreLog(log_data, current_log_->log_type()); |
| 73 current_log_.reset(); | 33 current_log_.reset(); |
| 74 } | 34 } |
| 75 | 35 |
| 76 void MetricsLogManager::StageNextLogForUpload() { | |
| 77 DCHECK(!has_staged_log()); | |
| 78 if (!initial_log_queue_.empty()) | |
| 79 initial_log_queue_.StageLog(); | |
| 80 else | |
| 81 ongoing_log_queue_.StageLog(); | |
| 82 } | |
| 83 | |
| 84 void MetricsLogManager::DiscardStagedLog() { | |
| 85 DCHECK(has_staged_log()); | |
| 86 if (initial_log_queue_.has_staged_log()) | |
| 87 initial_log_queue_.DiscardStagedLog(); | |
| 88 else | |
| 89 ongoing_log_queue_.DiscardStagedLog(); | |
| 90 DCHECK(!has_staged_log()); | |
| 91 } | |
| 92 | |
| 93 void MetricsLogManager::DiscardCurrentLog() { | 36 void MetricsLogManager::DiscardCurrentLog() { |
| 94 current_log_->CloseLog(); | 37 current_log_->CloseLog(); |
| 95 current_log_.reset(); | 38 current_log_.reset(); |
| 96 } | 39 } |
| 97 | 40 |
| 98 void MetricsLogManager::PauseCurrentLog() { | 41 void MetricsLogManager::PauseCurrentLog() { |
| 99 DCHECK(!paused_log_.get()); | 42 DCHECK(!paused_log_.get()); |
| 100 paused_log_ = std::move(current_log_); | 43 paused_log_ = std::move(current_log_); |
| 101 } | 44 } |
| 102 | 45 |
| 103 void MetricsLogManager::ResumePausedLog() { | 46 void MetricsLogManager::ResumePausedLog() { |
| 104 DCHECK(!current_log_.get()); | 47 DCHECK(!current_log_.get()); |
| 105 current_log_ = std::move(paused_log_); | 48 current_log_ = std::move(paused_log_); |
| 106 } | 49 } |
| 107 | 50 |
| 108 void MetricsLogManager::StoreLog(const std::string& log_data, | |
| 109 MetricsLog::LogType log_type) { | |
| 110 switch (log_type) { | |
| 111 case MetricsLog::INITIAL_STABILITY_LOG: | |
| 112 initial_log_queue_.StoreLog(log_data); | |
| 113 break; | |
| 114 case MetricsLog::ONGOING_LOG: | |
| 115 ongoing_log_queue_.StoreLog(log_data); | |
| 116 break; | |
| 117 } | |
| 118 } | |
| 119 | |
| 120 void MetricsLogManager::PersistUnsentLogs() { | |
| 121 DCHECK(unsent_logs_loaded_); | |
| 122 if (!unsent_logs_loaded_) | |
| 123 return; | |
| 124 | |
| 125 initial_log_queue_.SerializeLogs(); | |
| 126 ongoing_log_queue_.SerializeLogs(); | |
| 127 } | |
| 128 | |
| 129 void MetricsLogManager::LoadPersistedUnsentLogs() { | |
| 130 initial_log_queue_.DeserializeLogs(); | |
| 131 ongoing_log_queue_.DeserializeLogs(); | |
| 132 unsent_logs_loaded_ = true; | |
| 133 } | |
| 134 | |
| 135 } // namespace metrics | 51 } // namespace metrics |
| OLD | NEW |