package com.dowse.base.log;

import android.util.Log;
import com.dowse.base.util.ContextUtil;
import com.dowse.base.util.MD5Util;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/* loaded from: ds_base_2.0.9_23030112.aar:classes.jar:com/dowse/base/log/DSLog.class */
public class DSLog {
    private static final int MAX_SIZE_MB = 10;
    private static final String VERBOSE = "verbose";
    private static final String DEBUG = "debug";
    private static final String INFO = "info";
    private static final String WARN = "warn";
    private static final String ERROR = "error";
    private static String APP_NAME = "unknown";
    private static boolean is_open = true;
    private static List<String> logList;

    public static void closeLog() {
        is_open = false;
    }

    public static void bytesW(String tag, String msgInfo, byte[] data) {
        if (data == null || data.length <= 0) {
            return;
        }
        String str = msgInfo + " " + ByteTools.byteToHexStr(data);
        if (str != null && str.length() > 300) {
            str = str.substring(0, 300) + " ....";
        }
        v(tag, str);
    }

    public static void bytesNotSaveFile(String tag, String msgInfo, byte[] data) {
        String byteStr;
        if (data == null || data.length <= 0) {
            Log.d(tag, msgInfo + " data is null");
            return;
        }
        if (data.length > 500) {
            byteStr = " 图片视频的数据报文,不打印 ";
        } else {
            byteStr = ByteTools.byteToHexStr(data);
        }
        Log.d(tag, msgInfo + " " + byteStr);
    }

    public static void bytesNotSaveFile(String tag, String msgInfo, List<Byte> data) {
        if (data == null || data.size() <= 0) {
            Log.d(tag, msgInfo + " data is null");
            return;
        }
        String str = msgInfo + "   " + ByteTools.byteToHexStr3(data);
        if (str != null && str.length() > 200) {
            str = str.substring(0, 200) + " ....";
        }
        Log.d(tag, str);
    }

    public static void v(String tag, String msg) {
        if (is_open) {
            Log.v(tag, msg);
            writeLogfile(tag, msg, VERBOSE);
        }
    }

    public static void dNotFile(String tag, String msg) {
        if (is_open) {
            Log.d(tag, msg);
        }
    }

    public static void d(String tag, String msg) {
        if (is_open) {
            Log.d(tag, msg);
            writeLogfile(tag, msg, "debug");
        }
    }

    public static void i(String tag, String msg) {
        if (is_open) {
            Log.i(tag, msg);
            writeLogfile(tag, msg, INFO);
        }
    }

    public static void w(String tag, String msg) {
        if (is_open) {
            Log.w(tag, msg);
            writeLogfile(tag, msg, WARN);
        }
    }

    public static void e(String tag, String msg) {
        if (is_open) {
            Log.e(tag, msg);
            writeLogfile(tag, msg, ERROR);
        }
    }

    private static void showLog2Views(String content) {
        synchronized (DSLog.class) {
            if (content == null) {
                return;
            }
            if (logList == null) {
                logList = new ArrayList();
            }
            logList.add(content);
            if (logList.size() > 20) {
                logList.remove(0);
            }
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < logList.size(); i++) {
                sb.append(logList.get(i));
                sb.append("\n");
            }
        }
    }

    private static void writeLogfile(String tag, String msg, String type) {
        ThreadPoolTools.post(() -> {
            writeLogfileInSubThread(tag, msg, type);
        });
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static void writeLogfileInSubThread(String tag, String msg, String type) {
        if (APP_NAME.equals("unknown")) {
            APP_NAME = MD5Util.md5(ContextUtil.getApplication().getPackageName());
        }
        String logName = APP_NAME + ".log";
        SimpleDateFormat sdfTime = new SimpleDateFormat("HH:mm:ss.SSS");
        String time = sdfTime.format(new Date(System.currentTimeMillis()));
        String content = time + "  " + tag + "/" + type + ": " + msg;
        BufferedWriter out = null;
        String fileDir = Const.DEFAULT_LOG_PATH + APP_NAME;
        try {
            try {
                File dir = new File(fileDir);
                if (!dir.exists()) {
                    dir.mkdirs();
                }
                String filePath = fileDir + File.separator + logName;
                File logFile = new File(filePath);
                double logSizeMB = (logFile.length() / 1024) / 1024;
                if (logSizeMB > 10.0d) {
                    logFile.delete();
                }
                out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath, true)));
                out.write(content);
                out.write("\r\n");
                if (out != null) {
                    try {
                        out.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            } catch (Exception e2) {
                e2.printStackTrace();
                if (out != null) {
                    try {
                        out.close();
                    } catch (IOException e3) {
                        e3.printStackTrace();
                    }
                }
            }
        } catch (Throwable th) {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e4) {
                    e4.printStackTrace();
                    throw th;
                }
            }
            throw th;
        }
    }
}
