1 /* 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 */ 16 17 #include "uploader/metrics_log.h" 18 19 #include <string> 20 21 #include <base/files/file_util.h> 22 23 #include "uploader/proto/system_profile.pb.h" 24 #include "uploader/system_profile_setter.h" 25 26 // We use default values for the MetricsLogBase constructor as the setter will 27 // override them. 28 MetricsLog::MetricsLog() 29 : MetricsLogBase("", 0, metrics::MetricsLogBase::ONGOING_LOG, "") { 30 } 31 32 bool 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 54 bool 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 66 void MetricsLog::IncrementUserCrashCount(unsigned int count) { 67 metrics::SystemProfileProto::Stability* stability( 68 uma_proto()->mutable_system_profile()->mutable_stability()); 69 int current = stability->other_user_crash_count(); 70 stability->set_other_user_crash_count(current + count); 71 } 72 73 void MetricsLog::IncrementKernelCrashCount(unsigned int count) { 74 metrics::SystemProfileProto::Stability* stability( 75 uma_proto()->mutable_system_profile()->mutable_stability()); 76 int current = stability->kernel_crash_count(); 77 stability->set_kernel_crash_count(current + count); 78 } 79 80 void MetricsLog::IncrementUncleanShutdownCount(unsigned int count) { 81 metrics::SystemProfileProto::Stability* stability( 82 uma_proto()->mutable_system_profile()->mutable_stability()); 83 int current = stability->unclean_system_shutdown_count(); 84 stability->set_unclean_system_shutdown_count(current + count); 85 } 86 87 bool MetricsLog::PopulateSystemProfile(SystemProfileSetter* profile_setter) { 88 CHECK(profile_setter); 89 return profile_setter->Populate(uma_proto()); 90 } 91