android开发:LogUtils-很好用的Log打印类,自动输出方法和行数

15 篇文章 0 订阅

LogUtils-很好用的Log打印类,自动输出方法和行数


/**
 * LogUtils-很好用的Log打印类,自动输出方法和行数
 *
 * @author adayo
 * @version v0.1.1
 */
public class LogUtils implements Parcelable {

    private static boolean mLogEnable = true;
    public static final String PREFIX = constant.FOTA_LOG_TAG;

    private static String mClassname;
    private static ArrayList<String> mMethods;

    static {
        mClassname = LogUtils.class.getName();
        mMethods = new ArrayList<>();

        Method[] ms = LogUtils.class.getDeclaredMethods();
        for (Method m : ms) {
            mMethods.add(m.getName());
        }
    }

    public static void init(boolean logEnable) {
        mLogEnable = logEnable;
    }

    public static void d(String tag, String msg) {
        if (mLogEnable) {
            //Log.d(tag, getMsgWithLineNumber(msg));
            d(msg);
        }
    }

    public static void e(String tag, String msg) {
        if (mLogEnable) {
            //Log.e(tag, getMsgWithLineNumber(msg));
            d(msg);
        }
    }

    public static void i(String tag, String msg) {
        if (mLogEnable) {
            //Log.i(tag, getMsgWithLineNumber(msg));
            d(msg);
        }
    }

    public static void w(String tag, String msg) {
        if (mLogEnable) {
            //Log.w(tag, getMsgWithLineNumber(msg));
            d(msg);
        }
    }

    public static void v(String tag, String msg) {
        if (mLogEnable) {
            //Log.v(tag, getMsgWithLineNumber(msg));
            d(msg);
        }
    }


    public static void d(String msg) {
        if ( true) {
            String[] content = getMsgAndTagWithLineNumber(msg);
            Log.d(content[0], content[1]);
        }
    }

    public static void e(String msg) {
        if (mLogEnable) {
            String[] content = getMsgAndTagWithLineNumber(msg);
            Log.e(content[0], content[1]);
        }
    }

    public static void i(String msg) {
        if (mLogEnable) {
            String[] content = getMsgAndTagWithLineNumber(msg);
            Log.i(content[0], content[1]);
        }
    }

    public static void i() {
        if (mLogEnable) {
            String[] content = getMsgAndTagWithLineNumber("");
            Log.i(content[0], content[1]);
        }
    }

    public static void w(String msg) {
        if (mLogEnable) {
            String[] content = getMsgAndTagWithLineNumber(msg);
            Log.w(content[0], content[1]);
        }
    }

    public static void v(String msg) {
        if (mLogEnable) {
            String[] content = getMsgAndTagWithLineNumber(msg);
            Log.v(content[0], content[1]);
        }
    }

    public static String getMsgWithLineNumber(String msg) {
        try {
            for (StackTraceElement st : (new Throwable()).getStackTrace()) {
                if (mClassname.equals(st.getClassName()) || mMethods.contains(st.getMethodName())) {
                    continue;
                } else {
                    int b = st.getClassName().lastIndexOf(".") + 1;
                    String message = new StringBuilder(st.getClassName().substring(b)).append("->")
                            .append("[").append(st.getMethodName())
                            .append("():").append(st.getLineNumber())
                            .append("]").append(msg).toString();
                    return message;
                }
            }
        } catch (Exception e) {

        }
        return msg;
    }

    public static String[] getMsgAndTagWithLineNumber(String msg) {
        try {
            for (StackTraceElement st : (new Throwable()).getStackTrace()) {
                if (mClassname.equals(st.getClassName()) || mMethods.contains(st.getMethodName())) {
                    continue;
                } else {
                    int b = st.getClassName().lastIndexOf(".") + 1;
                    String TAG = PREFIX + st.getClassName().substring(b);
                    //String TAG = "fota";
                    String message = st.getMethodName() + "():" + st.getLineNumber() + "->" + msg;
                    String[] content = new String[]{TAG, message};
                    return content;
                }
            }
        } catch (Exception e) {

        }
        return new String[]{"universal tag", msg};
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
    }

    public LogUtils() {
    }

    protected LogUtils(Parcel in) {
    }

    public static final Parcelable.Creator<LogUtils> CREATOR = new Parcelable.Creator<LogUtils>() {
        @Override
        public LogUtils createFromParcel(Parcel source) {
            return new LogUtils(source);
        }

        @Override
        public LogUtils[] newArray(int size) {
            return new LogUtils[size];
        }
    };
}
**
 * Created by Y3985 on 2019/4/28
 */
public final class constant {
    // log 打印的 tag
    public static final String FOTA_LOG_TAG = "fota->";


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值