1 /* 2 * Copyright (C) 2018 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 com.android.car.settings.common; 18 19 import android.util.Log; 20 21 /** 22 * Helper class that wraps {@link Log} to log messages to logcat. The intended use for a Logger is 23 * to include one per file, using the class.getSimpleName as the prefix, like this: 24 * <pre> private static final Logger LOG = new Logger(MyClass.class); </pre> 25 * 26 * <p> 27 * The logger will log statements in this format: 28 * TAG: [PREFIX] MESSAGE 29 * 30 * <p> 31 * When logging verbose and debug logs, the logs should either be guarded by {@code if (LOG.isV())}, 32 * or a constant if (DEBUG). That DEBUG constant should be false on any submitted code. 33 */ 34 public final class Logger { 35 36 private static final String TAG = "CarSettings"; 37 private final String mPrefix; 38 Logger(Class<?> cls)39 public Logger(Class<?> cls) { 40 this(cls.getSimpleName()); 41 } 42 Logger(String prefix)43 public Logger(String prefix) { 44 mPrefix = "[" + prefix + "] "; 45 } 46 47 /** 48 * Returns true when it is desired to force log all messages. 49 */ forceAllLogging()50 protected boolean forceAllLogging() { 51 return false; 52 } 53 54 /** 55 * Logs a {@link Log#VERBOSE} log message. Will only be logged if {@link Log#VERBOSE} is 56 * loggable. This is a wrapper around {@link Log#v(String, String)}. 57 * 58 * @param message The message you would like logged. 59 */ v(String message)60 public void v(String message) { 61 if (isV()) { 62 Log.v(TAG, mPrefix.concat(message)); 63 } 64 } 65 66 /** 67 * Logs a {@link Log#VERBOSE} log message. Will only be logged if {@link Log#VERBOSE} is 68 * loggable. This is a wrapper around {@link Log#v(String, String, Throwable)}. 69 * 70 * @param message The message you would like logged. 71 * @param throwable An exception to log 72 */ v(String message, Throwable throwable)73 public void v(String message, Throwable throwable) { 74 if (isV()) { 75 Log.v(TAG, mPrefix.concat(message), throwable); 76 } 77 } 78 79 /** 80 * Logs a {@link Log#DEBUG} log message. Will only be logged if {@link Log#DEBUG} is 81 * loggable. This is a wrapper around {@link Log#d(String, String)}. 82 * 83 * @param message The message you would like logged. 84 */ d(String message)85 public void d(String message) { 86 if (isD()) { 87 Log.d(TAG, mPrefix.concat(message)); 88 } 89 } 90 91 /** 92 * Logs a {@link Log#DEBUG} log message. Will only be logged if {@link Log#DEBUG} is 93 * loggable. This is a wrapper around {@link Log#d(String, String, Throwable)}. 94 * 95 * @param message The message you would like logged. 96 * @param throwable An exception to log 97 */ d(String message, Throwable throwable)98 public void d(String message, Throwable throwable) { 99 if (isD()) { 100 Log.d(TAG, mPrefix.concat(message), throwable); 101 } 102 } 103 104 /** 105 * Logs a {@link Log#INFO} log message. Will only be logged if {@link Log#INFO} is loggable. 106 * This is a wrapper around {@link Log#i(String, String)}. 107 * 108 * @param message The message you would like logged. 109 */ i(String message)110 public void i(String message) { 111 if (isI()) { 112 Log.i(TAG, mPrefix.concat(message)); 113 } 114 } 115 116 /** 117 * Logs a {@link Log#INFO} log message. Will only be logged if {@link Log#INFO} is loggable. 118 * This is a wrapper around {@link Log#i(String, String, Throwable)}. 119 * 120 * @param message The message you would like logged. 121 * @param throwable An exception to log 122 */ i(String message, Throwable throwable)123 public void i(String message, Throwable throwable) { 124 if (isI()) { 125 Log.i(TAG, mPrefix.concat(message), throwable); 126 } 127 } 128 129 /** 130 * Logs a {@link Log#WARN} log message. This is a wrapper around {@link Log#w(String, String)}. 131 * 132 * @param message The message you would like logged. 133 */ w(String message)134 public void w(String message) { 135 Log.w(TAG, mPrefix.concat(message)); 136 } 137 138 /** 139 * Logs a {@link Log#WARN} log message. This is a wrapper around 140 * {@link Log#w(String, String, Throwable)}. 141 * 142 * @param message The message you would like logged. 143 * @param throwable An exception to log 144 */ w(String message, Throwable throwable)145 public void w(String message, Throwable throwable) { 146 Log.w(TAG, mPrefix.concat(message), throwable); 147 } 148 149 /** 150 * Logs a {@link Log#ERROR} log message. This is a wrapper around {@link Log#e(String, String)}. 151 * 152 * @param message The message you would like logged. 153 */ e(String message)154 public void e(String message) { 155 Log.e(TAG, mPrefix.concat(message)); 156 } 157 158 /** 159 * Logs a {@link Log#ERROR} log message. This is a wrapper around 160 * {@link Log#e(String, String, Throwable)}. 161 * 162 * @param message The message you would like logged. 163 * @param throwable An exception to log 164 */ e(String message, Throwable throwable)165 public void e(String message, Throwable throwable) { 166 Log.e(TAG, mPrefix.concat(message), throwable); 167 } 168 169 /** 170 * Logs a "What a Terrible Failure" as an {@link Log#ASSERT} log message. This is a wrapper 171 * around {@link Log#w(String, String)}. 172 * 173 * @param message The message you would like logged. 174 */ wtf(String message)175 public void wtf(String message) { 176 Log.wtf(TAG, mPrefix.concat(message)); 177 } 178 179 /** 180 * Logs a "What a Terrible Failure" as an {@link Log#ASSERT} log message. This is a wrapper 181 * around {@link Log#wtf(String, String, Throwable)}. 182 * 183 * @param message The message you would like logged. 184 * @param throwable An exception to log 185 */ wtf(String message, Throwable throwable)186 public void wtf(String message, Throwable throwable) { 187 Log.wtf(TAG, mPrefix.concat(message), throwable); 188 } 189 isV()190 private boolean isV() { 191 return Log.isLoggable(TAG, Log.VERBOSE) || forceAllLogging(); 192 } 193 isD()194 private boolean isD() { 195 return Log.isLoggable(TAG, Log.DEBUG) || forceAllLogging(); 196 } 197 isI()198 private boolean isI() { 199 return Log.isLoggable(TAG, Log.INFO) || forceAllLogging(); 200 } 201 202 /** 203 * Returns the tag used when wrapping {@link Log} methods. 204 */ getTag()205 public String getTag() { 206 return TAG; 207 } 208 209 @Override toString()210 public String toString() { 211 return "Logger[TAG=" + TAG + ", prefix=\"" + mPrefix + "\"]"; 212 } 213 } 214