• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 package com.android.providers.contacts.util;
18 
19 import android.os.SystemClock;
20 import android.util.StatsEvent;
21 import android.util.StatsLog;
22 
23 public class LogUtils {
24     // Keep in sync with ContactsProviderStatus#ResultType in
25     // frameworks/proto_logging/stats/atoms.proto file.
26     public interface ResultType {
27         int SUCCESS = 1;
28         int FAIL = 2;
29         int ILLEGAL_ARGUMENT = 3;
30         int UNSUPPORTED_OPERATION = 4;
31     }
32 
33     // Keep in sync with ContactsProviderStatus#ApiType in
34     // frameworks/proto_logging/stats/atoms.proto file.
35     public interface ApiType {
36         int QUERY = 1;
37         int INSERT = 2;
38         int UPDATE = 3;
39         int DELETE = 4;
40     }
41 
42     // Keep in sync with ContactsProviderStatus#TaskType in
43     // frameworks/proto_logging/stats/atoms.proto file.
44     public interface TaskType {
45         int DANGLING_CONTACTS_CLEANUP_TASK = 1;
46     }
47 
48     // Keep in sync with ContactsProviderStatus#CallerType in
49     // frameworks/proto_logging/stats/atoms.proto file.
50     public interface CallerType {
51         int CALLER_IS_SYNC_ADAPTER = 1;
52         int CALLER_IS_NOT_SYNC_ADAPTER = 2;
53     }
54 
55     private static final int STATSD_LOG_ATOM_ID = 301;
56 
57 
58     // The write methods must be called in the same order as the order of fields in the
59     // atom (frameworks/proto_logging/stats/atoms.proto) definition.
log(LogFields logFields)60     public static void log(LogFields logFields) {
61         StatsLog.write(StatsEvent.newBuilder()
62                 .setAtomId(STATSD_LOG_ATOM_ID)
63                 .writeInt(logFields.getApiType())
64                 .writeInt(logFields.getUriType())
65                 .writeInt(getCallerType(logFields.isCallerIsSyncAdapter()))
66                 .writeInt(getResultType(logFields.getException()))
67                 .writeInt(logFields.getResultCount())
68                 .writeLong(getLatencyMicros(logFields.getStartNanos()))
69                 .writeInt(logFields.getTaskType())
70                 .usePooledBuffer()
71                 .build());
72     }
73 
getCallerType(boolean callerIsSyncAdapter)74     private static int getCallerType(boolean callerIsSyncAdapter) {
75         return callerIsSyncAdapter
76                 ? CallerType.CALLER_IS_SYNC_ADAPTER : CallerType.CALLER_IS_NOT_SYNC_ADAPTER;
77     }
78 
getResultType(Exception exception)79     private static int getResultType(Exception exception) {
80         if (exception == null) {
81             return ResultType.SUCCESS;
82         } else if (exception instanceof IllegalArgumentException) {
83             return ResultType.ILLEGAL_ARGUMENT;
84         } else if (exception instanceof UnsupportedOperationException) {
85             return ResultType.UNSUPPORTED_OPERATION;
86         } else {
87             return ResultType.FAIL;
88         }
89     }
90 
getLatencyMicros(long startNanos)91     private static long getLatencyMicros(long startNanos) {
92         return (SystemClock.elapsedRealtimeNanos() - startNanos) / 1000;
93     }
94 }
95 
96 
97