1 7 8 package java.util.logging; 9 10 57 58 public class MemoryHandler extends Handler { 59 private final static int DEFAULT_SIZE = 1000; 60 private Level pushLevel; 61 private int size; 62 private Handler target; 63 private LogRecord buffer[]; 64 int start, count; 65 66 private void configure() { 70 LogManager manager = LogManager.getLogManager(); 71 String cname = getClass().getName(); 72 73 pushLevel = manager.getLevelProperty(cname +".push", Level.SEVERE); 74 size = manager.getIntProperty(cname + ".size", DEFAULT_SIZE); 75 if (size <= 0) { 76 size = DEFAULT_SIZE; 77 } 78 setLevel(manager.getLevelProperty(cname +".level", Level.ALL)); 79 setFilter(manager.getFilterProperty(cname +".filter", null)); 80 setFormatter(manager.getFormatterProperty(cname +".formatter", new SimpleFormatter ())); 81 } 82 83 87 public MemoryHandler() { 88 sealed = false; 89 configure(); 90 sealed = true; 91 92 String name = "???"; 93 try { 94 LogManager manager = LogManager.getLogManager(); 95 name = manager.getProperty("java.util.logging.MemoryHandler.target"); 96 Class clz = ClassLoader.getSystemClassLoader().loadClass(name); 97 target = (Handler ) clz.newInstance(); 98 } catch (Exception ex) { 99 throw new RuntimeException ("MemoryHandler can't load handler \"" + name + "\"" , ex); 100 } 101 init(); 102 } 103 104 private void init() { 106 buffer = new LogRecord [size]; 107 start = 0; 108 count = 0; 109 } 110 111 124 public MemoryHandler(Handler target, int size, Level pushLevel) { 125 if (target == null || pushLevel == null) { 126 throw new NullPointerException (); 127 } 128 if (size <= 0) { 129 throw new IllegalArgumentException (); 130 } 131 sealed = false; 132 configure(); 133 sealed = true; 134 this.target = target; 135 this.pushLevel = pushLevel; 136 this.size = size; 137 init(); 138 } 139 140 155 public synchronized void publish(LogRecord record) { 156 if (!isLoggable(record)) { 157 return; 158 } 159 int ix = (start+count)%buffer.length; 160 buffer[ix] = record; 161 if (count < buffer.length) { 162 count++; 163 } else { 164 start++; 165 } 166 if (record.getLevel().intValue() >= pushLevel.intValue()) { 167 push(); 168 } 169 } 170 171 176 public synchronized void push() { 177 for (int i = 0; i < count; i++) { 178 int ix = (start+i)%buffer.length; 179 LogRecord record = buffer[ix]; 180 target.publish(record); 181 } 182 start = 0; 184 count = 0; 185 } 186 187 193 public void flush() { 194 target.flush(); 195 } 196 197 204 public void close() throws SecurityException { 205 target.close(); 206 setLevel(Level.OFF); 207 } 208 209 218 public void setPushLevel(Level newLevel) throws SecurityException { 219 if (newLevel == null) { 220 throw new NullPointerException (); 221 } 222 LogManager manager = LogManager.getLogManager(); 223 checkAccess(); 224 pushLevel = newLevel; 225 } 226 227 232 public synchronized Level getPushLevel() { 233 return pushLevel; 234 } 235 236 249 public boolean isLoggable(LogRecord record) { 250 return super.isLoggable(record); 251 } 252 } 253 | Popular Tags |