blob: fcaa8c13c82cd32956f6d35c82ccb85c2420ca59 [file] [log] [blame]
Bertrand SIMONNET52e5b992015-08-10 15:18:00 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -070016
17#include "uploader/metrics_log.h"
18
19#include <string>
20
Bertrand SIMONNET05865042015-12-15 12:33:01 -080021#include <base/files/file_util.h>
22
Bertrand SIMONNET4b915ae2015-07-28 15:38:14 -070023#include "uploader/proto/system_profile.pb.h"
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -070024#include "uploader/system_profile_setter.h"
25
26// We use default values for the MetricsLogBase constructor as the setter will
27// override them.
28MetricsLog::MetricsLog()
29 : MetricsLogBase("", 0, metrics::MetricsLogBase::ONGOING_LOG, "") {
30}
31
Bertrand SIMONNET05865042015-12-15 12:33:01 -080032bool MetricsLog::LoadFromFile(const base::FilePath& saved_log) {
33 std::string encoded_log;
34 if (!base::ReadFileToString(saved_log, &encoded_log)) {
35 LOG(ERROR) << "Failed to read the metrics log backup from "
36 << saved_log.value();
37 return false;
38 }
39
40 if (!uma_proto()->ParseFromString(encoded_log)) {
41 LOG(ERROR) << "Failed to parse log from " << saved_log.value()
42 << ", deleting the log";
43 base::DeleteFile(saved_log, false);
44 uma_proto()->Clear();
45 return false;
46 }
47
48 VLOG(1) << uma_proto()->histogram_event_size() << " histograms loaded from "
49 << saved_log.value();
50
51 return true;
52}
53
54bool MetricsLog::SaveToFile(const base::FilePath& path) {
55 std::string encoded_log;
56 GetEncodedLog(&encoded_log);
57
58 if (static_cast<int>(encoded_log.size()) !=
59 base::WriteFile(path, encoded_log.data(), encoded_log.size())) {
60 LOG(ERROR) << "Failed to persist the current log to " << path.value();
61 return false;
62 }
63 return true;
64}
65
Bertrand SIMONNET6b8629a2015-11-18 13:46:33 -080066void MetricsLog::IncrementUserCrashCount(unsigned int count) {
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -070067 metrics::SystemProfileProto::Stability* stability(
68 uma_proto()->mutable_system_profile()->mutable_stability());
69 int current = stability->other_user_crash_count();
Bertrand SIMONNET6b8629a2015-11-18 13:46:33 -080070 stability->set_other_user_crash_count(current + count);
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -070071}
72
Bertrand SIMONNET6b8629a2015-11-18 13:46:33 -080073void MetricsLog::IncrementKernelCrashCount(unsigned int count) {
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -070074 metrics::SystemProfileProto::Stability* stability(
75 uma_proto()->mutable_system_profile()->mutable_stability());
76 int current = stability->kernel_crash_count();
Bertrand SIMONNET6b8629a2015-11-18 13:46:33 -080077 stability->set_kernel_crash_count(current + count);
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -070078}
79
Bertrand SIMONNET6b8629a2015-11-18 13:46:33 -080080void MetricsLog::IncrementUncleanShutdownCount(unsigned int count) {
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -070081 metrics::SystemProfileProto::Stability* stability(
82 uma_proto()->mutable_system_profile()->mutable_stability());
83 int current = stability->unclean_system_shutdown_count();
Bertrand SIMONNET6b8629a2015-11-18 13:46:33 -080084 stability->set_unclean_system_shutdown_count(current + count);
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -070085}
86
Bertrand SIMONNET1f146552015-08-19 18:13:02 -070087bool MetricsLog::PopulateSystemProfile(SystemProfileSetter* profile_setter) {
Bertrand SIMONNETf1aa3722016-01-22 13:15:54 -080088 CHECK(profile_setter);
Bertrand SIMONNET1f146552015-08-19 18:13:02 -070089 return profile_setter->Populate(uma_proto());
Bertrand SIMONNET46b49da2014-06-25 14:38:07 -070090}