1 /*
2 * Copyright (C) 2017 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 "metricslogger/metrics_logger.h"
18
19 #include <cstdlib>
20
21 #include <android-base/chrono_utils.h>
22 #include <log/event_tag_map.h>
23
24 using namespace android;
25
26 namespace {
27
28 const static int kStatsEventTag = 1937006964;
29 const static int kKeyValuePairAtomId = 83;
30 #ifdef __ANDROID__
31 EventTagMap* kEventTagMap = android_openEventTagMap(nullptr);
32 const int kSysuiMultiActionTag = android_lookupEventTagNum(
33 kEventTagMap, "sysui_multi_action", "(content|4)", ANDROID_LOG_UNKNOWN);
34 #else
35 // android_openEventTagMap does not work on host builds.
36 const int kSysuiMultiActionTag = 0;
37 #endif
38
getElapsedTimeNanoSinceBoot()39 int64_t getElapsedTimeNanoSinceBoot() {
40 return std::chrono::duration_cast<std::chrono::nanoseconds>(
41 android::base::boot_clock::now().time_since_epoch())
42 .count();
43 }
44
45 } // namespace
46
47 namespace android {
48 namespace metricslogger {
49
50 // Mirror com.android.internal.logging.MetricsLogger#histogram().
LogHistogram(const std::string & event,int32_t data)51 void LogHistogram(const std::string& event, int32_t data) {
52 android_log_event_list log(kSysuiMultiActionTag);
53 log << LOGBUILDER_CATEGORY << LOGBUILDER_HISTOGRAM << LOGBUILDER_NAME << event
54 << LOGBUILDER_BUCKET << data << LOGBUILDER_VALUE << 1 << LOG_ID_EVENTS;
55
56 stats_event_list stats_log(kStatsEventTag);
57 stats_log << getElapsedTimeNanoSinceBoot() << kKeyValuePairAtomId << LOGBUILDER_CATEGORY
58 << LOGBUILDER_HISTOGRAM << LOGBUILDER_NAME << event << LOGBUILDER_BUCKET << data
59 << LOGBUILDER_VALUE << 1;
60 stats_log.write(LOG_ID_STATS);
61 }
62
63 // Mirror com.android.internal.logging.MetricsLogger#count().
LogCounter(const std::string & name,int32_t val)64 void LogCounter(const std::string& name, int32_t val) {
65 android_log_event_list log(kSysuiMultiActionTag);
66 log << LOGBUILDER_CATEGORY << LOGBUILDER_COUNTER << LOGBUILDER_NAME << name << LOGBUILDER_VALUE
67 << val << LOG_ID_EVENTS;
68
69 stats_event_list stats_log(kStatsEventTag);
70 stats_log << getElapsedTimeNanoSinceBoot() << kKeyValuePairAtomId << LOGBUILDER_CATEGORY
71 << LOGBUILDER_COUNTER << LOGBUILDER_NAME << name << LOGBUILDER_VALUE << val;
72 stats_log.write(LOG_ID_STATS);
73 }
74
75 // Mirror com.android.internal.logging.MetricsLogger#action().
LogMultiAction(int32_t category,int32_t field,const std::string & value)76 void LogMultiAction(int32_t category, int32_t field, const std::string& value) {
77 android_log_event_list log(kSysuiMultiActionTag);
78 log << LOGBUILDER_CATEGORY << category << LOGBUILDER_TYPE << TYPE_ACTION
79 << field << value << LOG_ID_EVENTS;
80
81 stats_event_list stats_log(kStatsEventTag);
82 stats_log << getElapsedTimeNanoSinceBoot() << kKeyValuePairAtomId << LOGBUILDER_CATEGORY
83 << category << LOGBUILDER_TYPE << TYPE_ACTION << field << value;
84 stats_log.write(LOG_ID_STATS);
85 }
86
ComplexEventLogger(int category)87 ComplexEventLogger::ComplexEventLogger(int category)
88 : logger(kSysuiMultiActionTag), stats_logger(kStatsEventTag) {
89 logger << LOGBUILDER_CATEGORY << category;
90 stats_logger << getElapsedTimeNanoSinceBoot() << kKeyValuePairAtomId << LOGBUILDER_CATEGORY
91 << category;
92 }
93
SetPackageName(const std::string & package_name)94 void ComplexEventLogger::SetPackageName(const std::string& package_name) {
95 logger << LOGBUILDER_PACKAGENAME << package_name;
96 stats_logger << LOGBUILDER_PACKAGENAME << package_name;
97 }
98
AddTaggedData(int tag,int32_t value)99 void ComplexEventLogger::AddTaggedData(int tag, int32_t value) {
100 logger << tag << value;
101 stats_logger << tag << value;
102 }
103
AddTaggedData(int tag,const std::string & value)104 void ComplexEventLogger::AddTaggedData(int tag, const std::string& value) {
105 logger << tag << value;
106 stats_logger << tag << value;
107 }
108
AddTaggedData(int tag,int64_t value)109 void ComplexEventLogger::AddTaggedData(int tag, int64_t value) {
110 logger << tag << value;
111 stats_logger << tag << value;
112 }
113
AddTaggedData(int tag,float value)114 void ComplexEventLogger::AddTaggedData(int tag, float value) {
115 logger << tag << value;
116 stats_logger << tag << value;
117 }
118
Record()119 void ComplexEventLogger::Record() {
120 logger << LOG_ID_EVENTS;
121 stats_logger.write(LOG_ID_STATS);
122 }
123
124 } // namespace metricslogger
125 } // namespace android
126