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