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