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.util.Log; 22 23 import java.util.Objects; 24 25 /** 26 * Utilities for logging to logcat. 27 * 28 * @hide 29 */ 30 public final class LogUtil { 31 /** 32 * The {@link #piiTrace} logs are intended for sensitive data that can't be enabled in 33 * production, so they are build-gated by this constant. 34 * 35 * <p> 36 * 37 * <ul> 38 * <li>0: no tracing. 39 * <li>1: fast tracing (statuses/counts only) 40 * <li>2: full tracing (complete messages) 41 * </ul> 42 */ 43 private static final int PII_TRACE_LEVEL = 0; 44 45 private final String mTag; 46 LogUtil(@onNull String tag)47 public LogUtil(@NonNull String tag) { 48 mTag = Objects.requireNonNull(tag); 49 } 50 51 /** Returns whether piiTrace() is enabled (PII_TRACE_LEVEL > 0). */ isPiiTraceEnabled()52 public 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(@onNull String message)62 public void piiTrace(@NonNull String message) { 63 piiTrace(message, /*fastTraceObj=*/ null, /*fullTraceObj=*/ null); 64 } 65 66 /** 67 * If icing lib interaction tracing is enabled via {@link #PII_TRACE_LEVEL}, logs the provided 68 * message and object to logcat. 69 * 70 * <p>If {@link #PII_TRACE_LEVEL} is 0, nothing is logged and this method returns immediately. 71 * 72 * <p>Otherwise, {@code traceObj} is logged if it is non-null. 73 */ piiTrace(@onNull String message, @Nullable Object traceObj)74 public void piiTrace(@NonNull String message, @Nullable Object traceObj) { 75 piiTrace(message, /*fastTraceObj=*/ traceObj, /*fullTraceObj=*/ null); 76 } 77 78 /** 79 * If icing lib interaction tracing is enabled via {@link #PII_TRACE_LEVEL}, logs the provided 80 * message and objects to logcat. 81 * 82 * <p>If {@link #PII_TRACE_LEVEL} is 0, nothing is logged and this method returns immediately. 83 * 84 * <p>If {@link #PII_TRACE_LEVEL} is 1, {@code fastTraceObj} is logged if it is non-null. 85 * 86 * <p>If {@link #PII_TRACE_LEVEL} is 2, {@code fullTraceObj} is logged if it is non-null, else 87 * {@code fastTraceObj} is logged if it is non-null.. 88 */ piiTrace( @onNull String message, @Nullable Object fastTraceObj, @Nullable Object fullTraceObj)89 public void piiTrace( 90 @NonNull String message, @Nullable Object fastTraceObj, @Nullable Object fullTraceObj) { 91 if (PII_TRACE_LEVEL == 0) { 92 return; 93 } 94 StringBuilder builder = new StringBuilder("(trace) ").append(message); 95 if (PII_TRACE_LEVEL == 1 && fastTraceObj != null) { 96 builder.append(": ").append(fastTraceObj); 97 } else if (PII_TRACE_LEVEL == 2 && fullTraceObj != null) { 98 builder.append(": ").append(fullTraceObj); 99 } else if (PII_TRACE_LEVEL == 2 && fastTraceObj != null) { 100 builder.append(": ").append(fastTraceObj); 101 } 102 Log.i(mTag, builder.toString()); 103 } 104 } 105