package com.naz.sdkdemo.utils;

import android.util.Log;


/**
 * @description: 日志输出工具类
 * @created on: 2018/3/19 5:21
 */

public class LogUtils {
    private final static int LEVEL_V = 0;
    private final static int LEVEL_D = 1;
    private final static int LEVEL_I = 2;
    private final static int LEVEL_W = 3;
    private final static int LEVEL_E = 4;
    private final static int LEVEL_WTF = 5;

    private LogUtils(){
        /* Protect from instantiations */
    }

    public static boolean isDebuggable() {
        return true;
    }

    /**
     * 格式化log
     * @param className 类名
     * @param methodName 方法名
     * @param lineNumber 行号
     * @param msg 日志信息
     * @return
     */
    private static String formatLog(String className, String methodName, int lineNumber,String msg) {
        StringBuffer buffer = new StringBuffer();
        //.append(className).append(",")
        buffer.append("[").append(methodName).append(",").append(lineNumber)
                .append("] ");
        buffer.append(msg);
        return buffer.toString();
    }

    /**
     * 打印日志
     * @param level 输出级别
     * @param msg 日志信息
     */
    private static void printLog(int level, String msg){
        // Throwable instance must be created before any methods
        StackTraceElement elements = new Throwable().fillInStackTrace().getStackTrace()[2];
        String className = elements.getFileName();
        String methodName = elements.getMethodName();
        int lineNumber = elements.getLineNumber();
        switch (level){
            case LEVEL_V:
                Log.v(className, formatLog(className, methodName, lineNumber, msg));
                break;
            case LEVEL_D:
                Log.d(className, formatLog(className, methodName, lineNumber, msg));
                break;
            case LEVEL_I:
                Log.i(className, formatLog(className, methodName, lineNumber, msg));
                break;
            case LEVEL_W:
                Log.w(className, formatLog(className, methodName, lineNumber, msg));
                break;
            case LEVEL_E:
                Log.e(className, formatLog(className, methodName, lineNumber, msg));
                break;
            case LEVEL_WTF:
                Log.wtf(className, formatLog(className, methodName, lineNumber, msg));
                break;
        }
    }

    public static void E(String message){
        printLog(LEVEL_E, message);
    }

    public static void E(Exception e){
        if (null == e){
            return;
        }
        printLog(LEVEL_E, e.getMessage());
    }

    public static void I(String message){
        printLog(LEVEL_I, message);
    }

    public static void D(String message){
        printLog(LEVEL_D, message);
    }

    public static void V(String message){
        printLog(LEVEL_V, message);
    }

    public static void W(String message){
        printLog(LEVEL_W, message);
    }

    public static void WTF(String message){
        printLog(LEVEL_WTF, message);
    }


    /**
     * 截断输出日志
     * @param msg
     */
    public static void longlog(String tag, String msg) {
        if (tag == null || tag.length() == 0
                || msg == null || msg.length() == 0)
            return;

        int segmentSize = 3 * 1024;
        long length = msg.length();
        E("-----------------超长日志输出开始-----------------");
        // 长度小于等于限制直接打印
        if (length <= segmentSize ) {
            E(tag+msg);
        }else {
            // 循环分段打印日志
            while (msg.length() > segmentSize ) {
                String logContent = msg.substring(0, segmentSize );
                msg = msg.replace(logContent, "");
                E(tag+logContent);
            }
            // 打印剩余日志
            E(tag+msg);
        }
        E("-----------------超长日志输出结束-----------------");
    }

}
