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