码迷,mamicode.com
首页 > 移动开发 > 详细

Android 常用工具类之LogUtil,可以定位到代码行,双击跳转

时间:2015-04-25 11:52:29      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:

package cn.utils;

import android.util.Log;

public class LogUtils {
    public static boolean isDebug = true;

    private final static String APP_TAG = "myApp";

    /**
     * 获取相关数据:类名,方法名,行号等.用来定位行<br>
     * at cn.utils.MainActivity.onCreate(MainActivity.java:17) 就是用來定位行的代碼<br>
     * 
     * @return [ Thread:main, at
     *         cn.utils.MainActivity.onCreate(MainActivity.java:17)]
     */
    private static String getFunctionName() {
        StackTraceElement[] sts = Thread.currentThread().getStackTrace();
        if (sts != null) {
            for (StackTraceElement st : sts) {
                if (st.isNativeMethod()) {
                    continue;
                }
                if (st.getClassName().equals(Thread.class.getName())) {
                    continue;
                }
                if (st.getClassName().equals(LogUtils.class.getName())) {
                    continue;
                }
                return "[ Thread:" + Thread.currentThread().getName() + ", at " + st.getClassName() + "." + st.getMethodName()
                        + "(" + st.getFileName() + ":" + st.getLineNumber() + ")" + " ]";
            }
        }
        return null;
    }

    public static void w(String msg) {
        if (isDebug) {
       Log.w(APP_TAG, getMsgFormat(msg)); } }
public static void w(String tag, String msg) { if (isDebug) { Log.w(tag, getMsgFormat(msg)); } } /** 输出格式定义 */ private static String getMsgFormat(String msg) { return msg + " ;" + getFunctionName(); } }

 

Android 常用工具类之LogUtil,可以定位到代码行,双击跳转

标签:

原文地址:http://www.cnblogs.com/Westfalen/p/4455565.html

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