Log.java revision 1e01d16982e6b22ec4c0e2d6dc1e261eb6f92c8e
1ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com/*
2ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright (C) 2006 The Android Open Source Project
3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com *
4ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Licensed under the Apache License, Version 2.0 (the "License");
5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * you may not use this file except in compliance with the License.
6ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * You may obtain a copy of the License at
70397e9f3415b4646797f1b17e9a38b5deb864ff0reed *
88a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com *      http://www.apache.org/licenses/LICENSE-2.0
98a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com *
108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * Unless required by applicable law or agreed to in writing, software
110397e9f3415b4646797f1b17e9a38b5deb864ff0reed * distributed under the License is distributed on an "AS IS" BASIS,
128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * See the License for the specific language governing permissions and
148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * limitations under the License.
158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */
168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
17658650c5b42653f5f7d9857252b764333331be58tfarina@chromium.orgpackage android.util;
18d3aed39ab1ad88b05b9423ee1329c227d1f8f612Scroggo
19d3aed39ab1ad88b05b9423ee1329c227d1f8f612Scroggoimport com.android.internal.os.RuntimeInit;
208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comimport com.android.internal.util.FastPrintWriter;
218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
22e24ad23ae67ffcb0dc545b7e426cf08d102e0868commit-bot@chromium.orgimport java.io.PrintWriter;
238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comimport java.io.StringWriter;
2428fcae2ec77eb16a79e155f8d788b20457f1c951commit-bot@chromium.orgimport java.net.UnknownHostException;
258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
26f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com/**
27f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com * API for sending log output.
288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com *
298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * <p>Generally, use the Log.v() Log.d() Log.i() Log.w() and Log.e()
30658650c5b42653f5f7d9857252b764333331be58tfarina@chromium.org * methods.
31d3aed39ab1ad88b05b9423ee1329c227d1f8f612Scroggo *
32d3aed39ab1ad88b05b9423ee1329c227d1f8f612Scroggo * <p>The order in terms of verbosity, from least to most is
338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * ERROR, WARN, INFO, DEBUG, VERBOSE.  Verbose should never be compiled
348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * into an application except during development.  Debug logs are compiled
350397e9f3415b4646797f1b17e9a38b5deb864ff0reed * in but stripped at runtime.  Error, warning and info logs are always kept.
360397e9f3415b4646797f1b17e9a38b5deb864ff0reed *
370397e9f3415b4646797f1b17e9a38b5deb864ff0reed * <p><b>Tip:</b> A good convention is to declare a <code>TAG</code> constant
385957f47e8de0ff55b8c0bf84984d4915c7c72825reed@google.com * in your class:
395957f47e8de0ff55b8c0bf84984d4915c7c72825reed@google.com *
40f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com * <pre>private static final String TAG = "MyActivity";</pre>
41f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com *
42f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com * and use that in subsequent calls to the log methods.
43f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com * </p>
44f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com *
45f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com * <p><b>Tip:</b> Don't forget that when you make a call like
46f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com * <pre>Log.v(TAG, "index=" + i);</pre>
47f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com * that when you're building the string to pass into Log.d, the compiler uses a
48f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com * StringBuilder and at least three allocations occur: the StringBuilder
49f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com * itself, the buffer, and the String object.  Realistically, there is also
50f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com * another buffer allocation and copy, and even more pressure on the gc.
51f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com * That means that if your log message is filtered out, you might be doing
52f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com * significant work and incurring significant overhead.
53f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com */
54f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.compublic final class Log {
55f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com
56f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    /**
57f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com     * Priority constant for the println method; use Log.v.
58f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com     */
59e24ad23ae67ffcb0dc545b7e426cf08d102e0868commit-bot@chromium.org    public static final int VERBOSE = 2;
60e24ad23ae67ffcb0dc545b7e426cf08d102e0868commit-bot@chromium.org
618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /**
628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     * Priority constant for the println method; use Log.d.
63e24ad23ae67ffcb0dc545b7e426cf08d102e0868commit-bot@chromium.org     */
64e24ad23ae67ffcb0dc545b7e426cf08d102e0868commit-bot@chromium.org    public static final int DEBUG = 3;
65e24ad23ae67ffcb0dc545b7e426cf08d102e0868commit-bot@chromium.org
66d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    /**
67e24ad23ae67ffcb0dc545b7e426cf08d102e0868commit-bot@chromium.org     * Priority constant for the println method; use Log.i.
68e24ad23ae67ffcb0dc545b7e426cf08d102e0868commit-bot@chromium.org     */
69e24ad23ae67ffcb0dc545b7e426cf08d102e0868commit-bot@chromium.org    public static final int INFO = 4;
70e24ad23ae67ffcb0dc545b7e426cf08d102e0868commit-bot@chromium.org
718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /**
72d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     * Priority constant for the println method; use Log.w.
73d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     */
74d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    public static final int WARN = 5;
758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /**
77658650c5b42653f5f7d9857252b764333331be58tfarina@chromium.org     * Priority constant for the println method; use Log.e.
78658650c5b42653f5f7d9857252b764333331be58tfarina@chromium.org     */
798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    public static final int ERROR = 6;
80f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com
81f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    /**
82f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com     * Priority constant for the println method.
83f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com     */
84f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    public static final int ASSERT = 7;
85f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com
86f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    /**
87f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com     * Exception class used to capture a stack trace in {@link #wtf}.
88f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com     */
89f9bb7a8e2052d21e6a7f48d5b73d2ef97637a09ereed@google.com    private static class TerribleFailure extends Exception {
90e1ca705cac4b946993f6cbf798e2a0ba27e739f3reed@google.com        TerribleFailure(String msg, Throwable cause) { super(msg, cause); }
91e1ca705cac4b946993f6cbf798e2a0ba27e739f3reed@google.com    }
92f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com
93d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    /**
948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     * Interface to handle terrible failures from {@link #wtf}.
95d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     *
96d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     * @hide
978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     */
988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    public interface TerribleFailureHandler {
99f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        void onTerribleFailure(String tag, TerribleFailure what, boolean system);
100261b8e2ca1cf22303ad95267f0bdc6e87e1bbe70reed@google.com    }
101e1ca705cac4b946993f6cbf798e2a0ba27e739f3reed@google.com
102e1ca705cac4b946993f6cbf798e2a0ba27e739f3reed@google.com    private static TerribleFailureHandler sWtfHandler = new TerribleFailureHandler() {
103f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com            public void onTerribleFailure(String tag, TerribleFailure what, boolean system) {
104f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                RuntimeInit.wtf(tag, what, system);
1058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            }
106d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        };
107d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com
108d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    private Log() {
1098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
1108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /**
1128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     * Send a {@link #VERBOSE} log message.
1138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     * @param tag Used to identify the source of a log message.  It usually identifies
1148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     *        the class or activity where the log call occurs.
115658650c5b42653f5f7d9857252b764333331be58tfarina@chromium.org     * @param msg The message you would like logged.
116658650c5b42653f5f7d9857252b764333331be58tfarina@chromium.org     */
117d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    public static int v(String tag, String msg) {
1188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return println_native(LOG_ID_MAIN, VERBOSE, tag, msg);
1198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
120d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com
121d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    /**
1228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     * Send a {@link #VERBOSE} log message and log the exception.
123d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     * @param tag Used to identify the source of a log message.  It usually identifies
124d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     *        the class or activity where the log call occurs.
125d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     * @param msg The message you would like logged.
1268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     * @param tr An exception to log
127d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     */
1288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    public static int v(String tag, String msg, Throwable tr) {
1298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return println_native(LOG_ID_MAIN, VERBOSE, tag, msg + '\n' + getStackTraceString(tr));
1300397e9f3415b4646797f1b17e9a38b5deb864ff0reed    }
1310397e9f3415b4646797f1b17e9a38b5deb864ff0reed
132ce9dce0d8116cbbcbebdc1ad8329059c35baf4c0robertphillips@google.com    /**
133ce9dce0d8116cbbcbebdc1ad8329059c35baf4c0robertphillips@google.com     * Send a {@link #DEBUG} log message.
134d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     * @param tag Used to identify the source of a log message.  It usually identifies
135d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     *        the class or activity where the log call occurs.
1368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     * @param msg The message you would like logged.
137ce9dce0d8116cbbcbebdc1ad8329059c35baf4c0robertphillips@google.com     */
138ce9dce0d8116cbbcbebdc1ad8329059c35baf4c0robertphillips@google.com    public static int d(String tag, String msg) {
139f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        return println_native(LOG_ID_MAIN, DEBUG, tag, msg);
140d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    }
141d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com
142d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    /**
1438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     * Send a {@link #DEBUG} log message and log the exception.
1448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     * @param tag Used to identify the source of a log message.  It usually identifies
145d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     *        the class or activity where the log call occurs.
1468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     * @param msg The message you would like logged.
1478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     * @param tr An exception to log
148d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     */
149ce9dce0d8116cbbcbebdc1ad8329059c35baf4c0robertphillips@google.com    public static int d(String tag, String msg, Throwable tr) {
150d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        return println_native(LOG_ID_MAIN, DEBUG, tag, msg + '\n' + getStackTraceString(tr));
151d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    }
152d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com
1538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /**
154ce9dce0d8116cbbcbebdc1ad8329059c35baf4c0robertphillips@google.com     * Send an {@link #INFO} log message.
1558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     * @param tag Used to identify the source of a log message.  It usually identifies
1568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     *        the class or activity where the log call occurs.
157d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     * @param msg The message you would like logged.
1588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     */
1598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    public static int i(String tag, String msg) {
1608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return println_native(LOG_ID_MAIN, INFO, tag, msg);
161d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    }
1628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /**
164d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     * Send a {@link #INFO} log message and log the exception.
165d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     * @param tag Used to identify the source of a log message.  It usually identifies
166d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     *        the class or activity where the log call occurs.
1678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     * @param msg The message you would like logged.
1688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     * @param tr An exception to log
169658650c5b42653f5f7d9857252b764333331be58tfarina@chromium.org     */
170d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    public static int i(String tag, String msg, Throwable tr) {
171d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        return println_native(LOG_ID_MAIN, INFO, tag, msg + '\n' + getStackTraceString(tr));
1728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
173d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com
174d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    /**
175d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     * Send a {@link #WARN} log message.
1768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     * @param tag Used to identify the source of a log message.  It usually identifies
177d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     *        the class or activity where the log call occurs.
178d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     * @param msg The message you would like logged.
179d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     */
1808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    public static int w(String tag, String msg) {
1818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return println_native(LOG_ID_MAIN, WARN, tag, msg);
182658650c5b42653f5f7d9857252b764333331be58tfarina@chromium.org    }
183d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com
184d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    /**
185d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     * Send a {@link #WARN} log message and log the exception.
186d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     * @param tag Used to identify the source of a log message.  It usually identifies
187d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     *        the class or activity where the log call occurs.
188d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     * @param msg The message you would like logged.
189d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     * @param tr An exception to log
190d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     */
191d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    public static int w(String tag, String msg, Throwable tr) {
192d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        return println_native(LOG_ID_MAIN, WARN, tag, msg + '\n' + getStackTraceString(tr));
193d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    }
194d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com
195d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    /**
196d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     * Checks to see whether or not a log for the specified tag is loggable at the specified level.
197d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     *
198d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     *  The default level of any tag is set to INFO. This means that any level above and including
199d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     *  INFO will be logged. Before you make any calls to a logging method you should check to see
200d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     *  if your tag should be logged. You can change the default level by setting a system property:
201658650c5b42653f5f7d9857252b764333331be58tfarina@chromium.org     *      'setprop log.tag.&lt;YOUR_LOG_TAG> &lt;LEVEL>'
202d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     *  Where level is either VERBOSE, DEBUG, INFO, WARN, ERROR, ASSERT, or SUPPRESS. SUPPRESS will
203d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     *  turn off all logging for your tag. You can also create a local.prop file that with the
204d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     *  following in it:
205d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     *      'log.tag.&lt;YOUR_LOG_TAG>=&lt;LEVEL>'
206d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     *  and place that in /data/local.prop.
2078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     *
2088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     * @param tag The tag to check.
209658650c5b42653f5f7d9857252b764333331be58tfarina@chromium.org     * @param level The level to check.
2108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     * @return Whether or not that this is allowed to be logged.
2118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     * @throws IllegalArgumentException is thrown if the tag.length() > 23.
212d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     */
2138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    public static native boolean isLoggable(String tag, int level);
2148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
215d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    /*
2168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     * Send a {@link #WARN} log message and log the exception.
2178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     * @param tag Used to identify the source of a log message.  It usually identifies
2188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     *        the class or activity where the log call occurs.
2198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     * @param tr An exception to log
2208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     */
221d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    public static int w(String tag, Throwable tr) {
2228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return println_native(LOG_ID_MAIN, WARN, tag, getStackTraceString(tr));
2238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
2248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /**
2268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     * Send an {@link #ERROR} log message.
2278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     * @param tag Used to identify the source of a log message.  It usually identifies
2288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     *        the class or activity where the log call occurs.
2298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     * @param msg The message you would like logged.
2308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     */
231654d72f94c855ceae34db0ee82d92844835c0d29yangsu@google.com    public static int e(String tag, String msg) {
232d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        return println_native(LOG_ID_MAIN, ERROR, tag, msg);
233d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    }
2348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /**
2360ae6b245f2b79bc04f0801b08fcf05abcf98fd6creed@android.com     * Send a {@link #ERROR} log message and log the exception.
2370ae6b245f2b79bc04f0801b08fcf05abcf98fd6creed@android.com     * @param tag Used to identify the source of a log message.  It usually identifies
2388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     *        the class or activity where the log call occurs.
2390ae6b245f2b79bc04f0801b08fcf05abcf98fd6creed@android.com     * @param msg The message you would like logged.
2400ae6b245f2b79bc04f0801b08fcf05abcf98fd6creed@android.com     * @param tr An exception to log
2418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     */
2428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    public static int e(String tag, String msg, Throwable tr) {
2438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return println_native(LOG_ID_MAIN, ERROR, tag, msg + '\n' + getStackTraceString(tr));
244658650c5b42653f5f7d9857252b764333331be58tfarina@chromium.org    }
245658650c5b42653f5f7d9857252b764333331be58tfarina@chromium.org
246658650c5b42653f5f7d9857252b764333331be58tfarina@chromium.org    /**
247d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     * What a Terrible Failure: Report a condition that should never happen.
248d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     * The error will always be logged at level ASSERT with the call stack.
249d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     * Depending on system configuration, a report may be added to the
250d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     * {@link android.os.DropBoxManager} and/or the process may be terminated
251d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     * immediately with an error dialog.
2528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     * @param tag Used to identify the source of a log message.
2538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     * @param msg The message you would like logged.
254658650c5b42653f5f7d9857252b764333331be58tfarina@chromium.org     */
255d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    public static int wtf(String tag, String msg) {
256d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        return wtf(LOG_ID_MAIN, tag, msg, null, false, false);
257d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    }
2588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /**
260658650c5b42653f5f7d9857252b764333331be58tfarina@chromium.org     * Like {@link #wtf(String, String)}, but also writes to the log the full
261658650c5b42653f5f7d9857252b764333331be58tfarina@chromium.org     * call stack.
262d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     * @hide
263d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     */
264d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    public static int wtfStack(String tag, String msg) {
265d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        return wtf(LOG_ID_MAIN, tag, msg, null, true, false);
266d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    }
267d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com
268d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    /**
2698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     * What a Terrible Failure: Report an exception that should never happen.
2708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     * Similar to {@link #wtf(String, String)}, with an exception to log.
271658650c5b42653f5f7d9857252b764333331be58tfarina@chromium.org     * @param tag Used to identify the source of a log message.
2728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     * @param tr An exception to log.
2738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     */
274658650c5b42653f5f7d9857252b764333331be58tfarina@chromium.org    public static int wtf(String tag, Throwable tr) {
275d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        return wtf(LOG_ID_MAIN, tag, tr.getMessage(), tr, false, false);
2768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
2778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
278658650c5b42653f5f7d9857252b764333331be58tfarina@chromium.org    /**
279d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     * What a Terrible Failure: Report an exception that should never happen.
2808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     * Similar to {@link #wtf(String, Throwable)}, with a message as well.
2818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     * @param tag Used to identify the source of a log message.
282658650c5b42653f5f7d9857252b764333331be58tfarina@chromium.org     * @param msg The message you would like logged.
2838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     * @param tr An exception to log.  May be null.
2848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com     */
2858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    public static int wtf(String tag, String msg, Throwable tr) {
2864d5c26de0a24f86c37c1da8b0e30d11a550ea67breed@google.com        return wtf(LOG_ID_MAIN, tag, msg, tr, false, false);
2874d5c26de0a24f86c37c1da8b0e30d11a550ea67breed@google.com    }
2884d5c26de0a24f86c37c1da8b0e30d11a550ea67breed@google.com
289dd0cd34067d103ace7c6739405cb7885d8ad3fb2mike@reedtribe.org    static int wtf(int logId, String tag, String msg, Throwable tr, boolean localStack,
290dd0cd34067d103ace7c6739405cb7885d8ad3fb2mike@reedtribe.org            boolean system) {
291d3aed39ab1ad88b05b9423ee1329c227d1f8f612Scroggo        TerribleFailure what = new TerribleFailure(msg, tr);
2924d5c26de0a24f86c37c1da8b0e30d11a550ea67breed@google.com        int bytes = println_native(logId, ASSERT, tag, msg + '\n'
293d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com                + getStackTraceString(localStack ? what : tr));
2948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        sWtfHandler.onTerribleFailure(tag, what, system);
295d3aed39ab1ad88b05b9423ee1329c227d1f8f612Scroggo        return bytes;
296d3aed39ab1ad88b05b9423ee1329c227d1f8f612Scroggo    }
297d3aed39ab1ad88b05b9423ee1329c227d1f8f612Scroggo
298d3aed39ab1ad88b05b9423ee1329c227d1f8f612Scroggo    static void wtfQuiet(int logId, String tag, String msg, boolean system) {
299d3aed39ab1ad88b05b9423ee1329c227d1f8f612Scroggo        TerribleFailure what = new TerribleFailure(msg, null);
300d3aed39ab1ad88b05b9423ee1329c227d1f8f612Scroggo        sWtfHandler.onTerribleFailure(tag, what, system);
301d3aed39ab1ad88b05b9423ee1329c227d1f8f612Scroggo    }
302d3aed39ab1ad88b05b9423ee1329c227d1f8f612Scroggo
303d3aed39ab1ad88b05b9423ee1329c227d1f8f612Scroggo    /**
304d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com     * Sets the terrible failure handler, for testing.
305d3aed39ab1ad88b05b9423ee1329c227d1f8f612Scroggo     *
306d3aed39ab1ad88b05b9423ee1329c227d1f8f612Scroggo     * @return the old handler
307d3aed39ab1ad88b05b9423ee1329c227d1f8f612Scroggo     *
308d3aed39ab1ad88b05b9423ee1329c227d1f8f612Scroggo     * @hide
309d3aed39ab1ad88b05b9423ee1329c227d1f8f612Scroggo     */
310d3aed39ab1ad88b05b9423ee1329c227d1f8f612Scroggo    public static TerribleFailureHandler setWtfHandler(TerribleFailureHandler handler) {
3114d5c26de0a24f86c37c1da8b0e30d11a550ea67breed@google.com        if (handler == null) {
312d3aed39ab1ad88b05b9423ee1329c227d1f8f612Scroggo            throw new NullPointerException("handler == null");
313d3aed39ab1ad88b05b9423ee1329c227d1f8f612Scroggo        }
314d3aed39ab1ad88b05b9423ee1329c227d1f8f612Scroggo        TerribleFailureHandler oldHandler = sWtfHandler;
315d3aed39ab1ad88b05b9423ee1329c227d1f8f612Scroggo        sWtfHandler = handler;
3164d5c26de0a24f86c37c1da8b0e30d11a550ea67breed@google.com        return oldHandler;
317d3aed39ab1ad88b05b9423ee1329c227d1f8f612Scroggo    }
318d3aed39ab1ad88b05b9423ee1329c227d1f8f612Scroggo
319d3aed39ab1ad88b05b9423ee1329c227d1f8f612Scroggo    /**
320d3aed39ab1ad88b05b9423ee1329c227d1f8f612Scroggo     * Handy function to get a loggable stack trace from a Throwable
321d3aed39ab1ad88b05b9423ee1329c227d1f8f612Scroggo     * @param tr An exception to log
322d3aed39ab1ad88b05b9423ee1329c227d1f8f612Scroggo     */
3234d5c26de0a24f86c37c1da8b0e30d11a550ea67breed@google.com    public static String getStackTraceString(Throwable tr) {
324d3aed39ab1ad88b05b9423ee1329c227d1f8f612Scroggo        if (tr == null) {
325d3aed39ab1ad88b05b9423ee1329c227d1f8f612Scroggo            return "";
326d3aed39ab1ad88b05b9423ee1329c227d1f8f612Scroggo        }
327d3aed39ab1ad88b05b9423ee1329c227d1f8f612Scroggo
328d3aed39ab1ad88b05b9423ee1329c227d1f8f612Scroggo        // This is to reduce the amount of log spew that apps do in the non-error
3294d5c26de0a24f86c37c1da8b0e30d11a550ea67breed@google.com        // condition of the network being unavailable.
330d3aed39ab1ad88b05b9423ee1329c227d1f8f612Scroggo        Throwable t = tr;
331d3aed39ab1ad88b05b9423ee1329c227d1f8f612Scroggo        while (t != null) {
332d3aed39ab1ad88b05b9423ee1329c227d1f8f612Scroggo            if (t instanceof UnknownHostException) {
333d3aed39ab1ad88b05b9423ee1329c227d1f8f612Scroggo                return "";
334d3aed39ab1ad88b05b9423ee1329c227d1f8f612Scroggo            }
335d3aed39ab1ad88b05b9423ee1329c227d1f8f612Scroggo            t = t.getCause();
336d3aed39ab1ad88b05b9423ee1329c227d1f8f612Scroggo        }
337d3aed39ab1ad88b05b9423ee1329c227d1f8f612Scroggo
338d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        StringWriter sw = new StringWriter();
339d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        PrintWriter pw = new FastPrintWriter(sw, false, 256);
3408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        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     */
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
363    /** @hide */ public static native int println_native(int bufID,
364            int priority, String tag, String msg);
365}
366