1 /* 2 * Copyright (C) 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 package com.google.android.setupcompat.util; 17 18 import android.util.Log; 19 20 /** 21 * Helper class that wraps {@link Log} to log messages to logcat. This class consolidate the log 22 * {@link #TAG} in both SetupCompat and SetupDesign library. 23 * 24 * <p>When logging verbose and debug logs, the logs should either be guarded by {@code if 25 * (logger.isV())}, or a constant if (DEBUG). That DEBUG constant should be false on any submitted 26 * code. 27 */ 28 public final class Logger { 29 30 public static final String TAG = "SetupLibrary"; 31 32 private final String prefix; 33 Logger(Class<?> cls)34 public Logger(Class<?> cls) { 35 this(cls.getSimpleName()); 36 } 37 Logger(String prefix)38 public Logger(String prefix) { 39 this.prefix = "[" + prefix + "] "; 40 } 41 isV()42 public boolean isV() { 43 return Log.isLoggable(TAG, Log.VERBOSE); 44 } 45 isD()46 public boolean isD() { 47 return Log.isLoggable(TAG, Log.DEBUG); 48 } 49 isI()50 public boolean isI() { 51 return Log.isLoggable(TAG, Log.INFO); 52 } 53 atVerbose(String message)54 public void atVerbose(String message) { 55 if (isV()) { 56 Log.v(TAG, prefix.concat(message)); 57 } 58 } 59 atDebug(String message)60 public void atDebug(String message) { 61 if (isD()) { 62 Log.d(TAG, prefix.concat(message)); 63 } 64 } 65 atInfo(String message)66 public void atInfo(String message) { 67 if (isI()) { 68 Log.i(TAG, prefix.concat(message)); 69 } 70 } 71 w(String message)72 public void w(String message) { 73 Log.w(TAG, prefix.concat(message)); 74 } 75 e(String message)76 public void e(String message) { 77 Log.e(TAG, prefix.concat(message)); 78 } 79 e(String message, Throwable throwable)80 public void e(String message, Throwable throwable) { 81 Log.e(TAG, prefix.concat(message), throwable); 82 } 83 } 84