From b2d2e6dcf6ec38a2f24721f2bc925d634a6eb78d Mon Sep 17 00:00:00 2001 From: mac <user@users-MacBook-Pro.local> Date: 星期二, 05 十二月 2023 18:31:01 +0800 Subject: [PATCH] 2023年12月05日18:30:40 --- app/src/main/java/com/hdl/photovoltaic/other/HdlLogLogic.java | 206 +++++++++++++++++++++++++-------------------------- 1 files changed, 101 insertions(+), 105 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 f7f64e7..75225f8 100644 --- a/app/src/main/java/com/hdl/photovoltaic/other/HdlLogLogic.java +++ b/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 content 鑷畾涔変俊鎭弿杩� + */ + public static void print(String content) { + printBase(content, "0", false); + } + + /** + * java鎵撳嵃 + * + * @param content 鑷畾涔変俊鎭弿杩� + * @param isBoolean 鏄惁鍔犲叆鍐呭瓨(鏈湴鏃ュ織鐢ㄥ埌) + */ + public static void print(String content, boolean isBoolean) { + printBase(content, "0", isBoolean); + } + + /** + * java鎵撳嵃 + * + * @param msg 鑷畾涔変俊鎭弿杩� + * @param code 鐘舵�佺爜 + * @param isBoolean 鏄惁鍔犲叆鍐呭瓨(鏈湴鏃ュ織鐢ㄥ埌) + */ + public static void print(String msg, int code, boolean isBoolean) { + printBase(msg, code + "", isBoolean); + } + + + /** + * java鎵撳嵃鍜屽瓨鍌ㄦ棩蹇� + * + * @param msg 淇℃伅鎻忚堪 + * @param code 鐘舵�� + * @param isBoolean 鏄惁鍔犲叆鍐呭瓨(鏈湴鏃ュ織鐢ㄥ埌) + */ + private static void printBase(String msg, String code, boolean isBoolean) { + if (isPrintLogcat) { + return; + } + CustomLogObject customLogObject = new CustomLogObject(); + customLogObject.msgOrData = msg; + customLogObject.code = code; + String json = title; + json += customLogObject.getJointMessage(); + System.out.println(json); + if (isBoolean) { + 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 + ")"; + + } + } + + } -- Gitblit v1.8.0