001/*
002 * JGrapes Event Driven Framework
003 * Copyright (C) 2023 Michael N. Lipp
004 * 
005 * This program is free software; you can redistribute it and/or modify it 
006 * under the terms of the GNU General Public License as published by 
007 * the Free Software Foundation; either version 3 of the License, or 
008 * (at your option) any later version.
009 * 
010 * This program is distributed in the hope that it will be useful, but 
011 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
012 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
013 * for more details.
014 * 
015 * You should have received a copy of the GNU General Public License along 
016 * with this program; if not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jgrapes.webconlet.logviewer;
020
021import java.util.ArrayList;
022import java.util.LinkedList;
023import java.util.List;
024import java.util.logging.Handler;
025import java.util.logging.LogRecord;
026
027/**
028 * The Class LogViewerHandler.
029 */
030public class LogViewerHandler extends Handler {
031
032    private static LinkedList<LogRecord> buffer = new LinkedList<>();
033    private static LogViewerConlet conlet = null;
034
035    @Override
036    public void publish(LogRecord record) {
037        if (!isLoggable(record)) {
038            return;
039        }
040        synchronized (buffer) {
041            buffer.add(record);
042            while (buffer.size() > 100) {
043                buffer.remove(0);
044            }
045        }
046        if (conlet != null) {
047            conlet.addEntry(record);
048        }
049    }
050
051    /* default */ static List<LogRecord> setConlet(LogViewerConlet conlet) {
052        synchronized (buffer) {
053            LogViewerHandler.conlet = conlet;
054            return new ArrayList<LogRecord>(buffer);
055        }
056    }
057
058    @Override
059    public void flush() {
060    }
061
062    @Override
063    public void close() throws SecurityException {
064        buffer.clear();
065    }
066
067}