blob: ebcc1757a509a51107545b70786065ff609f5dfd [file] [log] [blame]
Sailesh Nepalf1c191d2014-03-07 18:17:39 -08001/*
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
17package com.android.telecomm;
18
Santos Cordona0e5f1a2014-03-31 21:43:00 -070019import android.net.Uri;
20import android.telephony.PhoneNumberUtils;
21
Sailesh Nepal91990782014-03-08 17:45:52 -080022import java.security.MessageDigest;
23import java.security.NoSuchAlgorithmException;
Evan Charlton8873e0a2014-03-19 15:49:41 -070024import java.util.IllegalFormatException;
25import java.util.Locale;
Sailesh Nepalf1c191d2014-03-07 18:17:39 -080026
27/**
28 * Manages logging for the entire module.
29 */
30public class Log {
31
32 // Generic tag for all In Call logging
33 private static final String TAG = "Telecomm";
34
Ihab Awad78ac0ce2014-07-18 13:32:53 -070035 public static final boolean FORCE_LOGGING = false; /* STOP SHIP if true */
Sailesh Nepalf1c191d2014-03-07 18:17:39 -080036 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 Charlton8873e0a2014-03-19 15:49:41 -0700119 String msg = buildMessage(prefix, format, args);
120 android.util.Log.wtf(TAG, msg, new IllegalStateException(msg));
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800121 }
122
123 public static void wtf(Object objectPrefix, String format, Object... args) {
Evan Charlton8873e0a2014-03-19 15:49:41 -0700124 String msg = buildMessage(getPrefixFromObject(objectPrefix), format, args);
125 android.util.Log.wtf(TAG, msg, new IllegalStateException(msg));
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800126 }
127
Santos Cordona0e5f1a2014-03-31 21:43:00 -0700128 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 Nepal91990782014-03-08 17:45:52 -0800156 /**
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 Charlton8873e0a2014-03-19 15:49:41 -0700184 int byteIntValue = bytes[i] & 0xff;
Sailesh Nepal91990782014-03-08 17:45:52 -0800185 if (byteIntValue < 0x10) {
186 hex.append("0");
187 }
188 hex.append(Integer.toString(byteIntValue, 16));
189 }
190
191 return hex.toString();
192 }
193
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800194 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 Cordon4f1b7b82014-05-25 21:03:04 -0700204 e("Log", ife, "IllegalFormatException: formatString='%s' numArgs=%d", format,
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800205 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}