blob: b1282a17fb232545283dea62ea6f35e18fd9fd8f [file] [log] [blame]
Jean-Baptiste Querud56b88a2012-11-07 07:48:57 -08001/*
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
17package com.android.volley;
18
19import android.os.SystemClock;
20import android.util.Log;
21
22import java.util.ArrayList;
23import java.util.List;
24import java.util.Locale;
25
26/** Logging helper class. */
27public class VolleyLog {
28 public static String TAG = "Volley";
29
30 public static final boolean DEBUG = Log.isLoggable(TAG, Log.VERBOSE);
31
32 public static void v(String format, Object... args) {
33 if (DEBUG) {
34 Log.v(TAG, buildMessage(format, args));
35 }
36 }
37
38 public static void d(String format, Object... args) {
39 Log.d(TAG, buildMessage(format, args));
40 }
41
42 public static void e(String format, Object... args) {
43 Log.e(TAG, buildMessage(format, args));
44 }
45
Evan Charltonc0d371c2012-11-16 15:58:05 -080046 public static void e(Throwable tr, String format, Object... args) {
47 Log.e(TAG, buildMessage(format, args), tr);
48 }
49
Jean-Baptiste Querud56b88a2012-11-07 07:48:57 -080050 public static void wtf(String format, Object... args) {
51 Log.wtf(TAG, buildMessage(format, args));
52 }
53
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 */
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
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. */
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 */
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
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. */
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}