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