1 /*
2  * Copyright (C) 2011 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 
17 package com.android.volley;
18 
19 import android.os.SystemClock;
20 import android.util.Log;
21 
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.Locale;
25 
26 /**
27  * Logging helper class.
28  * <p/>
29  * to see Volley logs call:<br/>
30  * {@code <android-sdk>/platform-tools/adb shell setprop log.tag.Volley VERBOSE}
31  */
32 public class VolleyLog {
33     public static String TAG = "Volley";
34 
35     public static boolean DEBUG = Log.isLoggable(TAG, Log.VERBOSE);
36 
37     /**
38      * Customize the log tag for your application, so that other apps
39      * using Volley don't mix their logs with yours.
40      * <br />
41      * Enable the log property for your tag before starting your app:
42      * <br />
43      * {@code adb shell setprop log.tag.&lt;tag&gt;}
44      */
setTag(String tag)45     public static void setTag(String tag) {
46         d("Changing log tag to %s", tag);
47         TAG = tag;
48 
49         // Reinitialize the DEBUG "constant"
50         DEBUG = Log.isLoggable(TAG, Log.VERBOSE);
51     }
52 
v(String format, Object... args)53     public static void v(String format, Object... args) {
54         if (DEBUG) {
55             Log.v(TAG, buildMessage(format, args));
56         }
57     }
58 
d(String format, Object... args)59     public static void d(String format, Object... args) {
60         Log.d(TAG, buildMessage(format, args));
61     }
62 
e(String format, Object... args)63     public static void e(String format, Object... args) {
64         Log.e(TAG, buildMessage(format, args));
65     }
66 
e(Throwable tr, String format, Object... args)67     public static void e(Throwable tr, String format, Object... args) {
68         Log.e(TAG, buildMessage(format, args), tr);
69     }
70 
wtf(String format, Object... args)71     public static void wtf(String format, Object... args) {
72         Log.wtf(TAG, buildMessage(format, args));
73     }
74 
wtf(Throwable tr, String format, Object... args)75     public static void wtf(Throwable tr, String format, Object... args) {
76         Log.wtf(TAG, buildMessage(format, args), tr);
77     }
78 
79     /**
80      * Formats the caller's provided message and prepends useful info like
81      * calling thread ID and method name.
82      */
buildMessage(String format, Object... args)83     private static String buildMessage(String format, Object... args) {
84         String msg = (args == null) ? format : String.format(Locale.US, format, args);
85         StackTraceElement[] trace = new Throwable().fillInStackTrace().getStackTrace();
86 
87         String caller = "<unknown>";
88         // Walk up the stack looking for the first caller outside of VolleyLog.
89         // It will be at least two frames up, so start there.
90         for (int i = 2; i < trace.length; i++) {
91             Class<?> clazz = trace[i].getClass();
92             if (!clazz.equals(VolleyLog.class)) {
93                 String callingClass = trace[i].getClassName();
94                 callingClass = callingClass.substring(callingClass.lastIndexOf('.') + 1);
95                 callingClass = callingClass.substring(callingClass.lastIndexOf('$') + 1);
96 
97                 caller = callingClass + "." + trace[i].getMethodName();
98                 break;
99             }
100         }
101         return String.format(Locale.US, "[%d] %s: %s",
102                 Thread.currentThread().getId(), caller, msg);
103     }
104 
105     /**
106      * A simple event log with records containing a name, thread ID, and timestamp.
107      */
108     static class MarkerLog {
109         public static final boolean ENABLED = VolleyLog.DEBUG;
110 
111         /** Minimum duration from first marker to last in an marker log to warrant logging. */
112         private static final long MIN_DURATION_FOR_LOGGING_MS = 0;
113 
114         private static class Marker {
115             public final String name;
116             public final long thread;
117             public final long time;
118 
Marker(String name, long thread, long time)119             public Marker(String name, long thread, long time) {
120                 this.name = name;
121                 this.thread = thread;
122                 this.time = time;
123             }
124         }
125 
126         private final List<Marker> mMarkers = new ArrayList<Marker>();
127         private boolean mFinished = false;
128 
129         /** Adds a marker to this log with the specified name. */
add(String name, long threadId)130         public synchronized void add(String name, long threadId) {
131             if (mFinished) {
132                 throw new IllegalStateException("Marker added to finished log");
133             }
134 
135             mMarkers.add(new Marker(name, threadId, SystemClock.elapsedRealtime()));
136         }
137 
138         /**
139          * Closes the log, dumping it to logcat if the time difference between
140          * the first and last markers is greater than {@link #MIN_DURATION_FOR_LOGGING_MS}.
141          * @param header Header string to print above the marker log.
142          */
finish(String header)143         public synchronized void finish(String header) {
144             mFinished = true;
145 
146             long duration = getTotalDuration();
147             if (duration <= MIN_DURATION_FOR_LOGGING_MS) {
148                 return;
149             }
150 
151             long prevTime = mMarkers.get(0).time;
152             d("(%-4d ms) %s", duration, header);
153             for (Marker marker : mMarkers) {
154                 long thisTime = marker.time;
155                 d("(+%-4d) [%2d] %s", (thisTime - prevTime), marker.thread, marker.name);
156                 prevTime = thisTime;
157             }
158         }
159 
160         @Override
finalize()161         protected void finalize() throws Throwable {
162             // Catch requests that have been collected (and hence end-of-lifed)
163             // but had no debugging output printed for them.
164             if (!mFinished) {
165                 finish("Request on the loose");
166                 e("Marker log finalized without finish() - uncaught exit point for request");
167             }
168         }
169 
170         /** Returns the time difference between the first and last events in this log. */
getTotalDuration()171         private long getTotalDuration() {
172             if (mMarkers.size() == 0) {
173                 return 0;
174             }
175 
176             long first = mMarkers.get(0).time;
177             long last = mMarkers.get(mMarkers.size() - 1).time;
178             return last - first;
179         }
180     }
181 }
182