Roll src/third_party/WebKit c63b89c:29324ab (svn 202546:202547)
[chromium-blink-merge.git] / components / metrics / metrics_log_manager.cc
bloba1cbac1936977699d6882a66032542d4a5072ea2
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/metrics/metrics_log_manager.h"
7 #include <algorithm>
9 #include "base/strings/string_util.h"
10 #include "components/metrics/metrics_log.h"
11 #include "components/metrics/metrics_pref_names.h"
13 namespace metrics {
15 namespace {
17 // The number of "initial" logs to save, and hope to send during a future Chrome
18 // session. Initial logs contain crash stats, and are pretty small.
19 const size_t kInitialLogsPersistLimit = 20;
21 // The number of ongoing logs to save persistently, and hope to
22 // send during a this or future sessions. Note that each log may be pretty
23 // large, as presumably the related "initial" log wasn't sent (probably nothing
24 // was, as the user was probably off-line). As a result, the log probably kept
25 // accumulating while the "initial" log was stalled, and couldn't be sent. As a
26 // result, we don't want to save too many of these mega-logs.
27 // A "standard shutdown" will create a small log, including just the data that
28 // was not yet been transmitted, and that is normal (to have exactly one
29 // ongoing_log_ at startup).
30 const size_t kOngoingLogsPersistLimit = 8;
32 // The number of bytes each of initial and ongoing logs that must be stored.
33 // This ensures that a reasonable amount of history will be stored even if there
34 // is a long series of very small logs.
35 const size_t kStorageByteLimitPerLogType = 300000;
37 } // namespace
39 MetricsLogManager::MetricsLogManager(PrefService* local_state,
40 size_t max_ongoing_log_size)
41 : unsent_logs_loaded_(false),
42 initial_log_queue_(local_state,
43 prefs::kMetricsInitialLogs,
44 kInitialLogsPersistLimit,
45 kStorageByteLimitPerLogType,
46 0),
47 ongoing_log_queue_(local_state,
48 prefs::kMetricsOngoingLogs,
49 kOngoingLogsPersistLimit,
50 kStorageByteLimitPerLogType,
51 max_ongoing_log_size) {}
53 MetricsLogManager::~MetricsLogManager() {}
55 void MetricsLogManager::BeginLoggingWithLog(scoped_ptr<MetricsLog> log) {
56 DCHECK(!current_log_);
57 current_log_ = log.Pass();
60 void MetricsLogManager::FinishCurrentLog() {
61 DCHECK(current_log_.get());
62 current_log_->CloseLog();
63 std::string log_data;
64 current_log_->GetEncodedLog(&log_data);
65 if (!log_data.empty())
66 StoreLog(log_data, current_log_->log_type());
67 current_log_.reset();
70 void MetricsLogManager::StageNextLogForUpload() {
71 DCHECK(!has_staged_log());
72 if (!initial_log_queue_.empty())
73 initial_log_queue_.StageLog();
74 else
75 ongoing_log_queue_.StageLog();
78 void MetricsLogManager::DiscardStagedLog() {
79 DCHECK(has_staged_log());
80 if (initial_log_queue_.has_staged_log())
81 initial_log_queue_.DiscardStagedLog();
82 else
83 ongoing_log_queue_.DiscardStagedLog();
84 DCHECK(!has_staged_log());
87 void MetricsLogManager::DiscardCurrentLog() {
88 current_log_->CloseLog();
89 current_log_.reset();
92 void MetricsLogManager::PauseCurrentLog() {
93 DCHECK(!paused_log_.get());
94 paused_log_.reset(current_log_.release());
97 void MetricsLogManager::ResumePausedLog() {
98 DCHECK(!current_log_.get());
99 current_log_.reset(paused_log_.release());
102 void MetricsLogManager::StoreLog(const std::string& log_data,
103 MetricsLog::LogType log_type) {
104 switch (log_type) {
105 case MetricsLog::INITIAL_STABILITY_LOG:
106 initial_log_queue_.StoreLog(log_data);
107 break;
108 case MetricsLog::ONGOING_LOG:
109 ongoing_log_queue_.StoreLog(log_data);
110 break;
114 void MetricsLogManager::PersistUnsentLogs() {
115 DCHECK(unsent_logs_loaded_);
116 if (!unsent_logs_loaded_)
117 return;
119 initial_log_queue_.SerializeLogs();
120 ongoing_log_queue_.SerializeLogs();
123 void MetricsLogManager::LoadPersistedUnsentLogs() {
124 initial_log_queue_.DeserializeLogs();
125 ongoing_log_queue_.DeserializeLogs();
126 unsent_logs_loaded_ = true;
129 } // namespace metrics