blob: 0eb991777d745ace588ee02572dee222180859ab [file] [log] [blame]
Ihab Awad60ac30b2014-05-20 22:32:12 -07001/*
2 * Copyright 2014, 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
Tyler Gunnef9f6f92014-09-12 22:16:17 -070017package android.telecom;
Ihab Awad60ac30b2014-05-20 22:32:12 -070018
Brad Ebinger4fb372f2016-10-05 15:47:28 -070019import android.content.Context;
Santos Cordon3c20d632016-02-25 16:12:35 -080020import android.net.Uri;
Hall Liue362e502016-01-07 17:35:54 -080021import android.os.AsyncTask;
youhei.x.miyoshib3cd7b52016-12-12 21:10:54 +090022import android.os.Build;
Brad Ebinger51b98342016-09-22 16:30:46 -070023import android.telecom.Logging.EventManager;
24import android.telecom.Logging.Session;
25import android.telecom.Logging.SessionManager;
Santos Cordon3c20d632016-02-25 16:12:35 -080026import android.telephony.PhoneNumberUtils;
27import android.text.TextUtils;
Hall Liue362e502016-01-07 17:35:54 -080028
Brad Ebinger51b98342016-09-22 16:30:46 -070029import com.android.internal.annotations.VisibleForTesting;
30import com.android.internal.util.IndentingPrintWriter;
31
Ihab Awad60ac30b2014-05-20 22:32:12 -070032import java.security.MessageDigest;
33import java.security.NoSuchAlgorithmException;
34import java.util.IllegalFormatException;
35import java.util.Locale;
36
37/**
38 * Manages logging for the entire module.
39 *
40 * @hide
41 */
Brad Ebinger51b98342016-09-22 16:30:46 -070042public class Log {
Ihab Awad60ac30b2014-05-20 22:32:12 -070043
Brad Ebinger51b98342016-09-22 16:30:46 -070044 private static final long EXTENDED_LOGGING_DURATION_MILLIS = 60000 * 30; // 30 minutes
Ihab Awad60ac30b2014-05-20 22:32:12 -070045
Brad Ebinger51b98342016-09-22 16:30:46 -070046 private static final int EVENTS_TO_CACHE = 10;
47 private static final int EVENTS_TO_CACHE_DEBUG = 20;
48
Tyler Gunn9bc35112018-04-23 09:52:25 -070049 /**
50 * When generating a bug report, include the last X dialable digits when logging phone numbers.
51 */
52 private static final int NUM_DIALABLE_DIGITS_TO_LOG = Build.IS_USER ? 0 : 2;
53
Brad Ebinger51b98342016-09-22 16:30:46 -070054 // Generic tag for all Telecom logging
55 @VisibleForTesting
56 public static String TAG = "TelecomFramework";
Brad Ebinger0c3541b2016-11-01 14:11:38 -070057 public static boolean DEBUG = isLoggable(android.util.Log.DEBUG);
58 public static boolean INFO = isLoggable(android.util.Log.INFO);
59 public static boolean VERBOSE = isLoggable(android.util.Log.VERBOSE);
60 public static boolean WARN = isLoggable(android.util.Log.WARN);
61 public static boolean ERROR = isLoggable(android.util.Log.ERROR);
Brad Ebinger51b98342016-09-22 16:30:46 -070062
63 private static final boolean FORCE_LOGGING = false; /* STOP SHIP if true */
Jeff Sharkey5ab02432017-06-27 11:01:36 -060064 private static final boolean USER_BUILD = Build.IS_USER;
Ihab Awad60ac30b2014-05-20 22:32:12 -070065
Brad Ebinger51b98342016-09-22 16:30:46 -070066 // Used to synchronize singleton logging lazy initialization
67 private static final Object sSingletonSync = new Object();
68 private static EventManager sEventManager;
69 private static SessionManager sSessionManager;
70
71 /**
72 * Tracks whether user-activated extended logging is enabled.
73 */
74 private static boolean sIsUserExtendedLoggingEnabled = false;
75
76 /**
77 * The time when user-activated extended logging should be ended. Used to determine when
78 * extended logging should automatically be disabled.
79 */
80 private static long sUserExtendedLoggingStopTime = 0;
81
82 private Log() {
83 }
84
85 public static void d(String prefix, String format, Object... args) {
86 if (sIsUserExtendedLoggingEnabled) {
87 maybeDisableLogging();
88 android.util.Slog.i(TAG, buildMessage(prefix, format, args));
89 } else if (DEBUG) {
90 android.util.Slog.d(TAG, buildMessage(prefix, format, args));
91 }
92 }
93
94 public static void d(Object objectPrefix, String format, Object... args) {
95 if (sIsUserExtendedLoggingEnabled) {
96 maybeDisableLogging();
97 android.util.Slog.i(TAG, buildMessage(getPrefixFromObject(objectPrefix), format, args));
98 } else if (DEBUG) {
99 android.util.Slog.d(TAG, buildMessage(getPrefixFromObject(objectPrefix), format, args));
100 }
101 }
102
103 public static void i(String prefix, String format, Object... args) {
104 if (INFO) {
105 android.util.Slog.i(TAG, buildMessage(prefix, format, args));
106 }
107 }
108
109 public static void i(Object objectPrefix, String format, Object... args) {
110 if (INFO) {
111 android.util.Slog.i(TAG, buildMessage(getPrefixFromObject(objectPrefix), format, args));
112 }
113 }
114
115 public static void v(String prefix, String format, Object... args) {
116 if (sIsUserExtendedLoggingEnabled) {
117 maybeDisableLogging();
118 android.util.Slog.i(TAG, buildMessage(prefix, format, args));
119 } else if (VERBOSE) {
120 android.util.Slog.v(TAG, buildMessage(prefix, format, args));
121 }
122 }
123
124 public static void v(Object objectPrefix, String format, Object... args) {
125 if (sIsUserExtendedLoggingEnabled) {
126 maybeDisableLogging();
127 android.util.Slog.i(TAG, buildMessage(getPrefixFromObject(objectPrefix), format, args));
128 } else if (VERBOSE) {
129 android.util.Slog.v(TAG, buildMessage(getPrefixFromObject(objectPrefix), format, args));
130 }
131 }
132
133 public static void w(String prefix, String format, Object... args) {
134 if (WARN) {
135 android.util.Slog.w(TAG, buildMessage(prefix, format, args));
136 }
137 }
138
139 public static void w(Object objectPrefix, String format, Object... args) {
140 if (WARN) {
141 android.util.Slog.w(TAG, buildMessage(getPrefixFromObject(objectPrefix), format, args));
142 }
143 }
144
145 public static void e(String prefix, Throwable tr, String format, Object... args) {
146 if (ERROR) {
147 android.util.Slog.e(TAG, buildMessage(prefix, format, args), tr);
148 }
149 }
150
151 public static void e(Object objectPrefix, Throwable tr, String format, Object... args) {
152 if (ERROR) {
153 android.util.Slog.e(TAG, buildMessage(getPrefixFromObject(objectPrefix), format, args),
154 tr);
155 }
156 }
157
158 public static void wtf(String prefix, Throwable tr, String format, Object... args) {
159 android.util.Slog.wtf(TAG, buildMessage(prefix, format, args), tr);
160 }
161
162 public static void wtf(Object objectPrefix, Throwable tr, String format, Object... args) {
163 android.util.Slog.wtf(TAG, buildMessage(getPrefixFromObject(objectPrefix), format, args),
164 tr);
165 }
166
167 public static void wtf(String prefix, String format, Object... args) {
168 String msg = buildMessage(prefix, format, args);
169 android.util.Slog.wtf(TAG, msg, new IllegalStateException(msg));
170 }
171
172 public static void wtf(Object objectPrefix, String format, Object... args) {
173 String msg = buildMessage(getPrefixFromObject(objectPrefix), format, args);
174 android.util.Slog.wtf(TAG, msg, new IllegalStateException(msg));
175 }
176
177 /**
178 * The ease of use methods below only act mostly as proxies to the Session and Event Loggers.
179 * They also control the lazy loaders of the singleton instances, which will never be loaded if
180 * the proxy methods aren't used.
181 *
182 * Please see each method's documentation inside of their respective implementations in the
183 * loggers.
184 */
185
Brad Ebinger4fb372f2016-10-05 15:47:28 -0700186 public static void setSessionContext(Context context) {
187 getSessionManager().setContext(context);
188 }
189
Brad Ebinger51b98342016-09-22 16:30:46 -0700190 public static void startSession(String shortMethodName) {
191 getSessionManager().startSession(shortMethodName, null);
192 }
193
Brad Ebinger3445f822016-10-24 16:40:49 -0700194 public static void startSession(Session.Info info, String shortMethodName) {
195 getSessionManager().startSession(info, shortMethodName, null);
196 }
197
Brad Ebinger51b98342016-09-22 16:30:46 -0700198 public static void startSession(String shortMethodName, String callerIdentification) {
199 getSessionManager().startSession(shortMethodName, callerIdentification);
200 }
201
Brad Ebingera0dc9762016-10-21 09:41:29 -0700202 public static void startSession(Session.Info info, String shortMethodName,
203 String callerIdentification) {
204 getSessionManager().startSession(info, shortMethodName, callerIdentification);
205 }
206
Brad Ebinger51b98342016-09-22 16:30:46 -0700207 public static Session createSubsession() {
208 return getSessionManager().createSubsession();
209 }
210
Brad Ebinger3445f822016-10-24 16:40:49 -0700211 public static Session.Info getExternalSession() {
212 return getSessionManager().getExternalSession();
213 }
214
Brad Ebinger51b98342016-09-22 16:30:46 -0700215 public static void cancelSubsession(Session subsession) {
216 getSessionManager().cancelSubsession(subsession);
217 }
218
219 public static void continueSession(Session subsession, String shortMethodName) {
220 getSessionManager().continueSession(subsession, shortMethodName);
221 }
222
223 public static void endSession() {
224 getSessionManager().endSession();
225 }
226
Brad Ebinger836efad2016-10-18 13:48:17 -0700227 public static void registerSessionListener(SessionManager.ISessionListener l) {
228 getSessionManager().registerSessionListener(l);
229 }
230
Brad Ebinger51b98342016-09-22 16:30:46 -0700231 public static String getSessionId() {
232 // If the Session logger has not been initialized, then there have been no sessions logged.
233 // Don't load it now!
234 synchronized (sSingletonSync) {
235 if (sSessionManager != null) {
236 return getSessionManager().getSessionId();
237 } else {
238 return "";
239 }
240 }
241 }
242
243 public static void addEvent(EventManager.Loggable recordEntry, String event) {
244 getEventManager().event(recordEntry, event, null);
245 }
246
247 public static void addEvent(EventManager.Loggable recordEntry, String event, Object data) {
248 getEventManager().event(recordEntry, event, data);
249 }
250
251 public static void addEvent(EventManager.Loggable recordEntry, String event, String format,
252 Object... args) {
253 getEventManager().event(recordEntry, event, format, args);
254 }
255
256 public static void registerEventListener(EventManager.EventListener e) {
257 getEventManager().registerEventListener(e);
258 }
259
260 public static void addRequestResponsePair(EventManager.TimedEventPair p) {
261 getEventManager().addRequestResponsePair(p);
262 }
263
264 public static void dumpEvents(IndentingPrintWriter pw) {
265 // If the Events logger has not been initialized, then there have been no events logged.
266 // Don't load it now!
267 synchronized (sSingletonSync) {
268 if (sEventManager != null) {
269 getEventManager().dumpEvents(pw);
270 } else {
271 pw.println("No Historical Events Logged.");
272 }
273 }
274 }
275
276 /**
Tyler Gunn2db81b52017-05-19 10:10:23 -0700277 * Dumps the events in a timeline format.
278 * @param pw The {@link IndentingPrintWriter} to write to.
279 * @hide
280 */
281 public static void dumpEventsTimeline(IndentingPrintWriter pw) {
282 // If the Events logger has not been initialized, then there have been no events logged.
283 // Don't load it now!
284 synchronized (sSingletonSync) {
285 if (sEventManager != null) {
286 getEventManager().dumpEventsTimeline(pw);
287 } else {
288 pw.println("No Historical Events Logged.");
289 }
290 }
291 }
292
293 /**
Brad Ebinger51b98342016-09-22 16:30:46 -0700294 * Enable or disable extended telecom logging.
295 *
296 * @param isExtendedLoggingEnabled {@code true} if extended logging should be enabled,
297 * {@code false} if it should be disabled.
298 */
299 public static void setIsExtendedLoggingEnabled(boolean isExtendedLoggingEnabled) {
300 // If the state hasn't changed, bail early.
301 if (sIsUserExtendedLoggingEnabled == isExtendedLoggingEnabled) {
302 return;
303 }
304
305 if (sEventManager != null) {
306 sEventManager.changeEventCacheSize(isExtendedLoggingEnabled ?
307 EVENTS_TO_CACHE_DEBUG : EVENTS_TO_CACHE);
308 }
309
310 sIsUserExtendedLoggingEnabled = isExtendedLoggingEnabled;
311 if (sIsUserExtendedLoggingEnabled) {
312 sUserExtendedLoggingStopTime = System.currentTimeMillis()
313 + EXTENDED_LOGGING_DURATION_MILLIS;
314 } else {
315 sUserExtendedLoggingStopTime = 0;
316 }
317 }
318
319 private static EventManager getEventManager() {
320 // Checking for null again outside of synchronization because we only need to synchronize
321 // during the lazy loading of the events logger. We don't need to synchronize elsewhere.
322 if (sEventManager == null) {
323 synchronized (sSingletonSync) {
324 if (sEventManager == null) {
325 sEventManager = new EventManager(Log::getSessionId);
326 return sEventManager;
327 }
328 }
329 }
330 return sEventManager;
331 }
332
Brad Ebinger8adafe72017-06-08 15:44:40 -0700333 @VisibleForTesting
334 public static SessionManager getSessionManager() {
Brad Ebinger51b98342016-09-22 16:30:46 -0700335 // Checking for null again outside of synchronization because we only need to synchronize
336 // during the lazy loading of the session logger. We don't need to synchronize elsewhere.
337 if (sSessionManager == null) {
338 synchronized (sSingletonSync) {
339 if (sSessionManager == null) {
340 sSessionManager = new SessionManager();
341 return sSessionManager;
342 }
343 }
344 }
345 return sSessionManager;
346 }
347
Brad Ebinger51b98342016-09-22 16:30:46 -0700348 public static void setTag(String tag) {
349 TAG = tag;
Brad Ebinger0c3541b2016-11-01 14:11:38 -0700350 DEBUG = isLoggable(android.util.Log.DEBUG);
351 INFO = isLoggable(android.util.Log.INFO);
352 VERBOSE = isLoggable(android.util.Log.VERBOSE);
353 WARN = isLoggable(android.util.Log.WARN);
354 ERROR = isLoggable(android.util.Log.ERROR);
Brad Ebinger51b98342016-09-22 16:30:46 -0700355 }
356
357 /**
358 * If user enabled extended logging is enabled and the time limit has passed, disables the
359 * extended logging.
360 */
361 private static void maybeDisableLogging() {
362 if (!sIsUserExtendedLoggingEnabled) {
363 return;
364 }
365
366 if (sUserExtendedLoggingStopTime < System.currentTimeMillis()) {
367 sUserExtendedLoggingStopTime = 0;
368 sIsUserExtendedLoggingEnabled = false;
369 }
370 }
371
Ihab Awad60ac30b2014-05-20 22:32:12 -0700372 public static boolean isLoggable(int level) {
373 return FORCE_LOGGING || android.util.Log.isLoggable(TAG, level);
374 }
375
Brad Ebinger51b98342016-09-22 16:30:46 -0700376 public static String piiHandle(Object pii) {
377 if (pii == null || VERBOSE) {
378 return String.valueOf(pii);
Ihab Awad60ac30b2014-05-20 22:32:12 -0700379 }
Ihab Awad60ac30b2014-05-20 22:32:12 -0700380
Brad Ebinger51b98342016-09-22 16:30:46 -0700381 StringBuilder sb = new StringBuilder();
382 if (pii instanceof Uri) {
383 Uri uri = (Uri) pii;
384 String scheme = uri.getScheme();
385
386 if (!TextUtils.isEmpty(scheme)) {
387 sb.append(scheme).append(":");
388 }
389
390 String textToObfuscate = uri.getSchemeSpecificPart();
391 if (PhoneAccount.SCHEME_TEL.equals(scheme)) {
Tyler Gunn9bc35112018-04-23 09:52:25 -0700392 int numDigitsToObfuscate = getDialableCount(textToObfuscate)
393 - NUM_DIALABLE_DIGITS_TO_LOG;
Brad Ebinger51b98342016-09-22 16:30:46 -0700394 for (int i = 0; i < textToObfuscate.length(); i++) {
395 char c = textToObfuscate.charAt(i);
Tyler Gunn9bc35112018-04-23 09:52:25 -0700396 boolean isDialable = PhoneNumberUtils.isDialable(c);
397 if (isDialable) {
398 numDigitsToObfuscate--;
399 }
400 sb.append(isDialable && numDigitsToObfuscate >= 0 ? "*" : c);
Brad Ebinger51b98342016-09-22 16:30:46 -0700401 }
402 } else if (PhoneAccount.SCHEME_SIP.equals(scheme)) {
403 for (int i = 0; i < textToObfuscate.length(); i++) {
404 char c = textToObfuscate.charAt(i);
405 if (c != '@' && c != '.') {
406 c = '*';
407 }
408 sb.append(c);
409 }
410 } else {
411 sb.append(pii(pii));
412 }
Ihab Awad60ac30b2014-05-20 22:32:12 -0700413 }
Ihab Awad60ac30b2014-05-20 22:32:12 -0700414
Brad Ebinger51b98342016-09-22 16:30:46 -0700415 return sb.toString();
Ihab Awad60ac30b2014-05-20 22:32:12 -0700416 }
417
418 /**
Tyler Gunn9bc35112018-04-23 09:52:25 -0700419 * Determines the number of dialable characters in a string.
420 * @param toCount The string to count dialable characters in.
421 * @return The count of dialable characters.
422 */
423 private static int getDialableCount(String toCount) {
424 int numDialable = 0;
425 for (char c : toCount.toCharArray()) {
426 if (PhoneNumberUtils.isDialable(c)) {
427 numDialable++;
428 }
429 }
430 return numDialable;
431 }
432
433 /**
Ihab Awad60ac30b2014-05-20 22:32:12 -0700434 * Redact personally identifiable information for production users.
youhei.x.miyoshib3cd7b52016-12-12 21:10:54 +0900435 * If we are running in verbose mode, return the original string,
Brad Ebingerf784b292017-12-22 13:45:27 -0800436 * and return "***" otherwise.
Ihab Awad60ac30b2014-05-20 22:32:12 -0700437 */
438 public static String pii(Object pii) {
439 if (pii == null || VERBOSE) {
440 return String.valueOf(pii);
441 }
Brad Ebingerf784b292017-12-22 13:45:27 -0800442 return "***";
Ihab Awad60ac30b2014-05-20 22:32:12 -0700443 }
444
445 private static String getPrefixFromObject(Object obj) {
446 return obj == null ? "<null>" : obj.getClass().getSimpleName();
447 }
448
449 private static String buildMessage(String prefix, String format, Object... args) {
Brad Ebinger51b98342016-09-22 16:30:46 -0700450 // Incorporate thread ID and calling method into prefix
451 String sessionName = getSessionId();
452 String sessionPostfix = TextUtils.isEmpty(sessionName) ? "" : ": " + sessionName;
453
Ihab Awad60ac30b2014-05-20 22:32:12 -0700454 String msg;
455 try {
456 msg = (args == null || args.length == 0) ? format
457 : String.format(Locale.US, format, args);
458 } catch (IllegalFormatException ife) {
Brad Ebinger4fb372f2016-10-05 15:47:28 -0700459 e(TAG, ife, "Log: IllegalFormatException: formatString='%s' numArgs=%d", format,
Ihab Awad60ac30b2014-05-20 22:32:12 -0700460 args.length);
461 msg = format + " (An error occurred while formatting the message.)";
462 }
Brad Ebinger51b98342016-09-22 16:30:46 -0700463 return String.format(Locale.US, "%s: %s%s", prefix, msg, sessionPostfix);
Ihab Awad60ac30b2014-05-20 22:32:12 -0700464 }
465}