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#CallerType in 43 // frameworks/proto_logging/stats/atoms.proto file. 44 public interface CallerType { 45 int CALLER_IS_SYNC_ADAPTER = 1; 46 int CALLER_IS_NOT_SYNC_ADAPTER = 2; 47 } 48 49 private static final int STATSD_LOG_ATOM_ID = 301; 50 log(LogFields logFields)51 public static void log(LogFields logFields) { 52 StatsLog.write(StatsEvent.newBuilder() 53 .setAtomId(STATSD_LOG_ATOM_ID) 54 .writeInt(logFields.getApiType()) 55 .writeInt(logFields.getUriType()) 56 .writeInt(getCallerType(logFields.isCallerIsSyncAdapter())) 57 .writeInt(getResultType(logFields.getException())) 58 .writeInt(logFields.getResultCount()) 59 .writeLong(getLatencyMicros(logFields.getStartNanos())) 60 .usePooledBuffer() 61 .build()); 62 } 63 getCallerType(boolean callerIsSyncAdapter)64 private static int getCallerType(boolean callerIsSyncAdapter) { 65 return callerIsSyncAdapter 66 ? CallerType.CALLER_IS_SYNC_ADAPTER : CallerType.CALLER_IS_NOT_SYNC_ADAPTER; 67 } 68 getResultType(Exception exception)69 private static int getResultType(Exception exception) { 70 if (exception == null) { 71 return ResultType.SUCCESS; 72 } else if (exception instanceof IllegalArgumentException) { 73 return ResultType.ILLEGAL_ARGUMENT; 74 } else if (exception instanceof UnsupportedOperationException) { 75 return ResultType.UNSUPPORTED_OPERATION; 76 } else { 77 return ResultType.FAIL; 78 } 79 } 80 getLatencyMicros(long startNanos)81 private static long getLatencyMicros(long startNanos) { 82 return (SystemClock.elapsedRealtimeNanos() - startNanos) / 1000; 83 } 84 } 85 86 87