Home | History | Annotate | Download | only in logger
      1 /*
      2  * Copyright (C) 2012 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 import android.util.Log;
     19 
     20 /**
     21  * Helper class which wraps Android's native Log utility in the Logger interface.  This way
     22  * normal DDMS output can be one of the many targets receiving and outputting logs simultaneously.
     23  */
     24 public class LogWrapper implements LogNode {
     25 
     26     // For piping:  The next node to receive Log data after this one has done its work.
     27     private LogNode mNext;
     28 
     29     /**
     30      * Returns the next LogNode in the linked list.
     31      */
     32     public LogNode getNext() {
     33         return mNext;
     34     }
     35 
     36     /**
     37      * Sets the LogNode data will be sent to..
     38      */
     39     public void setNext(LogNode node) {
     40         mNext = node;
     41     }
     42 
     43     /**
     44      * Prints data out to the console using Android's native log mechanism.
     45      * @param priority Log level of the data being logged.  Verbose, Error, etc.
     46      * @param tag Tag for for the log data.  Can be used to organize log statements.
     47      * @param msg The actual message to be logged. The actual message to be logged.
     48      * @param tr If an exception was thrown, this can be sent along for the logging facilities
     49      *           to extract and print useful information.
     50      */
     51     @Override
     52     public void println(int priority, String tag, String msg, Throwable tr) {
     53         // There actually are log methods that don't take a msg parameter.  For now,
     54         // if that's the case, just convert null to the empty string and move on.
     55         String useMsg = msg;
     56         if (useMsg == null) {
     57             useMsg = "";
     58         }
     59 
     60         // If an exeption was provided, convert that exception to a usable string and attach
     61         // it to the end of the msg method.
     62         if (tr != null) {
     63             msg += "\n" + Log.getStackTraceString(tr);
     64         }
     65 
     66         // This is functionally identical to Log.x(tag, useMsg);
     67         // For instance, if priority were Log.VERBOSE, this would be the same as Log.v(tag, useMsg)
     68         Log.println(priority, tag, useMsg);
     69 
     70         // If this isn't the last node in the chain, move things along.
     71         if (mNext != null) {
     72             mNext.println(priority, tag, msg, tr);
     73         }
     74     }
     75 }
     76