VolleyLog工具类

这个类对系统Log做了一些封装,支持release不打印log,而且可以VolleyLog.d(“xxxx”);已经一个d% s%格式的log

package com.android.volley;import android.os.SystemClock;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;/*** Logging helper class.** <p>to see Volley logs call:<br>* {@code <android-sdk>/platform-tools/adb shell setprop log.tag.Volley VERBOSE}*/
public class VolleyLog {public static String TAG = "Volley";public static boolean DEBUG = Log.isLoggable(TAG, Log.VERBOSE);/*** {@link Class#getName()} uses reflection and calling it on a potentially hot code path may* have some cost. To minimize this cost we fetch class name once here and use it later.*/private static final String CLASS_NAME = VolleyLog.class.getName();/*** Customize the log tag for your application, so that other apps using Volley don't mix their* logs with yours. <br>* Enable the log property for your tag before starting your app: <br>* {@code adb shell setprop log.tag.&lt;tag&gt;}*/public static void setTag(String tag) {d("Changing log tag to %s", tag);TAG = tag;// Reinitialize the DEBUG "constant"DEBUG = Log.isLoggable(TAG, Log.VERBOSE);}public static void v(String format, Object... args) {if (DEBUG) {Log.v(TAG, buildMessage(format, args));}}public static void d(String format, Object... args) {Log.d(TAG, buildMessage(format, args));}public static void e(String format, Object... args) {Log.e(TAG, buildMessage(format, args));}public static void e(Throwable tr, String format, Object... args) {Log.e(TAG, buildMessage(format, args), tr);}public static void wtf(String format, Object... args) {Log.wtf(TAG, buildMessage(format, args));}public static void wtf(Throwable tr, String format, Object... args) {Log.wtf(TAG, buildMessage(format, args), tr);}/*** Formats the caller's provided message and prepends useful info like calling thread ID and* method name.*/private static String buildMessage(String format, Object... args) {String msg = (args == null) ? format : String.format(Locale.US, format, args);StackTraceElement[] trace = new Throwable().fillInStackTrace().getStackTrace();String caller = "<unknown>";// Walk up the stack looking for the first caller outside of VolleyLog.// It will be at least two frames up, so start there.for (int i = 2; i < trace.length; i++) {String clazz = trace[i].getClassName();if (!clazz.equals(VolleyLog.CLASS_NAME)) {String callingClass = trace[i].getClassName();callingClass = callingClass.substring(callingClass.lastIndexOf('.') + 1);callingClass = callingClass.substring(callingClass.lastIndexOf('$') + 1);caller = callingClass + "." + trace[i].getMethodName();break;}}return String.format(Locale.US, "[%d] %s: %s", Thread.currentThread().getId(), caller, msg);}/** A simple event log with records containing a name, thread ID, and timestamp. */static class MarkerLog {public static final boolean ENABLED = VolleyLog.DEBUG;/** Minimum duration from first marker to last in an marker log to warrant logging. */private static final long MIN_DURATION_FOR_LOGGING_MS = 0;private static class Marker {public final String name;public final long thread;public final long time;public Marker(String name, long thread, long time) {this.name = name;this.thread = thread;this.time = time;}}private final List<Marker> mMarkers = new ArrayList<>();private boolean mFinished = false;/** Adds a marker to this log with the specified name. */public synchronized void add(String name, long threadId) {if (mFinished) {throw new IllegalStateException("Marker added to finished log");}mMarkers.add(new Marker(name, threadId, SystemClock.elapsedRealtime()));}/*** Closes the log, dumping it to logcat if the time difference between the first and last* markers is greater than {@link #MIN_DURATION_FOR_LOGGING_MS}.** @param header Header string to print above the marker log.*/public synchronized void finish(String header) {mFinished = true;long duration = getTotalDuration();if (duration <= MIN_DURATION_FOR_LOGGING_MS) {return;}long prevTime = mMarkers.get(0).time;d("(%-4d ms) %s", duration, header);for (Marker marker : mMarkers) {long thisTime = marker.time;d("(+%-4d) [%2d] %s", (thisTime - prevTime), marker.thread, marker.name);prevTime = thisTime;}}@Overrideprotected void finalize() throws Throwable {// Catch requests that have been collected (and hence end-of-lifed)// but had no debugging output printed for them.if (!mFinished) {finish("Request on the loose");e("Marker log finalized without finish() - uncaught exit point for request");d("");}}/** Returns the time difference between the first and last events in this log. */private long getTotalDuration() {if (mMarkers.size() == 0) {return 0;}long first = mMarkers.get(0).time;long last = mMarkers.get(mMarkers.size() - 1).time;return last - first;}}
}

Volley源码学习3--log类相关推荐

