View Javadoc

1   /*
2    * @(#) EventLogHandler.java Nov 18, 2003 Copyright (c) 2002-2005 Delft
3    * University of Technology Jaffalaan 5, 2628 BX Delft, the Netherlands. All
4    * rights reserved. This software is proprietary information of Delft University
5    * of Technology The code is published under the Lesser General Public License
6    */
7   package nl.tudelft.simulation.logger.handlers;
8   
9   import java.util.logging.Handler;
10  import java.util.logging.LogRecord;
11  
12  import nl.tudelft.simulation.event.Event;
13  import nl.tudelft.simulation.event.EventInterface;
14  import nl.tudelft.simulation.event.EventListenerInterface;
15  import nl.tudelft.simulation.event.EventProducer;
16  import nl.tudelft.simulation.event.EventProducerInterface;
17  import nl.tudelft.simulation.event.EventType;
18  
19  /***
20   * A EventLogHandler <br>
21   * (c) copyright 2002-2005 <a href="http://www.simulation.tudelft.nl">Delft
22   * University of Technology </a>, the Netherlands. <br>
23   * See for project information <a
24   * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
25   * License of use: <a href="http://www.gnu.org/copyleft/lesser.html">Lesser
26   * General Public License (LGPL) </a>, no warranty.
27   * 
28   * @version $Revision: 1.7 $ $Date: 2005/08/04 12:09:01 $
29   * @author <a href="http://www.peter-jacobs.com">Peter Jacobs </a>, <a
30   *         href="mailto:nlang@fbk.eur.nl">Niels Lang </a>
31   */
32  public class EventLogHandler extends Handler implements EventProducerInterface
33  {
34      /*** LOG_RECORD_PRODUCED_EVENT is fired whenever an log record is received */
35      public static final EventType LOG_RECORD_PRODUCED_EVENT = new EventType(
36              "LOG_RECORD_PRODUCED_EVENT");
37  
38      /*** our private postman */
39      private MyEventProducer postman = new MyEventProducer();
40  
41      /***
42       * constructs a new EventLogHandler
43       */
44      public EventLogHandler()
45      {
46          super();
47      }
48  
49      /***
50       * @see nl.tudelft.simulation.event.EventProducerInterface
51       *      #addListener(nl.tudelft.simulation.event.EventListenerInterface,
52       *      nl.tudelft.simulation.event.EventType)
53       */
54      public boolean addListener(final EventListenerInterface listener,
55              final EventType eventType)
56      {
57          return this.postman.addListener(listener, eventType);
58      }
59  
60      /***
61       * @see nl.tudelft.simulation.event.EventProducerInterface
62       *      #addListener(nl.tudelft.simulation.event.EventListenerInterface,
63       *      nl.tudelft.simulation.event.EventType, boolean)
64       */
65      public boolean addListener(final EventListenerInterface listener,
66              final EventType eventType, final boolean weak)
67      {
68          return this.postman.addListener(listener, eventType, weak);
69      }
70  
71      /***
72       * @see nl.tudelft.simulation.event.EventProducerInterface
73       *      #addListener(nl.tudelft.simulation.event.EventListenerInterface,
74       *      nl.tudelft.simulation.event.EventType, short)
75       */
76      public boolean addListener(final EventListenerInterface listener,
77              final EventType eventType, final short position)
78      {
79          return this.postman.addListener(listener, eventType, position);
80      }
81  
82      /***
83       * @see nl.tudelft.simulation.event.EventProducerInterface
84       *      #addListener(nl.tudelft.simulation.event.EventListenerInterface,
85       *      nl.tudelft.simulation.event.EventType, short,boolean)
86       */
87      public boolean addListener(final EventListenerInterface listener,
88              final EventType eventType, final short position, final boolean weak)
89      {
90          return this.postman.addListener(listener, eventType, position, weak);
91      }
92  
93      /***
94       * @see java.util.logging.Handler#close()
95       */
96      @Override
97  	public void close()
98      {
99          this.flush();
100     }
101 
102     /***
103      * @see java.util.logging.Handler#flush()
104      */
105     @Override
106 	public void flush()
107     {
108         // Nothing to do
109     }
110 
111     /***
112      * @see nl.tudelft.simulation.event.EventProducerInterface#getEventTypes()
113      */
114     public EventType[] getEventTypes()
115     {
116         return new EventType[] { EventLogHandler.LOG_RECORD_PRODUCED_EVENT };
117     }
118 
119     /***
120      * @see java.util.logging.Handler#publish(java.util.logging.LogRecord)
121      */
122     @Override
123 	public void publish(final LogRecord arg0)
124     {
125         this.postman.fireEvent(new Event(
126                 EventLogHandler.LOG_RECORD_PRODUCED_EVENT, this, this
127                         .getFormatter().format(arg0)));
128     }
129 
130     /***
131      * @see nl.tudelft.simulation.event.EventProducerInterface
132      *      #removeListener(nl.tudelft.simulation.event.EventListenerInterface,
133      *      nl.tudelft.simulation.event.EventType)
134      */
135     public boolean removeListener(final EventListenerInterface listener,
136             final EventType eventType)
137     {
138         return this.postman.removeListener(listener, eventType);
139     }
140 
141     /***
142      * A MyEventProducer is a more or less public Postman.
143      */
144     private class MyEventProducer extends EventProducer
145     {
146         /***
147          * @see nl.tudelft.simulation.event.EventProducer
148          *      #fireEvent(nl.tudelft.simulation.event.EventInterface)
149          */
150         @Override
151 		public EventInterface fireEvent(final EventInterface event)
152         {
153             return super.fireEvent(event);
154         }
155     }
156 }