1 /*
2  * Copyright (C) 2006 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 
17 package android.util;
18 
19 import com.android.internal.os.RuntimeInit;
20 import com.android.internal.util.FastPrintWriter;
21 
22 import java.io.PrintWriter;
23 import java.io.StringWriter;
24 import java.net.UnknownHostException;
25 
26 /**
27  * API for sending log output.
28  *
29  * <p>Generally, use the Log.v() Log.d() Log.i() Log.w() and Log.e()
30  * methods.
31  *
32  * <p>The order in terms of verbosity, from least to most is
33  * ERROR, WARN, INFO, DEBUG, VERBOSE.  Verbose should never be compiled
34  * into an application except during development.  Debug logs are compiled
35  * in but stripped at runtime.  Error, warning and info logs are always kept.
36  *
37  * <p><b>Tip:</b> A good convention is to declare a <code>TAG</code> constant
38  * in your class:
39  *
40  * <pre>private static final String TAG = "MyActivity";</pre>
41  *
42  * and use that in subsequent calls to the log methods.
43  * </p>
44  *
45  * <p><b>Tip:</b> Don't forget that when you make a call like
46  * <pre>Log.v(TAG, "index=" + i);</pre>
47  * that when you're building the string to pass into Log.d, the compiler uses a
48  * StringBuilder and at least three allocations occur: the StringBuilder
49  * itself, the buffer, and the String object.  Realistically, there is also
50  * another buffer allocation and copy, and even more pressure on the gc.
51  * That means that if your log message is filtered out, you might be doing
52  * significant work and incurring significant overhead.
53  */
54 public final class Log {
55 
56     /**
57      * Priority constant for the println method; use Log.v.
58      */
59     public static final int VERBOSE = 2;
60 
61     /**
62      * Priority constant for the println method; use Log.d.
63      */
64     public static final int DEBUG = 3;
65 
66     /**
67      * Priority constant for the println method; use Log.i.
68      */
69     public static final int INFO = 4;
70 
71     /**
72      * Priority constant for the println method; use Log.w.
73      */
74     public static final int WARN = 5;
75 
76     /**
77      * Priority constant for the println method; use Log.e.
78      */
79     public static final int ERROR = 6;
80 
81     /**
82      * Priority constant for the println method.
83      */
84     public static final int ASSERT = 7;
85 
86     /**
87      * Exception class used to capture a stack trace in {@link #wtf}.
88      */
89     private static class TerribleFailure extends Exception {
TerribleFailure(String msg, Throwable cause)90         TerribleFailure(String msg, Throwable cause) { super(msg, cause); }
91     }
92 
93     /**
94      * Interface to handle terrible failures from {@link #wtf}.
95      *
96      * @hide
97      */
98     public interface TerribleFailureHandler {
onTerribleFailure(String tag, TerribleFailure what, boolean system)99         void onTerribleFailure(String tag, TerribleFailure what, boolean system);
100     }
101 
102     private static TerribleFailureHandler sWtfHandler = new TerribleFailureHandler() {
103             public void onTerribleFailure(String tag, TerribleFailure what, boolean system) {
104                 RuntimeInit.wtf(tag, what, system);
105             }
106         };
107 
Log()108     private Log() {
109     }
110 
111     /**
112      * Send a {@link #VERBOSE} log message.
113      * @param tag Used to identify the source of a log message.  It usually identifies
114      *        the class or activity where the log call occurs.
115      * @param msg The message you would like logged.
116      */
v(String tag, String msg)117     public static int v(String tag, String msg) {
118         return println_native(LOG_ID_MAIN, VERBOSE, tag, msg);
119     }
120 
121     /**
122      * Send a {@link #VERBOSE} log message and log the exception.
123      * @param tag Used to identify the source of a log message.  It usually identifies
124      *        the class or activity where the log call occurs.
125      * @param msg The message you would like logged.
126      * @param tr An exception to log
127      */
v(String tag, String msg, Throwable tr)128     public static int v(String tag, String msg, Throwable tr) {
129         return println_native(LOG_ID_MAIN, VERBOSE, tag, msg + '\n' + getStackTraceString(tr));
130     }
131 
132     /**
133      * Send a {@link #DEBUG} log message.
134      * @param tag Used to identify the source of a log message.  It usually identifies
135      *        the class or activity where the log call occurs.
136      * @param msg The message you would like logged.
137      */
d(String tag, String msg)138     public static int d(String tag, String msg) {
139         return println_native(LOG_ID_MAIN, DEBUG, tag, msg);
140     }
141 
142     /**
143      * Send a {@link #DEBUG} log message and log the exception.
144      * @param tag Used to identify the source of a log message.  It usually identifies
145      *        the class or activity where the log call occurs.
146      * @param msg The message you would like logged.
147      * @param tr An exception to log
148      */
d(String tag, String msg, Throwable tr)149     public static int d(String tag, String msg, Throwable tr) {
150         return println_native(LOG_ID_MAIN, DEBUG, tag, msg + '\n' + getStackTraceString(tr));
151     }
152 
153     /**
154      * Send an {@link #INFO} log message.
155      * @param tag Used to identify the source of a log message.  It usually identifies
156      *        the class or activity where the log call occurs.
157      * @param msg The message you would like logged.
158      */
i(String tag, String msg)159     public static int i(String tag, String msg) {
160         return println_native(LOG_ID_MAIN, INFO, tag, msg);
161     }
162 
163     /**
164      * Send a {@link #INFO} log message and log the exception.
165      * @param tag Used to identify the source of a log message.  It usually identifies
166      *        the class or activity where the log call occurs.
167      * @param msg The message you would like logged.
168      * @param tr An exception to log
169      */
i(String tag, String msg, Throwable tr)170     public static int i(String tag, String msg, Throwable tr) {
171         return println_native(LOG_ID_MAIN, INFO, tag, msg + '\n' + getStackTraceString(tr));
172     }
173 
174     /**
175      * Send a {@link #WARN} log message.
176      * @param tag Used to identify the source of a log message.  It usually identifies
177      *        the class or activity where the log call occurs.
178      * @param msg The message you would like logged.
179      */
w(String tag, String msg)180     public static int w(String tag, String msg) {
181         return println_native(LOG_ID_MAIN, WARN, tag, msg);
182     }
183 
184     /**
185      * Send a {@link #WARN} log message and log the exception.
186      * @param tag Used to identify the source of a log message.  It usually identifies
187      *        the class or activity where the log call occurs.
188      * @param msg The message you would like logged.
189      * @param tr An exception to log
190      */
w(String tag, String msg, Throwable tr)191     public static int w(String tag, String msg, Throwable tr) {
192         return println_native(LOG_ID_MAIN, WARN, tag, msg + '\n' + getStackTraceString(tr));
193     }
194 
195     /**
196      * Checks to see whether or not a log for the specified tag is loggable at the specified level.
197      *
198      *  The default level of any tag is set to INFO. This means that any level above and including
199      *  INFO will be logged. Before you make any calls to a logging method you should check to see
200      *  if your tag should be logged. You can change the default level by setting a system property:
201      *      'setprop log.tag.&lt;YOUR_LOG_TAG> &lt;LEVEL>'
202      *  Where level is either VERBOSE, DEBUG, INFO, WARN, ERROR, ASSERT, or SUPPRESS. SUPPRESS will
203      *  turn off all logging for your tag. You can also create a local.prop file that with the
204      *  following in it:
205      *      'log.tag.&lt;YOUR_LOG_TAG>=&lt;LEVEL>'
206      *  and place that in /data/local.prop.
207      *
208      * @param tag The tag to check.
209      * @param level The level to check.
210      * @return Whether or not that this is allowed to be logged.
211      * @throws IllegalArgumentException is thrown if the tag.length() > 23.
212      */
isLoggable(String tag, int level)213     public static native boolean isLoggable(String tag, int level);
214 
215     /*
216      * Send a {@link #WARN} log message and log the exception.
217      * @param tag Used to identify the source of a log message.  It usually identifies
218      *        the class or activity where the log call occurs.
219      * @param tr An exception to log
220      */
w(String tag, Throwable tr)221     public static int w(String tag, Throwable tr) {
222         return println_native(LOG_ID_MAIN, WARN, tag, getStackTraceString(tr));
223     }
224 
225     /**
226      * Send an {@link #ERROR} log message.
227      * @param tag Used to identify the source of a log message.  It usually identifies
228      *        the class or activity where the log call occurs.
229      * @param msg The message you would like logged.
230      */
e(String tag, String msg)231     public static int e(String tag, String msg) {
232         return println_native(LOG_ID_MAIN, ERROR, tag, msg);
233     }
234 
235     /**
236      * Send a {@link #ERROR} log message and log the exception.
237      * @param tag Used to identify the source of a log message.  It usually identifies
238      *        the class or activity where the log call occurs.
239      * @param msg The message you would like logged.
240      * @param tr An exception to log
241      */
e(String tag, String msg, Throwable tr)242     public static int e(String tag, String msg, Throwable tr) {
243         return println_native(LOG_ID_MAIN, ERROR, tag, msg + '\n' + getStackTraceString(tr));
244     }
245 
246     /**
247      * What a Terrible Failure: Report a condition that should never happen.
248      * The error will always be logged at level ASSERT with the call stack.
249      * Depending on system configuration, a report may be added to the
250      * {@link android.os.DropBoxManager} and/or the process may be terminated
251      * immediately with an error dialog.
252      * @param tag Used to identify the source of a log message.
253      * @param msg The message you would like logged.
254      */
wtf(String tag, String msg)255     public static int wtf(String tag, String msg) {
256         return wtf(LOG_ID_MAIN, tag, msg, null, false, false);
257     }
258 
259     /**
260      * Like {@link #wtf(String, String)}, but also writes to the log the full
261      * call stack.
262      * @hide
263      */
wtfStack(String tag, String msg)264     public static int wtfStack(String tag, String msg) {
265         return wtf(LOG_ID_MAIN, tag, msg, null, true, false);
266     }
267 
268     /**
269      * What a Terrible Failure: Report an exception that should never happen.
270      * Similar to {@link #wtf(String, String)}, with an exception to log.
271      * @param tag Used to identify the source of a log message.
272      * @param tr An exception to log.
273      */
wtf(String tag, Throwable tr)274     public static int wtf(String tag, Throwable tr) {
275         return wtf(LOG_ID_MAIN, tag, tr.getMessage(), tr, false, false);
276     }
277 
278     /**
279      * What a Terrible Failure: Report an exception that should never happen.
280      * Similar to {@link #wtf(String, Throwable)}, with a message as well.
281      * @param tag Used to identify the source of a log message.
282      * @param msg The message you would like logged.
283      * @param tr An exception to log.  May be null.
284      */
wtf(String tag, String msg, Throwable tr)285     public static int wtf(String tag, String msg, Throwable tr) {
286         return wtf(LOG_ID_MAIN, tag, msg, tr, false, false);
287     }
288 
wtf(int logId, String tag, String msg, Throwable tr, boolean localStack, boolean system)289     static int wtf(int logId, String tag, String msg, Throwable tr, boolean localStack,
290             boolean system) {
291         TerribleFailure what = new TerribleFailure(msg, tr);
292         int bytes = println_native(logId, ASSERT, tag, msg + '\n'
293                 + getStackTraceString(localStack ? what : tr));
294         sWtfHandler.onTerribleFailure(tag, what, system);
295         return bytes;
296     }
297 
wtfQuiet(int logId, String tag, String msg, boolean system)298     static void wtfQuiet(int logId, String tag, String msg, boolean system) {
299         TerribleFailure what = new TerribleFailure(msg, null);
300         sWtfHandler.onTerribleFailure(tag, what, system);
301     }
302 
303     /**
304      * Sets the terrible failure handler, for testing.
305      *
306      * @return the old handler
307      *
308      * @hide
309      */
setWtfHandler(TerribleFailureHandler handler)310     public static TerribleFailureHandler setWtfHandler(TerribleFailureHandler handler) {
311         if (handler == null) {
312             throw new NullPointerException("handler == null");
313         }
314         TerribleFailureHandler oldHandler = sWtfHandler;
315         sWtfHandler = handler;
316         return oldHandler;
317     }
318 
319     /**
320      * Handy function to get a loggable stack trace from a Throwable
321      * @param tr An exception to log
322      */
getStackTraceString(Throwable tr)323     public static String getStackTraceString(Throwable tr) {
324         if (tr == null) {
325             return "";
326         }
327 
328         // This is to reduce the amount of log spew that apps do in the non-error
329         // condition of the network being unavailable.
330         Throwable t = tr;
331         while (t != null) {
332             if (t instanceof UnknownHostException) {
333                 return "";
334             }
335             t = t.getCause();
336         }
337 
338         StringWriter sw = new StringWriter();
339         PrintWriter pw = new FastPrintWriter(sw, false, 256);
340         tr.printStackTrace(pw);
341         pw.flush();
342         return sw.toString();
343     }
344 
345     /**
346      * Low-level logging call.
347      * @param priority The priority/type of this log message
348      * @param tag Used to identify the source of a log message.  It usually identifies
349      *        the class or activity where the log call occurs.
350      * @param msg The message you would like logged.
351      * @return The number of bytes written.
352      */
println(int priority, String tag, String msg)353     public static int println(int priority, String tag, String msg) {
354         return println_native(LOG_ID_MAIN, priority, tag, msg);
355     }
356 
357     /** @hide */ public static final int LOG_ID_MAIN = 0;
358     /** @hide */ public static final int LOG_ID_RADIO = 1;
359     /** @hide */ public static final int LOG_ID_EVENTS = 2;
360     /** @hide */ public static final int LOG_ID_SYSTEM = 3;
361     /** @hide */ public static final int LOG_ID_CRASH = 4;
362 
println_native(int bufID, int priority, String tag, String msg)363     /** @hide */ public static native int println_native(int bufID,
364             int priority, String tag, String msg);
365 }
366