• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.os.Build;
35 import android.text.TextUtils;
36 
37 /**
38  * Logger
39  *
40  * @hide
41  */
42 public class Logger {
43     /**
44      * DEBUG level
45      */
46     public static int DEBUG_LEVEL = 0;
47 
48     /**
49      * INFO level
50      */
51     public static int INFO_LEVEL = 1;
52 
53     /**
54      * WARN level
55      */
56     public static int WARN_LEVEL = 2;
57 
58     /**
59      * ERROR level
60      */
61     public static int ERROR_LEVEL = 3;
62 
63     /**
64      * FATAL level
65      */
66     public static int FATAL_LEVEL = 4;
67 
68     /**
69      * RCS test mode flag
70      */
71     public static boolean rcsTestMode = false;
72 
73     /**
74      * Trace level
75      */
76     public static int traceLevel = DEBUG_LEVEL;
77 
78     /**
79      * Log tag name
80      */
81     private static String tagname = "rcs";
82 
83     /**
84      * Classname
85      */
86     private String classname;
87 
88     /**
89      * Constructor
90      *
91      * @param classname Classname
92      */
Logger(String tagName, String classname)93     private Logger(String tagName, String classname) {
94         if(!TextUtils.isEmpty(tagName)) {
95             this.tagname = tagName;
96         }
97 
98         int index = classname.lastIndexOf('.');
99         if (index != -1) {
100             this.classname = classname.substring(index+1);
101         } else {
102             this.classname = classname;
103         }
104 
105         if(Build.IS_DEBUGGABLE || rcsTestMode){
106             traceLevel = DEBUG_LEVEL;
107         }else{
108             traceLevel = ERROR_LEVEL;
109         }
110     }
111 
setRcsTestMode(boolean test)112     public static void setRcsTestMode(boolean test) {
113         rcsTestMode = test;
114 
115         if (Build.IS_DEBUGGABLE || rcsTestMode) {
116             traceLevel = DEBUG_LEVEL;
117         } else {
118             traceLevel = ERROR_LEVEL;
119         }
120     }
121 
122     /**
123      * Is logger activated. Reserved for future debug tool to turn on/off the log only.
124      *
125      * @return boolean
126      */
isActivated()127     public boolean isActivated() {
128         return true;
129     }
130 
131     /**
132      * Debug trace
133      *
134      * @param trace Trace
135      */
debug(String trace)136     public void debug(String trace) {
137         if (isActivated() && (traceLevel <= DEBUG_LEVEL)) {
138             Log.d(tagname, "[" + classname +"] " + trace);
139         }
140     }
141 
142     /**
143      * Debug trace
144      *
145      * @param trace Trace
146      * @param e the exception which need to be printed.
147      */
debug(String trace, Throwable e)148     public void debug(String trace, Throwable e) {
149         if (isActivated() && (traceLevel <= DEBUG_LEVEL)) {
150             Log.d(tagname, "[" + classname +"] " + trace, e);
151         }
152     }
153 
154     /**
155      * Info trace
156      *
157      * @param trace Trace
158      */
info(String trace)159     public void info(String trace) {
160         if (isActivated() && (traceLevel <= INFO_LEVEL)) {
161             Log.i(tagname, "[" + classname +"] " + trace);
162         }
163     }
164 
165     /**
166      * Warning trace
167      *
168      * @param trace Trace
169      */
warn(String trace)170     public void warn(String trace) {
171         if (isActivated() && (traceLevel <= WARN_LEVEL)) {
172             Log.w(tagname, "[" + classname +"] " + trace);
173         }
174     }
175 
176     /**
177      * Error trace
178      *
179      * @param trace Trace
180      */
error(String trace)181     public void error(String trace) {
182         if (isActivated() && (traceLevel <= ERROR_LEVEL)) {
183             Log.e(tagname, "[" + classname +"] " + trace);
184         }
185     }
186 
187     /**
188      * Error trace
189      *
190      * @param trace Trace
191      * @param e Exception
192      */
error(String trace, Throwable e)193     public void error(String trace, Throwable e) {
194         if (isActivated() && (traceLevel <= ERROR_LEVEL)) {
195             Log.e(tagname, "[" + classname +"] " + trace, e);
196         }
197     }
198 
199     /*
200      * Print the debug log and don't consider the traceLevel
201      *
202      * @param trace Trace
203      * @param e Exception
204      */
print(String trace)205     public void print(String trace) {
206         Log.i(tagname, "[" + classname +"] " + trace);
207     }
208 
209     /**
210      * Print the debug log and don't consider the traceLevel
211      *
212      * @param trace Trace
213      * @param e Exception
214      */
print(String trace, Throwable e)215     public void print(String trace, Throwable e) {
216         Log.i(tagname, "[" + classname +"] " + trace, e);
217     }
218 
219     /**
220      * Create a static instance
221      *
222      * @param classname Classname
223      * @return Instance
224      */
getLogger(String tagName, String classname)225     public static synchronized Logger getLogger(String tagName, String classname) {
226         return new Logger(tagName, classname);
227     }
228 
229     /**
230      * Create a static instance
231      *
232      * @param classname Classname
233      * @return Instance
234      */
getLogger(String classname)235     public static synchronized Logger getLogger(String classname) {
236         return new Logger(tagname, classname);
237     }
238 }
239 
240