• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2022 Google LLC
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 package com.google.android.libraries.mobiledatadownload.internal.logging;
17 
18 import android.annotation.SuppressLint;
19 import android.util.Log;
20 import com.google.errorprone.annotations.FormatMethod;
21 import com.google.errorprone.annotations.FormatString;
22 import java.util.Locale;
23 import java.util.Random;
24 import javax.annotation.Nullable;
25 
26 /** Utility class for logging with the "MDD" tag. */
27 public class LogUtil {
28   public static final String TAG = "MDD";
29 
30   private static final Random random = new Random();
31 
getLogPriority()32   public static int getLogPriority() {
33     int level = Log.ASSERT;
34     while (level > Log.VERBOSE) {
35       if (!Log.isLoggable(TAG, level - 1)) {
36         break;
37       }
38       level--;
39     }
40     return level;
41   }
42 
v(String msg)43   public static int v(String msg) {
44     if (Log.isLoggable(TAG, Log.VERBOSE)) {
45       return Log.v(TAG, msg);
46     }
47     return 0;
48   }
49 
50   @FormatMethod
v(@ormatString String format, Object obj0)51   public static int v(@FormatString String format, Object obj0) {
52     if (Log.isLoggable(TAG, Log.VERBOSE)) {
53       String msg = format(format, obj0);
54       return Log.v(TAG, msg);
55     }
56     return 0;
57   }
58 
59   @FormatMethod
v(@ormatString String format, Object obj0, Object obj1)60   public static int v(@FormatString String format, Object obj0, Object obj1) {
61     if (Log.isLoggable(TAG, Log.VERBOSE)) {
62       String msg = format(format, obj0, obj1);
63       return Log.v(TAG, msg);
64     }
65     return 0;
66   }
67 
68   @FormatMethod
v(@ormatString String format, Object... params)69   public static int v(@FormatString String format, Object... params) {
70     if (Log.isLoggable(TAG, Log.VERBOSE)) {
71       String msg = format(format, params);
72       return Log.v(TAG, msg);
73     }
74     return 0;
75   }
76 
d(String msg)77   public static int d(String msg) {
78     if (Log.isLoggable(TAG, Log.DEBUG)) {
79       return Log.d(TAG, msg);
80     }
81     return 0;
82   }
83 
84   @FormatMethod
d(@ormatString String format, Object obj0)85   public static int d(@FormatString String format, Object obj0) {
86     if (Log.isLoggable(TAG, Log.DEBUG)) {
87       String msg = format(format, obj0);
88       return Log.d(TAG, msg);
89     }
90     return 0;
91   }
92 
93   @FormatMethod
d(@ormatString String format, Object obj0, Object obj1)94   public static int d(@FormatString String format, Object obj0, Object obj1) {
95     if (Log.isLoggable(TAG, Log.DEBUG)) {
96       String msg = format(format, obj0, obj1);
97       return Log.d(TAG, msg);
98     }
99     return 0;
100   }
101 
102   @FormatMethod
d(@ormatString String format, Object... params)103   public static int d(@FormatString String format, Object... params) {
104     if (Log.isLoggable(TAG, Log.DEBUG)) {
105       String msg = format(format, params);
106       return Log.d(TAG, msg);
107     }
108     return 0;
109   }
110 
111   @FormatMethod
d(@ullable Throwable tr, @FormatString String format, Object... params)112   public static int d(@Nullable Throwable tr, @FormatString String format, Object... params) {
113     if (Log.isLoggable(TAG, Log.DEBUG)) {
114       String msg = format(format, params);
115       return Log.d(TAG, msg, tr);
116     }
117     return 0;
118   }
119 
i(String msg)120   public static int i(String msg) {
121     if (Log.isLoggable(TAG, Log.INFO)) {
122       return Log.i(TAG, msg);
123     }
124     return 0;
125   }
126 
127   @FormatMethod
i(@ormatString String format, Object obj0)128   public static int i(@FormatString String format, Object obj0) {
129     if (Log.isLoggable(TAG, Log.INFO)) {
130       String msg = format(format, obj0);
131       return Log.i(TAG, msg);
132     }
133     return 0;
134   }
135 
136   @FormatMethod
i(@ormatString String format, Object obj0, Object obj1)137   public static int i(@FormatString String format, Object obj0, Object obj1) {
138     if (Log.isLoggable(TAG, Log.INFO)) {
139       String msg = format(format, obj0, obj1);
140       return Log.i(TAG, msg);
141     }
142     return 0;
143   }
144 
145   @FormatMethod
i(@ormatString String format, Object... params)146   public static int i(@FormatString String format, Object... params) {
147     if (Log.isLoggable(TAG, Log.INFO)) {
148       String msg = format(format, params);
149       return Log.i(TAG, msg);
150     }
151     return 0;
152   }
153 
e(String msg)154   public static int e(String msg) {
155     if (Log.isLoggable(TAG, Log.ERROR)) {
156       return Log.e(TAG, msg);
157     }
158     return 0;
159   }
160 
161   @FormatMethod
e(@ormatString String format, Object obj0)162   public static int e(@FormatString String format, Object obj0) {
163     if (Log.isLoggable(TAG, Log.ERROR)) {
164       String msg = format(format, obj0);
165       return Log.e(TAG, msg);
166     }
167     return 0;
168   }
169 
170   @FormatMethod
e(@ormatString String format, Object obj0, Object obj1)171   public static int e(@FormatString String format, Object obj0, Object obj1) {
172     if (Log.isLoggable(TAG, Log.ERROR)) {
173       String msg = format(format, obj0, obj1);
174       return Log.e(TAG, msg);
175     }
176     return 0;
177   }
178 
179   @FormatMethod
e(@ormatString String format, Object... params)180   public static int e(@FormatString String format, Object... params) {
181     if (Log.isLoggable(TAG, Log.ERROR)) {
182       String msg = format(format, params);
183       return Log.e(TAG, msg);
184     }
185     return 0;
186   }
187 
188   @SuppressLint("LogTagMismatch")
e(@ullable Throwable tr, String msg)189   public static int e(@Nullable Throwable tr, String msg) {
190     if (Log.isLoggable(TAG, Log.ERROR)) {
191       if (Log.isLoggable(TAG, Log.DEBUG)) {
192         return Log.e(TAG, msg, tr);
193       } else {
194         // If not DEBUG level, only print the throwable type and message.
195         msg = msg + ": " + tr;
196         return Log.e(TAG, msg);
197       }
198     }
199     return 0;
200   }
201 
202   @FormatMethod
e(@ullable Throwable tr, @FormatString String format, Object... params)203   public static int e(@Nullable Throwable tr, @FormatString String format, Object... params) {
204     return Log.isLoggable(TAG, Log.ERROR) ? e(tr, format(format, params)) : 0;
205   }
206 
w(String msg)207   public static int w(String msg) {
208     if (Log.isLoggable(TAG, Log.WARN)) {
209       return Log.w(TAG, msg);
210     }
211     return 0;
212   }
213 
214   @FormatMethod
w(@ormatString String format, Object obj0)215   public static int w(@FormatString String format, Object obj0) {
216     if (Log.isLoggable(TAG, Log.WARN)) {
217       String msg = format(format, obj0);
218       return Log.w(TAG, msg);
219     }
220     return 0;
221   }
222 
223   @FormatMethod
w(@ormatString String format, Object obj0, Object obj1)224   public static int w(@FormatString String format, Object obj0, Object obj1) {
225     if (Log.isLoggable(TAG, Log.WARN)) {
226       String msg = format(format, obj0, obj1);
227       return Log.w(TAG, msg);
228     }
229     return 0;
230   }
231 
232   @FormatMethod
w(@ormatString String format, Object... params)233   public static int w(@FormatString String format, Object... params) {
234     if (Log.isLoggable(TAG, Log.WARN)) {
235       String msg = format(format, params);
236       return Log.w(TAG, msg);
237     }
238     return 0;
239   }
240 
241   @SuppressLint("LogTagMismatch")
242   @FormatMethod
w(@ullable Throwable tr, @FormatString String format, Object... params)243   public static int w(@Nullable Throwable tr, @FormatString String format, Object... params) {
244     if (Log.isLoggable(TAG, Log.WARN)) {
245       if (Log.isLoggable(TAG, Log.DEBUG)) {
246         String msg = format(format, params);
247         return Log.w(TAG, msg, tr);
248       } else {
249         // If not DEBUG level, only print the throwable type and message.
250         String msg = format(format, params) + ": " + tr;
251         return Log.w(TAG, msg);
252       }
253     }
254     return 0;
255   }
256 
257   @FormatMethod
format(@ormatString String format, Object... args)258   private static String format(@FormatString String format, Object... args) {
259     return String.format(Locale.US, format, args);
260   }
261 
shouldSampleInterval(long sampleInterval)262   public static boolean shouldSampleInterval(long sampleInterval) {
263     if (sampleInterval <= 0L) {
264       if (sampleInterval < 0L) {
265         LogUtil.e("Bad sample interval: %d", sampleInterval);
266       }
267       return false;
268     } else {
269       return (random.nextLong() % sampleInterval) == 0;
270     }
271   }
272 
LogUtil()273   private LogUtil() {}
274 }
275