From 80f2ca2df62ff1cd03046864af504245be078eb2 Mon Sep 17 00:00:00 2001 From: wjc <1243177876@qq.com> Date: 星期四, 05 六月 2025 10:37:28 +0800 Subject: [PATCH] 2025年06月05日10:37:26 --- app/src/main/java/com/hdl/photovoltaic/other/HdlLogLogic.java | 137 ++++++++++++++++++++++++++++++++++++++++----- 1 files changed, 122 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/com/hdl/photovoltaic/other/HdlLogLogic.java b/app/src/main/java/com/hdl/photovoltaic/other/HdlLogLogic.java index 741a0b8..dab21db 100644 --- a/app/src/main/java/com/hdl/photovoltaic/other/HdlLogLogic.java +++ b/app/src/main/java/com/hdl/photovoltaic/other/HdlLogLogic.java @@ -1,32 +1,139 @@ package com.hdl.photovoltaic.other; -import android.util.Log; + +import android.util.LruCache; + +import com.google.gson.Gson; +import com.hdl.photovoltaic.config.UserConfigManage; +import com.hdl.photovoltaic.utils.TimeUtils; /** * 鏃ュ織閫昏緫 */ public class HdlLogLogic { - private static volatile HdlLogLogic sHdlLogLogic; + //鏄惁鍚姩鎵撳嵃鍒癓ogcat涓婄殑鏍囩(true:琛ㄧず鎵撳嵃锛宖alse锛氳〃绀轰笉鎵撳嵃) + public static boolean isStartPrintLogcat = true; + + //鏄惁鍚姩鍔犲埌鍐呭瓨閲岄潰鐨勬爣绛�(true:琛ㄧず鍔犲叆锛宖alse锛氳〃绀轰笉鍔犲叆) + public static boolean isStartAddToMemory = true; + + //榛樿鎵撳嵃鏍囬 + private static String logTitle = "";//鑷畾涔夎緭鍑烘墦鍗颁俊鎭� + + private static final Gson gson = new Gson(); + + private static final CustomLogObject customLogObject = new CustomLogObject(); /** - * 鑾峰彇褰撳墠瀵硅薄 + * java鎵撳嵃 * - * @return HdlLogLogic + * @param customizeContentFormat 鑷畾涔夊唴瀹规牸寮� */ - public static synchronized HdlLogLogic getInstance() { - if (sHdlLogLogic == null) { - synchronized (HdlLogLogic.class) { - if (sHdlLogLogic == null) { - sHdlLogLogic = new HdlLogLogic(); - } - } - } - return sHdlLogLogic; + public static void print(String customizeContentFormat) { + printBase(customizeContentFormat, "0", false); } - public static void print(String tag, String mgs) { - Log.d(tag, mgs); + /** + * 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) { + try { +// //濡傛灉杩欓噷棰戠箒鐨刵ew瀵硅薄鍙兘浼氬奖鍝嶅姞杞芥暟鎹�熷害 +// CustomLogObject customLogObject = new CustomLogObject(); + customLogObject.setMsgOrData(msg); + customLogObject.setCode(code); + String json = logTitle + (UserConfigManage.getInstance().isBAccount() ? "(B绔�)==" : "(C绔�)==") + gson.toJson(customLogObject); + if (isStartPrintLogcat) { + System.out.println(json.replace("\\", "")); + } + if (isStartAddToMemory) { + if (isAddToMemory) { + writeLog(json); + } + } + } catch (Exception ignored) { + } + } + + + /** + * 鍐欏叆鏃ュ織鍐呭鍒版湰鍦� + * + * @param strLog 鍐欏叆鍐呭 + */ + public static void writeLog(String strLog) { + synchronized (HdlLogLogic.class) { + strLog = TimeUtils.getTimeFromTimestamp(System.currentTimeMillis(), true) + "s%" + " " + strLog;//鍔犳墦鍗版椂闂�(s%鍗犱綅绗︼紝璁剧疆鏄剧ず鏃堕棿涓嶅悓瀛椾綋棰滆壊) + + HdlFileLogic.getInstance().appendFile(HdlFileLogic.getInstance().getLogFileNamePath(), strLog.replace("\\", "")); + } + } + + /** + * 璇诲彇鏃ュ織鍐呭 + * + * @return 鏃ュ織鍐呭 + */ + public static String readLog() { + return HdlFileLogic.getInstance().readFile(HdlFileLogic.getInstance().getLogFileNamePath()); + } + + + /** + * 鑷畾涔夋棩蹇楃被 + */ + static class CustomLogObject { + + //淇℃伅鎻忚堪 + private String msgOrData; + + //鐘舵�佺爜 + private String code; + + public String getMsgOrData() { + return msgOrData == null ? "" : msgOrData; + } + + public void setMsgOrData(String msgOrData) { + this.msgOrData = msgOrData; + } + + public String getCode() { + return code == null ? "0" : code; + } + + public void setCode(String code) { + this.code = code; + } + + } -- Gitblit v1.8.0