[jboss-svn-commits] JBoss Common SVN: r3332 - in jboss-logmanager/trunk/src/main/java/org/jboss/logmanager: handlers and 1 other directory.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Sat Jul 4 16:13:07 EDT 2009


Author: david.lloyd at jboss.com
Date: 2009-07-04 16:13:07 -0400 (Sat, 04 Jul 2009)
New Revision: 3332

Added:
   jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/handlers/NullHandler.java
Modified:
   jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/ExtHandler.java
   jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/handlers/ConsoleHandler.java
   jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/handlers/OutputStreamHandler.java
   jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/handlers/SizeRotatingFileHandler.java
Log:
Some handler tweaks and improvements

Modified: jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/ExtHandler.java
===================================================================
--- jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/ExtHandler.java	2009-07-03 20:25:33 UTC (rev 3331)
+++ jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/ExtHandler.java	2009-07-04 20:13:07 UTC (rev 3332)
@@ -38,6 +38,7 @@
 
     private static final String LOGGER_CLASS_NAME = org.jboss.logmanager.Logger.class.getName();
     private static final Permission CONTROL_PERMISSION = new LoggingPermission("control", null);
+    private volatile boolean autoflush;
 
     /**
      * The sub-handlers for this handler.  May only be updated using the {@link #handlersUpdater} atomic updater.  The array
@@ -54,6 +55,7 @@
     /** {@inheritDoc} */
     public final void publish(final LogRecord record) {
         publish((record instanceof ExtLogRecord) ? (ExtLogRecord) record : new ExtLogRecord(record, LOGGER_CLASS_NAME));
+        if (autoflush) flush();
     }
 
     /**
@@ -75,7 +77,7 @@
      * @throws SecurityException if a security manager exists and if the caller does not have {@code LoggingPermission(control)}
      */
     public void addHandler(Handler handler) throws SecurityException {
-        LogContext.checkAccess();
+        checkAccess();
         if (handler == null) {
             throw new NullPointerException("handler is null");
         }
@@ -89,7 +91,7 @@
      * @throws SecurityException if a security manager exists and if the caller does not have {@code LoggingPermission(control)}
      */
     public void removeHandler(Handler handler) throws SecurityException {
-        LogContext.checkAccess();
+        checkAccess();
         if (handler == null) {
             return;
         }
@@ -112,13 +114,33 @@
      * @throws SecurityException if a security manager exists and if the caller does not have {@code LoggingPermission(control)}
      */
     public Handler[] clearHandlers() throws SecurityException {
-        LogContext.checkAccess();
+        checkAccess();
         final Handler[] handlers = this.handlers;
         handlersUpdater.clear(this);
         return handlers.length > 0 ? handlers.clone() : handlers;
     }
 
     /**
+     * Determine if this handler will auto-flush.
+     *
+     * @return {@code true} if auto-flush is enabled
+     */
+    public boolean isAutoflush() {
+        return autoflush;
+    }
+
+    /**
+     * Change the autoflush setting for this handler.
+     *
+     * @param autoflush {@code true} to automatically flush after each write; false otherwise
+     * @throws SecurityException if a security manager exists and if the caller does not have {@code LoggingPermission(control)}
+     */
+    public void setAutoflush(final boolean autoflush) throws SecurityException {
+        checkAccess();
+        this.autoflush = autoflush;
+    }
+
+    /**
      * Check access.
      *
      * @throws SecurityException if a security manager is installed and the caller does not have the {@code "control" LoggingPermission}

Modified: jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/handlers/ConsoleHandler.java
===================================================================
--- jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/handlers/ConsoleHandler.java	2009-07-03 20:25:33 UTC (rev 3331)
+++ jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/handlers/ConsoleHandler.java	2009-07-04 20:13:07 UTC (rev 3332)
@@ -23,6 +23,7 @@
 package org.jboss.logmanager.handlers;
 
 import java.io.OutputStream;
+import java.util.EnumMap;
 
 import java.util.logging.Formatter;
 
@@ -33,12 +34,37 @@
  */
 public class ConsoleHandler extends OutputStreamHandler {
     private static final OutputStream out = System.out;
+    private static final OutputStream err = System.err;
 
     /**
+     * The target stream type.
+     */
+    public enum Target {
+
+        /**
+         * The target for {@link System#out}.
+         */
+        SYSTEM_OUT,
+        /**
+         * The target for {@link System#err}.
+         */
+        SYSTEM_ERR,
+    }
+
+    private static final EnumMap<Target, OutputStream> targets;
+
+    static {
+        final EnumMap<Target, OutputStream> map = new EnumMap<Target, OutputStream>(Target.class);
+        map.put(Target.SYSTEM_ERR, err);
+        map.put(Target.SYSTEM_OUT, out);
+        targets = map;
+    }
+
+    /**
      * Construct a new instance.
      */
     public ConsoleHandler() {
-        super(out, Formatters.nullFormatter());
+        this(Formatters.nullFormatter());
     }
 
     /**
@@ -47,6 +73,34 @@
      * @param formatter the formatter to use
      */
     public ConsoleHandler(final Formatter formatter) {
-        super(out, formatter);
+        this(Target.SYSTEM_OUT, formatter);
     }
+
+    /**
+     * Construct a new instance.
+     *
+     * @param target the target to write to, or {@code null} to start with an uninitialized target
+     */
+    public ConsoleHandler(final Target target) {
+        this(target, Formatters.nullFormatter());
+    }
+
+    /**
+     * Construct a new instance.
+     *
+     * @param target the target to write to, or {@code null} to start with an uninitialized target
+     * @param formatter the formatter to use
+     */
+    public ConsoleHandler(final Target target, final Formatter formatter) {
+        super(targets.get(target), formatter);
+    }
+
+    /**
+     * Set the target for this console handler.
+     *
+     * @param target the target to write to, or {@code null} to clear the target
+     */
+    public void setTarget(Target target) {
+        setOutputStream(targets.get(target));
+    }
 }

Added: jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/handlers/NullHandler.java
===================================================================
--- jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/handlers/NullHandler.java	                        (rev 0)
+++ jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/handlers/NullHandler.java	2009-07-04 20:13:07 UTC (rev 3332)
@@ -0,0 +1,45 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.logmanager.handlers;
+
+import org.jboss.logmanager.ExtHandler;
+import org.jboss.logmanager.ExtLogRecord;
+
+/**
+ * A handler which performs no action other than to run any attached filter.
+ */
+public final class NullHandler extends ExtHandler {
+
+    /** {@inheritDoc} */
+    public void publish(final ExtLogRecord record) {
+        isLoggable(record);
+    }
+
+    /** {@inheritDoc} */
+    public void flush() {
+    }
+
+    /** {@inheritDoc} */
+    public void close() throws SecurityException {
+    }
+}

Modified: jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/handlers/OutputStreamHandler.java
===================================================================
--- jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/handlers/OutputStreamHandler.java	2009-07-03 20:25:33 UTC (rev 3331)
+++ jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/handlers/OutputStreamHandler.java	2009-07-04 20:13:07 UTC (rev 3332)
@@ -111,6 +111,6 @@
     }
 
     private void updateWriter(final OutputStream newOutputStream, final String encoding) throws UnsupportedEncodingException {
-        setWriter(newOutputStream == null ? null : encoding == null ? new OutputStreamWriter(newOutputStream) : new OutputStreamWriter(newOutputStream, encoding));
+        super.setWriter(newOutputStream == null ? null : encoding == null ? new OutputStreamWriter(newOutputStream) : new OutputStreamWriter(newOutputStream, encoding));
     }
 }

