android studio带文件和行输出的logcat

android studio带文件和行输出的logcat

实现的效果

可以直接在logcat窗口中跳转至代码处
因之前写的C/C++打印都是带上文件名和行数的,自学到android 的logcat,发现错误时候会直接鼠标点击跳转到相应的代码处,遂自己封装下一个函数,能够打印文件,行数并且支持直接的鼠标跳转,很方便调试。deep参数可以自定义深度,比如 1 代表上一个方法的调用处,但是有时候可能有问题,不带深度的打印目前没有发现问题。

package m.heqiang.main;

import android.util.Log;


/**
 * Created by heqiang on 2019/2/18.
 */

public class LogPrint {
    private static String TAG = "conlog ";

    public static String Line() {
        StackTraceElement ste = new Throwable().getStackTrace()[2];
        return "(" + ste.getFileName() + ":" + ste.getLineNumber() + ") ";
    }

    public static String Line(int deep) {
        StackTraceElement ste = new Throwable().getStackTrace()[2 + deep];
        return "(" + ste.getFileName() + ":" + ste.getLineNumber() + ") ";
    }

    public static void i(String Str) {
        Log.i(TAG + Line(), Str);
    }

    public static void w(String Str) {
        Log.w(TAG + Line(), Str);
    }

    public static void d(String Str) {
        Log.d(TAG + Line(), Str);
    }

    public static void d(String Str, int deep) {
        Log.d(TAG + Line(deep + 1), Str);
    }

    public static void e(String Str) {
        Log.e(TAG + Line(), Str);
    }

    public static void e(String Str, int deep) {
        Log.e(TAG + Line(deep + 1), Str);
    }

    public static void c(Exception e) {
        e.printStackTrace();
    }
}

猜你喜欢

转载自blog.csdn.net/hajistark/article/details/88103785