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