| 1 | /* This file is part of the project "Hilbert II" - http://www.qedeq.org |
| 2 | * |
| 3 | * Copyright 2000-2014, Michael Meyling <mime@qedeq.org>. |
| 4 | * |
| 5 | * "Hilbert II" is free software; you can redistribute |
| 6 | * it and/or modify it under the terms of the GNU General Public |
| 7 | * License as published by the Free Software Foundation; either |
| 8 | * version 2 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | */ |
| 15 | |
| 16 | package org.qedeq.kernel.bo.log; |
| 17 | |
| 18 | import java.io.PrintStream; |
| 19 | import java.util.ArrayList; |
| 20 | import java.util.List; |
| 21 | |
| 22 | import org.qedeq.base.trace.Trace; |
| 23 | import org.qedeq.kernel.bo.common.QedeqBo; |
| 24 | |
| 25 | |
| 26 | /** |
| 27 | * This class organizes the logging of module events. |
| 28 | * |
| 29 | * TODO mime 20080317: must this be a singleton? |
| 30 | * |
| 31 | * @author Michael Meyling |
| 32 | */ |
| 33 | public final class ModuleEventLog implements ModuleEventListener { |
| 34 | |
| 35 | /** This class. */ |
| 36 | private static final Class CLASS = ModuleEventLog.class; |
| 37 | |
| 38 | /** The one and only instance. */ |
| 39 | private static ModuleEventLog instance = new ModuleEventLog(); |
| 40 | |
| 41 | /** The loggers. */ |
| 42 | private List loggers = new ArrayList(); |
| 43 | |
| 44 | |
| 45 | /** |
| 46 | * Get instance of Logger. |
| 47 | * |
| 48 | * @return singleton |
| 49 | */ |
| 50 | public static final ModuleEventLog getInstance() { |
| 51 | return instance; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Don't use me outside of this class. |
| 56 | */ |
| 57 | private ModuleEventLog() { |
| 58 | // nothing to do |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Add listener. |
| 63 | * |
| 64 | * @param log Add this listener. |
| 65 | */ |
| 66 | public final void addLog(final ModuleEventListener log) { |
| 67 | loggers.add(log); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Remove listener. |
| 72 | * |
| 73 | * @param log Remove this listener. |
| 74 | */ |
| 75 | public final void removeLog(final ModuleEventListener log) { |
| 76 | loggers.remove(log); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Add stream listener. |
| 81 | * |
| 82 | * @param out Put messages into this stream. |
| 83 | */ |
| 84 | public final void addLog(final PrintStream out) { |
| 85 | final ModuleEventListener log = new DefaultModuleEventListener(out); |
| 86 | loggers.add(log); |
| 87 | } |
| 88 | |
| 89 | public void addModule(final QedeqBo prop) { |
| 90 | for (int i = 0; i < loggers.size(); i++) { |
| 91 | try { // we don't know if the ModuleEventListener is free of programming errors... |
| 92 | ((ModuleEventListener) loggers.get(i)).addModule(prop); |
| 93 | } catch (RuntimeException e) { |
| 94 | Trace.fatal(CLASS, this, "addModule", |
| 95 | "ModuleEventListener throwed RuntimeException", e); |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | public void stateChanged(final QedeqBo prop) { |
| 101 | for (int i = 0; i < loggers.size(); i++) { |
| 102 | try { // we don't know if the ModuleEventListener is free of programming errors... |
| 103 | ((ModuleEventListener) loggers.get(i)).stateChanged(prop); |
| 104 | } catch (RuntimeException e) { |
| 105 | Trace.fatal(CLASS, this, "stateChanged", |
| 106 | "ModuleEventListener throwed RuntimeException", e); |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | public void removeModule(final QedeqBo prop) { |
| 112 | for (int i = 0; i < loggers.size(); i++) { |
| 113 | try { // we don't know if the ModuleEventListener is free of programming errors... |
| 114 | ((ModuleEventListener) loggers.get(i)).removeModule(prop); |
| 115 | } catch (RuntimeException e) { |
| 116 | Trace.fatal(CLASS, this, "removeModule", |
| 117 | "ModuleEventListener throwed RuntimeException", e); |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | |
| 123 | } |