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