1 /* 2 * Copyright (C) 2020 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.deskclock 18 19 import android.os.Build 20 import android.util.Log 21 22 object LogUtils { 23 /** Default logger used for generic logging, i.e TAG. when a specific log tag isn't specified.*/ 24 private val DEFAULT_LOGGER = Logger("AlarmClock") 25 26 @JvmStatic vnull27 fun v(message: String, vararg args: Any?) { 28 DEFAULT_LOGGER.v(message, *args) 29 } 30 31 @JvmStatic dnull32 fun d(message: String, vararg args: Any?) { 33 DEFAULT_LOGGER.d(message, *args) 34 } 35 36 @JvmStatic inull37 fun i(message: String, vararg args: Any?) { 38 DEFAULT_LOGGER.i(message, *args) 39 } 40 41 @JvmStatic wnull42 fun w(message: String, vararg args: Any?) { 43 DEFAULT_LOGGER.w(message, *args) 44 } 45 enull46 fun e(message: String, vararg args: Any?) { 47 DEFAULT_LOGGER.e(message, *args) 48 } 49 50 @JvmStatic enull51 fun e(message: String, e: Throwable) { 52 DEFAULT_LOGGER.e(message, e) 53 } 54 wtfnull55 fun wtf(message: String, vararg args: Any?) { 56 DEFAULT_LOGGER.wtf(message, *args) 57 } 58 59 @JvmStatic wtfnull60 fun wtf(e: Throwable) { 61 DEFAULT_LOGGER.wtf(e) 62 } 63 64 class Logger(val logTag: String) { 65 val isVerboseLoggable: Boolean 66 get() = DEBUG || Log.isLoggable(logTag, Log.VERBOSE) 67 68 val isDebugLoggable: Boolean 69 get() = DEBUG || Log.isLoggable(logTag, Log.DEBUG) 70 71 val isInfoLoggable: Boolean 72 get() = DEBUG || Log.isLoggable(logTag, Log.INFO) 73 74 val isWarnLoggable: Boolean 75 get() = DEBUG || Log.isLoggable(logTag, Log.WARN) 76 77 val isErrorLoggable: Boolean 78 get() = DEBUG || Log.isLoggable(logTag, Log.ERROR) 79 80 val isWtfLoggable: Boolean 81 get() = DEBUG || Log.isLoggable(logTag, Log.ASSERT) 82 vnull83 fun v(message: String, vararg args: Any?) { 84 if (isVerboseLoggable) { 85 Log.v(logTag, if (args.isEmpty() || args[0] == null) { 86 message 87 } else { 88 String.format(message, *args) 89 }) 90 } 91 } 92 dnull93 fun d(message: String, vararg args: Any?) { 94 if (isDebugLoggable) { 95 Log.d(logTag, if (args.isEmpty() || args[0] == null) { 96 message 97 } else { 98 String.format(message, *args) 99 }) 100 } 101 } 102 inull103 fun i(message: String, vararg args: Any?) { 104 if (isInfoLoggable) { 105 Log.i(logTag, if (args.isEmpty() || args[0] == null) { 106 message 107 } else { 108 String.format(message, *args) 109 }) 110 } 111 } 112 wnull113 fun w(message: String, vararg args: Any?) { 114 if (isWarnLoggable) { 115 Log.w(logTag, if (args.isEmpty() || args[0] == null) { 116 message 117 } else { 118 String.format(message, *args) 119 }) 120 } 121 } 122 enull123 fun e(message: String, vararg args: Any?) { 124 if (isErrorLoggable) { 125 Log.e(logTag, if (args.isEmpty() || args[0] == null) { 126 message 127 } else { 128 String.format(message, *args) 129 }) 130 } 131 } 132 enull133 fun e(message: String, e: Throwable) { 134 if (isErrorLoggable) { 135 Log.e(logTag, message, e) 136 } 137 } 138 wtfnull139 fun wtf(message: String, vararg args: Any?) { 140 if (isWtfLoggable) { 141 Log.wtf(logTag, if (args.isEmpty() || args[0] == null) { 142 message 143 } else { 144 String.format(message, *args) 145 }) 146 } 147 } 148 wtfnull149 fun wtf(e: Throwable) { 150 if (isWtfLoggable) { 151 Log.wtf(logTag, e) 152 } 153 } 154 155 companion object { 156 /** Log everything for debug builds or if running on a dev device. */ 157 val DEBUG = (BuildConfig.DEBUG || "eng" == Build.TYPE || "userdebug" == Build.TYPE) 158 } 159 } 160 }