Home | History | Annotate | Download | only in logger
      1 /*
      2  * Copyright (C) 2013 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 package com.example.android.common.logger;
     17 
     18 /**
     19  * Helper class for a list (or tree) of LoggerNodes.
     20  *
     21  * <p>When this is set as the head of the list,
     22  * an instance of it can function as a drop-in replacement for {@link android.util.Log}.
     23  * Most of the methods in this class server only to map a method call in Log to its equivalent
     24  * in LogNode.</p>
     25  */
     26 public class Log {
     27 
     28     // Grabbing the native values from Android's native logging facilities,
     29     // to make for easy migration and interop.
     30     public static final int NONE = -1;
     31 
     32     public static final int VERBOSE = android.util.Log.VERBOSE;
     33 
     34     public static final int DEBUG = android.util.Log.DEBUG;
     35 
     36     public static final int INFO = android.util.Log.INFO;
     37 
     38     public static final int WARN = android.util.Log.WARN;
     39 
     40     public static final int ERROR = android.util.Log.ERROR;
     41 
     42     public static final int ASSERT = android.util.Log.ASSERT;
     43 
     44     // Stores the beginning of the LogNode topology.
     45     private static LogNode mLogNode;
     46 
     47     /**
     48      * Returns the next LogNode in the linked list.
     49      */
     50     public static LogNode getLogNode() {
     51         return mLogNode;
     52     }
     53 
     54     /**
     55      * Sets the LogNode data will be sent to.
     56      */
     57     public static void setLogNode(LogNode node) {
     58         mLogNode = node;
     59     }
     60 
     61     /**
     62      * Instructs the LogNode to print the log data provided. Other LogNodes can
     63      * be chained to the end of the LogNode as desired.
     64      *
     65      * @param priority Log level of the data being logged. Verbose, Error, etc.
     66      * @param tag      Tag for for the log data. Can be used to organize log statements.
     67      * @param msg      The actual message to be logged.
     68      * @param tr       If an exception was thrown, this can be sent along for the logging
     69      *                 facilities
     70      *                 to extract and print useful information.
     71      */
     72     public static void println(int priority, String tag, String msg, Throwable tr) {
     73         if (mLogNode != null) {
     74             mLogNode.println(priority, tag, msg, tr);
     75         }
     76     }
     77 
     78     /**
     79      * Instructs the LogNode to print the log data provided. Other LogNodes can
     80      * be chained to the end of the LogNode as desired.
     81      *
     82      * @param priority Log level of the data being logged. Verbose, Error, etc.
     83      * @param tag      Tag for for the log data. Can be used to organize log statements.
     84      * @param msg      The actual message to be logged. The actual message to be logged.
     85      */
     86     public static void println(int priority, String tag, String msg) {
     87         println(priority, tag, msg, null);
     88     }
     89 
     90     /**
     91      * Prints a message at VERBOSE priority.
     92      *
     93      * @param tag Tag for for the log data. Can be used to organize log statements.
     94      * @param msg The actual message to be logged.
     95      * @param tr  If an exception was thrown, this can be sent along for the logging facilities
     96      *            to extract and print useful information.
     97      */
     98     public static void v(String tag, String msg, Throwable tr) {
     99         println(VERBOSE, tag, msg, tr);
    100     }
    101 
    102     /**
    103      * Prints a message at VERBOSE priority.
    104      *
    105      * @param tag Tag for for the log data. Can be used to organize log statements.
    106      * @param msg The actual message to be logged.
    107      */
    108     public static void v(String tag, String msg) {
    109         v(tag, msg, null);
    110     }
    111 
    112 
    113     /**
    114      * Prints a message at DEBUG priority.
    115      *
    116      * @param tag Tag for for the log data. Can be used to organize log statements.
    117      * @param msg The actual message to be logged.
    118      * @param tr  If an exception was thrown, this can be sent along for the logging facilities
    119      *            to extract and print useful information.
    120      */
    121     public static void d(String tag, String msg, Throwable tr) {
    122         println(DEBUG, tag, msg, tr);
    123     }
    124 
    125     /**
    126      * Prints a message at DEBUG priority.
    127      *
    128      * @param tag Tag for for the log data. Can be used to organize log statements.
    129      * @param msg The actual message to be logged.
    130      */
    131     public static void d(String tag, String msg) {
    132         d(tag, msg, null);
    133     }
    134 
    135     /**
    136      * Prints a message at INFO priority.
    137      *
    138      * @param tag Tag for for the log data. Can be used to organize log statements.
    139      * @param msg The actual message to be logged.
    140      * @param tr  If an exception was thrown, this can be sent along for the logging facilities
    141      *            to extract and print useful information.
    142      */
    143     public static void i(String tag, String msg, Throwable tr) {
    144         println(INFO, tag, msg, tr);
    145     }
    146 
    147     /**
    148      * Prints a message at INFO priority.
    149      *
    150      * @param tag Tag for for the log data. Can be used to organize log statements.
    151      * @param msg The actual message to be logged.
    152      */
    153     public static void i(String tag, String msg) {
    154         i(tag, msg, null);
    155     }
    156 
    157     /**
    158      * Prints a message at WARN priority.
    159      *
    160      * @param tag Tag for for the log data. Can be used to organize log statements.
    161      * @param msg The actual message to be logged.
    162      * @param tr  If an exception was thrown, this can be sent along for the logging facilities
    163      *            to extract and print useful information.
    164      */
    165     public static void w(String tag, String msg, Throwable tr) {
    166         println(WARN, tag, msg, tr);
    167     }
    168 
    169     /**
    170      * Prints a message at WARN priority.
    171      *
    172      * @param tag Tag for for the log data. Can be used to organize log statements.
    173      * @param msg The actual message to be logged.
    174      */
    175     public static void w(String tag, String msg) {
    176         w(tag, msg, null);
    177     }
    178 
    179     /**
    180      * Prints a message at WARN priority.
    181      *
    182      * @param tag Tag for for the log data. Can be used to organize log statements.
    183      * @param tr  If an exception was thrown, this can be sent along for the logging facilities
    184      *            to extract and print useful information.
    185      */
    186     public static void w(String tag, Throwable tr) {
    187         w(tag, null, tr);
    188     }
    189 
    190     /**
    191      * Prints a message at ERROR priority.
    192      *
    193      * @param tag Tag for for the log data. Can be used to organize log statements.
    194      * @param msg The actual message to be logged.
    195      * @param tr  If an exception was thrown, this can be sent along for the logging facilities
    196      *            to extract and print useful information.
    197      */
    198     public static void e(String tag, String msg, Throwable tr) {
    199         println(ERROR, tag, msg, tr);
    200     }
    201 
    202     /**
    203      * Prints a message at ERROR priority.
    204      *
    205      * @param tag Tag for for the log data. Can be used to organize log statements.
    206      * @param msg The actual message to be logged.
    207      */
    208     public static void e(String tag, String msg) {
    209         e(tag, msg, null);
    210     }
    211 
    212     /**
    213      * Prints a message at ASSERT priority.
    214      *
    215      * @param tag Tag for for the log data. Can be used to organize log statements.
    216      * @param msg The actual message to be logged.
    217      * @param tr  If an exception was thrown, this can be sent along for the logging facilities
    218      *            to extract and print useful information.
    219      */
    220     public static void wtf(String tag, String msg, Throwable tr) {
    221         println(ASSERT, tag, msg, tr);
    222     }
    223 
    224     /**
    225      * Prints a message at ASSERT priority.
    226      *
    227      * @param tag Tag for for the log data. Can be used to organize log statements.
    228      * @param msg The actual message to be logged.
    229      */
    230     public static void wtf(String tag, String msg) {
    231         wtf(tag, msg, null);
    232     }
    233 
    234     /**
    235      * Prints a message at ASSERT priority.
    236      *
    237      * @param tag Tag for for the log data. Can be used to organize log statements.
    238      * @param tr  If an exception was thrown, this can be sent along for the logging facilities
    239      *            to extract and print useful information.
    240      */
    241     public static void wtf(String tag, Throwable tr) {
    242         wtf(tag, null, tr);
    243     }
    244 }
    245