The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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; |
Dianne Hackborn | 8c84109 | 2013-06-24 13:46:13 -0700 | [diff] [blame] | 20 | import com.android.internal.util.FastPrintWriter; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | |
| 22 | import java.io.PrintWriter; |
| 23 | import java.io.StringWriter; |
Joe Onorato | dba50c7 | 2011-05-19 13:28:50 -0700 | [diff] [blame] | 24 | import java.net.UnknownHostException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 86 | /** |
| 87 | * Exception class used to capture a stack trace in {@link #wtf()}. |
| 88 | */ |
| 89 | private static class TerribleFailure extends Exception { |
| 90 | TerribleFailure(String msg, Throwable cause) { super(msg, cause); } |
| 91 | } |
| 92 | |
Brad Fitzpatrick | 44dc76a | 2010-06-02 15:12:05 -0700 | [diff] [blame] | 93 | /** |
| 94 | * Interface to handle terrible failures from {@link #wtf()}. |
| 95 | * |
| 96 | * @hide |
| 97 | */ |
| 98 | public interface TerribleFailureHandler { |
| 99 | void onTerribleFailure(String tag, TerribleFailure what); |
| 100 | } |
| 101 | |
| 102 | private static TerribleFailureHandler sWtfHandler = new TerribleFailureHandler() { |
| 103 | public void onTerribleFailure(String tag, TerribleFailure what) { |
| 104 | RuntimeInit.wtf(tag, what); |
| 105 | } |
| 106 | }; |
| 107 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | */ |
| 117 | public static int v(String tag, String msg) { |
Joe Onorato | 00bb938 | 2010-02-26 18:07:01 -0800 | [diff] [blame] | 118 | return println_native(LOG_ID_MAIN, VERBOSE, tag, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | */ |
| 128 | public static int v(String tag, String msg, Throwable tr) { |
Joe Onorato | 00bb938 | 2010-02-26 18:07:01 -0800 | [diff] [blame] | 129 | return println_native(LOG_ID_MAIN, VERBOSE, tag, msg + '\n' + getStackTraceString(tr)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | */ |
| 138 | public static int d(String tag, String msg) { |
Joe Onorato | 00bb938 | 2010-02-26 18:07:01 -0800 | [diff] [blame] | 139 | return println_native(LOG_ID_MAIN, DEBUG, tag, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | */ |
| 149 | public static int d(String tag, String msg, Throwable tr) { |
Joe Onorato | 00bb938 | 2010-02-26 18:07:01 -0800 | [diff] [blame] | 150 | return println_native(LOG_ID_MAIN, DEBUG, tag, msg + '\n' + getStackTraceString(tr)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | */ |
| 159 | public static int i(String tag, String msg) { |
Joe Onorato | 00bb938 | 2010-02-26 18:07:01 -0800 | [diff] [blame] | 160 | return println_native(LOG_ID_MAIN, INFO, tag, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | */ |
| 170 | public static int i(String tag, String msg, Throwable tr) { |
Joe Onorato | 00bb938 | 2010-02-26 18:07:01 -0800 | [diff] [blame] | 171 | return println_native(LOG_ID_MAIN, INFO, tag, msg + '\n' + getStackTraceString(tr)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | */ |
| 180 | public static int w(String tag, String msg) { |
Joe Onorato | 00bb938 | 2010-02-26 18:07:01 -0800 | [diff] [blame] | 181 | return println_native(LOG_ID_MAIN, WARN, tag, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | */ |
| 191 | public static int w(String tag, String msg, Throwable tr) { |
Joe Onorato | 00bb938 | 2010-02-26 18:07:01 -0800 | [diff] [blame] | 192 | return println_native(LOG_ID_MAIN, WARN, tag, msg + '\n' + getStackTraceString(tr)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Checks to see whether or not a log for the specified tag is loggable at the specified level. |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 197 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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.<YOUR_LOG_TAG> <LEVEL>' |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 202 | * Where level is either VERBOSE, DEBUG, INFO, WARN, ERROR, ASSERT, or SUPPRESS. SUPPRESS will |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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.<YOUR_LOG_TAG>=<LEVEL>' |
| 206 | * and place that in /data/local.prop. |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 207 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | */ |
| 213 | public static native boolean isLoggable(String tag, int level); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 214 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | */ |
| 221 | public static int w(String tag, Throwable tr) { |
Joe Onorato | 00bb938 | 2010-02-26 18:07:01 -0800 | [diff] [blame] | 222 | return println_native(LOG_ID_MAIN, WARN, tag, getStackTraceString(tr)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | */ |
| 231 | public static int e(String tag, String msg) { |
Joe Onorato | 00bb938 | 2010-02-26 18:07:01 -0800 | [diff] [blame] | 232 | return println_native(LOG_ID_MAIN, ERROR, tag, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | */ |
| 242 | public static int e(String tag, String msg, Throwable tr) { |
Joe Onorato | 00bb938 | 2010-02-26 18:07:01 -0800 | [diff] [blame] | 243 | return println_native(LOG_ID_MAIN, ERROR, tag, msg + '\n' + getStackTraceString(tr)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | /** |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 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. |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 254 | */ |
| 255 | public static int wtf(String tag, String msg) { |
| 256 | return wtf(tag, msg, null); |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * What a Terrible Failure: Report an exception that should never happen. |
| 261 | * Similar to {@link #wtf(String, String)}, with an exception to log. |
| 262 | * @param tag Used to identify the source of a log message. |
| 263 | * @param tr An exception to log. |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 264 | */ |
| 265 | public static int wtf(String tag, Throwable tr) { |
| 266 | return wtf(tag, tr.getMessage(), tr); |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * What a Terrible Failure: Report an exception that should never happen. |
| 271 | * Similar to {@link #wtf(String, Throwable)}, with a message as well. |
| 272 | * @param tag Used to identify the source of a log message. |
| 273 | * @param msg The message you would like logged. |
| 274 | * @param tr An exception to log. May be null. |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 275 | */ |
| 276 | public static int wtf(String tag, String msg, Throwable tr) { |
Brad Fitzpatrick | 44dc76a | 2010-06-02 15:12:05 -0700 | [diff] [blame] | 277 | TerribleFailure what = new TerribleFailure(msg, tr); |
Brad Fitzpatrick | 26d8fa9 | 2010-12-15 13:02:09 -0800 | [diff] [blame] | 278 | int bytes = println_native(LOG_ID_MAIN, ASSERT, tag, msg + '\n' + getStackTraceString(tr)); |
Brad Fitzpatrick | 44dc76a | 2010-06-02 15:12:05 -0700 | [diff] [blame] | 279 | sWtfHandler.onTerribleFailure(tag, what); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 280 | return bytes; |
| 281 | } |
| 282 | |
| 283 | /** |
Brad Fitzpatrick | 44dc76a | 2010-06-02 15:12:05 -0700 | [diff] [blame] | 284 | * Sets the terrible failure handler, for testing. |
| 285 | * |
| 286 | * @return the old handler |
| 287 | * |
| 288 | * @hide |
| 289 | */ |
| 290 | public static TerribleFailureHandler setWtfHandler(TerribleFailureHandler handler) { |
| 291 | if (handler == null) { |
| 292 | throw new NullPointerException("handler == null"); |
| 293 | } |
| 294 | TerribleFailureHandler oldHandler = sWtfHandler; |
| 295 | sWtfHandler = handler; |
| 296 | return oldHandler; |
| 297 | } |
| 298 | |
| 299 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 300 | * Handy function to get a loggable stack trace from a Throwable |
| 301 | * @param tr An exception to log |
| 302 | */ |
| 303 | public static String getStackTraceString(Throwable tr) { |
| 304 | if (tr == null) { |
| 305 | return ""; |
| 306 | } |
Joe Onorato | dba50c7 | 2011-05-19 13:28:50 -0700 | [diff] [blame] | 307 | |
| 308 | // This is to reduce the amount of log spew that apps do in the non-error |
| 309 | // condition of the network being unavailable. |
| 310 | Throwable t = tr; |
| 311 | while (t != null) { |
| 312 | if (t instanceof UnknownHostException) { |
| 313 | return ""; |
| 314 | } |
| 315 | t = t.getCause(); |
| 316 | } |
| 317 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 318 | StringWriter sw = new StringWriter(); |
Dianne Hackborn | 8c84109 | 2013-06-24 13:46:13 -0700 | [diff] [blame] | 319 | PrintWriter pw = new FastPrintWriter(sw, false, 256); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 320 | tr.printStackTrace(pw); |
Dianne Hackborn | 8c84109 | 2013-06-24 13:46:13 -0700 | [diff] [blame] | 321 | pw.flush(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 322 | return sw.toString(); |
| 323 | } |
| 324 | |
| 325 | /** |
| 326 | * Low-level logging call. |
| 327 | * @param priority The priority/type of this log message |
| 328 | * @param tag Used to identify the source of a log message. It usually identifies |
| 329 | * the class or activity where the log call occurs. |
| 330 | * @param msg The message you would like logged. |
| 331 | * @return The number of bytes written. |
| 332 | */ |
Joe Onorato | 00bb938 | 2010-02-26 18:07:01 -0800 | [diff] [blame] | 333 | public static int println(int priority, String tag, String msg) { |
| 334 | return println_native(LOG_ID_MAIN, priority, tag, msg); |
| 335 | } |
| 336 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 337 | /** @hide */ public static final int LOG_ID_MAIN = 0; |
| 338 | /** @hide */ public static final int LOG_ID_RADIO = 1; |
| 339 | /** @hide */ public static final int LOG_ID_EVENTS = 2; |
| 340 | /** @hide */ public static final int LOG_ID_SYSTEM = 3; |
Joe Onorato | 00bb938 | 2010-02-26 18:07:01 -0800 | [diff] [blame] | 341 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 342 | /** @hide */ public static native int println_native(int bufID, |
| 343 | int priority, String tag, String msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 344 | } |