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 List<LogRecord> buffer = new LinkedList<>(); 033 private static LogViewerConlet conlet; 034 035 @Override 036 @SuppressWarnings("PMD.AvoidSynchronizedStatement") 037 public void publish(LogRecord record) { 038 if (!isLoggable(record)) { 039 return; 040 } 041 synchronized (buffer) { 042 buffer.add(record); 043 while (buffer.size() > 100) { 044 buffer.remove(0); 045 } 046 } 047 if (conlet != null) { 048 conlet.addEntry(record); 049 } 050 } 051 052 @SuppressWarnings("PMD.AvoidSynchronizedStatement") 053 /* default */ static List<LogRecord> setConlet(LogViewerConlet conlet) { 054 synchronized (buffer) { 055 LogViewerHandler.conlet = conlet; 056 return new ArrayList<>(buffer); 057 } 058 } 059 060 @Override 061 public void flush() { 062 // Nothing to do. 063 } 064 065 @Override 066 public void close() { 067 buffer.clear(); 068 } 069 070}