aboutsummaryrefslogtreecommitdiff
path: root/src/com/android/providers/contacts/util/LogUtils.java
blob: 414096454ec44bbfe74b856be170c602da8329c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.providers.contacts.util;

import android.os.SystemClock;
import android.util.StatsEvent;
import android.util.StatsLog;

public class LogUtils {
    // Keep in sync with ContactsProviderStatus#ResultType in
    // frameworks/proto_logging/stats/atoms.proto file.
    public interface ResultType {
        int SUCCESS = 1;
        int FAIL = 2;
        int ILLEGAL_ARGUMENT = 3;
        int UNSUPPORTED_OPERATION = 4;
    }

    // Keep in sync with ContactsProviderStatus#ApiType in
    // frameworks/proto_logging/stats/atoms.proto file.
    public interface ApiType {
        int QUERY = 1;
        int INSERT = 2;
        int UPDATE = 3;
        int DELETE = 4;
        int CALL = 5;
        int GAL_CALL = 6;
    }

    // Keep in sync with ContactsProviderStatus#TaskType in
    // frameworks/proto_logging/stats/atoms.proto file.
    public interface TaskType {
        int DANGLING_CONTACTS_CLEANUP_TASK = 1;
    }

    // Keep in sync with ContactsProviderStatus#CallerType in
    // frameworks/proto_logging/stats/atoms.proto file.
    public interface CallerType {
        int CALLER_IS_SYNC_ADAPTER = 1;
        int CALLER_IS_NOT_SYNC_ADAPTER = 2;
    }

    private static final int STATSD_LOG_ATOM_ID = 301;

    // The write methods must be called in the same order as the order of fields in the
    // atom (frameworks/proto_logging/stats/atoms.proto) definition.
    public static void log(LogFields logFields) {
        StatsLog.write(StatsEvent.newBuilder()
                .setAtomId(STATSD_LOG_ATOM_ID)
                .writeInt(logFields.getApiType())
                .writeInt(logFields.getUriType())
                .writeInt(getCallerType(logFields.isCallerIsSyncAdapter()))
                .writeInt(getResultType(logFields.getException()))
                .writeInt(logFields.getResultCount())
                .writeLong(getLatencyMicros(logFields.getStartNanos()))
                .writeInt(logFields.getTaskType())
                .writeInt(0) // Not used yet.
                .writeInt(logFields.getUid())
                .usePooledBuffer()
                .build());
    }

    private static int getCallerType(boolean callerIsSyncAdapter) {
        return callerIsSyncAdapter
                ? CallerType.CALLER_IS_SYNC_ADAPTER : CallerType.CALLER_IS_NOT_SYNC_ADAPTER;
    }

    private static int getResultType(Exception exception) {
        if (exception == null) {
            return ResultType.SUCCESS;
        } else if (exception instanceof IllegalArgumentException) {
            return ResultType.ILLEGAL_ARGUMENT;
        } else if (exception instanceof UnsupportedOperationException) {
            return ResultType.UNSUPPORTED_OPERATION;
        } else {
            return ResultType.FAIL;
        }
    }

    private static long getLatencyMicros(long startNanos) {
        return (SystemClock.elapsedRealtimeNanos() - startNanos) / 1000;
    }
}