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