1 /*
2  * Copyright (C) 2022 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 #pragma once
18 
19 #include <fstream>
20 
21 #include "statslog_odsign.h"
22 
23 // Class to store CompOsArtifactsCheck related metrics.
24 // These are flushed to a file kOdsignMetricsFile and consumed by
25 // System Server (in class OdsignStatsLogger) & sent to statsd.
26 class StatsReporter {
27   public:
28     // Keep in sync with the EarlyBootCompOsArtifactsCheckReported definition in
29     // proto_logging/stats/atoms.proto.
30     struct CompOsArtifactsCheckRecord {
31         bool current_artifacts_ok = false;
32         bool comp_os_pending_artifacts_exists = false;
33         bool use_comp_os_generated_artifacts = false;
34     };
35 
36     // Keep in sync with the OdsignReported definition in proto_logging/stats/atoms.proto.
37     struct OdsignRecord {
38         int32_t status = art::metrics::statsd::ODSIGN_REPORTED__STATUS__STATUS_UNSPECIFIED;
39     };
40 
41     // The report is flushed (from buffer) into a file by the destructor.
42     ~StatsReporter();
43 
44     // Returns a mutable CompOS record. The pointer remains valid for the lifetime of this
45     // StatsReporter. If this function is not called, no CompOS record will be logged.
46     CompOsArtifactsCheckRecord* GetOrCreateComposArtifactsCheckRecord();
47 
48     // Returns a mutable odsign record. The pointer remains valid for the lifetime of this
49     // StatsReporter.
GetOdsignRecord()50     OdsignRecord* GetOdsignRecord() { return &odsign_record_; }
51 
52     // Enables/disables odsign metrics.
SetOdsignRecordEnabled(bool value)53     void SetOdsignRecordEnabled(bool value) { odsign_record_enabled_ = value; }
54 
55   private:
56     // Temporary buffer which stores the metrics.
57     std::unique_ptr<CompOsArtifactsCheckRecord> comp_os_artifacts_check_record_;
58 
59     OdsignRecord odsign_record_;
60     bool odsign_record_enabled_ = true;
61 };
62