mac
2024-02-02 ba84a556dc93fcf75e6ec76f999344d3f96ad788
app/src/main/java/com/hdl/photovoltaic/other/HdlLogLogic.java
@@ -1,117 +1,113 @@
package com.hdl.photovoltaic.other;
import android.util.Log;
import com.google.gson.Gson;
import com.hdl.photovoltaic.utils.TimeUtils;
/**
 * 日志逻辑
 */
public class HdlLogLogic {
    /**
     * 是否需要打印(true不需要打印)
     */
    public static boolean isDebug = false;
    private static volatile HdlLogLogic sHdlLogLogic;
    /**
     * 获取当前对象
     *
     * @return HdlLogLogic
     */
    public static synchronized HdlLogLogic getInstance() {
        if (sHdlLogLogic == null) {
            synchronized (HdlLogLogic.class) {
                if (sHdlLogLogic == null) {
                    sHdlLogLogic = new HdlLogLogic();
                }
            }
        }
        return sHdlLogLogic;
    }
    /**
     * android打印
     *
     * @param tag 标签
     * @param mgs 错误信息
     */
    public static void print(String tag, String mgs) {
        if (isDebug) {
            return;
        }
        Log.d(tag, mgs);
    }
    /**
     * java打印
     *
     * @param mgs 输出信息
     */
    public static void print(String mgs) {
        if (isDebug) {
            return;
        }
        System.out.println(mgs);
    }
    /**
     * java打印
     *
     * @param tag  标题
     * @param mgs  错误信息
     * @param code 错误码
     */
    public static void print(String tag, String mgs, int code) {
        if (isDebug) {
            return;
        }
//        StringBuilder b = new StringBuilder();
//        b.append(title);
//        b.append(tag);
//        b.
        System.out.println(title + tag + "======" + mgs + "(" + code + ")");
    }
    /**
     * java打印
     *
     * @param tag  标题
     * @param mgs  错误信息
     * @param code 错误码
     */
    public static void print(String tag, String mgs, String code) {
        if (isDebug) {
            return;
        }
        System.out.println(title + tag + "======" + mgs + "(" + code + ")");
    }
    /**
     * java打印
     *
     * @param tag 标题
     * @param o   错误信息
     */
    public static void print(String tag, Object o) {
        if (isDebug) {
            return;
        }
        if (o == null) {
            System.out.println(title + tag);
        } else {
            try {
                System.out.println(title + tag + "======" + new Gson().toJson(o));
            } catch (Exception e) {
                System.out.println(title + tag + "======" + e.getMessage());
            }
        }
    }
    //是否需要打印到Logcat上(true不需要打印)
    public static boolean isPrintLogcat = false;
    //默认打印标题
    private static final String title = "自定义输出打印信息:";
    /**
     * java打印
     *
     * @param customizeContentFormat 自定义内容格式
     */
    public static void print(String customizeContentFormat) {
        printBase(customizeContentFormat, "0", false);
    }
    /**
     * java打印
     *
     * @param customizeContentFormat 自定义内容格式
     * @param isAddToMemory          是否加入内存(本地日志用到)
     */
    public static void print(String customizeContentFormat, boolean isAddToMemory) {
        printBase(customizeContentFormat, "0", isAddToMemory);
    }
    /**
     * java打印
     *
     * @param msg           自定义内容格式
     * @param code          状态码
     * @param isAddToMemory 是否加入内存(本地日志用到)
     */
    public static void print(String msg, int code, boolean isAddToMemory) {
        printBase(msg, code + "", isAddToMemory);
    }
    /**
     * java打印和存储日志
     *
     * @param msg           信息描述
     * @param code          状态
     * @param isAddToMemory 是否加入内存(本地日志用到)
     */
    private static void printBase(String msg, String code, boolean isAddToMemory) {
        if (isPrintLogcat) {
            return;
        }
        CustomLogObject customLogObject = new CustomLogObject();
        customLogObject.msgOrData = msg;
        customLogObject.code = code;
        String json = title;
        json += customLogObject.getJointMessage();
        System.out.println(json);
        if (isAddToMemory) {
            writeLog(customLogObject.getJointMessage());
        }
    }
    /**
     * 写入日志内容到本地
     *
     * @param strLog 写入内容
     */
    public static void writeLog(String strLog) {
        synchronized (HdlLogLogic.class) {
            strLog = TimeUtils.getTimeFromTimestamp(System.currentTimeMillis()) + "s%" + " " + strLog;//加打印时间(s%占位符,设置显示时间不同字体颜色)
            HdlFileLogic.getInstance().appendFile(HdlFileLogic.getInstance().getLogFileNamePath(), strLog);
        }
    }
    /**
     * 读取日志内容
     *
     * @return 日志内容
     */
    public static String readLog() {
        return HdlFileLogic.getInstance().readFile(HdlFileLogic.getInstance().getLogFileNamePath());
    }
    /**
     * 自定义日志类
     */
    static class CustomLogObject {
        //信息描述
        public String msgOrData = "";
        //状态码
        public String code = "0";
        public String getJointMessage() {
            if ("0".equals(code)) {
                return msgOrData;
            }
            return msgOrData + "(" + code + ")";
        }
    }
}