1 /*
2 * Copyright (C) 2020 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 "src/android_stats/statsd_logging_helper.h"
18
19 #include <cstdint>
20 #include <string>
21 #include <vector>
22
23 #include "perfetto/base/build_config.h"
24 #include "src/android_stats/perfetto_atoms.h"
25
26 #if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID) && \
27 PERFETTO_BUILDFLAG(PERFETTO_ANDROID_BUILD)
28 #include "src/android_internal/lazy_library_loader.h" // nogncheck
29 #include "src/android_internal/statsd_logging.h" // nogncheck
30 #endif
31
32 namespace perfetto::android_stats {
33
34 // Make sure we don't accidentally log on non-Android tree build. Note that even
35 // removing this ifdef still doesn't make uploads work on OS_ANDROID.
36 // PERFETTO_LAZY_LOAD will return a nullptr on non-Android and non-in-tree
37 // builds as libperfetto_android_internal will not be available.
38 #if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID) && \
39 PERFETTO_BUILDFLAG(PERFETTO_ANDROID_BUILD)
40
MaybeLogUploadEvent(PerfettoStatsdAtom atom,int64_t uuid_lsb,int64_t uuid_msb,const std::string & trigger_name)41 void MaybeLogUploadEvent(PerfettoStatsdAtom atom,
42 int64_t uuid_lsb,
43 int64_t uuid_msb,
44 const std::string& trigger_name) {
45 PERFETTO_LAZY_LOAD(android_internal::StatsdLogUploadEvent, log_event_fn);
46 if (log_event_fn) {
47 log_event_fn(atom, uuid_lsb, uuid_msb, trigger_name.c_str());
48 }
49 }
50
MaybeLogTriggerEvent(PerfettoTriggerAtom atom,const std::string & trigger_name)51 void MaybeLogTriggerEvent(PerfettoTriggerAtom atom,
52 const std::string& trigger_name) {
53 PERFETTO_LAZY_LOAD(android_internal::StatsdLogTriggerEvent, log_event_fn);
54 if (log_event_fn) {
55 log_event_fn(atom, trigger_name.c_str());
56 }
57 }
58
MaybeLogTriggerEvents(PerfettoTriggerAtom atom,const std::vector<std::string> & triggers)59 void MaybeLogTriggerEvents(PerfettoTriggerAtom atom,
60 const std::vector<std::string>& triggers) {
61 PERFETTO_LAZY_LOAD(android_internal::StatsdLogTriggerEvent, log_event_fn);
62 if (log_event_fn) {
63 for (const std::string& trigger_name : triggers) {
64 log_event_fn(atom, trigger_name.c_str());
65 }
66 }
67 }
68
69 #else
70 void MaybeLogUploadEvent(PerfettoStatsdAtom,
71 int64_t,
72 int64_t,
73 const std::string&) {}
74 void MaybeLogTriggerEvent(PerfettoTriggerAtom, const std::string&) {}
75 void MaybeLogTriggerEvents(PerfettoTriggerAtom,
76 const std::vector<std::string>&) {}
77 #endif
78
79 } // namespace perfetto::android_stats
80