Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [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 | |
| 17 | package com.android.telecomm; |
| 18 | |
Santos Cordon | a0e5f1a | 2014-03-31 21:43:00 -0700 | [diff] [blame] | 19 | import android.net.Uri; |
| 20 | import android.telephony.PhoneNumberUtils; |
| 21 | |
Sailesh Nepal | 9199078 | 2014-03-08 17:45:52 -0800 | [diff] [blame] | 22 | import java.security.MessageDigest; |
| 23 | import java.security.NoSuchAlgorithmException; |
Evan Charlton | 8873e0a | 2014-03-19 15:49:41 -0700 | [diff] [blame] | 24 | import java.util.IllegalFormatException; |
| 25 | import java.util.Locale; |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 26 | |
| 27 | /** |
| 28 | * Manages logging for the entire module. |
| 29 | */ |
| 30 | public class Log { |
| 31 | |
| 32 | // Generic tag for all In Call logging |
| 33 | private static final String TAG = "Telecomm"; |
| 34 | |
Santos Cordon | 4f1b7b8 | 2014-05-25 21:03:04 -0700 | [diff] [blame^] | 35 | public static final boolean FORCE_LOGGING = false; /* STOP SHIP if true */ |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 36 | public static final boolean DEBUG = isLoggable(android.util.Log.DEBUG); |
| 37 | public static final boolean INFO = isLoggable(android.util.Log.INFO); |
| 38 | public static final boolean VERBOSE = isLoggable(android.util.Log.VERBOSE); |
| 39 | public static final boolean WARN = isLoggable(android.util.Log.WARN); |
| 40 | public static final boolean ERROR = isLoggable(android.util.Log.ERROR); |
| 41 | |
| 42 | private Log() {} |
| 43 | |
| 44 | public static boolean isLoggable(int level) { |
| 45 | return FORCE_LOGGING || android.util.Log.isLoggable(TAG, level); |
| 46 | } |
| 47 | |
| 48 | public static void d(String prefix, String format, Object... args) { |
| 49 | if (DEBUG) { |
| 50 | android.util.Log.d(TAG, buildMessage(prefix, format, args)); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | public static void d(Object objectPrefix, String format, Object... args) { |
| 55 | if (DEBUG) { |
| 56 | android.util.Log.d(TAG, buildMessage(getPrefixFromObject(objectPrefix), format, args)); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | public static void i(String prefix, String format, Object... args) { |
| 61 | if (INFO) { |
| 62 | android.util.Log.i(TAG, buildMessage(prefix, format, args)); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | public static void i(Object objectPrefix, String format, Object... args) { |
| 67 | if (INFO) { |
| 68 | android.util.Log.i(TAG, buildMessage(getPrefixFromObject(objectPrefix), format, args)); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | public static void v(String prefix, String format, Object... args) { |
| 73 | if (VERBOSE) { |
| 74 | android.util.Log.v(TAG, buildMessage(prefix, format, args)); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | public static void v(Object objectPrefix, String format, Object... args) { |
| 79 | if (VERBOSE) { |
| 80 | android.util.Log.v(TAG, buildMessage(getPrefixFromObject(objectPrefix), format, args)); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | public static void w(String prefix, String format, Object... args) { |
| 85 | if (WARN) { |
| 86 | android.util.Log.w(TAG, buildMessage(prefix, format, args)); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | public static void w(Object objectPrefix, String format, Object... args) { |
| 91 | if (WARN) { |
| 92 | android.util.Log.w(TAG, buildMessage(getPrefixFromObject(objectPrefix), format, args)); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | public static void e(String prefix, Throwable tr, String format, Object... args) { |
| 97 | if (ERROR) { |
| 98 | android.util.Log.e(TAG, buildMessage(prefix, format, args), tr); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | public static void e(Object objectPrefix, Throwable tr, String format, Object... args) { |
| 103 | if (ERROR) { |
| 104 | android.util.Log.e(TAG, buildMessage(getPrefixFromObject(objectPrefix), format, args), |
| 105 | tr); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | public static void wtf(String prefix, Throwable tr, String format, Object... args) { |
| 110 | android.util.Log.wtf(TAG, buildMessage(prefix, format, args), tr); |
| 111 | } |
| 112 | |
| 113 | public static void wtf(Object objectPrefix, Throwable tr, String format, Object... args) { |
| 114 | android.util.Log.wtf(TAG, buildMessage(getPrefixFromObject(objectPrefix), format, args), |
| 115 | tr); |
| 116 | } |
| 117 | |
| 118 | public static void wtf(String prefix, String format, Object... args) { |
Evan Charlton | 8873e0a | 2014-03-19 15:49:41 -0700 | [diff] [blame] | 119 | String msg = buildMessage(prefix, format, args); |
| 120 | android.util.Log.wtf(TAG, msg, new IllegalStateException(msg)); |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | public static void wtf(Object objectPrefix, String format, Object... args) { |
Evan Charlton | 8873e0a | 2014-03-19 15:49:41 -0700 | [diff] [blame] | 124 | String msg = buildMessage(getPrefixFromObject(objectPrefix), format, args); |
| 125 | android.util.Log.wtf(TAG, msg, new IllegalStateException(msg)); |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 126 | } |
| 127 | |
Santos Cordon | a0e5f1a | 2014-03-31 21:43:00 -0700 | [diff] [blame] | 128 | public static String piiHandle(Object pii) { |
| 129 | if (pii == null || VERBOSE) { |
| 130 | return String.valueOf(pii); |
| 131 | } |
| 132 | |
| 133 | if (pii instanceof Uri) { |
| 134 | Uri uri = (Uri) pii; |
| 135 | |
| 136 | // All Uri's which are not "tel" go through normal pii() method. |
| 137 | if (!"tel".equals(uri.getScheme())) { |
| 138 | return pii(pii); |
| 139 | } else { |
| 140 | pii = uri.getSchemeSpecificPart(); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | String originalString = String.valueOf(pii); |
| 145 | StringBuilder stringBuilder = new StringBuilder(originalString.length()); |
| 146 | for (char c : originalString.toCharArray()) { |
| 147 | if (PhoneNumberUtils.isDialable(c)) { |
| 148 | stringBuilder.append('*'); |
| 149 | } else { |
| 150 | stringBuilder.append(c); |
| 151 | } |
| 152 | } |
| 153 | return stringBuilder.toString(); |
| 154 | } |
| 155 | |
Sailesh Nepal | 9199078 | 2014-03-08 17:45:52 -0800 | [diff] [blame] | 156 | /** |
| 157 | * Redact personally identifiable information for production users. |
| 158 | * If we are running in verbose mode, return the original string, otherwise |
| 159 | * return a SHA-1 hash of the input string. |
| 160 | */ |
| 161 | public static String pii(Object pii) { |
| 162 | if (pii == null || VERBOSE) { |
| 163 | return String.valueOf(pii); |
| 164 | } |
| 165 | return "[" + secureHash(String.valueOf(pii).getBytes()) + "]"; |
| 166 | } |
| 167 | |
| 168 | private static String secureHash(byte[] input) { |
| 169 | MessageDigest messageDigest; |
| 170 | try { |
| 171 | messageDigest = MessageDigest.getInstance("SHA-1"); |
| 172 | } catch (NoSuchAlgorithmException e) { |
| 173 | return null; |
| 174 | } |
| 175 | messageDigest.update(input); |
| 176 | byte[] result = messageDigest.digest(); |
| 177 | return encodeHex(result); |
| 178 | } |
| 179 | |
| 180 | private static String encodeHex(byte[] bytes) { |
| 181 | StringBuffer hex = new StringBuffer(bytes.length * 2); |
| 182 | |
| 183 | for (int i = 0; i < bytes.length; i++) { |
Evan Charlton | 8873e0a | 2014-03-19 15:49:41 -0700 | [diff] [blame] | 184 | int byteIntValue = bytes[i] & 0xff; |
Sailesh Nepal | 9199078 | 2014-03-08 17:45:52 -0800 | [diff] [blame] | 185 | if (byteIntValue < 0x10) { |
| 186 | hex.append("0"); |
| 187 | } |
| 188 | hex.append(Integer.toString(byteIntValue, 16)); |
| 189 | } |
| 190 | |
| 191 | return hex.toString(); |
| 192 | } |
| 193 | |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 194 | private static String getPrefixFromObject(Object obj) { |
| 195 | return obj == null ? "<null>" : obj.getClass().getSimpleName(); |
| 196 | } |
| 197 | |
| 198 | private static String buildMessage(String prefix, String format, Object... args) { |
| 199 | String msg; |
| 200 | try { |
| 201 | msg = (args == null || args.length == 0) ? format |
| 202 | : String.format(Locale.US, format, args); |
| 203 | } catch (IllegalFormatException ife) { |
Santos Cordon | 4f1b7b8 | 2014-05-25 21:03:04 -0700 | [diff] [blame^] | 204 | e("Log", ife, "IllegalFormatException: formatString='%s' numArgs=%d", format, |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 205 | args.length); |
| 206 | msg = format + " (An error occurred while formatting the message.)"; |
| 207 | } |
| 208 | return String.format(Locale.US, "%s: %s", prefix, msg); |
| 209 | } |
| 210 | } |