Home | History | Annotate | Download | only in util
      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 {
     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 {
     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 
    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) {
    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      */
    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      */
    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      */
    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      */
    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      */
    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      */
    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      */
    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      */
    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      */
    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      */
    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      */
    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      */
    255     public static int wtf(String tag, String msg) {
    256         return wtf(LOG_ID_MAIN, tag, msg, null, false);
    257     }
    258 
    259     /**
    260      * Like {@link #wtf(String, String)}, but also writes to the log the full
    261      * call stack.
    262      * @hide
    263      */
    264     public static int wtfStack(String tag, String msg) {
    265         return wtf(LOG_ID_MAIN, tag, msg, null, true);
    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      */
    274     public static int wtf(String tag, Throwable tr) {
    275         return wtf(LOG_ID_MAIN, tag, tr.getMessage(), tr, 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      */
    285     public static int wtf(String tag, String msg, Throwable tr) {
    286         return wtf(LOG_ID_MAIN, tag, msg, tr, false);
    287     }
    288 
    289     static int wtf(int logId, String tag, String msg, Throwable tr, boolean localStack) {
    290         TerribleFailure what = new TerribleFailure(msg, tr);
    291         int bytes = println_native(logId, ASSERT, tag, msg + '\n'
    292                 + getStackTraceString(localStack ? what : tr));
    293         sWtfHandler.onTerribleFailure(tag, what);
    294         return bytes;
    295     }
    296 
    297     /**
    298      * Sets the terrible failure handler, for testing.
    299      *
    300      * @return the old handler
    301      *
    302      * @hide
    303      */
    304     public static TerribleFailureHandler setWtfHandler(TerribleFailureHandler handler) {
    305         if (handler == null) {
    306             throw new NullPointerException("handler == null");
    307         }
    308         TerribleFailureHandler oldHandler = sWtfHandler;
    309         sWtfHandler = handler;
    310         return oldHandler;
    311     }
    312 
    313     /**
    314      * Handy function to get a loggable stack trace from a Throwable
    315      * @param tr An exception to log
    316      */
    317     public static String getStackTraceString(Throwable tr) {
    318         if (tr == null) {
    319             return "";
    320         }
    321 
    322         // This is to reduce the amount of log spew that apps do in the non-error
    323         // condition of the network being unavailable.
    324         Throwable t = tr;
    325         while (t != null) {
    326             if (t instanceof UnknownHostException) {
    327                 return "";
    328             }
    329             t = t.getCause();
    330         }
    331 
    332         StringWriter sw = new StringWriter();
    333         PrintWriter pw = new FastPrintWriter(sw, false, 256);
    334         tr.printStackTrace(pw);
    335         pw.flush();
    336         return sw.toString();
    337     }
    338 
    339     /**
    340      * Low-level logging call.
    341      * @param priority The priority/type of this log message
    342      * @param tag Used to identify the source of a log message.  It usually identifies
    343      *        the class or activity where the log call occurs.
    344      * @param msg The message you would like logged.
    345      * @return The number of bytes written.
    346      */
    347     public static int println(int priority, String tag, String msg) {
    348         return println_native(LOG_ID_MAIN, priority, tag, msg);
    349     }
    350 
    351     /** @hide */ public static final int LOG_ID_MAIN = 0;
    352     /** @hide */ public static final int LOG_ID_RADIO = 1;
    353     /** @hide */ public static final int LOG_ID_EVENTS = 2;
    354     /** @hide */ public static final int LOG_ID_SYSTEM = 3;
    355 
    356     /** @hide */ public static native int println_native(int bufID,
    357             int priority, String tag, String msg);
    358 }
    359