MemoryHandler.java revision adc40417389a57bc7a831f55334bffcc07343eb9
1/* 2 * Copyright (c) 2000, 2004, Oracle and/or its affiliates. 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26package 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 75public 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 // Android-changed: Try to load the class from the context class loader. 117 try { 118 Class<?> clz = Thread.currentThread().getContextClassLoader() 119 .loadClass(name); 120 target = (Handler) clz.newInstance(); 121 } catch (Exception innerE) { 122 throw new RuntimeException( 123 "MemoryHandler can't load handler \"" + name + "\"" , innerE); 124 } 125 } 126 init(); 127 } 128 129 // Initialize. Size is a count of LogRecords. 130 private void init() { 131 buffer = new LogRecord[size]; 132 start = 0; 133 count = 0; 134 } 135 136 /** 137 * Create a <tt>MemoryHandler</tt>. 138 * <p> 139 * The <tt>MemoryHandler</tt> is configured based on <tt>LogManager</tt> 140 * properties (or their default values) except that the given <tt>pushLevel</tt> 141 * argument and buffer size argument are used. 142 * 143 * @param target the Handler to which to publish output. 144 * @param size the number of log records to buffer (must be greater than zero) 145 * @param pushLevel message level to push on 146 * 147 * @throws IllegalArgumentException if size is <= 0 148 */ 149 public MemoryHandler(Handler target, int size, Level pushLevel) { 150 if (target == null || pushLevel == null) { 151 throw new NullPointerException(); 152 } 153 if (size <= 0) { 154 throw new IllegalArgumentException(); 155 } 156 sealed = false; 157 configure(); 158 sealed = true; 159 this.target = target; 160 this.pushLevel = pushLevel; 161 this.size = size; 162 init(); 163 } 164 165 /** 166 * Store a <tt>LogRecord</tt> in an internal buffer. 167 * <p> 168 * If there is a <tt>Filter</tt>, its <tt>isLoggable</tt> 169 * method is called to check if the given log record is loggable. 170 * If not we return. Otherwise the given record is copied into 171 * an internal circular buffer. Then the record's level property is 172 * compared with the <tt>pushLevel</tt>. If the given level is 173 * greater than or equal to the <tt>pushLevel</tt> then <tt>push</tt> 174 * is called to write all buffered records to the target output 175 * <tt>Handler</tt>. 176 * 177 * @param record description of the log event. A null record is 178 * silently ignored and is not published 179 */ 180 public synchronized void publish(LogRecord record) { 181 if (!isLoggable(record)) { 182 return; 183 } 184 int ix = (start+count)%buffer.length; 185 buffer[ix] = record; 186 if (count < buffer.length) { 187 count++; 188 } else { 189 start++; 190 start %= buffer.length; 191 } 192 if (record.getLevel().intValue() >= pushLevel.intValue()) { 193 push(); 194 } 195 } 196 197 /** 198 * Push any buffered output to the target <tt>Handler</tt>. 199 * <p> 200 * The buffer is then cleared. 201 */ 202 public synchronized void push() { 203 for (int i = 0; i < count; i++) { 204 int ix = (start+i)%buffer.length; 205 LogRecord record = buffer[ix]; 206 target.publish(record); 207 } 208 // Empty the buffer. 209 start = 0; 210 count = 0; 211 } 212 213 /** 214 * Causes a flush on the target <tt>Handler</tt>. 215 * <p> 216 * Note that the current contents of the <tt>MemoryHandler</tt> 217 * buffer are <b>not</b> written out. That requires a "push". 218 */ 219 public void flush() { 220 target.flush(); 221 } 222 223 /** 224 * Close the <tt>Handler</tt> and free all associated resources. 225 * This will also close the target <tt>Handler</tt>. 226 * 227 * @exception SecurityException if a security manager exists and if 228 * the caller does not have <tt>LoggingPermission("control")</tt>. 229 */ 230 public void close() throws SecurityException { 231 target.close(); 232 setLevel(Level.OFF); 233 } 234 235 /** 236 * Set the <tt>pushLevel</tt>. After a <tt>LogRecord</tt> is copied 237 * into our internal buffer, if its level is greater than or equal to 238 * the <tt>pushLevel</tt>, then <tt>push</tt> will be called. 239 * 240 * @param newLevel the new value of the <tt>pushLevel</tt> 241 * @exception SecurityException if a security manager exists and if 242 * the caller does not have <tt>LoggingPermission("control")</tt>. 243 */ 244 public void setPushLevel(Level newLevel) throws SecurityException { 245 if (newLevel == null) { 246 throw new NullPointerException(); 247 } 248 LogManager manager = LogManager.getLogManager(); 249 checkPermission(); 250 pushLevel = newLevel; 251 } 252 253 /** 254 * Get the <tt>pushLevel</tt>. 255 * 256 * @return the value of the <tt>pushLevel</tt> 257 */ 258 public synchronized Level getPushLevel() { 259 return pushLevel; 260 } 261 262 /** 263 * Check if this <tt>Handler</tt> would actually log a given 264 * <tt>LogRecord</tt> into its internal buffer. 265 * <p> 266 * This method checks if the <tt>LogRecord</tt> has an appropriate level and 267 * whether it satisfies any <tt>Filter</tt>. However it does <b>not</b> 268 * check whether the <tt>LogRecord</tt> would result in a "push" of the 269 * buffer contents. It will return false if the <tt>LogRecord</tt> is null. 270 * <p> 271 * @param record a <tt>LogRecord</tt> 272 * @return true if the <tt>LogRecord</tt> would be logged. 273 * 274 */ 275 public boolean isLoggable(LogRecord record) { 276 return super.isLoggable(record); 277 } 278} 279