1 /* 2 * Copyright (C) 2023 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.adservices; 18 19 import android.util.Log; 20 21 import com.google.errorprone.annotations.FormatMethod; 22 import com.google.errorprone.annotations.FormatString; 23 24 import java.util.Locale; 25 26 /** 27 * Logger factory to create logger for logging to logcat with the various logcat tags. 28 * 29 * @hide 30 */ 31 public final class LoggerFactory { 32 // NOTE: new log tags here also need to add to setAllLogcatTags() in 33 // AbstractAdServicesFlagsSetterRule class 34 public static final String TAG = "adservices"; 35 public static final String TOPICS_TAG = "adservices.topics"; 36 public static final String FLEDGE_TAG = "adservices.fledge"; 37 public static final String KANON_TAG = "adservices.kanon"; 38 public static final String MEASUREMENT_TAG = "adservices.measurement"; 39 public static final String UI_TAG = "adservices.ui"; 40 public static final String ADID_TAG = "adservices.adid"; 41 public static final String APPSETID_TAG = "adservices.appsetid"; 42 43 private static final Logger sLogger = new Logger(TAG); 44 private static final Logger sTopicsLogger = new Logger(TOPICS_TAG); 45 private static final Logger sFledgeLogger = new Logger(FLEDGE_TAG); 46 private static final Logger sKAnonLogger = new Logger(KANON_TAG); 47 private static final Logger sMeasurementLogger = new Logger(MEASUREMENT_TAG); 48 private static final Logger sUILogger = new Logger(UI_TAG); 49 private static final Logger sAdIDLogger = new Logger(ADID_TAG); 50 private static final Logger sAppSetIDLogger = new Logger(APPSETID_TAG); 51 getLogger()52 public static Logger getLogger() { 53 return sLogger; 54 } 55 getTopicsLogger()56 public static Logger getTopicsLogger() { 57 return sTopicsLogger; 58 } 59 getFledgeLogger()60 public static Logger getFledgeLogger() { 61 return sFledgeLogger; 62 } 63 getKAnonLogger()64 public static Logger getKAnonLogger() { 65 return sKAnonLogger; 66 } 67 getMeasurementLogger()68 public static Logger getMeasurementLogger() { 69 return sMeasurementLogger; 70 } 71 getUILogger()72 public static Logger getUILogger() { 73 return sUILogger; 74 } 75 getAdIDLogger()76 public static Logger getAdIDLogger() { 77 return sAdIDLogger; 78 } 79 getAppSetIDLogger()80 public static Logger getAppSetIDLogger() { 81 return sAppSetIDLogger; 82 } 83 84 /** 85 * Logger for logging to logcat with the various logcat tags. 86 * 87 * @hide 88 */ 89 public static final class Logger { 90 private final String mTag; 91 Logger(String tag)92 private Logger(String tag) { 93 mTag = tag; 94 } 95 96 /** Log the message as VERBOSE. Return The number of bytes written. */ v(String msg)97 public int v(String msg) { 98 if (Log.isLoggable(mTag, Log.VERBOSE)) { 99 return Log.v(mTag, msg); 100 } 101 return 0; 102 } 103 104 /** Log the message as VERBOSE. Return The number of bytes written. */ 105 @FormatMethod v(@ormatString String format, Object... params)106 public int v(@FormatString String format, Object... params) { 107 if (Log.isLoggable(mTag, Log.VERBOSE)) { 108 String msg = format(format, params); 109 return Log.v(mTag, msg); 110 } 111 return 0; 112 } 113 114 /** Log the message as DEBUG. Return The number of bytes written. */ d(String msg)115 public int d(String msg) { 116 if (Log.isLoggable(mTag, Log.DEBUG)) { 117 return Log.d(mTag, msg); 118 } 119 return 0; 120 } 121 122 /** Log the message as DEBUG. Return The number of bytes written. */ 123 @FormatMethod d(@ormatString String format, Object... params)124 public int d(@FormatString String format, Object... params) { 125 if (Log.isLoggable(mTag, Log.DEBUG)) { 126 String msg = format(format, params); 127 return Log.d(mTag, msg); 128 } 129 return 0; 130 } 131 132 /** Log the message as DEBUG. Return The number of bytes written. */ 133 @FormatMethod d(Throwable tr, @FormatString String format, Object... params)134 public int d(Throwable tr, @FormatString String format, Object... params) { 135 if (Log.isLoggable(mTag, Log.DEBUG)) { 136 String msg = format(format, params); 137 return Log.d(mTag, msg, tr); 138 } 139 return 0; 140 } 141 142 /** Log the message as INFO. Return The number of bytes written. */ i(String msg)143 public int i(String msg) { 144 if (Log.isLoggable(mTag, Log.INFO)) { 145 return Log.i(mTag, msg); 146 } 147 return 0; 148 } 149 150 /** Log the message as INFO. Return The number of bytes written */ 151 @FormatMethod i(@ormatString String format, Object... params)152 public int i(@FormatString String format, Object... params) { 153 if (Log.isLoggable(mTag, Log.INFO)) { 154 String msg = format(format, params); 155 return Log.i(mTag, msg); 156 } 157 return 0; 158 } 159 160 /** Log the message as WARNING. Return The number of bytes written */ w(String msg)161 public int w(String msg) { 162 if (Log.isLoggable(mTag, Log.WARN)) { 163 return Log.w(mTag, msg); 164 } 165 return 0; 166 } 167 168 /** Log the message as WARNING. Return The number of bytes written */ 169 @FormatMethod w(@ormatString String format, Object... params)170 public int w(@FormatString String format, Object... params) { 171 if (Log.isLoggable(mTag, Log.WARN)) { 172 String msg = format(format, params); 173 return Log.w(mTag, msg); 174 } 175 return 0; 176 } 177 178 /** Log the message as ERROR. Return The number of bytes written */ e(String msg)179 public int e(String msg) { 180 if (Log.isLoggable(mTag, Log.ERROR)) { 181 return Log.e(mTag, msg); 182 } 183 return 0; 184 } 185 186 /** Log the message as ERROR. Return The number of bytes written */ 187 @FormatMethod e(@ormatString String format, Object... params)188 public int e(@FormatString String format, Object... params) { 189 if (Log.isLoggable(mTag, Log.ERROR)) { 190 String msg = format(format, params); 191 return Log.e(mTag, msg); 192 } 193 return 0; 194 } 195 196 /** Log the message as ERROR. Return The number of bytes written */ e(Throwable tr, String msg)197 public int e(Throwable tr, String msg) { 198 if (Log.isLoggable(mTag, Log.ERROR)) { 199 if (Log.isLoggable(mTag, Log.DEBUG)) { 200 return Log.e(mTag, msg, tr); 201 } else { 202 // If not DEBUG level, only print the throwable type and message. 203 msg = msg + ": " + tr; 204 return Log.e(mTag, msg); 205 } 206 } 207 return 0; 208 } 209 210 /** Log the message as ERROR. Return The number of bytes written */ 211 @FormatMethod e(Throwable tr, @FormatString String format, Object... params)212 public int e(Throwable tr, @FormatString String format, Object... params) { 213 return Log.isLoggable(mTag, Log.ERROR) ? e(tr, format(format, params)) : 0; 214 } 215 216 /** Log the message as WARNING. Return The number of bytes written */ 217 @FormatMethod w(Throwable tr, @FormatString String format, Object... params)218 public int w(Throwable tr, @FormatString String format, Object... params) { 219 if (Log.isLoggable(mTag, Log.WARN)) { 220 if (Log.isLoggable(mTag, Log.DEBUG)) { 221 String msg = format(format, params); 222 return Log.w(mTag, msg, tr); 223 } else { 224 // If not DEBUG level, only print the throwable type and message. 225 String msg = format(format, params) + ": " + tr; 226 return Log.w(mTag, msg); 227 } 228 } 229 return 0; 230 } 231 232 /** 233 * Logs the message as DEBUG and returns the number of bytes written. 234 * 235 * @deprecated This method is an overload for safety; please use {@link #d(Throwable, 236 * String, Object...)} instead. 237 */ 238 @Deprecated d(String msg, Throwable tr)239 public int d(String msg, Throwable tr) { 240 @SuppressWarnings("FormatStringAnnotation") 241 int result = d(tr, msg); 242 return result; 243 } 244 245 /** 246 * Logs the message as WARNING and returns the number of bytes written. 247 * 248 * @deprecated This method is an overload for safety; please use {@link #w(Throwable, 249 * String, Object...)} instead. 250 */ 251 @Deprecated w(String msg, Throwable tr)252 public int w(String msg, Throwable tr) { 253 @SuppressWarnings("FormatStringAnnotation") 254 int result = w(tr, msg); 255 return result; 256 } 257 258 /** 259 * Logs the message as ERROR and returns the number of bytes written. 260 * 261 * @deprecated This method is an overload for safety; please use {@link #e(Throwable, 262 * String)} or {@link #e(Throwable, String, Object...)} instead. 263 */ 264 @Deprecated e(String msg, Throwable tr)265 public int e(String msg, Throwable tr) { 266 @SuppressWarnings("FormatStringAnnotation") 267 int result = e(tr, msg); 268 return result; 269 } 270 271 @Override toString()272 public String toString() { 273 return "Logger[tag=" + mTag + "]"; 274 } 275 format(String format, Object... args)276 private static String format(String format, Object... args) { 277 return String.format(Locale.US, format, args); 278 } 279 } 280 } 281