package com.emato.ich.utils;

import com.emato.ich.message.ICHPublishClient;
import com.emato.ich.message.ICHTopic;

import org.eclipse.paho.client.mqttv3.MqttMessage;

import java.io.ByteArrayOutputStream;

public class LoggingUtils {


    public static void sendErrorLog(String cause, Throwable throwable){

        // TODO 发送异常信息
        MqttMessage mqttMessage = new MqttMessage();
        mqttMessage.setQos(1);
        mqttMessage.setPayload((cause + ": " + (getStackTrace(throwable).toString())).getBytes());
        ICHPublishClient.getInstance().publish(String.format(ICHTopic.ERROR_LOG_REPORT, BaseUtils.getClientId()), mqttMessage);
    }

    private static ByteArrayOutputStream getStackTrace(Throwable t) {
        ByteArrayOutputStream buf = new java.io.ByteArrayOutputStream();
        t.printStackTrace(new java.io.PrintWriter(buf, true));
        return buf;
    }

}
