• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2021 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 android.app.appsearch.util;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.annotation.Size;
22 import android.util.Log;
23 
24 /**
25  * Utilities for logging to logcat.
26  *
27  * @hide
28  */
29 public final class LogUtil {
30     /** Whether to log {@link Log#VERBOSE} and {@link Log#DEBUG} logs. */
31     // TODO(b/232285376): If it becomes possible to detect an eng build, turn this on by default
32     //  for eng builds.
33     public static final boolean DEBUG = false;
34 
35     /**
36      * The {@link #piiTrace} logs are intended for sensitive data that can't be enabled in
37      * production, so they are build-gated by this constant.
38      *
39      * <p>
40      *
41      * <ul>
42      *   <li>0: no tracing.
43      *   <li>1: fast tracing (statuses/counts only)
44      *   <li>2: full tracing (complete messages)
45      * </ul>
46      */
47     private static final int PII_TRACE_LEVEL = 0;
48 
LogUtil()49     private LogUtil() {}
50 
51     /** Returns whether piiTrace() is enabled (PII_TRACE_LEVEL > 0). */
isPiiTraceEnabled()52     public static boolean isPiiTraceEnabled() {
53         return PII_TRACE_LEVEL > 0;
54     }
55 
56     /**
57      * If icing lib interaction tracing is enabled via {@link #PII_TRACE_LEVEL}, logs the provided
58      * message to logcat.
59      *
60      * <p>If {@link #PII_TRACE_LEVEL} is 0, nothing is logged and this method returns immediately.
61      */
piiTrace( @izemin = 0, max = 23) @onNull String tag, @NonNull String message)62     public static void piiTrace(
63             @Size(min = 0, max = 23) @NonNull String tag, @NonNull String message) {
64         piiTrace(tag, message, /*fastTraceObj=*/ null, /*fullTraceObj=*/ null);
65     }
66 
67     /**
68      * If icing lib interaction tracing is enabled via {@link #PII_TRACE_LEVEL}, logs the provided
69      * message and object to logcat.
70      *
71      * <p>If {@link #PII_TRACE_LEVEL} is 0, nothing is logged and this method returns immediately.
72      *
73      * <p>Otherwise, {@code traceObj} is logged if it is non-null.
74      */
piiTrace( @izemin = 0, max = 23) @onNull String tag, @NonNull String message, @Nullable Object traceObj)75     public static void piiTrace(
76             @Size(min = 0, max = 23) @NonNull String tag,
77             @NonNull String message,
78             @Nullable Object traceObj) {
79         piiTrace(tag, message, /*fastTraceObj=*/ traceObj, /*fullTraceObj=*/ null);
80     }
81 
82     /**
83      * If icing lib interaction tracing is enabled via {@link #PII_TRACE_LEVEL}, logs the provided
84      * message and objects to logcat.
85      *
86      * <p>If {@link #PII_TRACE_LEVEL} is 0, nothing is logged and this method returns immediately.
87      *
88      * <p>If {@link #PII_TRACE_LEVEL} is 1, {@code fastTraceObj} is logged if it is non-null.
89      *
90      * <p>If {@link #PII_TRACE_LEVEL} is 2, {@code fullTraceObj} is logged if it is non-null, else
91      * {@code fastTraceObj} is logged if it is non-null..
92      */
piiTrace( @izemin = 0, max = 23) @onNull String tag, @NonNull String message, @Nullable Object fastTraceObj, @Nullable Object fullTraceObj)93     public static void piiTrace(
94             @Size(min = 0, max = 23) @NonNull String tag,
95             @NonNull String message,
96             @Nullable Object fastTraceObj,
97             @Nullable Object fullTraceObj) {
98         if (PII_TRACE_LEVEL == 0) {
99             return;
100         }
101         StringBuilder builder = new StringBuilder("(trace) ").append(message);
102         if (PII_TRACE_LEVEL == 1 && fastTraceObj != null) {
103             builder.append(": ").append(fastTraceObj);
104         } else if (PII_TRACE_LEVEL == 2 && fullTraceObj != null) {
105             builder.append(": ").append(fullTraceObj);
106         } else if (PII_TRACE_LEVEL == 2 && fastTraceObj != null) {
107             builder.append(": ").append(fastTraceObj);
108         }
109         Log.i(tag, builder.toString());
110     }
111 }
112