1 /*
2  * Copyright (C) 2016 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.dialer.common;
18 
19 import android.support.annotation.NonNull;
20 import android.support.annotation.Nullable;
21 import android.telephony.PhoneNumberUtils;
22 import android.text.TextUtils;
23 
24 /** Provides logging functions. */
25 public class LogUtil {
26 
27   public static final String TAG = "Dialer";
28   private static final String SEPARATOR = " - ";
29 
LogUtil()30   private LogUtil() {}
31 
32   /**
33    * Log at a verbose level. Verbose logs should generally be filtered out, but may be useful when
34    * additional information is needed (e.g. to see how a particular flow evolved). These logs will
35    * not generally be available on production builds.
36    *
37    * @param tag An identifier to allow searching for related logs. Generally of the form
38    *     'Class.method'.
39    * @param msg The message you would like logged, possibly with format arguments.
40    * @param args Optional arguments to be used in the formatted string.
41    * @see {@link String#format(String, Object...)}
42    * @see {@link android.util.Log#v(String, String)}
43    */
v(@onNull String tag, @Nullable String msg, @Nullable Object... args)44   public static void v(@NonNull String tag, @Nullable String msg, @Nullable Object... args) {
45     println(android.util.Log.VERBOSE, TAG, tag, msg, args);
46   }
47 
48   /**
49    * Log at a debug level. Debug logs should provide known-useful information to aid in
50    * troubleshooting or evaluating flow. These logs will not generally be available on production
51    * builds.
52    *
53    * @param tag An identifier to allow searching for related logs. Generally of the form
54    *     'Class.method'
55    * @param msg The message you would like logged, possibly with format arguments
56    * @param args Optional arguments to be used in the formatted string
57    * @see {@link String#format(String, Object...)}
58    * @see {@link android.util.Log#d(String, String)}
59    */
d(@onNull String tag, @Nullable String msg, @Nullable Object... args)60   public static void d(@NonNull String tag, @Nullable String msg, @Nullable Object... args) {
61     println(android.util.Log.DEBUG, TAG, tag, msg, args);
62   }
63 
64   /**
65    * Log at an info level. Info logs provide information that would be useful to have on production
66    * builds for troubleshooting.
67    *
68    * @param tag An identifier to allow searching for related logs. Generally of the form
69    *     'Class.method'.
70    * @param msg The message you would like logged, possibly with format arguments.
71    * @param args Optional arguments to be used in the formatted string.
72    * @see {@link String#format(String, Object...)}
73    * @see {@link android.util.Log#i(String, String)}
74    */
i(@onNull String tag, @Nullable String msg, @Nullable Object... args)75   public static void i(@NonNull String tag, @Nullable String msg, @Nullable Object... args) {
76     println(android.util.Log.INFO, TAG, tag, msg, args);
77   }
78 
79   /**
80    * Log entry into a method at the info level.
81    *
82    * @param tag An identifier to allow searching for related logs. Generally of the form
83    *     'Class.method'.
84    */
enterBlock(String tag)85   public static void enterBlock(String tag) {
86     println(android.util.Log.INFO, TAG, tag, "enter");
87   }
88 
89   /**
90    * Log at a warn level. Warn logs indicate a possible error (e.g. a default switch branch was hit,
91    * or a null object was expected to be non-null), but recovery is possible. This may be used when
92    * it is not guaranteed that an indeterminate or bad state was entered, just that something may
93    * have gone wrong.
94    *
95    * @param tag An identifier to allow searching for related logs. Generally of the form
96    *     'Class.method'.
97    * @param msg The message you would like logged, possibly with format arguments.
98    * @param args Optional arguments to be used in the formatted string.
99    * @see {@link String#format(String, Object...)}
100    * @see {@link android.util.Log#w(String, String)}
101    */
w(@onNull String tag, @Nullable String msg, @Nullable Object... args)102   public static void w(@NonNull String tag, @Nullable String msg, @Nullable Object... args) {
103     println(android.util.Log.WARN, TAG, tag, msg, args);
104   }
105 
106   /**
107    * Log at an error level. Error logs are used when it is known that an error occurred and is
108    * possibly fatal. This is used to log information that will be useful for troubleshooting a crash
109    * or other severe condition (e.g. error codes, state values, etc.).
110    *
111    * @param tag An identifier to allow searching for related logs. Generally of the form
112    *     'Class.method'.
113    * @param msg The message you would like logged, possibly with format arguments.
114    * @param args Optional arguments to be used in the formatted string.
115    * @see {@link String#format(String, Object...)}
116    * @see {@link android.util.Log#e(String, String)}
117    */
e(@onNull String tag, @Nullable String msg, @Nullable Object... args)118   public static void e(@NonNull String tag, @Nullable String msg, @Nullable Object... args) {
119     println(android.util.Log.ERROR, TAG, tag, msg, args);
120   }
121 
122   /**
123    * Log an exception at an error level. Error logs are used when it is known that an error occurred
124    * and is possibly fatal. This is used to log information that will be useful for troubleshooting
125    * a crash or other severe condition (e.g. error codes, state values, etc.).
126    *
127    * @param tag An identifier to allow searching for related logs. Generally of the form
128    *     'Class.method'.
129    * @param msg The message you would like logged.
130    * @param throwable The exception to log.
131    * @see {@link String#format(String, Object...)}
132    * @see {@link android.util.Log#e(String, String)}
133    */
e(@onNull String tag, @Nullable String msg, @NonNull Throwable throwable)134   public static void e(@NonNull String tag, @Nullable String msg, @NonNull Throwable throwable) {
135     if (!TextUtils.isEmpty(msg)) {
136       println(
137           android.util.Log.ERROR,
138           TAG,
139           tag,
140           msg + "\n" + android.util.Log.getStackTraceString(throwable));
141     }
142   }
143 
144   /**
145    * Used for log statements where we don't want to log various strings (e.g., usernames) with
146    * default logging to avoid leaking PII in logcat.
147    *
148    * @return text as is if {@value #TAG}'s log level is set to DEBUG or VERBOSE or on non-release
149    *     builds; returns a redacted version otherwise.
150    */
sanitizePii(@ullable Object object)151   public static String sanitizePii(@Nullable Object object) {
152     if (object == null) {
153       return "null";
154     }
155     if (isDebugEnabled()) {
156       return object.toString();
157     }
158     return "Redacted-" + object.toString().length() + "-chars";
159   }
160 
161   /** Anonymizes char to prevent logging personally identifiable information. */
sanitizeDialPadChar(char ch)162   public static char sanitizeDialPadChar(char ch) {
163     if (isDebugEnabled()) {
164       return ch;
165     }
166     if (is12Key(ch)) {
167       return '*';
168     }
169     return ch;
170   }
171 
172   /** Anonymizes the phone number to prevent logging personally identifiable information. */
sanitizePhoneNumber(@ullable String phoneNumber)173   public static String sanitizePhoneNumber(@Nullable String phoneNumber) {
174     if (isDebugEnabled()) {
175       return phoneNumber;
176     }
177     if (phoneNumber == null) {
178       return null;
179     }
180     StringBuilder stringBuilder = new StringBuilder(phoneNumber.length());
181     for (char c : phoneNumber.toCharArray()) {
182       stringBuilder.append(sanitizeDialPadChar(c));
183     }
184     return stringBuilder.toString();
185   }
186 
isVerboseEnabled()187   public static boolean isVerboseEnabled() {
188     return android.util.Log.isLoggable(TAG, android.util.Log.VERBOSE);
189   }
190 
isDebugEnabled()191   public static boolean isDebugEnabled() {
192     return android.util.Log.isLoggable(TAG, android.util.Log.DEBUG);
193   }
194 
is12Key(char ch)195   private static boolean is12Key(char ch) {
196     return PhoneNumberUtils.is12Key(ch);
197   }
198 
println( int level, @NonNull String tag, @NonNull String localTag, @Nullable String msg, @Nullable Object... args)199   private static void println(
200       int level,
201       @NonNull String tag,
202       @NonNull String localTag,
203       @Nullable String msg,
204       @Nullable Object... args) {
205     // Formatted message is computed lazily if required.
206     String formattedMsg;
207     // Either null is passed as a single argument or more than one argument is passed.
208     boolean hasArgs = args == null || args.length > 0;
209     if ((level >= android.util.Log.INFO) || android.util.Log.isLoggable(tag, level)) {
210       formattedMsg = localTag;
211       if (!TextUtils.isEmpty(msg)) {
212         formattedMsg += SEPARATOR + (hasArgs ? String.format(msg, args) : msg);
213       }
214       android.util.Log.println(level, tag, formattedMsg);
215     }
216   }
217 }
218