Ihab Awad | 60ac30b | 2014-05-20 22:32:12 -0700 | [diff] [blame] | 1 | /* |
| 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 Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 17 | package android.telecom; |
Ihab Awad | 60ac30b | 2014-05-20 22:32:12 -0700 | [diff] [blame] | 18 | |
Tyler Gunn | ffbcd89 | 2020-05-04 15:01:59 -0700 | [diff] [blame] | 19 | import android.annotation.NonNull; |
Artur Satayev | 2ebb31c | 2020-01-08 12:24:36 +0000 | [diff] [blame] | 20 | import android.compat.annotation.UnsupportedAppUsage; |
Tyler Gunn | ffbcd89 | 2020-05-04 15:01:59 -0700 | [diff] [blame] | 21 | import android.content.ComponentName; |
Brad Ebinger | 4fb372f | 2016-10-05 15:47:28 -0700 | [diff] [blame] | 22 | import android.content.Context; |
Santos Cordon | 3c20d63 | 2016-02-25 16:12:35 -0800 | [diff] [blame] | 23 | import android.net.Uri; |
youhei.x.miyoshi | b3cd7b5 | 2016-12-12 21:10:54 +0900 | [diff] [blame] | 24 | import android.os.Build; |
Brad Ebinger | 51b9834 | 2016-09-22 16:30:46 -0700 | [diff] [blame] | 25 | import android.telecom.Logging.EventManager; |
| 26 | import android.telecom.Logging.Session; |
| 27 | import android.telecom.Logging.SessionManager; |
Santos Cordon | 3c20d63 | 2016-02-25 16:12:35 -0800 | [diff] [blame] | 28 | import android.telephony.PhoneNumberUtils; |
| 29 | import android.text.TextUtils; |
Hall Liu | e362e50 | 2016-01-07 17:35:54 -0800 | [diff] [blame] | 30 | |
Brad Ebinger | 51b9834 | 2016-09-22 16:30:46 -0700 | [diff] [blame] | 31 | import com.android.internal.annotations.VisibleForTesting; |
| 32 | import com.android.internal.util.IndentingPrintWriter; |
| 33 | |
Tyler Gunn | ffbcd89 | 2020-05-04 15:01:59 -0700 | [diff] [blame] | 34 | import java.util.Arrays; |
Ihab Awad | 60ac30b | 2014-05-20 22:32:12 -0700 | [diff] [blame] | 35 | import java.util.IllegalFormatException; |
| 36 | import java.util.Locale; |
Tyler Gunn | ffbcd89 | 2020-05-04 15:01:59 -0700 | [diff] [blame] | 37 | import java.util.stream.Collectors; |
Ihab Awad | 60ac30b | 2014-05-20 22:32:12 -0700 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * Manages logging for the entire module. |
| 41 | * |
| 42 | * @hide |
| 43 | */ |
Brad Ebinger | 51b9834 | 2016-09-22 16:30:46 -0700 | [diff] [blame] | 44 | public class Log { |
Ihab Awad | 60ac30b | 2014-05-20 22:32:12 -0700 | [diff] [blame] | 45 | |
Brad Ebinger | 51b9834 | 2016-09-22 16:30:46 -0700 | [diff] [blame] | 46 | private static final long EXTENDED_LOGGING_DURATION_MILLIS = 60000 * 30; // 30 minutes |
Ihab Awad | 60ac30b | 2014-05-20 22:32:12 -0700 | [diff] [blame] | 47 | |
Brad Ebinger | 51b9834 | 2016-09-22 16:30:46 -0700 | [diff] [blame] | 48 | private static final int EVENTS_TO_CACHE = 10; |
| 49 | private static final int EVENTS_TO_CACHE_DEBUG = 20; |
| 50 | |
Tyler Gunn | 9bc3511 | 2018-04-23 09:52:25 -0700 | [diff] [blame] | 51 | /** |
| 52 | * When generating a bug report, include the last X dialable digits when logging phone numbers. |
| 53 | */ |
| 54 | private static final int NUM_DIALABLE_DIGITS_TO_LOG = Build.IS_USER ? 0 : 2; |
| 55 | |
Brad Ebinger | 51b9834 | 2016-09-22 16:30:46 -0700 | [diff] [blame] | 56 | // Generic tag for all Telecom logging |
| 57 | @VisibleForTesting |
| 58 | public static String TAG = "TelecomFramework"; |
Brad Ebinger | 0c3541b | 2016-11-01 14:11:38 -0700 | [diff] [blame] | 59 | public static boolean DEBUG = isLoggable(android.util.Log.DEBUG); |
| 60 | public static boolean INFO = isLoggable(android.util.Log.INFO); |
| 61 | public static boolean VERBOSE = isLoggable(android.util.Log.VERBOSE); |
| 62 | public static boolean WARN = isLoggable(android.util.Log.WARN); |
| 63 | public static boolean ERROR = isLoggable(android.util.Log.ERROR); |
Brad Ebinger | 51b9834 | 2016-09-22 16:30:46 -0700 | [diff] [blame] | 64 | |
| 65 | private static final boolean FORCE_LOGGING = false; /* STOP SHIP if true */ |
Jeff Sharkey | 5ab0243 | 2017-06-27 11:01:36 -0600 | [diff] [blame] | 66 | private static final boolean USER_BUILD = Build.IS_USER; |
Ihab Awad | 60ac30b | 2014-05-20 22:32:12 -0700 | [diff] [blame] | 67 | |
Brad Ebinger | 51b9834 | 2016-09-22 16:30:46 -0700 | [diff] [blame] | 68 | // Used to synchronize singleton logging lazy initialization |
| 69 | private static final Object sSingletonSync = new Object(); |
| 70 | private static EventManager sEventManager; |
| 71 | private static SessionManager sSessionManager; |
Tyler Gunn | 4d54cca | 2023-03-16 23:00:04 +0000 | [diff] [blame] | 72 | private static Object sLock = null; |
Brad Ebinger | 51b9834 | 2016-09-22 16:30:46 -0700 | [diff] [blame] | 73 | |
| 74 | /** |
| 75 | * Tracks whether user-activated extended logging is enabled. |
| 76 | */ |
| 77 | private static boolean sIsUserExtendedLoggingEnabled = false; |
| 78 | |
| 79 | /** |
Thomas Stuart | a1ca66f | 2022-04-28 11:04:40 -0700 | [diff] [blame] | 80 | * Enabled in telecom testing to help gate log statements causing log spew. |
| 81 | */ |
| 82 | private static boolean sIsUnitTestingEnabled = false; |
| 83 | |
| 84 | /** |
Brad Ebinger | 51b9834 | 2016-09-22 16:30:46 -0700 | [diff] [blame] | 85 | * The time when user-activated extended logging should be ended. Used to determine when |
| 86 | * extended logging should automatically be disabled. |
| 87 | */ |
| 88 | private static long sUserExtendedLoggingStopTime = 0; |
| 89 | |
| 90 | private Log() { |
| 91 | } |
| 92 | |
| 93 | public static void d(String prefix, String format, Object... args) { |
| 94 | if (sIsUserExtendedLoggingEnabled) { |
| 95 | maybeDisableLogging(); |
| 96 | android.util.Slog.i(TAG, buildMessage(prefix, format, args)); |
| 97 | } else if (DEBUG) { |
| 98 | android.util.Slog.d(TAG, buildMessage(prefix, format, args)); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | public static void d(Object objectPrefix, String format, Object... args) { |
| 103 | if (sIsUserExtendedLoggingEnabled) { |
| 104 | maybeDisableLogging(); |
| 105 | android.util.Slog.i(TAG, buildMessage(getPrefixFromObject(objectPrefix), format, args)); |
| 106 | } else if (DEBUG) { |
| 107 | android.util.Slog.d(TAG, buildMessage(getPrefixFromObject(objectPrefix), format, args)); |
| 108 | } |
| 109 | } |
| 110 | |
Mathew Inwood | 5d123b6 | 2020-11-04 09:29:36 +0000 | [diff] [blame] | 111 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
Brad Ebinger | 51b9834 | 2016-09-22 16:30:46 -0700 | [diff] [blame] | 112 | public static void i(String prefix, String format, Object... args) { |
| 113 | if (INFO) { |
| 114 | android.util.Slog.i(TAG, buildMessage(prefix, format, args)); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | public static void i(Object objectPrefix, String format, Object... args) { |
| 119 | if (INFO) { |
| 120 | android.util.Slog.i(TAG, buildMessage(getPrefixFromObject(objectPrefix), format, args)); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | public static void v(String prefix, String format, Object... args) { |
| 125 | if (sIsUserExtendedLoggingEnabled) { |
| 126 | maybeDisableLogging(); |
| 127 | android.util.Slog.i(TAG, buildMessage(prefix, format, args)); |
| 128 | } else if (VERBOSE) { |
| 129 | android.util.Slog.v(TAG, buildMessage(prefix, format, args)); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | public static void v(Object objectPrefix, String format, Object... args) { |
| 134 | if (sIsUserExtendedLoggingEnabled) { |
| 135 | maybeDisableLogging(); |
| 136 | android.util.Slog.i(TAG, buildMessage(getPrefixFromObject(objectPrefix), format, args)); |
| 137 | } else if (VERBOSE) { |
| 138 | android.util.Slog.v(TAG, buildMessage(getPrefixFromObject(objectPrefix), format, args)); |
| 139 | } |
| 140 | } |
| 141 | |
Mathew Inwood | 5d123b6 | 2020-11-04 09:29:36 +0000 | [diff] [blame] | 142 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
Brad Ebinger | 51b9834 | 2016-09-22 16:30:46 -0700 | [diff] [blame] | 143 | public static void w(String prefix, String format, Object... args) { |
| 144 | if (WARN) { |
| 145 | android.util.Slog.w(TAG, buildMessage(prefix, format, args)); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | public static void w(Object objectPrefix, String format, Object... args) { |
| 150 | if (WARN) { |
| 151 | android.util.Slog.w(TAG, buildMessage(getPrefixFromObject(objectPrefix), format, args)); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | public static void e(String prefix, Throwable tr, String format, Object... args) { |
| 156 | if (ERROR) { |
| 157 | android.util.Slog.e(TAG, buildMessage(prefix, format, args), tr); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | public static void e(Object objectPrefix, Throwable tr, String format, Object... args) { |
| 162 | if (ERROR) { |
| 163 | android.util.Slog.e(TAG, buildMessage(getPrefixFromObject(objectPrefix), format, args), |
| 164 | tr); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | public static void wtf(String prefix, Throwable tr, String format, Object... args) { |
| 169 | android.util.Slog.wtf(TAG, buildMessage(prefix, format, args), tr); |
| 170 | } |
| 171 | |
| 172 | public static void wtf(Object objectPrefix, Throwable tr, String format, Object... args) { |
| 173 | android.util.Slog.wtf(TAG, buildMessage(getPrefixFromObject(objectPrefix), format, args), |
| 174 | tr); |
| 175 | } |
| 176 | |
| 177 | public static void wtf(String prefix, String format, Object... args) { |
| 178 | String msg = buildMessage(prefix, format, args); |
| 179 | android.util.Slog.wtf(TAG, msg, new IllegalStateException(msg)); |
| 180 | } |
| 181 | |
| 182 | public static void wtf(Object objectPrefix, String format, Object... args) { |
| 183 | String msg = buildMessage(getPrefixFromObject(objectPrefix), format, args); |
| 184 | android.util.Slog.wtf(TAG, msg, new IllegalStateException(msg)); |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * The ease of use methods below only act mostly as proxies to the Session and Event Loggers. |
| 189 | * They also control the lazy loaders of the singleton instances, which will never be loaded if |
| 190 | * the proxy methods aren't used. |
| 191 | * |
| 192 | * Please see each method's documentation inside of their respective implementations in the |
| 193 | * loggers. |
| 194 | */ |
| 195 | |
Brad Ebinger | 4fb372f | 2016-10-05 15:47:28 -0700 | [diff] [blame] | 196 | public static void setSessionContext(Context context) { |
| 197 | getSessionManager().setContext(context); |
| 198 | } |
| 199 | |
Brad Ebinger | 51b9834 | 2016-09-22 16:30:46 -0700 | [diff] [blame] | 200 | public static void startSession(String shortMethodName) { |
| 201 | getSessionManager().startSession(shortMethodName, null); |
| 202 | } |
| 203 | |
Brad Ebinger | 3445f82 | 2016-10-24 16:40:49 -0700 | [diff] [blame] | 204 | public static void startSession(Session.Info info, String shortMethodName) { |
| 205 | getSessionManager().startSession(info, shortMethodName, null); |
| 206 | } |
| 207 | |
Brad Ebinger | 51b9834 | 2016-09-22 16:30:46 -0700 | [diff] [blame] | 208 | public static void startSession(String shortMethodName, String callerIdentification) { |
| 209 | getSessionManager().startSession(shortMethodName, callerIdentification); |
| 210 | } |
| 211 | |
Brad Ebinger | a0dc976 | 2016-10-21 09:41:29 -0700 | [diff] [blame] | 212 | public static void startSession(Session.Info info, String shortMethodName, |
| 213 | String callerIdentification) { |
| 214 | getSessionManager().startSession(info, shortMethodName, callerIdentification); |
| 215 | } |
| 216 | |
Brad Ebinger | 51b9834 | 2016-09-22 16:30:46 -0700 | [diff] [blame] | 217 | public static Session createSubsession() { |
| 218 | return getSessionManager().createSubsession(); |
| 219 | } |
| 220 | |
Brad Ebinger | 3445f82 | 2016-10-24 16:40:49 -0700 | [diff] [blame] | 221 | public static Session.Info getExternalSession() { |
| 222 | return getSessionManager().getExternalSession(); |
| 223 | } |
| 224 | |
Tyler Gunn | ffbcd89 | 2020-05-04 15:01:59 -0700 | [diff] [blame] | 225 | /** |
| 226 | * Retrieves external session information, providing a context for the recipient of the session |
| 227 | * info where the external session came from. |
| 228 | * @param ownerInfo The external owner info. |
| 229 | * @return New {@link Session.Info} instance with owner info set. |
| 230 | */ |
| 231 | public static Session.Info getExternalSession(@NonNull String ownerInfo) { |
| 232 | return getSessionManager().getExternalSession(ownerInfo); |
| 233 | } |
| 234 | |
Brad Ebinger | 51b9834 | 2016-09-22 16:30:46 -0700 | [diff] [blame] | 235 | public static void cancelSubsession(Session subsession) { |
| 236 | getSessionManager().cancelSubsession(subsession); |
| 237 | } |
| 238 | |
| 239 | public static void continueSession(Session subsession, String shortMethodName) { |
| 240 | getSessionManager().continueSession(subsession, shortMethodName); |
| 241 | } |
| 242 | |
| 243 | public static void endSession() { |
| 244 | getSessionManager().endSession(); |
| 245 | } |
| 246 | |
Brad Ebinger | 836efad | 2016-10-18 13:48:17 -0700 | [diff] [blame] | 247 | public static void registerSessionListener(SessionManager.ISessionListener l) { |
| 248 | getSessionManager().registerSessionListener(l); |
| 249 | } |
| 250 | |
Brad Ebinger | 51b9834 | 2016-09-22 16:30:46 -0700 | [diff] [blame] | 251 | public static String getSessionId() { |
| 252 | // If the Session logger has not been initialized, then there have been no sessions logged. |
| 253 | // Don't load it now! |
| 254 | synchronized (sSingletonSync) { |
| 255 | if (sSessionManager != null) { |
| 256 | return getSessionManager().getSessionId(); |
| 257 | } else { |
| 258 | return ""; |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | public static void addEvent(EventManager.Loggable recordEntry, String event) { |
| 264 | getEventManager().event(recordEntry, event, null); |
| 265 | } |
| 266 | |
| 267 | public static void addEvent(EventManager.Loggable recordEntry, String event, Object data) { |
| 268 | getEventManager().event(recordEntry, event, data); |
| 269 | } |
| 270 | |
| 271 | public static void addEvent(EventManager.Loggable recordEntry, String event, String format, |
| 272 | Object... args) { |
| 273 | getEventManager().event(recordEntry, event, format, args); |
| 274 | } |
| 275 | |
| 276 | public static void registerEventListener(EventManager.EventListener e) { |
| 277 | getEventManager().registerEventListener(e); |
| 278 | } |
| 279 | |
| 280 | public static void addRequestResponsePair(EventManager.TimedEventPair p) { |
| 281 | getEventManager().addRequestResponsePair(p); |
| 282 | } |
| 283 | |
| 284 | public static void dumpEvents(IndentingPrintWriter pw) { |
| 285 | // If the Events logger has not been initialized, then there have been no events logged. |
| 286 | // Don't load it now! |
| 287 | synchronized (sSingletonSync) { |
| 288 | if (sEventManager != null) { |
| 289 | getEventManager().dumpEvents(pw); |
| 290 | } else { |
| 291 | pw.println("No Historical Events Logged."); |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | /** |
Tyler Gunn | 2db81b5 | 2017-05-19 10:10:23 -0700 | [diff] [blame] | 297 | * Dumps the events in a timeline format. |
| 298 | * @param pw The {@link IndentingPrintWriter} to write to. |
| 299 | * @hide |
| 300 | */ |
| 301 | public static void dumpEventsTimeline(IndentingPrintWriter pw) { |
| 302 | // If the Events logger has not been initialized, then there have been no events logged. |
| 303 | // Don't load it now! |
| 304 | synchronized (sSingletonSync) { |
| 305 | if (sEventManager != null) { |
| 306 | getEventManager().dumpEventsTimeline(pw); |
| 307 | } else { |
| 308 | pw.println("No Historical Events Logged."); |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | /** |
Brad Ebinger | 51b9834 | 2016-09-22 16:30:46 -0700 | [diff] [blame] | 314 | * Enable or disable extended telecom logging. |
| 315 | * |
| 316 | * @param isExtendedLoggingEnabled {@code true} if extended logging should be enabled, |
| 317 | * {@code false} if it should be disabled. |
| 318 | */ |
| 319 | public static void setIsExtendedLoggingEnabled(boolean isExtendedLoggingEnabled) { |
| 320 | // If the state hasn't changed, bail early. |
| 321 | if (sIsUserExtendedLoggingEnabled == isExtendedLoggingEnabled) { |
| 322 | return; |
| 323 | } |
| 324 | |
| 325 | if (sEventManager != null) { |
| 326 | sEventManager.changeEventCacheSize(isExtendedLoggingEnabled ? |
| 327 | EVENTS_TO_CACHE_DEBUG : EVENTS_TO_CACHE); |
| 328 | } |
| 329 | |
| 330 | sIsUserExtendedLoggingEnabled = isExtendedLoggingEnabled; |
| 331 | if (sIsUserExtendedLoggingEnabled) { |
| 332 | sUserExtendedLoggingStopTime = System.currentTimeMillis() |
| 333 | + EXTENDED_LOGGING_DURATION_MILLIS; |
| 334 | } else { |
| 335 | sUserExtendedLoggingStopTime = 0; |
| 336 | } |
| 337 | } |
| 338 | |
Thomas Stuart | a1ca66f | 2022-04-28 11:04:40 -0700 | [diff] [blame] | 339 | /** |
| 340 | * Enabled when tests are running to help gate log statements causing log spew. |
| 341 | * |
| 342 | * @param isEnabled {@code true} if running unit tests. false otherwise. |
| 343 | * |
| 344 | */ |
| 345 | public static void setUnitTestingEnabled(boolean isEnabled) { |
| 346 | sIsUnitTestingEnabled = isEnabled; |
| 347 | } |
| 348 | |
| 349 | public static boolean isUnitTestingEnabled() { |
| 350 | return sIsUnitTestingEnabled; |
| 351 | } |
| 352 | |
Brad Ebinger | 51b9834 | 2016-09-22 16:30:46 -0700 | [diff] [blame] | 353 | private static EventManager getEventManager() { |
| 354 | // Checking for null again outside of synchronization because we only need to synchronize |
| 355 | // during the lazy loading of the events logger. We don't need to synchronize elsewhere. |
| 356 | if (sEventManager == null) { |
| 357 | synchronized (sSingletonSync) { |
| 358 | if (sEventManager == null) { |
| 359 | sEventManager = new EventManager(Log::getSessionId); |
| 360 | return sEventManager; |
| 361 | } |
| 362 | } |
| 363 | } |
| 364 | return sEventManager; |
| 365 | } |
| 366 | |
Brad Ebinger | 8adafe7 | 2017-06-08 15:44:40 -0700 | [diff] [blame] | 367 | @VisibleForTesting |
| 368 | public static SessionManager getSessionManager() { |
Brad Ebinger | 51b9834 | 2016-09-22 16:30:46 -0700 | [diff] [blame] | 369 | // Checking for null again outside of synchronization because we only need to synchronize |
| 370 | // during the lazy loading of the session logger. We don't need to synchronize elsewhere. |
| 371 | if (sSessionManager == null) { |
| 372 | synchronized (sSingletonSync) { |
| 373 | if (sSessionManager == null) { |
| 374 | sSessionManager = new SessionManager(); |
| 375 | return sSessionManager; |
| 376 | } |
| 377 | } |
| 378 | } |
| 379 | return sSessionManager; |
| 380 | } |
| 381 | |
Brad Ebinger | 51b9834 | 2016-09-22 16:30:46 -0700 | [diff] [blame] | 382 | public static void setTag(String tag) { |
| 383 | TAG = tag; |
Brad Ebinger | 0c3541b | 2016-11-01 14:11:38 -0700 | [diff] [blame] | 384 | DEBUG = isLoggable(android.util.Log.DEBUG); |
| 385 | INFO = isLoggable(android.util.Log.INFO); |
| 386 | VERBOSE = isLoggable(android.util.Log.VERBOSE); |
| 387 | WARN = isLoggable(android.util.Log.WARN); |
| 388 | ERROR = isLoggable(android.util.Log.ERROR); |
Brad Ebinger | 51b9834 | 2016-09-22 16:30:46 -0700 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | /** |
Tyler Gunn | 4d54cca | 2023-03-16 23:00:04 +0000 | [diff] [blame] | 392 | * Sets the main telecom sync lock used within Telecom. This is used when building log messages |
| 393 | * so that we can identify places in the code where we are doing something outside of the |
| 394 | * Telecom lock. |
| 395 | * @param lock The lock. |
| 396 | */ |
| 397 | public static void setLock(Object lock) { |
| 398 | // Don't do lock monitoring on user builds. |
| 399 | if (!Build.IS_USER) { |
| 400 | sLock = lock; |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | /** |
Brad Ebinger | 51b9834 | 2016-09-22 16:30:46 -0700 | [diff] [blame] | 405 | * If user enabled extended logging is enabled and the time limit has passed, disables the |
| 406 | * extended logging. |
| 407 | */ |
| 408 | private static void maybeDisableLogging() { |
| 409 | if (!sIsUserExtendedLoggingEnabled) { |
| 410 | return; |
| 411 | } |
| 412 | |
| 413 | if (sUserExtendedLoggingStopTime < System.currentTimeMillis()) { |
| 414 | sUserExtendedLoggingStopTime = 0; |
| 415 | sIsUserExtendedLoggingEnabled = false; |
| 416 | } |
| 417 | } |
| 418 | |
Ihab Awad | 60ac30b | 2014-05-20 22:32:12 -0700 | [diff] [blame] | 419 | public static boolean isLoggable(int level) { |
| 420 | return FORCE_LOGGING || android.util.Log.isLoggable(TAG, level); |
| 421 | } |
| 422 | |
Tyler Gunn | fd74d53f | 2019-02-13 13:15:56 -0800 | [diff] [blame] | 423 | /** |
| 424 | * Generates an obfuscated string for a calling handle in {@link Uri} format, or a raw phone |
| 425 | * phone number in {@link String} format. |
| 426 | * @param pii The information to obfuscate. |
| 427 | * @return The obfuscated string. |
| 428 | */ |
Brad Ebinger | 51b9834 | 2016-09-22 16:30:46 -0700 | [diff] [blame] | 429 | public static String piiHandle(Object pii) { |
| 430 | if (pii == null || VERBOSE) { |
| 431 | return String.valueOf(pii); |
Ihab Awad | 60ac30b | 2014-05-20 22:32:12 -0700 | [diff] [blame] | 432 | } |
Ihab Awad | 60ac30b | 2014-05-20 22:32:12 -0700 | [diff] [blame] | 433 | |
Brad Ebinger | 51b9834 | 2016-09-22 16:30:46 -0700 | [diff] [blame] | 434 | StringBuilder sb = new StringBuilder(); |
| 435 | if (pii instanceof Uri) { |
| 436 | Uri uri = (Uri) pii; |
| 437 | String scheme = uri.getScheme(); |
| 438 | |
| 439 | if (!TextUtils.isEmpty(scheme)) { |
| 440 | sb.append(scheme).append(":"); |
| 441 | } |
| 442 | |
| 443 | String textToObfuscate = uri.getSchemeSpecificPart(); |
| 444 | if (PhoneAccount.SCHEME_TEL.equals(scheme)) { |
Tyler Gunn | fd74d53f | 2019-02-13 13:15:56 -0800 | [diff] [blame] | 445 | obfuscatePhoneNumber(sb, textToObfuscate); |
Brad Ebinger | 51b9834 | 2016-09-22 16:30:46 -0700 | [diff] [blame] | 446 | } else if (PhoneAccount.SCHEME_SIP.equals(scheme)) { |
| 447 | for (int i = 0; i < textToObfuscate.length(); i++) { |
| 448 | char c = textToObfuscate.charAt(i); |
| 449 | if (c != '@' && c != '.') { |
| 450 | c = '*'; |
| 451 | } |
| 452 | sb.append(c); |
| 453 | } |
| 454 | } else { |
| 455 | sb.append(pii(pii)); |
| 456 | } |
Tyler Gunn | fd74d53f | 2019-02-13 13:15:56 -0800 | [diff] [blame] | 457 | } else if (pii instanceof String) { |
| 458 | String number = (String) pii; |
| 459 | obfuscatePhoneNumber(sb, number); |
Ihab Awad | 60ac30b | 2014-05-20 22:32:12 -0700 | [diff] [blame] | 460 | } |
Ihab Awad | 60ac30b | 2014-05-20 22:32:12 -0700 | [diff] [blame] | 461 | |
Brad Ebinger | 51b9834 | 2016-09-22 16:30:46 -0700 | [diff] [blame] | 462 | return sb.toString(); |
Ihab Awad | 60ac30b | 2014-05-20 22:32:12 -0700 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | /** |
Tyler Gunn | fd74d53f | 2019-02-13 13:15:56 -0800 | [diff] [blame] | 466 | * Obfuscates a phone number, allowing NUM_DIALABLE_DIGITS_TO_LOG digits to be exposed for the |
| 467 | * phone number. |
| 468 | * @param sb String buffer to write obfuscated number to. |
| 469 | * @param phoneNumber The number to obfuscate. |
| 470 | */ |
| 471 | private static void obfuscatePhoneNumber(StringBuilder sb, String phoneNumber) { |
| 472 | int numDigitsToObfuscate = getDialableCount(phoneNumber) |
| 473 | - NUM_DIALABLE_DIGITS_TO_LOG; |
| 474 | for (int i = 0; i < phoneNumber.length(); i++) { |
| 475 | char c = phoneNumber.charAt(i); |
| 476 | boolean isDialable = PhoneNumberUtils.isDialable(c); |
| 477 | if (isDialable) { |
| 478 | numDigitsToObfuscate--; |
| 479 | } |
| 480 | sb.append(isDialable && numDigitsToObfuscate >= 0 ? "*" : c); |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | /** |
Tyler Gunn | 9bc3511 | 2018-04-23 09:52:25 -0700 | [diff] [blame] | 485 | * Determines the number of dialable characters in a string. |
| 486 | * @param toCount The string to count dialable characters in. |
| 487 | * @return The count of dialable characters. |
| 488 | */ |
| 489 | private static int getDialableCount(String toCount) { |
| 490 | int numDialable = 0; |
| 491 | for (char c : toCount.toCharArray()) { |
| 492 | if (PhoneNumberUtils.isDialable(c)) { |
| 493 | numDialable++; |
| 494 | } |
| 495 | } |
| 496 | return numDialable; |
| 497 | } |
| 498 | |
| 499 | /** |
Ihab Awad | 60ac30b | 2014-05-20 22:32:12 -0700 | [diff] [blame] | 500 | * Redact personally identifiable information for production users. |
youhei.x.miyoshi | b3cd7b5 | 2016-12-12 21:10:54 +0900 | [diff] [blame] | 501 | * If we are running in verbose mode, return the original string, |
Brad Ebinger | f784b29 | 2017-12-22 13:45:27 -0800 | [diff] [blame] | 502 | * and return "***" otherwise. |
Ihab Awad | 60ac30b | 2014-05-20 22:32:12 -0700 | [diff] [blame] | 503 | */ |
| 504 | public static String pii(Object pii) { |
| 505 | if (pii == null || VERBOSE) { |
| 506 | return String.valueOf(pii); |
| 507 | } |
Brad Ebinger | f784b29 | 2017-12-22 13:45:27 -0800 | [diff] [blame] | 508 | return "***"; |
Ihab Awad | 60ac30b | 2014-05-20 22:32:12 -0700 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | private static String getPrefixFromObject(Object obj) { |
| 512 | return obj == null ? "<null>" : obj.getClass().getSimpleName(); |
| 513 | } |
| 514 | |
| 515 | private static String buildMessage(String prefix, String format, Object... args) { |
Brad Ebinger | 51b9834 | 2016-09-22 16:30:46 -0700 | [diff] [blame] | 516 | // Incorporate thread ID and calling method into prefix |
| 517 | String sessionName = getSessionId(); |
| 518 | String sessionPostfix = TextUtils.isEmpty(sessionName) ? "" : ": " + sessionName; |
| 519 | |
Ihab Awad | 60ac30b | 2014-05-20 22:32:12 -0700 | [diff] [blame] | 520 | String msg; |
| 521 | try { |
| 522 | msg = (args == null || args.length == 0) ? format |
| 523 | : String.format(Locale.US, format, args); |
| 524 | } catch (IllegalFormatException ife) { |
Brad Ebinger | 4fb372f | 2016-10-05 15:47:28 -0700 | [diff] [blame] | 525 | e(TAG, ife, "Log: IllegalFormatException: formatString='%s' numArgs=%d", format, |
Ihab Awad | 60ac30b | 2014-05-20 22:32:12 -0700 | [diff] [blame] | 526 | args.length); |
| 527 | msg = format + " (An error occurred while formatting the message.)"; |
| 528 | } |
Tyler Gunn | 4d54cca | 2023-03-16 23:00:04 +0000 | [diff] [blame] | 529 | // If a lock was set, check if this thread holds that lock and output an emoji that lets |
| 530 | // the developer know whether a log message came from within the Telecom lock or not. |
| 531 | String isLocked = sLock != null ? (Thread.holdsLock(sLock) ? "\uD83D\uDD12" : "❗") : ""; |
| 532 | return String.format(Locale.US, "%s: %s%s%s", prefix, msg, sessionPostfix, isLocked); |
Ihab Awad | 60ac30b | 2014-05-20 22:32:12 -0700 | [diff] [blame] | 533 | } |
Tyler Gunn | ffbcd89 | 2020-05-04 15:01:59 -0700 | [diff] [blame] | 534 | |
| 535 | /** |
| 536 | * Generates an abbreviated version of the package name from a component. |
| 537 | * E.g. com.android.phone becomes cap |
| 538 | * @param componentName The component name to abbreviate. |
| 539 | * @return Abbreviation of empty string if component is null. |
| 540 | * @hide |
| 541 | */ |
| 542 | public static String getPackageAbbreviation(ComponentName componentName) { |
| 543 | if (componentName == null) { |
| 544 | return ""; |
| 545 | } |
| 546 | return getPackageAbbreviation(componentName.getPackageName()); |
| 547 | } |
| 548 | |
| 549 | /** |
| 550 | * Generates an abbreviated version of the package name. |
| 551 | * E.g. com.android.phone becomes cap |
| 552 | * @param packageName The packageName name to abbreviate. |
| 553 | * @return Abbreviation of empty string if package is null. |
| 554 | * @hide |
| 555 | */ |
| 556 | public static String getPackageAbbreviation(String packageName) { |
| 557 | if (packageName == null) { |
| 558 | return ""; |
| 559 | } |
| 560 | return Arrays.stream(packageName.split("\\.")) |
Tyler Gunn | d582184 | 2021-02-05 11:12:57 -0800 | [diff] [blame] | 561 | .map(s -> s.length() == 0 ? "" : s.substring(0, 1)) |
Tyler Gunn | ffbcd89 | 2020-05-04 15:01:59 -0700 | [diff] [blame] | 562 | .collect(Collectors.joining("")); |
| 563 | } |
Ihab Awad | 60ac30b | 2014-05-20 22:32:12 -0700 | [diff] [blame] | 564 | } |