package be.iminds.ilabt.jfed.experimenter_gui.debug;

import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.AppenderBase;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collection;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;

/**
 * User: twalcari
 * Date: 7/1/2015
 * Time: 12:12
 */
public class LogViewAppender extends AppenderBase<ILoggingEvent> {
    private static final Logger LOG = LoggerFactory.getLogger(LogViewAppender.class);

    private static final int MAX_LOGGING_ENTRIES = 10_000;
    private final BlockingQueue<ILoggingEvent> log = new LinkedBlockingDeque<>(MAX_LOGGING_ENTRIES);

    {
        setName("LOGVIEWER");
    }

    public LogViewAppender() {
    }


    public LogViewAppender(Collection<? extends ILoggingEvent> events) {
        log.addAll(events);
        LOG.debug("LogViewAppender was initialized with {} events", events.size());
    }

    @Override
    protected void append(ILoggingEvent loggingEvent) {
        log.offer(loggingEvent);
    }

    public void drainTo(Collection<ILoggingEvent> collection) {
        log.drainTo(collection);
    }

}
