blob: 2ab0525a33c4e4122cd605577b0820d9f1e861bd [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
Santos Cordon3c20d632016-02-25 16:12:35 -080019import android.net.Uri;
Hall Liue362e502016-01-07 17:35:54 -080020import android.os.AsyncTask;
Santos Cordon3c20d632016-02-25 16:12:35 -080021import android.telephony.PhoneNumberUtils;
22import android.text.TextUtils;
Hall Liue362e502016-01-07 17:35:54 -080023
Ihab Awad60ac30b2014-05-20 22:32:12 -070024import java.security.MessageDigest;
25import java.security.NoSuchAlgorithmException;
26import java.util.IllegalFormatException;
27import java.util.Locale;
28
29/**
30 * Manages logging for the entire module.
31 *
32 * @hide
33 */
34final public class Log {
35
Tyler Gunnef9f6f92014-09-12 22:16:17 -070036 // Generic tag for all Telecom Framework logging
37 private static final String TAG = "TelecomFramework";
Ihab Awad60ac30b2014-05-20 22:32:12 -070038
Yorke Leef3dba2f2014-09-04 11:33:38 -070039 public static final boolean FORCE_LOGGING = false; /* STOP SHIP if true */
Ihab Awad60ac30b2014-05-20 22:32:12 -070040 public static final boolean DEBUG = isLoggable(android.util.Log.DEBUG);
41 public static final boolean INFO = isLoggable(android.util.Log.INFO);
42 public static final boolean VERBOSE = isLoggable(android.util.Log.VERBOSE);
43 public static final boolean WARN = isLoggable(android.util.Log.WARN);
44 public static final boolean ERROR = isLoggable(android.util.Log.ERROR);
45
Hall Liue362e502016-01-07 17:35:54 -080046 private static MessageDigest sMessageDigest;
47
Ihab Awad60ac30b2014-05-20 22:32:12 -070048 private Log() {}
49
Hall Liue362e502016-01-07 17:35:54 -080050 public static void initMd5Sum() {
51 new AsyncTask<Void, Void, Void>() {
52 @Override
53 public Void doInBackground(Void... args) {
54 MessageDigest md;
55 try {
56 md = MessageDigest.getInstance("SHA-1");
57 } catch (NoSuchAlgorithmException e) {
58 md = null;
59 }
60 sMessageDigest = md;
61 return null;
62 }
63 }.execute();
64 }
65
Ihab Awad60ac30b2014-05-20 22:32:12 -070066 public static boolean isLoggable(int level) {
67 return FORCE_LOGGING || android.util.Log.isLoggable(TAG, level);
68 }
69
70 public static void d(String prefix, String format, Object... args) {
71 if (DEBUG) {
72 android.util.Log.d(TAG, buildMessage(prefix, format, args));
73 }
74 }
75
76 public static void d(Object objectPrefix, String format, Object... args) {
77 if (DEBUG) {
78 android.util.Log.d(TAG, buildMessage(getPrefixFromObject(objectPrefix), format, args));
79 }
80 }
81
82 public static void i(String prefix, String format, Object... args) {
83 if (INFO) {
84 android.util.Log.i(TAG, buildMessage(prefix, format, args));
85 }
86 }
87
88 public static void i(Object objectPrefix, String format, Object... args) {
89 if (INFO) {
90 android.util.Log.i(TAG, buildMessage(getPrefixFromObject(objectPrefix), format, args));
91 }
92 }
93
94 public static void v(String prefix, String format, Object... args) {
95 if (VERBOSE) {
96 android.util.Log.v(TAG, buildMessage(prefix, format, args));
97 }
98 }
99
100 public static void v(Object objectPrefix, String format, Object... args) {
101 if (VERBOSE) {
102 android.util.Log.v(TAG, buildMessage(getPrefixFromObject(objectPrefix), format, args));
103 }
104 }
105
106 public static void w(String prefix, String format, Object... args) {
107 if (WARN) {
108 android.util.Log.w(TAG, buildMessage(prefix, format, args));
109 }
110 }
111
112 public static void w(Object objectPrefix, String format, Object... args) {
113 if (WARN) {
114 android.util.Log.w(TAG, buildMessage(getPrefixFromObject(objectPrefix), format, args));
115 }
116 }
117
118 public static void e(String prefix, Throwable tr, String format, Object... args) {
119 if (ERROR) {
120 android.util.Log.e(TAG, buildMessage(prefix, format, args), tr);
121 }
122 }
123
124 public static void e(Object objectPrefix, Throwable tr, String format, Object... args) {
125 if (ERROR) {
126 android.util.Log.e(TAG, buildMessage(getPrefixFromObject(objectPrefix), format, args),
127 tr);
128 }
129 }
130
131 public static void wtf(String prefix, Throwable tr, String format, Object... args) {
132 android.util.Log.wtf(TAG, buildMessage(prefix, format, args), tr);
133 }
134
135 public static void wtf(Object objectPrefix, Throwable tr, String format, Object... args) {
136 android.util.Log.wtf(TAG, buildMessage(getPrefixFromObject(objectPrefix), format, args),
137 tr);
138 }
139
140 public static void wtf(String prefix, String format, Object... args) {
141 String msg = buildMessage(prefix, format, args);
142 android.util.Log.wtf(TAG, msg, new IllegalStateException(msg));
143 }
144
145 public static void wtf(Object objectPrefix, String format, Object... args) {
146 String msg = buildMessage(getPrefixFromObject(objectPrefix), format, args);
147 android.util.Log.wtf(TAG, msg, new IllegalStateException(msg));
148 }
149
150 /**
151 * Redact personally identifiable information for production users.
152 * If we are running in verbose mode, return the original string, otherwise
153 * return a SHA-1 hash of the input string.
154 */
155 public static String pii(Object pii) {
156 if (pii == null || VERBOSE) {
157 return String.valueOf(pii);
Santos Cordon3c20d632016-02-25 16:12:35 -0800158 } if (pii instanceof Uri) {
159 return piiUri((Uri) pii);
Ihab Awad60ac30b2014-05-20 22:32:12 -0700160 }
161 return "[" + secureHash(String.valueOf(pii).getBytes()) + "]";
162 }
163
Santos Cordon3c20d632016-02-25 16:12:35 -0800164 private static String piiUri(Uri handle) {
165 StringBuilder sb = new StringBuilder();
166 String scheme = handle.getScheme();
167 if (!TextUtils.isEmpty(scheme)) {
168 sb.append(scheme).append(":");
169 }
170 String value = handle.getSchemeSpecificPart();
171 if (!TextUtils.isEmpty(value)) {
172 for (int i = 0; i < value.length(); i++) {
173 char c = value.charAt(i);
174 if (PhoneNumberUtils.isStartsPostDial(c)) {
175 sb.append(c);
176 } else if (PhoneNumberUtils.isDialable(c)) {
177 sb.append("*");
178 } else if (('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z')) {
179 sb.append("*");
180 } else {
181 sb.append(c);
182 }
183 }
184 }
185 return sb.toString();
186
187 }
188
Ihab Awad60ac30b2014-05-20 22:32:12 -0700189 private static String secureHash(byte[] input) {
Hall Liue362e502016-01-07 17:35:54 -0800190 if (sMessageDigest != null) {
191 sMessageDigest.reset();
192 sMessageDigest.update(input);
193 byte[] result = sMessageDigest.digest();
194 return encodeHex(result);
195 } else {
196 return "Uninitialized SHA1";
Ihab Awad60ac30b2014-05-20 22:32:12 -0700197 }
Ihab Awad60ac30b2014-05-20 22:32:12 -0700198 }
199
200 private static String encodeHex(byte[] bytes) {
201 StringBuffer hex = new StringBuffer(bytes.length * 2);
202
203 for (int i = 0; i < bytes.length; i++) {
204 int byteIntValue = bytes[i] & 0xff;
205 if (byteIntValue < 0x10) {
206 hex.append("0");
207 }
208 hex.append(Integer.toString(byteIntValue, 16));
209 }
210
211 return hex.toString();
212 }
213
214 private static String getPrefixFromObject(Object obj) {
215 return obj == null ? "<null>" : obj.getClass().getSimpleName();
216 }
217
218 private static String buildMessage(String prefix, String format, Object... args) {
219 String msg;
220 try {
221 msg = (args == null || args.length == 0) ? format
222 : String.format(Locale.US, format, args);
223 } catch (IllegalFormatException ife) {
224 wtf("Log", ife, "IllegalFormatException: formatString='%s' numArgs=%d", format,
225 args.length);
226 msg = format + " (An error occurred while formatting the message.)";
227 }
228 return String.format(Locale.US, "%s: %s", prefix, msg);
229 }
230}