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.app.appsearch.AppSearchEnvironmentFactory; 23 import android.util.Log; 24 25 /** 26 * Utilities for logging to logcat. 27 * 28 * @hide 29 */ 30 public final class LogUtil { 31 /** Whether to log {@link Log#VERBOSE} and {@link Log#DEBUG} logs. */ 32 // TODO(b/232285376): If it becomes possible to detect an eng build, turn this on by default 33 // for eng builds. 34 public static final boolean DEBUG = false; 35 36 public static final boolean INFO = 37 AppSearchEnvironmentFactory.getEnvironmentInstance().isInfoLoggingEnabled(); 38 39 /** 40 * The {@link #piiTrace} logs are intended for sensitive data that can't be enabled in 41 * production, so they are build-gated by this constant. 42 * 43 * <p> 44 * 45 * <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 * 77 * <p>Otherwise, {@code traceObj} is logged if it is non-null. 78 */ piiTrace( @izemin = 0, max = 23) @onNull String tag, @NonNull String message, @Nullable Object traceObj)79 public static void piiTrace( 80 @Size(min = 0, max = 23) @NonNull String tag, 81 @NonNull String message, 82 @Nullable Object traceObj) { 83 piiTrace(tag, message, /* fastTraceObj= */ traceObj, /* fullTraceObj= */ null); 84 } 85 86 /** 87 * If icing lib interaction tracing is enabled via {@link #PII_TRACE_LEVEL}, logs the provided 88 * message and objects to logcat. 89 * 90 * <p>If {@link #PII_TRACE_LEVEL} is 0, nothing is logged and this method returns immediately. 91 * 92 * <p>If {@link #PII_TRACE_LEVEL} is 1, {@code fastTraceObj} is logged if it is non-null. 93 * 94 * <p>If {@link #PII_TRACE_LEVEL} is 2, {@code fullTraceObj} is logged if it is non-null, else 95 * {@code fastTraceObj} is logged if it is non-null.. 96 */ piiTrace( @izemin = 0, max = 23) @onNull String tag, @NonNull String message, @Nullable Object fastTraceObj, @Nullable Object fullTraceObj)97 public static void piiTrace( 98 @Size(min = 0, max = 23) @NonNull String tag, 99 @NonNull String message, 100 @Nullable Object fastTraceObj, 101 @Nullable Object fullTraceObj) { 102 if (PII_TRACE_LEVEL == 0 || !INFO) { 103 return; 104 } 105 StringBuilder builder = new StringBuilder("(trace) ").append(message); 106 if (PII_TRACE_LEVEL == 1 && fastTraceObj != null) { 107 builder.append(": ").append(fastTraceObj); 108 } else if (PII_TRACE_LEVEL == 2 && fullTraceObj != null) { 109 builder.append(": ").append(fullTraceObj); 110 } else if (PII_TRACE_LEVEL == 2 && fastTraceObj != null) { 111 builder.append(": ").append(fastTraceObj); 112 } 113 Log.i(tag, builder.toString()); 114 } 115 } 116