blob: e3de3071365256369799dfa6f299cfe0c26360ec [file] [log] [blame]
David Chen0a368b22017-12-06 16:28:16 -08001/*
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
17package android.util;
18
yrobe6d7f92018-05-04 13:02:53 -070019import android.os.IStatsManager;
20import android.os.RemoteException;
21import android.os.ServiceManager;
Chenjie Yu33a61412018-03-14 22:28:47 -070022
David Chen0a368b22017-12-06 16:28:16 -080023/**
24 * StatsLog provides an API for developers to send events to statsd. The events can be used to
David Chenc1a3a0d2018-02-21 18:58:23 -080025 * define custom metrics inside statsd.
David Chen0a368b22017-12-06 16:28:16 -080026 */
27public final class StatsLog extends StatsLogInternal {
yrobe6d7f92018-05-04 13:02:53 -070028 private static final String TAG = "StatsLog";
29 private static final boolean DEBUG = false;
30
31 private static IStatsManager sService;
David Chen0a368b22017-12-06 16:28:16 -080032
33 private StatsLog() {}
34
35 /**
36 * Logs a start event.
37 *
yrobe6d7f92018-05-04 13:02:53 -070038 * @param label developer-chosen label.
David Chen0a368b22017-12-06 16:28:16 -080039 * @return True if the log request was sent to statsd.
40 */
41 public static boolean logStart(int label) {
yrobe6d7f92018-05-04 13:02:53 -070042 synchronized (StatsLog.class) {
43 try {
44 IStatsManager service = getIStatsManagerLocked();
45 if (service == null) {
46 if (DEBUG) Slog.d(TAG, "Failed to find statsd when logging start");
47 return false;
48 }
49 service.sendAppBreadcrumbAtom(label,
50 StatsLog.APP_BREADCRUMB_REPORTED__STATE__START);
51 return true;
52 } catch (RemoteException e) {
53 sService = null;
54 if (DEBUG) Slog.d(TAG, "Failed to connect to statsd when logging start");
55 return false;
56 }
David Chen0a368b22017-12-06 16:28:16 -080057 }
David Chen0a368b22017-12-06 16:28:16 -080058 }
59
60 /**
61 * Logs a stop event.
62 *
yrobe6d7f92018-05-04 13:02:53 -070063 * @param label developer-chosen label.
David Chen0a368b22017-12-06 16:28:16 -080064 * @return True if the log request was sent to statsd.
65 */
66 public static boolean logStop(int label) {
yrobe6d7f92018-05-04 13:02:53 -070067 synchronized (StatsLog.class) {
68 try {
69 IStatsManager service = getIStatsManagerLocked();
70 if (service == null) {
71 if (DEBUG) Slog.d(TAG, "Failed to find statsd when logging stop");
72 return false;
73 }
74 service.sendAppBreadcrumbAtom(label, StatsLog.APP_BREADCRUMB_REPORTED__STATE__STOP);
75 return true;
76 } catch (RemoteException e) {
77 sService = null;
78 if (DEBUG) Slog.d(TAG, "Failed to connect to statsd when logging stop");
79 return false;
80 }
David Chen0a368b22017-12-06 16:28:16 -080081 }
David Chen0a368b22017-12-06 16:28:16 -080082 }
83
84 /**
85 * Logs an event that does not represent a start or stop boundary.
86 *
yrobe6d7f92018-05-04 13:02:53 -070087 * @param label developer-chosen label.
David Chen0a368b22017-12-06 16:28:16 -080088 * @return True if the log request was sent to statsd.
89 */
90 public static boolean logEvent(int label) {
yrobe6d7f92018-05-04 13:02:53 -070091 synchronized (StatsLog.class) {
92 try {
93 IStatsManager service = getIStatsManagerLocked();
94 if (service == null) {
95 if (DEBUG) Slog.d(TAG, "Failed to find statsd when logging event");
96 return false;
97 }
98 service.sendAppBreadcrumbAtom(
99 label, StatsLog.APP_BREADCRUMB_REPORTED__STATE__UNSPECIFIED);
100 return true;
101 } catch (RemoteException e) {
102 sService = null;
103 if (DEBUG) Slog.d(TAG, "Failed to connect to statsd when logging event");
104 return false;
105 }
David Chen0a368b22017-12-06 16:28:16 -0800106 }
yrobe6d7f92018-05-04 13:02:53 -0700107 }
108
109 private static IStatsManager getIStatsManagerLocked() throws RemoteException {
110 if (sService != null) {
111 return sService;
112 }
113 sService = IStatsManager.Stub.asInterface(ServiceManager.getService("stats"));
114 return sService;
David Chen0a368b22017-12-06 16:28:16 -0800115 }
116}