码迷,mamicode.com
首页 > 其他好文 > 详细

*Dlog类实现

时间:2016-09-18 11:47:40      阅读:108      评论:0      收藏:0      [点我收藏+]

标签:

import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.UnknownHostException;

import android.util.Log;

public class Dlog
{

    /**
     * Log 级别,设置为某个级别后,低于该级别的log将不会输出
     */
    public static final int UNKNOWN = 0;
    public static final int DEFAULT = 1;
    public static final int VERBOSE = 2;
    public static final int DEBUG = 3;
    public static final int INFO = 4;
    public static final int WARN = 5;
    public static final int ERROR = 6;
    public static final int ASSERT = 7;
    public static final int SILENT = 8;

    private static int mPriority = DEFAULT;
    private static String mTag = "Application";

    private Dlog()
    {
    }

    public static void setTag(String tag)
    {
        mTag = tag;
    }

    public static int v(String tag, String msg)
    {
        return println(VERBOSE, tag, msg);
    }

    public static int v(String msg)
    {
        return println(VERBOSE, mTag, msg);
    }

    public static int v(String tag, String msg, Throwable tr)
    {
        return println(VERBOSE, tag, msg + ‘\n‘ + getStackTraceString(tr));
    }

    public static int v(String msg, Throwable tr)
    {
        return println(VERBOSE, mTag, msg + "\n" + getStackTraceString(tr));
    }

    public static int v(String tag, String format, Object... args)
    {
        return println(VERBOSE, tag, format(format, args));
    }

    public static int v(String format, Object... args)
    {
        return println(VERBOSE, mTag, format(format, args));
    }

    public static int d(String tag, String msg)
    {
        return println(DEBUG, tag, msg);
    }

    public static int d(String msg)
    {
        return println(DEBUG, mTag, msg);
    }

    public static int d(String tag, String msg, Throwable tr)
    {
        return println(DEBUG, tag, msg + ‘\n‘ + getStackTraceString(tr));
    }

    public static int d(String msg, Throwable tr)
    {
        return println(DEBUG, mTag, msg + "\n" + getStackTraceString(tr));
    }

    public static int d(String tag, String format, Object... args)
    {
        return println(DEBUG, tag, format(format, args));
    }

    public static int d(String format, Object... args)
    {
        return println(DEBUG, mTag, format(format, args));
    }

    public static int i(String tag, String msg)
    {
        return println(INFO, tag, msg);
    }

    public static int i(String msg)
    {
        return println(INFO, mTag, msg);
    }

    public static int i(String tag, String msg, Throwable tr)
    {
        return println(INFO, tag, msg + "\n" + getStackTraceString(tr));
    }

    public static int i(String msg, Throwable tr)
    {
        return println(INFO, mTag, msg + "\n" + getStackTraceString(tr));
    }

    public static int i(String tag, String format, Object... args)
    {
        return println(INFO, tag, format(format, args));
    }

    public static int i(String format, Object... args)
    {
        return println(INFO, mTag, format(format, args));
    }

    public static int w(String tag, String msg)
    {
        return println(WARN, tag, msg);
    }

    public static int w(String msg)
    {
        return println(WARN, mTag, msg);
    }

    public static int w(String tag, String msg, Throwable tr)
    {
        return println(WARN, tag, msg + "\n" + getStackTraceString(tr));
    }

    public static int w(String msg, Throwable tr)
    {
        return println(WARN, mTag, msg + "\n" + getStackTraceString(tr));
    }

    public static int w(String tag, String format, Object... args)
    {
        return println(WARN, tag, format(format, args));
    }

    public static int w(String format, Object... args)
    {
        return println(WARN, mTag, format(format, args));
    }

    public static int e(String tag, String msg)
    {
        return println(ERROR, tag, msg);
    }

    public static int e(String msg)
    {
        return println(ERROR, mTag, msg);
    }

    public static int e(String tag, String msg, Throwable tr)
    {
        return println(ERROR, tag, msg + "\n" + getStackTraceString(tr));
    }

    public static int e(String msg, Throwable tr)
    {
        return println(ERROR, mTag, msg + "\n" + getStackTraceString(tr));
    }

    public static int e(String tag, String format, Object... args)
    {
        return println(ERROR, tag, format(format, args));
    }

    public static int e(String format, Object... args)
    {
        return println(ERROR, mTag, format(format, args));
    }

    public static int a(String tag, String msg)
    {
        return println(ASSERT, tag, msg);
    }

    public static int a(String msg)
    {
        return println(ASSERT, mTag, msg);
    }

    public static int a(String tag, String msg, Throwable tr)
    {
        return println(ASSERT, tag, msg + "\n" + getStackTraceString(tr));
    }

    public static int a(String msg, Throwable tr)
    {
        return println(ASSERT, mTag, msg + "\n" + getStackTraceString(tr));
    }

    public static int a(String tag, String format, Object... args)
    {
        return println(ASSERT, tag, format(format, args));
    }

    public static int a(String format, Object... args)
    {
        return println(ASSERT, mTag, format(format, args));
    }

    public static int wtf(String msg)
    {
        return Log.wtf(mTag, msg, null);
    }

    public static int wtf(String tag, String msg)
    {
        return Log.wtf(tag, msg, null);
    }

    public static int wtf(String tag, Throwable tr)
    {
        return Log.wtf(tag, tr.getMessage(), tr);
    }

    public static int wtf(String tag, String msg, Throwable tr)
    {
        return Log.wtf(tag, msg, tr);
    }

    public static String getStackTraceString(Throwable tr)
    {
        if (tr == null)
        {
            return "";
        }

        // This is to reduce the amount of log spew that apps do in the
        // non-error
        // condition of the network being unavailable.
        Throwable t = tr;
        while (t != null)
        {
            if (t instanceof UnknownHostException)
            {
                return "";
            }
            t = t.getCause();
        }

        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        tr.printStackTrace(pw);
        return sw.toString();
    }

    public static int setPriority(int priority)
    {
        int prePriority = mPriority;
        mPriority = priority;
        return prePriority;
    }

    public static int getPriority()
    {
        return mPriority;
    }

    public static boolean isLoggable(String tag, int level)
    {
        return Log.isLoggable(tag, level);
    }

    public static int println(int priority, String tag, String msg)
    {
        if (priority < mPriority)
            return 0;
        // 需要导入framework包以支持hide api,println_native为本地方法,效率比println高
        return Log.println_native(Log.LOG_ID_MAIN, priority, tag, msg);
        // return Log.println(priority, tag, msg);
    }

    private static String format(String format, Object... args)
    {
        try
        {
            return String.format(format, args);
        } catch (Exception e)
        {
            return "";
        }
    }
}

 

*Dlog类实现

标签:

原文地址:http://www.cnblogs.com/HellcNQB/p/5880835.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!