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