src/share/classes/java/util/logging/MemoryHandler.java

Print this page




 171         this.pushLevel = pushLevel;
 172         this.size = size;
 173         init();
 174     }
 175 
 176     /**
 177      * Store a <tt>LogRecord</tt> in an internal buffer.
 178      * <p>
 179      * If there is a <tt>Filter</tt>, its <tt>isLoggable</tt>
 180      * method is called to check if the given log record is loggable.
 181      * If not we return.  Otherwise the given record is copied into
 182      * an internal circular buffer.  Then the record's level property is
 183      * compared with the <tt>pushLevel</tt>. If the given level is
 184      * greater than or equal to the <tt>pushLevel</tt> then <tt>push</tt>
 185      * is called to write all buffered records to the target output
 186      * <tt>Handler</tt>.
 187      *
 188      * @param  record  description of the log event. A null record is
 189      *                 silently ignored and is not published
 190      */

 191     public synchronized void publish(LogRecord record) {
 192         if (!isLoggable(record)) {
 193             return;
 194         }
 195         int ix = (start+count)%buffer.length;
 196         buffer[ix] = record;
 197         if (count < buffer.length) {
 198             count++;
 199         } else {
 200             start++;
 201             start %= buffer.length;
 202         }
 203         if (record.getLevel().intValue() >= pushLevel.intValue()) {
 204             push();
 205         }
 206     }
 207 
 208     /**
 209      * Push any buffered output to the target <tt>Handler</tt>.
 210      * <p>
 211      * The buffer is then cleared.
 212      */
 213     public synchronized void push() {
 214         for (int i = 0; i < count; i++) {
 215             int ix = (start+i)%buffer.length;
 216             LogRecord record = buffer[ix];
 217             target.publish(record);
 218         }
 219         // Empty the buffer.
 220         start = 0;
 221         count = 0;
 222     }
 223 
 224     /**
 225      * Causes a flush on the target <tt>Handler</tt>.
 226      * <p>
 227      * Note that the current contents of the <tt>MemoryHandler</tt>
 228      * buffer are <b>not</b> written out.  That requires a "push".
 229      */

 230     public void flush() {
 231         target.flush();
 232     }
 233 
 234     /**
 235      * Close the <tt>Handler</tt> and free all associated resources.
 236      * This will also close the target <tt>Handler</tt>.
 237      *
 238      * @exception  SecurityException  if a security manager exists and if
 239      *             the caller does not have <tt>LoggingPermission("control")</tt>.
 240      */

 241     public void close() throws SecurityException {
 242         target.close();
 243         setLevel(Level.OFF);
 244     }
 245 
 246     /**
 247      * Set the <tt>pushLevel</tt>.  After a <tt>LogRecord</tt> is copied
 248      * into our internal buffer, if its level is greater than or equal to
 249      * the <tt>pushLevel</tt>, then <tt>push</tt> will be called.
 250      *
 251      * @param newLevel the new value of the <tt>pushLevel</tt>
 252      * @exception  SecurityException  if a security manager exists and if
 253      *             the caller does not have <tt>LoggingPermission("control")</tt>.
 254      */
 255     public void setPushLevel(Level newLevel) throws SecurityException {
 256         if (newLevel == null) {
 257             throw new NullPointerException();
 258         }
 259         LogManager manager = LogManager.getLogManager();
 260         checkPermission();

 261         pushLevel = newLevel;
 262     }

 263 
 264     /**
 265      * Get the <tt>pushLevel</tt>.
 266      *
 267      * @return the value of the <tt>pushLevel</tt>
 268      */
 269     public synchronized Level getPushLevel() {
 270         return pushLevel;
 271     }
 272 
 273     /**
 274      * Check if this <tt>Handler</tt> would actually log a given
 275      * <tt>LogRecord</tt> into its internal buffer.
 276      * <p>
 277      * This method checks if the <tt>LogRecord</tt> has an appropriate level and
 278      * whether it satisfies any <tt>Filter</tt>.  However it does <b>not</b>
 279      * check whether the <tt>LogRecord</tt> would result in a "push" of the
 280      * buffer contents. It will return false if the <tt>LogRecord</tt> is null.
 281      * <p>
 282      * @param record  a <tt>LogRecord</tt>
 283      * @return true if the <tt>LogRecord</tt> would be logged.
 284      *
 285      */

 286     public boolean isLoggable(LogRecord record) {
 287         return super.isLoggable(record);
 288     }
 289 }


 171         this.pushLevel = pushLevel;
 172         this.size = size;
 173         init();
 174     }
 175 
 176     /**
 177      * Store a <tt>LogRecord</tt> in an internal buffer.
 178      * <p>
 179      * If there is a <tt>Filter</tt>, its <tt>isLoggable</tt>
 180      * method is called to check if the given log record is loggable.
 181      * If not we return.  Otherwise the given record is copied into
 182      * an internal circular buffer.  Then the record's level property is
 183      * compared with the <tt>pushLevel</tt>. If the given level is
 184      * greater than or equal to the <tt>pushLevel</tt> then <tt>push</tt>
 185      * is called to write all buffered records to the target output
 186      * <tt>Handler</tt>.
 187      *
 188      * @param  record  description of the log event. A null record is
 189      *                 silently ignored and is not published
 190      */
 191     @Override
 192     public synchronized void publish(LogRecord record) {
 193         if (!isLoggable(record)) {
 194             return;
 195         }
 196         int ix = (start+count)%buffer.length;
 197         buffer[ix] = record;
 198         if (count < buffer.length) {
 199             count++;
 200         } else {
 201             start++;
 202             start %= buffer.length;
 203         }
 204         if (record.getLevel().intValue() >= pushLevel.intValue()) {
 205             push();
 206         }
 207     }
 208 
 209     /**
 210      * Push any buffered output to the target <tt>Handler</tt>.
 211      * <p>
 212      * The buffer is then cleared.
 213      */
 214     public synchronized void push() {
 215         for (int i = 0; i < count; i++) {
 216             int ix = (start+i)%buffer.length;
 217             LogRecord record = buffer[ix];
 218             target.publish(record);
 219         }
 220         // Empty the buffer.
 221         start = 0;
 222         count = 0;
 223     }
 224 
 225     /**
 226      * Causes a flush on the target <tt>Handler</tt>.
 227      * <p>
 228      * Note that the current contents of the <tt>MemoryHandler</tt>
 229      * buffer are <b>not</b> written out.  That requires a "push".
 230      */
 231     @Override
 232     public void flush() {
 233         target.flush();
 234     }
 235 
 236     /**
 237      * Close the <tt>Handler</tt> and free all associated resources.
 238      * This will also close the target <tt>Handler</tt>.
 239      *
 240      * @exception  SecurityException  if a security manager exists and if
 241      *             the caller does not have <tt>LoggingPermission("control")</tt>.
 242      */
 243     @Override
 244     public void close() throws SecurityException {
 245         target.close();
 246         setLevel(Level.OFF);
 247     }
 248 
 249     /**
 250      * Set the <tt>pushLevel</tt>.  After a <tt>LogRecord</tt> is copied
 251      * into our internal buffer, if its level is greater than or equal to
 252      * the <tt>pushLevel</tt>, then <tt>push</tt> will be called.
 253      *
 254      * @param newLevel the new value of the <tt>pushLevel</tt>
 255      * @exception  SecurityException  if a security manager exists and if
 256      *             the caller does not have <tt>LoggingPermission("control")</tt>.
 257      */
 258     public void setPushLevel(Level newLevel) throws SecurityException {
 259         if (newLevel == null) {
 260             throw new NullPointerException();
 261         }
 262         LogManager manager = LogManager.getLogManager();
 263         checkPermission();
 264         synchronized (this) {
 265             pushLevel = newLevel;
 266         }
 267     }
 268 
 269     /**
 270      * Get the <tt>pushLevel</tt>.
 271      *
 272      * @return the value of the <tt>pushLevel</tt>
 273      */
 274     public synchronized Level getPushLevel() {
 275         return pushLevel;
 276     }
 277 
 278     /**
 279      * Check if this <tt>Handler</tt> would actually log a given
 280      * <tt>LogRecord</tt> into its internal buffer.
 281      * <p>
 282      * This method checks if the <tt>LogRecord</tt> has an appropriate level and
 283      * whether it satisfies any <tt>Filter</tt>.  However it does <b>not</b>
 284      * check whether the <tt>LogRecord</tt> would result in a "push" of the
 285      * buffer contents. It will return false if the <tt>LogRecord</tt> is null.
 286      * <p>
 287      * @param record  a <tt>LogRecord</tt>
 288      * @return true if the <tt>LogRecord</tt> would be logged.
 289      *
 290      */
 291     @Override
 292     public boolean isLoggable(LogRecord record) {
 293         return super.isLoggable(record);
 294     }
 295 }