| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.telecom; |
| 18 |
|
| 19 |
import android.annotation.NonNull; |
| 20 |
import android.compat.annotation.UnsupportedAppUsage; |
| 21 |
import android.content.ComponentName; |
| 22 |
import android.content.Context; |
| 23 |
import android.net.Uri; |
| 24 |
import android.os.Build; |
| 25 |
import android.telecom.Logging.EventManager; |
| 26 |
import android.telecom.Logging.Session; |
| 27 |
import android.telecom.Logging.SessionManager; |
| 28 |
import android.telephony.PhoneNumberUtils; |
| 29 |
import android.text.TextUtils; |
| 30 |
|
| 31 |
import com.android.internal.annotations.VisibleForTesting; |
| 32 |
import com.android.internal.util.IndentingPrintWriter; |
| 33 |
|
| 34 |
import java.util.Arrays; |
| 35 |
import java.util.IllegalFormatException; |
| 36 |
import java.util.Locale; |
| 37 |
import java.util.stream.Collectors; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public class Log { |
| 45 |
|
| 46 |
private static final long EXTENDED_LOGGING_DURATION_MILLIS = 60000 * 30; |
| 47 |
|
| 48 |
private static final int EVENTS_TO_CACHE = 10; |
| 49 |
private static final int EVENTS_TO_CACHE_DEBUG = 20; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
private static final int NUM_DIALABLE_DIGITS_TO_LOG = Build.IS_USER ? 0 : 2; |
| 55 |
|
| 56 |
|
| 57 |
@VisibleForTesting |
| 58 |
public static String TAG = "TelecomFramework"; |
| 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); |
| 64 |
|
| 65 |
private static final boolean FORCE_LOGGING = false; |
| 66 |
private static final boolean USER_BUILD = Build.IS_USER; |
| 67 |
|
| 68 |
|
| 69 |
private static final Object sSingletonSync = new Object(); |
| 70 |
private static EventManager sEventManager; |
| 71 |
private static SessionManager sSessionManager; |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
private static boolean sIsUserExtendedLoggingEnabled = false; |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
private static boolean sIsUnitTestingEnabled = false; |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
private static long sUserExtendedLoggingStopTime = 0; |
| 88 |
|
| 89 |
private Log() { |
| 90 |
} |
| 91 |
|
| 92 |
public static void d(String prefix, String format, Object... args) { |
| 93 |
if (sIsUserExtendedLoggingEnabled) { |
| 94 |
maybeDisableLogging(); |
| 95 |
android.util.Slog.i(TAG, buildMessage(prefix, format, args)); |
| 96 |
} else if (DEBUG) { |
| 97 |
android.util.Slog.d(TAG, buildMessage(prefix, format, args)); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
public static void d(Object objectPrefix, String format, Object... args) { |
| 102 |
if (sIsUserExtendedLoggingEnabled) { |
| 103 |
maybeDisableLogging(); |
| 104 |
android.util.Slog.i(TAG, buildMessage(getPrefixFromObject(objectPrefix), format, args)); |
| 105 |
} else if (DEBUG) { |
| 106 |
android.util.Slog.d(TAG, buildMessage(getPrefixFromObject(objectPrefix), format, args)); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 111 |
public static void i(String prefix, String format, Object... args) { |
| 112 |
if (INFO) { |
| 113 |
android.util.Slog.i(TAG, buildMessage(prefix, format, args)); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
public static void i(Object objectPrefix, String format, Object... args) { |
| 118 |
if (INFO) { |
| 119 |
android.util.Slog.i(TAG, buildMessage(getPrefixFromObject(objectPrefix), format, args)); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
public static void v(String prefix, String format, Object... args) { |
| 124 |
if (sIsUserExtendedLoggingEnabled) { |
| 125 |
maybeDisableLogging(); |
| 126 |
android.util.Slog.i(TAG, buildMessage(prefix, format, args)); |
| 127 |
} else if (VERBOSE) { |
| 128 |
android.util.Slog.v(TAG, buildMessage(prefix, format, args)); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
public static void v(Object objectPrefix, String format, Object... args) { |
| 133 |
if (sIsUserExtendedLoggingEnabled) { |
| 134 |
maybeDisableLogging(); |
| 135 |
android.util.Slog.i(TAG, buildMessage(getPrefixFromObject(objectPrefix), format, args)); |
| 136 |
} else if (VERBOSE) { |
| 137 |
android.util.Slog.v(TAG, buildMessage(getPrefixFromObject(objectPrefix), format, args)); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 142 |
public static void w(String prefix, String format, Object... args) { |
| 143 |
if (WARN) { |
| 144 |
android.util.Slog.w(TAG, buildMessage(prefix, format, args)); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
public static void w(Object objectPrefix, String format, Object... args) { |
| 149 |
if (WARN) { |
| 150 |
android.util.Slog.w(TAG, buildMessage(getPrefixFromObject(objectPrefix), format, args)); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
public static void e(String prefix, Throwable tr, String format, Object... args) { |
| 155 |
if (ERROR) { |
| 156 |
android.util.Slog.e(TAG, buildMessage(prefix, format, args), tr); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
public static void e(Object objectPrefix, Throwable tr, String format, Object... args) { |
| 161 |
if (ERROR) { |
| 162 |
android.util.Slog.e(TAG, buildMessage(getPrefixFromObject(objectPrefix), format, args), |
| 163 |
tr); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
public static void wtf(String prefix, Throwable tr, String format, Object... args) { |
| 168 |
android.util.Slog.wtf(TAG, buildMessage(prefix, format, args), tr); |
| 169 |
} |
| 170 |
|
| 171 |
public static void wtf(Object objectPrefix, Throwable tr, String format, Object... args) { |
| 172 |
android.util.Slog.wtf(TAG, buildMessage(getPrefixFromObject(objectPrefix), format, args), |
| 173 |
tr); |
| 174 |
} |
| 175 |
|
| 176 |
public static void wtf(String prefix, String format, Object... args) { |
| 177 |
String msg = buildMessage(prefix, format, args); |
| 178 |
android.util.Slog.wtf(TAG, msg, new IllegalStateException(msg)); |
| 179 |
} |
| 180 |
|
| 181 |
public static void wtf(Object objectPrefix, String format, Object... args) { |
| 182 |
String msg = buildMessage(getPrefixFromObject(objectPrefix), format, args); |
| 183 |
android.util.Slog.wtf(TAG, msg, new IllegalStateException(msg)); |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
public static void setSessionContext(Context context) { |
| 196 |
getSessionManager().setContext(context); |
| 197 |
} |
| 198 |
|
| 199 |
public static void startSession(String shortMethodName) { |
| 200 |
getSessionManager().startSession(shortMethodName, null); |
| 201 |
} |
| 202 |
|
| 203 |
public static void startSession(Session.Info info, String shortMethodName) { |
| 204 |
getSessionManager().startSession(info, shortMethodName, null); |
| 205 |
} |
| 206 |
|
| 207 |
public static void startSession(String shortMethodName, String callerIdentification) { |
| 208 |
getSessionManager().startSession(shortMethodName, callerIdentification); |
| 209 |
} |
| 210 |
|
| 211 |
public static void startSession(Session.Info info, String shortMethodName, |
| 212 |
String callerIdentification) { |
| 213 |
getSessionManager().startSession(info, shortMethodName, callerIdentification); |
| 214 |
} |
| 215 |
|
| 216 |
public static Session createSubsession() { |
| 217 |
return getSessionManager().createSubsession(); |
| 218 |
} |
| 219 |
|
| 220 |
public static Session.Info getExternalSession() { |
| 221 |
return getSessionManager().getExternalSession(); |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
public static Session.Info getExternalSession(@NonNull String ownerInfo) { |
| 231 |
return getSessionManager().getExternalSession(ownerInfo); |
| 232 |
} |
| 233 |
|
| 234 |
public static void cancelSubsession(Session subsession) { |
| 235 |
getSessionManager().cancelSubsession(subsession); |
| 236 |
} |
| 237 |
|
| 238 |
public static void continueSession(Session subsession, String shortMethodName) { |
| 239 |
getSessionManager().continueSession(subsession, shortMethodName); |
| 240 |
} |
| 241 |
|
| 242 |
public static void endSession() { |
| 243 |
getSessionManager().endSession(); |
| 244 |
} |
| 245 |
|
| 246 |
public static void registerSessionListener(SessionManager.ISessionListener l) { |
| 247 |
getSessionManager().registerSessionListener(l); |
| 248 |
} |
| 249 |
|
| 250 |
public static String getSessionId() { |
| 251 |
|
| 252 |
|
| 253 |
synchronized (sSingletonSync) { |
| 254 |
if (sSessionManager != null) { |
| 255 |
return getSessionManager().getSessionId(); |
| 256 |
} else { |
| 257 |
return ""; |
| 258 |
} |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
public static void addEvent(EventManager.Loggable recordEntry, String event) { |
| 263 |
getEventManager().event(recordEntry, event, null); |
| 264 |
} |
| 265 |
|
| 266 |
public static void addEvent(EventManager.Loggable recordEntry, String event, Object data) { |
| 267 |
getEventManager().event(recordEntry, event, data); |
| 268 |
} |
| 269 |
|
| 270 |
public static void addEvent(EventManager.Loggable recordEntry, String event, String format, |
| 271 |
Object... args) { |
| 272 |
getEventManager().event(recordEntry, event, format, args); |
| 273 |
} |
| 274 |
|
| 275 |
public static void registerEventListener(EventManager.EventListener e) { |
| 276 |
getEventManager().registerEventListener(e); |
| 277 |
} |
| 278 |
|
| 279 |
public static void addRequestResponsePair(EventManager.TimedEventPair p) { |
| 280 |
getEventManager().addRequestResponsePair(p); |
| 281 |
} |
| 282 |
|
| 283 |
public static void dumpEvents(IndentingPrintWriter pw) { |
| 284 |
|
| 285 |
|
| 286 |
synchronized (sSingletonSync) { |
| 287 |
if (sEventManager != null) { |
| 288 |
getEventManager().dumpEvents(pw); |
| 289 |
} else { |
| 290 |
pw.println("No Historical Events Logged."); |
| 291 |
} |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
public static void dumpEventsTimeline(IndentingPrintWriter pw) { |
| 301 |
|
| 302 |
|
| 303 |
synchronized (sSingletonSync) { |
| 304 |
if (sEventManager != null) { |
| 305 |
getEventManager().dumpEventsTimeline(pw); |
| 306 |
} else { |
| 307 |
pw.println("No Historical Events Logged."); |
| 308 |
} |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
public static void setIsExtendedLoggingEnabled(boolean isExtendedLoggingEnabled) { |
| 319 |
|
| 320 |
if (sIsUserExtendedLoggingEnabled == isExtendedLoggingEnabled) { |
| 321 |
return; |
| 322 |
} |
| 323 |
|
| 324 |
if (sEventManager != null) { |
| 325 |
sEventManager.changeEventCacheSize(isExtendedLoggingEnabled ? |
| 326 |
EVENTS_TO_CACHE_DEBUG : EVENTS_TO_CACHE); |
| 327 |
} |
| 328 |
|
| 329 |
sIsUserExtendedLoggingEnabled = isExtendedLoggingEnabled; |
| 330 |
if (sIsUserExtendedLoggingEnabled) { |
| 331 |
sUserExtendedLoggingStopTime = System.currentTimeMillis() |
| 332 |
+ EXTENDED_LOGGING_DURATION_MILLIS; |
| 333 |
} else { |
| 334 |
sUserExtendedLoggingStopTime = 0; |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
public static void setUnitTestingEnabled(boolean isEnabled) { |
| 345 |
sIsUnitTestingEnabled = isEnabled; |
| 346 |
} |
| 347 |
|
| 348 |
public static boolean isUnitTestingEnabled() { |
| 349 |
return sIsUnitTestingEnabled; |
| 350 |
} |
| 351 |
|
| 352 |
private static EventManager getEventManager() { |
| 353 |
|
| 354 |
|
| 355 |
if (sEventManager == null) { |
| 356 |
synchronized (sSingletonSync) { |
| 357 |
if (sEventManager == null) { |
| 358 |
sEventManager = new EventManager(Log::getSessionId); |
| 359 |
return sEventManager; |
| 360 |
} |
| 361 |
} |
| 362 |
} |
| 363 |
return sEventManager; |
| 364 |
} |
| 365 |
|
| 366 |
@VisibleForTesting |
| 367 |
public static SessionManager getSessionManager() { |
| 368 |
|
| 369 |
|
| 370 |
if (sSessionManager == null) { |
| 371 |
synchronized (sSingletonSync) { |
| 372 |
if (sSessionManager == null) { |
| 373 |
sSessionManager = new SessionManager(); |
| 374 |
return sSessionManager; |
| 375 |
} |
| 376 |
} |
| 377 |
} |
| 378 |
return sSessionManager; |
| 379 |
} |
| 380 |
|
| 381 |
public static void setTag(String tag) { |
| 382 |
TAG = tag; |
| 383 |
DEBUG = isLoggable(android.util.Log.DEBUG); |
| 384 |
INFO = isLoggable(android.util.Log.INFO); |
| 385 |
VERBOSE = isLoggable(android.util.Log.VERBOSE); |
| 386 |
WARN = isLoggable(android.util.Log.WARN); |
| 387 |
ERROR = isLoggable(android.util.Log.ERROR); |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
private static void maybeDisableLogging() { |
| 395 |
if (!sIsUserExtendedLoggingEnabled) { |
| 396 |
return; |
| 397 |
} |
| 398 |
|
| 399 |
if (sUserExtendedLoggingStopTime < System.currentTimeMillis()) { |
| 400 |
sUserExtendedLoggingStopTime = 0; |
| 401 |
sIsUserExtendedLoggingEnabled = false; |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
public static boolean isLoggable(int level) { |
| 406 |
return FORCE_LOGGING || android.util.Log.isLoggable(TAG, level); |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
public static String piiHandle(Object pii) { |
| 416 |
if (pii == null || VERBOSE) { |
| 417 |
return String.valueOf(pii); |
| 418 |
} |
| 419 |
|
| 420 |
StringBuilder sb = new StringBuilder(); |
| 421 |
if (pii instanceof Uri) { |
| 422 |
Uri uri = (Uri) pii; |
| 423 |
String scheme = uri.getScheme(); |
| 424 |
|
| 425 |
if (!TextUtils.isEmpty(scheme)) { |
| 426 |
sb.append(scheme).append(":"); |
| 427 |
} |
| 428 |
|
| 429 |
String textToObfuscate = uri.getSchemeSpecificPart(); |
| 430 |
if (PhoneAccount.SCHEME_TEL.equals(scheme)) { |
| 431 |
obfuscatePhoneNumber(sb, textToObfuscate); |
| 432 |
} else if (PhoneAccount.SCHEME_SIP.equals(scheme)) { |
| 433 |
for (int i = 0; i < textToObfuscate.length(); i++) { |
| 434 |
char c = textToObfuscate.charAt(i); |
| 435 |
if (c != '@' && c != '.') { |
| 436 |
c = '*'; |
| 437 |
} |
| 438 |
sb.append(c); |
| 439 |
} |
| 440 |
} else { |
| 441 |
sb.append(pii(pii)); |
| 442 |
} |
| 443 |
} else if (pii instanceof String) { |
| 444 |
String number = (String) pii; |
| 445 |
obfuscatePhoneNumber(sb, number); |
| 446 |
} |
| 447 |
|
| 448 |
return sb.toString(); |
| 449 |
} |
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
private static void obfuscatePhoneNumber(StringBuilder sb, String phoneNumber) { |
| 458 |
int numDigitsToObfuscate = getDialableCount(phoneNumber) |
| 459 |
- NUM_DIALABLE_DIGITS_TO_LOG; |
| 460 |
for (int i = 0; i < phoneNumber.length(); i++) { |
| 461 |
char c = phoneNumber.charAt(i); |
| 462 |
boolean isDialable = PhoneNumberUtils.isDialable(c); |
| 463 |
if (isDialable) { |
| 464 |
numDigitsToObfuscate--; |
| 465 |
} |
| 466 |
sb.append(isDialable && numDigitsToObfuscate >= 0 ? "*" : c); |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
private static int getDialableCount(String toCount) { |
| 476 |
int numDialable = 0; |
| 477 |
for (char c : toCount.toCharArray()) { |
| 478 |
if (PhoneNumberUtils.isDialable(c)) { |
| 479 |
numDialable++; |
| 480 |
} |
| 481 |
} |
| 482 |
return numDialable; |
| 483 |
} |
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
public static String pii(Object pii) { |
| 491 |
if (pii == null || VERBOSE) { |
| 492 |
return String.valueOf(pii); |
| 493 |
} |
| 494 |
return "***"; |
| 495 |
} |
| 496 |
|
| 497 |
private static String getPrefixFromObject(Object obj) { |
| 498 |
return obj == null ? "<null>" : obj.getClass().getSimpleName(); |
| 499 |
} |
| 500 |
|
| 501 |
private static String buildMessage(String prefix, String format, Object... args) { |
| 502 |
|
| 503 |
String sessionName = getSessionId(); |
| 504 |
String sessionPostfix = TextUtils.isEmpty(sessionName) ? "" : ": " + sessionName; |
| 505 |
|
| 506 |
String msg; |
| 507 |
try { |
| 508 |
msg = (args == null || args.length == 0) ? format |
| 509 |
: String.format(Locale.US, format, args); |
| 510 |
} catch (IllegalFormatException ife) { |
| 511 |
e(TAG, ife, "Log: IllegalFormatException: formatString='%s' numArgs=%d", format, |
| 512 |
args.length); |
| 513 |
msg = format + " (An error occurred while formatting the message.)"; |
| 514 |
} |
| 515 |
return String.format(Locale.US, "%s: %s%s", prefix, msg, sessionPostfix); |
| 516 |
} |
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
public static String getPackageAbbreviation(ComponentName componentName) { |
| 526 |
if (componentName == null) { |
| 527 |
return ""; |
| 528 |
} |
| 529 |
return getPackageAbbreviation(componentName.getPackageName()); |
| 530 |
} |
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
public static String getPackageAbbreviation(String packageName) { |
| 540 |
if (packageName == null) { |
| 541 |
return ""; |
| 542 |
} |
| 543 |
return Arrays.stream(packageName.split("\\.")) |
| 544 |
.map(s -> s.length() == 0 ? "" : s.substring(0, 1)) |
| 545 |
.collect(Collectors.joining("")); |
| 546 |
} |
| 547 |
} |