package cd.casic.framework.commons.util.watcher;

import lombok.extern.slf4j.Slf4j;

/**
 * @author by mianbin
 * @Classname LogUtils
 * @Description 流水线和watcher监控日志的工具类，放一起
 * @Date 2025/3/18 11:26
 */
@Slf4j
public class LogUtils {

    /**
     * 计算[watcher].createTime与当前时间的毫秒数的耗时在[warnThreshold]与[errorThreshold]之间，
     * 会将[watcher]序列化为字符串并打印到WARN日志，当超出[errorThreshold]会打印ERROR日志。否则什么都不会打印
     */
    public static void printCostTimeWE(Watcher watcher, Long warnThreshold, Long errorThreshold) {
        watcher.stop();
        long cost = System.currentTimeMillis() - watcher.getCreateTime();
        if (cost >= warnThreshold) {
            if (cost > errorThreshold) {
                log.error(watcher + " cost " + cost + " ms");
            } else {
                log.warn(watcher + " cost " + cost + " ms");
            }
        }
    }

    public static void costTime(String message, Long startTime, Long warnThreshold, Long errorThreshold) {
        long cost = System.currentTimeMillis() - startTime;
        if (cost < warnThreshold) {
            log.info(message + " cost " + cost + " ms");
        } else if (cost >= warnThreshold && cost < errorThreshold) {
            log.warn(message + " cost " + cost + " ms");
        } else {
            log.error(message + " cost " + cost + " ms");
        }
    }

    public static void printCostTimeWE(Watcher watcher) {
        printCostTimeWE(watcher, 1000L, 5000L);
    }
}
