package com.xuqm.base.common;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.orhanobut.logger.Logger;

import java.util.Locale;

/**
 * 日志库用的是 KLog
 * 平常自己调试时候不想用那么多，就随便写了个类
 */
public class LogHelper {


    public static void d(String tag, Object object) {
        Logger.t(tag).d(object);
    }

    public static void d(Object object) {
        StackTraceElement caller = getCallerStackTraceElement();
        String tag = generateTag(caller);
        Logger.t(tag).d(object);
    }

    public static void d(@NonNull String message, @Nullable Object... args) {
        Logger.t(message).d(args);
    }

    public static void e(String tag, Object object) {
        Logger.t(tag).e(object.toString());
    }

    public static void e(Object object) {
        if (null == object){
            return;
        }
        StackTraceElement caller = getCallerStackTraceElement();
        String tag = generateTag(caller);
        Logger.t(tag).e("=====>" + object.toString());
    }

    public static void e(String msg, Throwable tr) {
        StackTraceElement caller = getCallerStackTraceElement();
        String tag = generateTag(caller);
        Logger.t(tag).e(tr, msg);
    }

    public static void e(String tag, String msg, Throwable tr) {
        Logger.t(tag).e(tr, msg);
    }

    public static void json(String msg) {
        StackTraceElement caller = getCallerStackTraceElement();
        String tag = generateTag(caller);
        Logger.t(tag).json(msg);
    }

    public static void json(String tag, String msg) {
        Logger.t(tag).json(msg);
    }


    private static String generateTag(StackTraceElement caller) {
        String tag = "%s.%s(L:%d)";
        String callerClazzName = caller.getClassName();
        callerClazzName = callerClazzName.substring(callerClazzName.lastIndexOf(".") + 1);
        return String.format(Locale.getDefault(), tag, callerClazzName, caller.getMethodName(), caller.getLineNumber());
    }

    private static StackTraceElement getCallerStackTraceElement() {
        return Thread.currentThread().getStackTrace()[4];
    }
}