  1. JDK11源码学习05 | HashMap类

    JDK11源码学习05 | HashMap类 JDK11源码学习01 | Map接口 JDK11源码学习02 | AbstractMap抽象类 JDK11源码学习03 | Serializable接口 ...

  2. Volley源码学习2--Error类

    Volley 网络请求错误返回 VolleyError package com.android.volley;/** Exception style class encapsulating Volle ...

  3. Mono源码学习笔记:Console类(四)

    NullStream 类 (internal class) 以下就是 mcs/class/corlib/System.IO/NullStream.cs: 01: namespace System.IO ...

  4. JAVA源码学习(一)——String类

    一.String类的不可变性 源码: public final class String//final修饰类,不可继承 private final char value[];//String类实际返回 ...

  5. Volley源码学习2--cache类

    这是一些cache缓存的基本接口,没有身体特别的地方 package com.android.volley;import java.util.Collections; import java.util ...

  6. Mono源码学习笔记:Console类(三)

    Buffer 类 (public static class) 以下就是 mcs/class/corlib/System/Buffer.cs: 001: // 002: // System.Buffer ...

  7. Volley源码学习1--volley结构图

    volley结构图 从这张图可以了解volley整个工作原理. 1 当客户端发生一个请求的时候 2 会先从缓存中去查找,是不是有缓存 3 如果请求不能从缓存中得到服务,那么它将被放置在网络队列中.第一 ...

  8. 基于Qt5.14.2和mingw的Qt源码学习(三) — 元对象系统简介及moc工具是如何保存类属性和方法的

    基于Qt5.14.2和mingw的Qt源码学习(三) - 元对象系统简介及moc工具是如何保存类属性和方法的 一.什么是元对象系统 1.元对象系统目的 2.实现元对象系统的关键 3.元对象系统的其他一 ...

  9. Volley 源码解析之网络请求

    Volley源码分析三部曲 Volley 源码解析之网络请求 Volley 源码解析之图片请求 Volley 源码解析之缓存机制 Volley 是 Google 推出的一款网络通信框架,非常适合数据量 ...

最新文章

  1. java enter_Java UI.enter方法代码示例
  2. 成功解决TypeError: ‘float’ object cannot be interpreted as an index
  3. python含金量最高的考试_中国最难的五大考试,第二个含金量最高,考过年薪30万起!...
  4. Linux 源码安装 Python3 和 pip3
  5. Java 向量元素的索引值
  6. echarts折现变曲线_echarts将折线图改为曲线图
  7. 济南学习 Day 5 T2 am
  8. ajax存储表单数据,使用ajax json将表单数据存储到数据库php
  9. oracle聚合函数
  10. Java基础面试题大全
  11. gradle7打包libs目录中的jar生成fat jar
  12. Java基础笔试练习(八)
  13. 游戏浅谈1-传奇,跑跑卡丁车
  14. ibm p740做虚拟服务器,POWER740 主机板的拆除与安装图文说明
  15. Django 浏览器报错 MIME 类型(“text/html”)不匹配(X-Content-Type-Options: nosniff)
  16. 对sizeof的一点点理解
  17. 几款支持GB28181的平台
  18. Jmeter书中不会教你的(94)——将时间戳转换为日期格式
  19. PyCharm打包失败及Pyinstall无法安装问题的解决
  20. 【小5聊】情人节送给爱人的心形创意相册

热门文章

  1. c语言转图形化,「分享」C语言如何编写图形界面
  2. linux 如何自定义安装路径,Linux下安装mysql并自定义数据的存储路径
  3. c语音学习-输入一个字母,输出其对应的ASCII码
  4. 选择在何处重构(下)
  5. 阿里云、蚂蚁开源 Nydus——容器镜像加速服务
  6. 使用Angular和ASP.net Core的Raw Websockets迷你游戏
  7. MooTool 1.0.0 发布,开发者常备桌面小工具
  8. sql数据表改为自动递增显示与其他表关联_MySQL萌新第一季 第四话-数据表的基本操作...
  9. 我的世界java版如何装mod_Minecraft如何安装Mod?Minecraft添加Mod的方法
  10. 软件加入使用时间_信考宝典 中考软件简介