1 /* 2 * Copyright (C) 2012 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 android.telephony; 18 19 import android.os.Build; 20 import android.text.TextUtils; 21 import android.util.Log; 22 23 import android.annotation.UnsupportedAppUsage; 24 import android.util.Base64; 25 26 import java.security.MessageDigest; 27 import java.security.NoSuchAlgorithmException; 28 29 30 /** 31 * A class to log strings to the RADIO LOG. 32 * 33 * @hide 34 */ 35 public final class Rlog { 36 37 private static final boolean USER_BUILD = Build.IS_USER; 38 Rlog()39 private Rlog() { 40 } 41 42 @UnsupportedAppUsage v(String tag, String msg)43 public static int v(String tag, String msg) { 44 return Log.println_native(Log.LOG_ID_RADIO, Log.VERBOSE, tag, msg); 45 } 46 v(String tag, String msg, Throwable tr)47 public static int v(String tag, String msg, Throwable tr) { 48 return Log.println_native(Log.LOG_ID_RADIO, Log.VERBOSE, tag, 49 msg + '\n' + Log.getStackTraceString(tr)); 50 } 51 52 @UnsupportedAppUsage d(String tag, String msg)53 public static int d(String tag, String msg) { 54 return Log.println_native(Log.LOG_ID_RADIO, Log.DEBUG, tag, msg); 55 } 56 57 @UnsupportedAppUsage d(String tag, String msg, Throwable tr)58 public static int d(String tag, String msg, Throwable tr) { 59 return Log.println_native(Log.LOG_ID_RADIO, Log.DEBUG, tag, 60 msg + '\n' + Log.getStackTraceString(tr)); 61 } 62 63 @UnsupportedAppUsage i(String tag, String msg)64 public static int i(String tag, String msg) { 65 return Log.println_native(Log.LOG_ID_RADIO, Log.INFO, tag, msg); 66 } 67 68 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) i(String tag, String msg, Throwable tr)69 public static int i(String tag, String msg, Throwable tr) { 70 return Log.println_native(Log.LOG_ID_RADIO, Log.INFO, tag, 71 msg + '\n' + Log.getStackTraceString(tr)); 72 } 73 74 @UnsupportedAppUsage w(String tag, String msg)75 public static int w(String tag, String msg) { 76 return Log.println_native(Log.LOG_ID_RADIO, Log.WARN, tag, msg); 77 } 78 79 @UnsupportedAppUsage w(String tag, String msg, Throwable tr)80 public static int w(String tag, String msg, Throwable tr) { 81 return Log.println_native(Log.LOG_ID_RADIO, Log.WARN, tag, 82 msg + '\n' + Log.getStackTraceString(tr)); 83 } 84 w(String tag, Throwable tr)85 public static int w(String tag, Throwable tr) { 86 return Log.println_native(Log.LOG_ID_RADIO, Log.WARN, tag, Log.getStackTraceString(tr)); 87 } 88 89 @UnsupportedAppUsage e(String tag, String msg)90 public static int e(String tag, String msg) { 91 return Log.println_native(Log.LOG_ID_RADIO, Log.ERROR, tag, msg); 92 } 93 94 @UnsupportedAppUsage e(String tag, String msg, Throwable tr)95 public static int e(String tag, String msg, Throwable tr) { 96 return Log.println_native(Log.LOG_ID_RADIO, Log.ERROR, tag, 97 msg + '\n' + Log.getStackTraceString(tr)); 98 } 99 println(int priority, String tag, String msg)100 public static int println(int priority, String tag, String msg) { 101 return Log.println_native(Log.LOG_ID_RADIO, priority, tag, msg); 102 } 103 isLoggable(String tag, int level)104 public static boolean isLoggable(String tag, int level) { 105 return Log.isLoggable(tag, level); 106 } 107 108 /** 109 * Redact personally identifiable information for production users. 110 * @param tag used to identify the source of a log message 111 * @param pii the personally identifiable information we want to apply secure hash on. 112 * @return If tag is loggable in verbose mode or pii is null, return the original input. 113 * otherwise return a secure Hash of input pii 114 */ pii(String tag, Object pii)115 public static String pii(String tag, Object pii) { 116 String val = String.valueOf(pii); 117 if (pii == null || TextUtils.isEmpty(val) || isLoggable(tag, Log.VERBOSE)) { 118 return val; 119 } 120 return "[" + secureHash(val.getBytes()) + "]"; 121 } 122 123 /** 124 * Redact personally identifiable information for production users. 125 * @param enablePiiLogging set when caller explicitly want to enable sensitive logging. 126 * @param pii the personally identifiable information we want to apply secure hash on. 127 * @return If enablePiiLogging is set to true or pii is null, return the original input. 128 * otherwise return a secure Hash of input pii 129 */ pii(boolean enablePiiLogging, Object pii)130 public static String pii(boolean enablePiiLogging, Object pii) { 131 String val = String.valueOf(pii); 132 if (pii == null || TextUtils.isEmpty(val) || enablePiiLogging) { 133 return val; 134 } 135 return "[" + secureHash(val.getBytes()) + "]"; 136 } 137 138 /** 139 * Returns a secure hash (using the SHA1 algorithm) of the provided input. 140 * 141 * @return "****" if the build type is user, otherwise the hash 142 * @param input the bytes for which the secure hash should be computed. 143 */ secureHash(byte[] input)144 private static String secureHash(byte[] input) { 145 // Refrain from logging user personal information in user build. 146 if (USER_BUILD) { 147 return "****"; 148 } 149 150 MessageDigest messageDigest; 151 152 try { 153 messageDigest = MessageDigest.getInstance("SHA-1"); 154 } catch (NoSuchAlgorithmException e) { 155 return "####"; 156 } 157 158 byte[] result = messageDigest.digest(input); 159 return Base64.encodeToString( 160 result, Base64.URL_SAFE | Base64.NO_PADDING | Base64.NO_WRAP); 161 } 162 } 163 164