Modified: jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/handlers/SizeRotatingFileHandler.java
===================================================================
--- jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/handlers/SizeRotatingFileHandler.java	2009-07-03 20:25:33 UTC (rev 3331)
+++ jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/handlers/SizeRotatingFileHandler.java	2009-07-04 20:13:07 UTC (rev 3332)
@@ -55,7 +55,10 @@
      * @param rotateSize the number of bytes before the log is rotated
      */
     public void setRotateSize(final long rotateSize) {
-        this.rotateSize = rotateSize;
+        checkAccess();
+        synchronized (outputLock) {
+            this.rotateSize = rotateSize;
+        }
     }
 
     /**
@@ -64,7 +67,10 @@
      * @param maxBackupIndex the maximum backup index
      */
     public void setMaxBackupIndex(final int maxBackupIndex) {
-        this.maxBackupIndex = maxBackupIndex;
+        checkAccess();
+        synchronized (outputLock) {
+            this.maxBackupIndex = maxBackupIndex;
+        }
     }
 
     /** {@inheritDoc} */
@@ -73,6 +79,10 @@
         if (currentSize > rotateSize && maxBackupIndex > 0) {
             try {
                 final File file = getFile();
+                if (file == null) {
+                    // no file is set; a direct output stream or writer was specified
+                    return;
+                }
                 // close the old file.
                 setFile(null);
                 // rotate.  First, drop the max file (if any), then move each file to the next higher slot.




More information about the jboss-svn-commits mailing list