1 /*
2  * Copyright (c) 2015, Motorola Mobility LLC
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *     - Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     - Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *     - Neither the name of Motorola Mobility nor the
13  *       names of its contributors may be used to endorse or promote products
14  *       derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
18  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MOTOROLA MOBILITY LLC BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
26  * DAMAGE.
27  */
28 
29 package com.android.ims.internal;
30 
31 import java.lang.String;
32 import android.util.Log;
33 
34 import android.text.TextUtils;
35 
36 /**
37  * Logger
38  *
39  * @hide
40  */
41 public class Logger {
42 
43     private static boolean VERBOSE = isLoggable(android.util.Log.VERBOSE);
44     private static boolean DEBUG = isLoggable(android.util.Log.DEBUG);
45     private static boolean INFO = isLoggable(android.util.Log.INFO);
46     private static boolean WARN = isLoggable(android.util.Log.WARN);
47     private static boolean ERROR = isLoggable(android.util.Log.ERROR);
48 
49     /**
50      * RCS test mode flag
51      */
52     private static boolean mRcsTestMode = false;
53 
54     /**
55      * Log tag name
56      */
57     private static String TAG = "rcs";
58 
59     /**
60      * Classname
61      */
62     private String mClassName;
63 
64     /**
65      * Constructor
66      *
67      * @param mClassName Classname
68      */
Logger(String tagName, String mClassName)69     private Logger(String tagName, String mClassName) {
70         if(!TextUtils.isEmpty(tagName)) {
71             TAG = tagName;
72         }
73 
74         int index = mClassName.lastIndexOf('.');
75         if (index != -1) {
76             this.mClassName = mClassName.substring(index+1);
77         } else {
78             this.mClassName = mClassName;
79         }
80     }
81 
setRcsTestMode(boolean test)82     public static void setRcsTestMode(boolean test) {
83         mRcsTestMode = test;
84         // Reset log-ability of each mode.
85         DEBUG = isLoggable(android.util.Log.DEBUG);
86         INFO = isLoggable(android.util.Log.INFO);
87         VERBOSE = isLoggable(android.util.Log.VERBOSE);
88         WARN = isLoggable(android.util.Log.WARN);
89         ERROR = isLoggable(android.util.Log.ERROR);
90     }
91 
92     /**
93      * Is logger activated. Reserved for future debug tool to turn on/off the log only.
94      *
95      * @return boolean
96      */
isActivated()97     private boolean isActivated() {
98         return true;
99     }
100 
101     /**
102      * Verbose trace
103      *
104      * @param trace Trace
105      */
verbose(String trace)106     public void verbose(String trace) {
107         if (isActivated() && VERBOSE) {
108             Log.d(TAG, "[" + mClassName +"] " + trace);
109         }
110     }
111 
112     /**
113      * Debug trace
114      *
115      * @param trace Trace
116      */
debug(String trace)117     public void debug(String trace) {
118         if (isActivated() && DEBUG) {
119             Log.d(TAG, "[" + mClassName +"] " + trace);
120         }
121     }
122 
123     /**
124      * Debug trace
125      *
126      * @param trace Trace
127      * @param e the exception which need to be printed.
128      */
debug(String trace, Throwable e)129     public void debug(String trace, Throwable e) {
130         if (isActivated() && DEBUG) {
131             Log.d(TAG, "[" + mClassName +"] " + trace, e);
132         }
133     }
134 
135     /**
136      * Info trace
137      *
138      * @param trace Trace
139      */
info(String trace)140     public void info(String trace) {
141         if (isActivated() && INFO) {
142             Log.i(TAG, "[" + mClassName +"] " + trace);
143         }
144     }
145 
146     /**
147      * Warning trace
148      *
149      * @param trace Trace
150      */
warn(String trace)151     public void warn(String trace) {
152         if (isActivated() && WARN) {
153             Log.w(TAG, "[" + mClassName +"] " + trace);
154         }
155     }
156 
157     /**
158      * Error trace
159      *
160      * @param trace Trace
161      */
error(String trace)162     public void error(String trace) {
163         if (isActivated() && ERROR) {
164             Log.e(TAG, "[" + mClassName +"] " + trace);
165         }
166     }
167 
168     /**
169      * Error trace
170      *
171      * @param trace Trace
172      * @param e Exception
173      */
error(String trace, Throwable e)174     public void error(String trace, Throwable e) {
175         if (isActivated() && ERROR) {
176             Log.e(TAG, "[" + mClassName +"] " + trace, e);
177         }
178     }
179 
180     /*
181      * Print the debug log and don't consider the traceLevel
182      *
183      * @param trace Trace
184      * @param e Exception
185      */
print(String trace)186     public void print(String trace) {
187         Log.i(TAG, "[" + mClassName +"] " + trace);
188     }
189 
190     /**
191      * Print the debug log and don't consider the traceLevel
192      *
193      * @param trace Trace
194      * @param e Exception
195      */
print(String trace, Throwable e)196     public void print(String trace, Throwable e) {
197         Log.i(TAG, "[" + mClassName +"] " + trace, e);
198     }
199 
200     // Hide all numbers except for the last two
hidePhoneNumberPii(String number)201     public static String hidePhoneNumberPii(String number) {
202         if(TextUtils.isEmpty(number) || mRcsTestMode || number.length() <= 2) {
203             return number;
204         }
205         StringBuilder sb = new StringBuilder(number.length());
206         sb.append("...*");
207         sb.append(number.substring(number.length()-2, number.length()));
208         return sb.toString();
209     }
210 
211     /**
212      * Determines if the debug level is currently loggable.
213      */
isLoggable(int level)214     private static boolean isLoggable(int level) {
215         return mRcsTestMode || android.util.Log.isLoggable(TAG, level);
216     }
217 
218     /**
219      * Create a static instance
220      *
221      * @param classname Classname
222      * @return Instance
223      */
getLogger(String tagName, String classname)224     public static synchronized Logger getLogger(String tagName, String classname) {
225         return new Logger(tagName, classname);
226     }
227 
228     /**
229      * Create a static instance
230      *
231      * @param classname Classname
232      * @return Instance
233      */
getLogger(String classname)234     public static synchronized Logger getLogger(String classname) {
235         return new Logger(TAG, classname);
236     }
237 }
238 
239