1 /* 2 * Copyright (C) 2022 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.devicelockcontroller.util; 18 19 import android.os.Build; 20 import android.util.Log; 21 import android.util.Slog; 22 23 /** 24 * Utility class for logging. Only messages that are loggable for the given tag/log level 25 * are effectively logged. 26 */ 27 public final class LogUtil { 28 @SuppressWarnings("IsLoggableTagLength") // only an issue for android <= 7.0 (error prone). isLoggable(String tag, int level)29 private static boolean isLoggable(String tag, int level) { 30 return Build.isDebuggable() || Log.isLoggable(tag, level); 31 } 32 LogUtil()33 private LogUtil() {} 34 35 /** Log the message as DEBUG. */ d(String tag, String msg, Throwable tr)36 public static int d(String tag, String msg, Throwable tr) { 37 if (isLoggable(tag, Log.DEBUG)) { 38 return Slog.d(tag, msg, tr); 39 } 40 41 return 0; 42 } 43 44 /** Log the message as DEBUG. */ d(String tag, String msg)45 public static int d(String tag, String msg) { 46 if (isLoggable(tag, Log.DEBUG)) { 47 return Slog.d(tag, msg); 48 } 49 50 return 0; 51 } 52 53 /** Log the message as ERROR. */ e(String tag, String msg, Throwable tr)54 public static int e(String tag, String msg, Throwable tr) { 55 if (isLoggable(tag, Log.ERROR)) { 56 return Slog.e(tag, msg, tr); 57 } 58 59 return 0; 60 } 61 62 /** Log the message as ERROR. */ e(String tag, String msg)63 public static int e(String tag, String msg) { 64 if (isLoggable(tag, Log.ERROR)) { 65 return Slog.e(tag, msg); 66 } 67 68 return 0; 69 } 70 71 /** Log the message as INFO. */ i(String tag, String msg, Throwable tr)72 public static int i(String tag, String msg, Throwable tr) { 73 if (isLoggable(tag, Log.INFO)) { 74 return Slog.i(tag, msg, tr); 75 } 76 77 return 0; 78 } 79 80 /** Log the message as INFO. */ i(String tag, String msg)81 public static int i(String tag, String msg) { 82 if (isLoggable(tag, Log.INFO)) { 83 return Slog.i(tag, msg); 84 } 85 86 return 0; 87 } 88 89 /** Log the message as VERBOSE. */ v(String tag, String msg, Throwable tr)90 public static int v(String tag, String msg, Throwable tr) { 91 if (isLoggable(tag, Log.VERBOSE)) { 92 return Slog.v(tag, msg, tr); 93 } 94 95 return 0; 96 } 97 98 /** Log the message as VERBOSE. */ v(String tag, String msg)99 public static int v(String tag, String msg) { 100 if (isLoggable(tag, Log.VERBOSE)) { 101 return Slog.v(tag, msg); 102 } 103 104 return 0; 105 } 106 107 /** Log the message as WARN. */ w(String tag, String msg, Throwable tr)108 public static int w(String tag, String msg, Throwable tr) { 109 if (isLoggable(tag, Log.WARN)) { 110 return Slog.w(tag, msg, tr); 111 } 112 113 return 0; 114 } 115 116 /** Log the message as WARN. */ w(String tag, Throwable tr)117 public static int w(String tag, Throwable tr) { 118 if (isLoggable(tag, Log.WARN)) { 119 return Slog.w(tag, tr); 120 } 121 122 return 0; 123 } 124 125 /** Log the message as WARN. */ w(String tag, String msg)126 public static int w(String tag, String msg) { 127 if (isLoggable(tag, Log.WARN)) { 128 return Slog.w(tag, msg); 129 } 130 131 return 0; 132 } 133 } 134