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