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 int CALL = 5; 41 } 42 43 // Keep in sync with ContactsProviderStatus#CallerType in 44 // frameworks/proto_logging/stats/atoms.proto file. 45 public interface CallerType { 46 int CALLER_IS_SYNC_ADAPTER = 1; 47 int CALLER_IS_NOT_SYNC_ADAPTER = 2; 48 } 49 50 // Keep in sync with ContactsProviderStatus#MethodCall in 51 // frameworks/proto_logging/stats/atoms.proto file. 52 public interface MethodCall { 53 int ADD_SIM_ACCOUNTS = 1; 54 int REMOVE_SIM_ACCOUNTS = 2; 55 int GET_SIM_ACCOUNTS = 3; 56 } 57 58 private static final int STATSD_LOG_ATOM_ID = 301; 59 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(0) // Empty value for TaskType 70 .writeInt(logFields.getMethodCall()) 71 .usePooledBuffer() 72 .build()); 73 } 74 getCallerType(boolean callerIsSyncAdapter)75 private static int getCallerType(boolean callerIsSyncAdapter) { 76 return callerIsSyncAdapter 77 ? CallerType.CALLER_IS_SYNC_ADAPTER : CallerType.CALLER_IS_NOT_SYNC_ADAPTER; 78 } 79 getResultType(Exception exception)80 private static int getResultType(Exception exception) { 81 if (exception == null) { 82 return ResultType.SUCCESS; 83 } else if (exception instanceof IllegalArgumentException) { 84 return ResultType.ILLEGAL_ARGUMENT; 85 } else if (exception instanceof UnsupportedOperationException) { 86 return ResultType.UNSUPPORTED_OPERATION; 87 } else { 88 return ResultType.FAIL; 89 } 90 } 91 getLatencyMicros(long startNanos)92 private static long getLatencyMicros(long startNanos) { 93 return (SystemClock.elapsedRealtimeNanos() - startNanos) / 1000; 94 } 95 } 96 97 98