package com.imdroid.common.util;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.file.FileAppender;

import java.io.File;

/**
 * 设备发送过来的错误消息记录器
 * 错误消息：长度字段与实际消息体长度不匹配
 *
 * @author LiGang
 * @date 2023/4/8 11:15
 */
public class WrongMessageRecorder {

    private FileAppender appender;

    public static final WrongMessageRecorder INSTANCE = new WrongMessageRecorder();

    private WrongMessageRecorder() {
        try {
            String filePath = "/home/devop/runtime/sideslope_wrong_message_" + DateUtil.today() + ".log";
            File file = new File(filePath);
            if (!file.exists()) {
                file.createNewFile();
            }
            appender = new FileAppender(file, 2, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void append(String message) {
        try {
            appender.append(DateUtil.now() + ": " + message);
            appender.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
