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