Log.java 6.72 KB
package com.chudiangameplay.android.tool;

import android.text.TextUtils;

import com.chudiangameplay.android.BuildConfig;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * 通用日志打印类 (安全控制)
 * 只有A/B版打印   其他版本不打印
 */
public final class Log {
    private static final String TAG = "test";
    private static boolean logEnabled = false;

    static {
//        if (Config.curVersion == Config.IS_ALPHA ) {
        if (BuildConfig.DEBUG) {
            logEnabled = true;
        }
    }

    public static void w(String msg) {
        if (logEnabled) {
            android.util.Log.w(TAG, getLocation() + msg);
        }
    }

    public static void w(String tag, String msg) {
        if (logEnabled) {
            android.util.Log.w(tag, getLocation() + msg);
        }
    }

    public static void w(String tag, String msg, Throwable e) {
        if (logEnabled) {
            android.util.Log.w(tag, getLocation() + msg, e);
        }
    }

    public static void w() {
        if (logEnabled) {
            android.util.Log.w(TAG, getLocation());
        }
    }

    public static void d() {
        if (logEnabled) {
            android.util.Log.v(TAG, getLocation());
        }
    }

    public static void d(String msg) {
        if (logEnabled) {
            android.util.Log.d(TAG, getLocation() + msg);
        }
    }

    public static void d(String tag, String msg) {
        if (logEnabled) {
            android.util.Log.d(tag, getLocation() + msg);
        }
    }

    public static void d(String tag, String msg, Throwable e) {
        if (logEnabled) {
            android.util.Log.d(tag, getLocation() + msg, e);
        }
    }

    public static void i(String msg) {
        if (logEnabled) {
//            android.util.Log.i(TAG, getLocation() + msg);
            i(TAG, msg);
        }
    }

    public static void i(String tag, String msg) {
        if (logEnabled) {
//            if (msg.length() > 4000) {
//                for (int i = 0; i < msg.length(); i += 4000) {
//                    if (i + 4000 < msg.length())
//                        android.util.Log.i(tag , getLocation() +msg.substring(i, i + 4000));
//                    else
//                        android.util.Log.i(tag , getLocation() +msg.substring(i, msg.length()));
//                }
//            } else {
//                android.util.Log.i(tag, getLocation() +msg);
//            }
            printJson(tag, msg);
        }
    }

    public static void i() {
        if (logEnabled) {
            android.util.Log.i(TAG, getLocation());
        }
    }

    public static void e(String msg) {
        if (logEnabled) {
            android.util.Log.e(TAG, getLocation() + msg);
        }
    }

    public static void e(String msg, Throwable e) {
        if (logEnabled) {
            android.util.Log.e(TAG, getLocation() + msg, e);
        }
    }

    public static void e(String TAG, String msg) {
        if (logEnabled) {
            android.util.Log.e(TAG, getLocation() + msg);
        }
    }

    public static void e(String TAG, String msg, Throwable e) {
        if (logEnabled) {
            android.util.Log.e(TAG, getLocation() + msg, e);
        }
    }

    public static void e(Throwable e) {
        if (logEnabled) {
            android.util.Log.e(TAG, getLocation(), e);
        }
    }

    public static void e() {
        if (logEnabled) {
            android.util.Log.e(TAG, getLocation());
        }
    }

    public static void v(String TAG, String msg, Throwable e) {
        if (logEnabled) {
            android.util.Log.v(TAG, getLocation(), e);
        }
    }

    public static void v(String TAG, String msg) {
        if (logEnabled) {
            android.util.Log.v(TAG, getLocation());
        }
    }

    public static void v(String msg) {
        if (logEnabled) {
            android.util.Log.v(TAG, msg);
        }
    }

    private static String getLocation() {
        final String className = Log.class.getName();
        final StackTraceElement[] traces = Thread.currentThread()
                .getStackTrace();
        boolean found = false;

        for (StackTraceElement trace : traces) {
            try {
                if (found) {
                    if (!trace.getClassName().startsWith(className)) {
                        Class<?> clazz = Class.forName(trace.getClassName());
                        return "[" + getClassName(clazz) + ":"
                                + trace.getMethodName() + ":"
                                + trace.getLineNumber() + "]: ";
                    }
                } else if (trace.getClassName().startsWith(className)) {
                    found = true;
                }
            } catch (ClassNotFoundException ignored) {
            }
        }

        return "[]: ";
    }

    private static String getClassName(Class<?> clazz) {
        if (clazz != null) {
            if (!TextUtils.isEmpty(clazz.getSimpleName())) {
                return clazz.getSimpleName();
            }

            return getClassName(clazz.getEnclosingClass());
        }

        return "";
    }


    public static final String LINE_SEPARATOR = System.getProperty("line.separator");

    public static void printLine(String tag, boolean isTop) {
        if (isTop) {
            Log.d(tag, "╔═══════════════════════════════════════════════════════════════════════════════════════");
        } else {
            Log.d(tag, "╚═══════════════════════════════════════════════════════════════════════════════════════");
        }
    }
    public static void printJson(String tag, String msg) {
        String message;
        try {
            if (msg.startsWith("{")) {
                JSONObject jsonObject = new JSONObject(msg);
                message = jsonObject.toString(4);//最重要的方法,就一行,返回格式化的json字符串,其中的数字4是缩进字符数
            } else if (msg.startsWith("[")) {
                JSONArray jsonArray = new JSONArray(msg);
                message = jsonArray.toString(4);
            } else {
                message = msg;
            }
        } catch (JSONException e) {
            message = msg;
        }

        printLine(tag, true);
        message = LINE_SEPARATOR + message;
        String[] lines = message.split(LINE_SEPARATOR);
        for (String line : lines) {
            Log.d(tag, "║ " + line);
        }
        printLine(tag, false);
    }
}