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      */
getNext()32     public LogNode getNext() {
33         return mNext;
34     }
35 
36     /**
37      * Sets the LogNode data will be sent to..
38      */
setNext(LogNode node)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      *
46      * @param priority Log level of the data being logged.  Verbose, Error, etc.
47      * @param tag      Tag for for the log data.  Can be used to organize log statements.
48      * @param msg      The actual message to be logged. The actual message to be logged.
49      * @param tr       If an exception was thrown, this can be sent along for the logging
50      *                 facilities
51      *                 to extract and print useful information.
52      */
53     @Override
println(int priority, String tag, String msg, Throwable tr)54     public void println(int priority, String tag, String msg, Throwable tr) {
55         // There actually are log methods that don't take a msg parameter.  For now,
56         // if that's the case, just convert null to the empty string and move on.
57         String useMsg = msg;
58         if (useMsg == null) {
59             useMsg = "";
60         }
61 
62         // If an exeption was provided, convert that exception to a usable string and attach
63         // it to the end of the msg method.
64         if (tr != null) {
65             msg += "\n" + Log.getStackTraceString(tr);
66         }
67 
68         // This is functionally identical to Log.x(tag, useMsg);
69         // For instance, if priority were Log.VERBOSE, this would be the same as Log.v(tag, useMsg)
70         Log.println(priority, tag, useMsg);
71 
72         // If this isn't the last node in the chain, move things along.
73         if (mNext != null) {
74             mNext.println(priority, tag, msg, tr);
75         }
76     }
77 }
78