VolleyLog-学习Google封装的Log

代码贴出来学习一下

/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

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. */
public class VolleyLog {
    public static String TAG = "Volley";

    public static boolean DEBUG = Log.isLoggable(TAG, Log.VERBOSE);

    /**
     * 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.<tag>}
     */
    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++) {
            Class<?> clazz = trace[i].getClass();
            if (!clazz.equals(VolleyLog.class)) {
                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<Marker>();
        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;
            }
        }

        @Override
        protected 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");
            }
        }

        /** 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;
        }
    }
}
时间: 2024-08-26 03:52:29

VolleyLog-学习Google封装的Log的相关文章

【C语言学习】封装和模块化思想

刚学习完C后,做的关于C的课程设计是在一个源文件中放了几百行代码,而且各个功能之间都是相互依赖的,这样就会很麻烦.因为当我要修改某个地方的时候,就会牵连着要修改喝多的地方.而在实际的程序设计中,这也是不可取的.因此,模块化和封装的思想就显得很重要了!!! ★static变量 static变量的一个显著的作用就是可以实现一个模块的封装. static存储类别的特性决定了static声明的全局变量只能被本源文件的函数引用.当在一个源文件中定义一个static全局变量后,其他文件就不能通过使用"ext

Androd封装一个Log打印工具一键实现打印不打印

在写项目的时候,我们是避免不了用到安卓里面的Log打印工具的,但是当代码越写越多的时候我们这加个Log 那加个Log,当我们项目要上线的时候,我们总会忘记哪有Log,很麻烦啊,当时杀人的心都有了,现在封装一个Log工具,只需要定义boolean就可以一键实现打印和不打印功能,直接复制过去拿到项目里面用就行! 不要感谢我,请叫我雷锋 package com.example.qlog; import java.util.Calendar; public final class QLog { publ

开源中国源码学习(三)——Log日志上传

在AppStart中开启了一个服务LogUploadService用来上传应用程序的日志. 采用的是start的方式开启服务,代码如下: Intent uploadLog = new Intent(this, LogUploadService.class); startService(uploadLog); 一.功能介绍: 在服务LogUploadService被开启后,根据情况进行如下几种操作: 读取osc本地文件夹下的日志信息 如果日志信息为空,服务停止-- LogUploadService

Daydream VR入门基础教程,学习Google VR for Android全景应用示例SimpleVrPanorama制作VR全景应用

前言 前两篇介绍了Daydream Android VR开发环境的搭建和官方VR Demo寻宝游戏的演示,这篇我们来一起研究下示例项目SimpleVrPanorama,同时通过了解它来了解如何开发一款VR全景图形应用. -------------------------------------------------------------------------------------------------------------------- Daydream快速入门开发基础教程一:And

Direct-X学习笔记--封装一个网格模型类

之前学习了网格模型的导入,绘制,了解了X文件等相关知识,但是,那样绘制比较麻烦,而且绘制一个模型需要好多代码,完全是面向过程的思维,这次,学习一下怎么把网格模型的导入以及绘制等功能封装在一个类中.顺便加深一下对World Transform的理解.感觉自己的3D思维还是没有培养起来,想绘制一个对象,绘制出来和想象中的位置相差甚远. 一.复习一下网格模型相关知识 网格模型就是一个我们在美术工具中制作好的资源,通过一些API接口我们可以将美术童鞋做好的模型很方便的导入程序中.我们只需要了解怎样从文件

学习Google Protocol buffer之语法

上一篇结尾的时候问了几个问题,其实主要就是这个protoBuffer协议的语法,弄清楚语法后边才好开展工作嘛,不然大眼而对小眼儿,互相不认识,就没法玩耍了.其实就是学习怎么用google提供的这套 protocol buffer language 来组织我用的数据,那个神奇的proto文件的来龙去脉也得搞搞清楚. 好吧,还是看个例子,也是官方的: message SearchRequest { required string query = 1; optional int32 page_numb

Programming With Objective-C---- Encapsulating Data ---- Objective-C 学习(三) 封装数据

Programming with Objective-C Encapsulating Data In addition to the messaging behavior covered in the previous chapter, an object also encapsulates data through its properties. 除了前一章讲述的消息方法(messaging behavior), 对象还能通过它的特性(properties)来封装数据. This chapte

Linux 0.12和Linux 0.11内核学习——Google邮件列表

亲,你在学习Linux 0.12或0.11内核吗?快来加入我们吧,就缺你了!!! 为什么选用邮件列表呢?因为赵炯博士那个论坛交流不是很方便,经常发了贴没人回,人气相比十年前论坛刚成立时弱了不少.很多人,很多元老级别的人物,消失了...再也没有出现过. 而QQ群很繁杂,比如你肯定会因为一些兴趣爱好加入一些QQ群,但是也就刚加进去或者自己有什么要问的时候说几句,之后就屏蔽了,因为每天都有人在聊天,什么内容都有,想退群却又怕以后有用,不退吧又很烦,只能屏蔽了潜水. 而邮件列表,你可以订阅主题,实时追踪

Netty学习——Google Protobuf的初步了解

学习参考的官网: https://developers.google.com/protocol-buffers/docs/javatutorial 简单指南详解:这个文档写的简直是太详细了. 本篇从下面三个步骤进行介绍: I. Define message formats in a .proto file. II. Use the protocol buffer compiler. III. Use the Java protocol buffer API to write and read m