1 /*
2 * Copyright 2000-2004 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26 package java.util.logging;
27
28 /**
29 * <tt>Handler</tt> that buffers requests in a circular buffer in memory.
30 * <p>
31 * Normally this <tt>Handler</tt> simply stores incoming <tt>LogRecords</tt>
32 * into its memory buffer and discards earlier records. This buffering
33 * is very cheap and avoids formatting costs. On certain trigger
34 * conditions, the <tt>MemoryHandler</tt> will push out its current buffer
35 * contents to a target <tt>Handler</tt>, which will typically publish
36 * them to the outside world.
37 * <p>
38 * There are three main models for triggering a push of the buffer:
39 * <ul>
40 * <li>
41 * An incoming <tt>LogRecord</tt> has a type that is greater than
42 * a pre-defined level, the <tt>pushLevel</tt>.
43 * <li>
44 * An external class calls the <tt>push</tt> method explicitly.
45 * <li>
46 * A subclass overrides the <tt>log</tt> method and scans each incoming
47 * <tt>LogRecord</tt> and calls <tt>push</tt> if a record matches some
48 * desired criteria.
49 * </ul>
50 * <p>
51 * <b>Configuration:</b>
52 * By default each <tt>MemoryHandler</tt> is initialized using the following
53 * LogManager configuration properties. If properties are not defined
54 * (or have invalid values) then the specified default values are used.
55 * If no default value is defined then a RuntimeException is thrown.
56 * <ul>
57 * <li> java.util.logging.MemoryHandler.level
58 * specifies the level for the <tt>Handler</tt>
59 * (defaults to <tt>Level.ALL</tt>).
60 * <li> java.util.logging.MemoryHandler.filter
61 * specifies the name of a <tt>Filter</tt> class to use
62 * (defaults to no <tt>Filter</tt>).
63 * <li> java.util.logging.MemoryHandler.size
64 * defines the buffer size (defaults to 1000).
65 * <li> java.util.logging.MemoryHandler.push
66 * defines the <tt>pushLevel</tt> (defaults to <tt>level.SEVERE</tt>).
67 * <li> java.util.logging.MemoryHandler.target
68 * specifies the name of the target <tt>Handler </tt> class.
69 * (no default).
70 * </ul>
71 *
72 * @since 1.4
73 */
74
75 public class MemoryHandler extends Handler {
76 private final static int DEFAULT_SIZE = 1000;
77 private Level pushLevel;
78 private int size;
79 private Handler target;
80 private LogRecord buffer[];
81 int start, count;
82
83 // Private method to configure a ConsoleHandler from LogManager
84 // properties and/or default values as specified in the class
85 // javadoc.
86 private void configure() {
87 LogManager manager = LogManager.getLogManager();
88 String cname = getClass().getName();
89
90 pushLevel = manager.getLevelProperty(cname +".push", Level.SEVERE);
91 size = manager.getIntProperty(cname + ".size", DEFAULT_SIZE);
92 if (size <= 0) {
93 size = DEFAULT_SIZE;
94 }
95 setLevel(manager.getLevelProperty(cname +".level", Level.ALL));
96 setFilter(manager.getFilterProperty(cname +".filter", null));
97 setFormatter(manager.getFormatterProperty(cname +".formatter", new SimpleFormatter()));
98 }
99
100 /**
101 * Create a <tt>MemoryHandler</tt> and configure it based on
102 * <tt>LogManager</tt> configuration properties.
103 */
104 public MemoryHandler() {
105 sealed = false;
106 configure();
107 sealed = true;
108
109 String name = "???";
110 try {
111 LogManager manager = LogManager.getLogManager();
112 name = manager.getProperty("java.util.logging.MemoryHandler.target");
113 Class clz = ClassLoader.getSystemClassLoader().loadClass(name);
114 target = (Handler) clz.newInstance();
115 } catch (Exception ex) {
116 throw new RuntimeException("MemoryHandler can't load handler \"" + name + "\"" , ex);
117 }
118 init();
119 }
120
121 // Initialize. Size is a count of LogRecords.
122 private void init() {
123 buffer = new LogRecord[size];
124 start = 0;
125 count = 0;
126 }
127
128 /**
129 * Create a <tt>MemoryHandler</tt>.
130 * <p>
131 * The <tt>MemoryHandler</tt> is configured based on <tt>LogManager</tt>
132 * properties (or their default values) except that the given <tt>pushLevel</tt>
133 * argument and buffer size argument are used.
134 *
135 * @param target the Handler to which to publish output.
136 * @param size the number of log records to buffer (must be greater than zero)
137 * @param pushLevel message level to push on
138 *
139 * @throws IllegalArgumentException is size is <= 0
140 */
141 public MemoryHandler(Handler target, int size, Level pushLevel) {
142 if (target == null || pushLevel == null) {
143 throw new NullPointerException();
144 }
145 if (size <= 0) {
146 throw new IllegalArgumentException();
147 }
148 sealed = false;
149 configure();
150 sealed = true;
151 this.target = target;
152 this.pushLevel = pushLevel;
153 this.size = size;
154 init();
155 }
156
157 /**
158 * Store a <tt>LogRecord</tt> in an internal buffer.
159 * <p>
160 * If there is a <tt>Filter</tt>, its <tt>isLoggable</tt>
161 * method is called to check if the given log record is loggable.
162 * If not we return. Otherwise the given record is copied into
163 * an internal circular buffer. Then the record's level property is
164 * compared with the <tt>pushLevel</tt>. If the given level is
165 * greater than or equal to the <tt>pushLevel</tt> then <tt>push</tt>
166 * is called to write all buffered records to the target output
167 * <tt>Handler</tt>.
168 *
169 * @param record description of the log event. A null record is
170 * silently ignored and is not published
171 */
172 public synchronized void publish(LogRecord record) {
173 if (!isLoggable(record)) {
174 return;
175 }
176 int ix = (start+count)%buffer.length;
177 buffer[ix] = record;
178 if (count < buffer.length) {
179 count++;
180 } else {
181 start++;
182 start %= buffer.length;
183 }
184 if (record.getLevel().intValue() >= pushLevel.intValue()) {
185 push();
186 }
187 }
188
189 /**
190 * Push any buffered output to the target <tt>Handler</tt>.
191 * <p>
192 * The buffer is then cleared.
193 */
194 public synchronized void push() {
195 for (int i = 0; i < count; i++) {
196 int ix = (start+i)%buffer.length;
197 LogRecord record = buffer[ix];
198 target.publish(record);
199 }
200 // Empty the buffer.
201 start = 0;
202 count = 0;
203 }
204
205 /**
206 * Causes a flush on the target <tt>Handler</tt>.
207 * <p>
208 * Note that the current contents of the <tt>MemoryHandler</tt>
209 * buffer are <b>not</b> written out. That requires a "push".
210 */
211 public void flush() {
212 target.flush();
213 }
214
215 /**
216 * Close the <tt>Handler</tt> and free all associated resources.
217 * This will also close the target <tt>Handler</tt>.
218 *
219 * @exception SecurityException if a security manager exists and if
220 * the caller does not have <tt>LoggingPermission("control")</tt>.
221 */
222 public void close() throws SecurityException {
223 target.close();
224 setLevel(Level.OFF);
225 }
226
227 /**
228 * Set the <tt>pushLevel</tt>. After a <tt>LogRecord</tt> is copied
229 * into our internal buffer, if its level is greater than or equal to
230 * the <tt>pushLevel</tt>, then <tt>push</tt> will be called.
231 *
232 * @param newLevel the new value of the <tt>pushLevel</tt>
233 * @exception SecurityException if a security manager exists and if
234 * the caller does not have <tt>LoggingPermission("control")</tt>.
235 */
236 public void setPushLevel(Level newLevel) throws SecurityException {
237 if (newLevel == null) {
238 throw new NullPointerException();
239 }
240 LogManager manager = LogManager.getLogManager();
241 checkAccess();
242 pushLevel = newLevel;
243 }
244
245 /**
246 * Get the <tt>pushLevel</tt>.
247 *
248 * @return the value of the <tt>pushLevel</tt>
249 */
250 public synchronized Level getPushLevel() {
251 return pushLevel;
252 }
253
254 /**
255 * Check if this <tt>Handler</tt> would actually log a given
256 * <tt>LogRecord</tt> into its internal buffer.
257 * <p>
258 * This method checks if the <tt>LogRecord</tt> has an appropriate level and
259 * whether it satisfies any <tt>Filter</tt>. However it does <b>not</b>
260 * check whether the <tt>LogRecord</tt> would result in a "push" of the
261 * buffer contents. It will return false if the <tt>LogRecord</tt> is Null.
262 * <p>
263 * @param record a <tt>LogRecord</tt>
264 * @return true if the <tt>LogRecord</tt> would be logged.
265 *
266 */
267 public boolean isLoggable(LogRecord record) {
268 return super.isLoggable(record);
269 }
270 }