--- /dev/null
+/*
+ * jFCPlib-high-level-client - Logging.java -
+ * Copyright © 2008 David Roden
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+package net.pterodactylus.util.logging;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.logging.Handler;
+import java.util.logging.Level;
+import java.util.logging.LogRecord;
+import java.util.logging.Logger;
+
+/**
+ * Sets up logging.
+ *
+ * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
+ * @version $Id$
+ */
+public class Logging {
+
+ /** The log handler. */
+ private static final LogHandler logHandler = new LogHandler();
+
+ /**
+ * Adds a listener to the log handler.
+ *
+ * @param loggingListener
+ * The listener to add
+ */
+ public static void addLoggingListener(LoggingListener loggingListener) {
+ logHandler.addLoggingListener(loggingListener);
+ }
+
+ /**
+ * Removes a listener from the log handler.
+ *
+ * @param loggingListener
+ * The listener to remove
+ */
+ public static void removeLoggingListener(LoggingListener loggingListener) {
+ logHandler.removeLoggingListener(loggingListener);
+ }
+
+ /**
+ * Sets up logging and installs the log handler.
+ */
+ public static void setup() {
+ Logger rootLogger = Logger.getAnonymousLogger().getParent();
+ rootLogger.addHandler(logHandler);
+ rootLogger.setLevel(Level.ALL);
+ logHandler.setLevel(Level.INFO);
+ }
+
+ /**
+ * The log handler simply forwards every log message it receives to all
+ * registered listeners.
+ *
+ * @see LoggingListener
+ * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
+ * @version $Id$
+ */
+ private static class LogHandler extends Handler {
+
+ /**
+ * Package-private constructor.
+ */
+ LogHandler() {
+ }
+
+ /** The list of the listeners. */
+ private final List<LoggingListener> loggingListeners = Collections.synchronizedList(new ArrayList<LoggingListener>());
+
+ //
+ // EVENT MANAGEMENT
+ //
+
+ /**
+ * Adds a listener to the log handler.
+ *
+ * @param loggingListener
+ * The listener to add
+ */
+ public void addLoggingListener(LoggingListener loggingListener) {
+ loggingListeners.add(loggingListener);
+ }
+
+ /**
+ * Removes a listener from the log handler.
+ *
+ * @param loggingListener
+ * The listener to remove
+ */
+ public void removeLoggingListener(LoggingListener loggingListener) {
+ loggingListeners.remove(loggingListener);
+ }
+
+ /**
+ * Notifies all listeners that a log record was received.
+ *
+ * @param logRecord
+ * The received log record
+ */
+ private void fireLogged(LogRecord logRecord) {
+ for (LoggingListener loggingListener: loggingListeners) {
+ loggingListener.logged(logRecord);
+ }
+ }
+
+ //
+ // INTERFACE Handler
+ //
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void close() throws SecurityException {
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void flush() {
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void publish(LogRecord logRecord) {
+ fireLogged(logRecord);
+ }
+
+ }
+
+}
--- /dev/null
+/*
+ * jFCPlib-high-level-client - LoggingListener.java -
+ * Copyright © 2008 David Roden
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+package net.pterodactylus.util.logging;
+
+import java.util.EventListener;
+import java.util.logging.LogRecord;
+
+/**
+ * Interface for components that want to received logged messages.
+ *
+ * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
+ * @version $Id$
+ */
+public interface LoggingListener extends EventListener {
+
+ /**
+ * Notifies a listener that a new log record was received.
+ *
+ * @param logRecord
+ * The received log record
+ */
+ public void logged(LogRecord logRecord);
+
+}