package com.yanzuoguang.util.log;

import com.yanzuoguang.util.helper.DateHelper;
import com.yanzuoguang.util.helper.StringHelper;

/**
 * 日志处理默认处理函数
 *
 * @author 颜佐光
 */
public class LogDefault implements RunnableLog {

    /**
     * 需要处理的日志
     *
     * @param info 日志列表
     */
    @Override
    public void run(LogInfo info) {
        if (info == null) {
            return;
        }
        Class<?> cls = info.getCls();
        String clsName = StringHelper.EMPTY;
        if (cls != null) {
            clsName = info.getCls().getSimpleName();
        }

        StringBuilder sb = new StringBuilder(String.format("/* %s pid:%d t:%d/%d ms %s %s */ %s",
                DateHelper.getDateTimeString("HH:mm:ss.SSS", info.getNow()),
                Thread.currentThread().getId(),
                info.getTime(),
                info.getTotalTime(),
                clsName,
                info.getTag(),
                info.getMessage()
        ));
        Throwable ex = info.getException();
        if (ex != null) {
            sb.append(ex.getClass().getName());
            sb.append(ex.getMessage());
            System.err.println(sb.toString());
            ex.printStackTrace();
        } else {
            System.out.println(sb.toString());
        }
    }
}