wjc
2024-12-04 a399dc449dc962c088c2cacbc4c32d503ced816f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package com.hdl.photovoltaic.other;
 
 
import android.util.LruCache;
 
import com.google.gson.Gson;
import com.hdl.photovoltaic.config.UserConfigManage;
import com.hdl.photovoltaic.utils.TimeUtils;
 
/**
 * 日志逻辑
 */
public class HdlLogLogic {
 
    //是否启动打印到Logcat上的标签(true:表示打印,false:表示不打印)
    public static boolean isStartPrintLogcat = true;
 
    //是否启动加到内存里面的标签(true:表示加入,false:表示不加入)
    public static boolean isStartAddToMemory = false;
 
    //默认打印标题
    private static String logTitle = "自定义输出打印信息";
 
    private static final Gson gson = new Gson();
 
    private static final CustomLogObject customLogObject = new CustomLogObject();
 
    /**
     * 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) {
        try {
//            //如果这里频繁的new对象可能会影响加载数据速度
//            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);
            }
            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()) + "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 {
 
        //信息描述
        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;
        }
 
 
    }
 
 
}