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 <log/event_tag_map.h>
22 #include <log/log_event_list.h>
23
24 namespace {
25
26 #ifdef __ANDROID__
27 EventTagMap* kEventTagMap = android_openEventTagMap(nullptr);
28 const int kSysuiMultiActionTag = android_lookupEventTagNum(
29 kEventTagMap, "sysui_multi_action", "(content|4)", ANDROID_LOG_UNKNOWN);
30 #else
31 // android_openEventTagMap does not work on host builds.
32 const int kSysuiMultiActionTag = 0;
33 #endif
34
35 } // namespace
36
37 namespace android {
38 namespace metricslogger {
39
40 // Mirror com.android.internal.logging.MetricsLogger#histogram().
LogHistogram(const std::string & event,int32_t data)41 void LogHistogram(const std::string& event, int32_t data) {
42 android_log_event_list log(kSysuiMultiActionTag);
43 log << LOGBUILDER_CATEGORY << LOGBUILDER_HISTOGRAM << LOGBUILDER_NAME << event
44 << LOGBUILDER_BUCKET << data << LOGBUILDER_VALUE << 1 << LOG_ID_EVENTS;
45 }
46
47 // Mirror com.android.internal.logging.MetricsLogger#count().
LogCounter(const std::string & name,int32_t val)48 void LogCounter(const std::string& name, int32_t val) {
49 android_log_event_list log(kSysuiMultiActionTag);
50 log << LOGBUILDER_CATEGORY << LOGBUILDER_COUNTER << LOGBUILDER_NAME << name << LOGBUILDER_VALUE
51 << val << LOG_ID_EVENTS;
52 }
53
54 // Mirror com.android.internal.logging.MetricsLogger#action().
LogMultiAction(int32_t category,int32_t field,const std::string & value)55 void LogMultiAction(int32_t category, int32_t field, const std::string& value) {
56 android_log_event_list log(kSysuiMultiActionTag);
57 log << LOGBUILDER_CATEGORY << category << LOGBUILDER_TYPE << TYPE_ACTION
58 << field << value << LOG_ID_EVENTS;
59 }
60
ComplexEventLogger(int category)61 ComplexEventLogger::ComplexEventLogger(int category) : logger(kSysuiMultiActionTag) {
62 logger << LOGBUILDER_CATEGORY << category;
63 }
64
SetPackageName(const std::string & package_name)65 void ComplexEventLogger::SetPackageName(const std::string& package_name) {
66 logger << LOGBUILDER_PACKAGENAME << package_name;
67 }
68
AddTaggedData(int tag,int32_t value)69 void ComplexEventLogger::AddTaggedData(int tag, int32_t value) {
70 logger << tag << value;
71 }
72
AddTaggedData(int tag,const std::string & value)73 void ComplexEventLogger::AddTaggedData(int tag, const std::string& value) {
74 logger << tag << value;
75 }
76
AddTaggedData(int tag,int64_t value)77 void ComplexEventLogger::AddTaggedData(int tag, int64_t value) {
78 logger << tag << value;
79 }
80
AddTaggedData(int tag,float value)81 void ComplexEventLogger::AddTaggedData(int tag, float value) {
82 logger << tag << value;
83 }
84
Record()85 void ComplexEventLogger::Record() {
86 logger << LOG_ID_EVENTS;
87 }
88
89 } // namespace metricslogger
90 } // namespace android
91