1
2
3
4
5
6
7 package nl.tudelft.simulation.logger.handlers;
8
9 import java.util.ArrayList;
10 import java.util.Collections;
11 import java.util.List;
12 import java.util.logging.Formatter;
13 import java.util.logging.Handler;
14 import java.util.logging.LogRecord;
15
16 /***
17 * A MemoryHandler which makes it possible to push to a dynamic target. <br>
18 * (c) copyright 2002-2005 <a href="http://www.simulation.tudelft.nl">Delft
19 * University of Technology </a>, the Netherlands. <br>
20 * See for project information <a href="http://www.simulation.tudelft.nl">
21 * www.simulation.tudelft.nl </a> <br>
22 * License of use: <a href="http://www.gnu.org/copyleft/lesser.html">Lesser
23 * General Public License (LGPL) </a>, no warranty.
24 *
25 * @version $Revision: 1.7 $ $Date: 2005/08/04 12:09:01 $
26 * @author <a href="http://www.peter-jacobs.com">Peter Jacobs </a>, <a
27 * href="mailto:nlang@fbk.eur.nl">Niels Lang </a>
28 */
29 public class MemoryHandler extends Handler
30 {
31 /*** the size of the memoryHandler */
32 private int size = 100;
33
34 /*** the entires of the handler */
35 private List<LogRecord> entries = Collections
36 .synchronizedList(new ArrayList<LogRecord>(100));
37
38 /***
39 * constructs a new MemoryHandler
40 */
41 public MemoryHandler()
42 {
43 super();
44 }
45
46 /***
47 * constructs a new MemoryHandler
48 *
49 * @param size the size of the memoryHandler
50 */
51 public MemoryHandler(final int size)
52 {
53 super();
54 this.size = size;
55 }
56
57 /***
58 * @see java.util.logging.Handler#close()
59 */
60 @Override
61 public void close()
62 {
63 this.flush();
64 }
65
66 /***
67 * @see java.util.logging.Handler#flush()
68 */
69 @Override
70 public void flush()
71 {
72 this.entries.clear();
73 }
74
75 /***
76 * @see java.util.logging.Handler#getFormatter()
77 */
78 @Override
79 public Formatter getFormatter()
80 {
81 return null;
82 }
83
84 /***
85 * returns the size
86 *
87 * @return int the number of records this handler can hold
88 */
89 public int getSize()
90 {
91 return this.size;
92 }
93
94 /***
95 * @see java.util.logging.Handler#publish(java.util.logging.LogRecord)
96 */
97 @Override
98 public synchronized void publish(final LogRecord logRecord)
99 {
100 this.entries.add(logRecord);
101 if (this.entries.size() > this.size)
102 {
103 this.entries.subList(0, this.entries.size() - this.size).clear();
104 }
105 }
106
107 /***
108 * pushes the memory to a handler
109 *
110 * @param target the target
111 */
112 public synchronized void push(final Handler target)
113 {
114 for (LogRecord record : this.entries)
115 {
116 target.publish(record);
117 }
118 this.flush();
119 }
120
121 /***
122 * @see java.util.logging.Handler#setFormatter(java.util.logging.Formatter)
123 */
124 @Override
125 public void setFormatter(final Formatter arg0)
126 {
127 throw new IllegalArgumentException("Cannot set a " + arg0
128 + " on this handler");
129 }
130
131 /***
132 * sets the number of records this handler can hold
133 *
134 * @param size the size
135 */
136 public void setSize(final int size)
137 {
138 this.size = size;
139 }
140 }