You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by be...@apache.org on 2008/05/31 12:50:39 UTC

svn commit: r661996 - in /maven/shared/trunk/maven-invoker/src/main/java/org/apache/maven/shared/invoker: PrintStreamHandler.java PrintStreamLogger.java SystemOutHandler.java SystemOutLogger.java

Author: bentmann
Date: Sat May 31 03:50:38 2008
New Revision: 661996

URL: http://svn.apache.org/viewvc?rev=661996&view=rev
Log:
o Generalized SystemOut(Logger|Handler) to PrintStream(Logger|Handler)

Added:
    maven/shared/trunk/maven-invoker/src/main/java/org/apache/maven/shared/invoker/PrintStreamHandler.java   (with props)
    maven/shared/trunk/maven-invoker/src/main/java/org/apache/maven/shared/invoker/PrintStreamLogger.java   (with props)
Modified:
    maven/shared/trunk/maven-invoker/src/main/java/org/apache/maven/shared/invoker/SystemOutHandler.java
    maven/shared/trunk/maven-invoker/src/main/java/org/apache/maven/shared/invoker/SystemOutLogger.java

Added: maven/shared/trunk/maven-invoker/src/main/java/org/apache/maven/shared/invoker/PrintStreamHandler.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-invoker/src/main/java/org/apache/maven/shared/invoker/PrintStreamHandler.java?rev=661996&view=auto
==============================================================================
--- maven/shared/trunk/maven-invoker/src/main/java/org/apache/maven/shared/invoker/PrintStreamHandler.java (added)
+++ maven/shared/trunk/maven-invoker/src/main/java/org/apache/maven/shared/invoker/PrintStreamHandler.java Sat May 31 03:50:38 2008
@@ -0,0 +1,85 @@
+package org.apache.maven.shared.invoker;
+
+import java.io.PrintStream;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/**
+ * Offers an output handler that writes to a print stream like {@link System#out}.
+ * 
+ * @version $Id$
+ * @since 2.0.9
+ */
+public class PrintStreamHandler
+    implements InvocationOutputHandler
+{
+
+    /**
+     * The print stream to write to, never <code>null</code>.
+     */
+    private PrintStream out;
+
+    /**
+     * A flag whether the print stream should be flushed after each line.
+     */
+    private boolean alwaysFlush;
+
+    /**
+     * Creates a new output handler that writes to {@link System#out}.
+     */
+    public PrintStreamHandler()
+    {
+        this( System.out, false );
+    }
+
+    /**
+     * Creates a new output handler that writes to the specified print stream.
+     * 
+     * @param out The print stream to write to, must not be <code>null</code>.
+     * @param alwaysFlush A flag whether the print stream should be flushed after each line.
+     */
+    public PrintStreamHandler( PrintStream out, boolean alwaysFlush )
+    {
+        if ( out == null )
+        {
+            throw new NullPointerException( "missing output stream" );
+        }
+        this.out = out;
+        this.alwaysFlush = alwaysFlush;
+    }
+
+    public void consumeLine( String line )
+    {
+        if ( line == null )
+        {
+            out.println();
+        }
+        else
+        {
+            out.println( line );
+        }
+
+        if ( alwaysFlush )
+        {
+            out.flush();
+        }
+    }
+
+}

Propchange: maven/shared/trunk/maven-invoker/src/main/java/org/apache/maven/shared/invoker/PrintStreamHandler.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/shared/trunk/maven-invoker/src/main/java/org/apache/maven/shared/invoker/PrintStreamHandler.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: maven/shared/trunk/maven-invoker/src/main/java/org/apache/maven/shared/invoker/PrintStreamLogger.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-invoker/src/main/java/org/apache/maven/shared/invoker/PrintStreamLogger.java?rev=661996&view=auto
==============================================================================
--- maven/shared/trunk/maven-invoker/src/main/java/org/apache/maven/shared/invoker/PrintStreamLogger.java (added)
+++ maven/shared/trunk/maven-invoker/src/main/java/org/apache/maven/shared/invoker/PrintStreamLogger.java Sat May 31 03:50:38 2008
@@ -0,0 +1,233 @@
+package org.apache.maven.shared.invoker;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.io.PrintStream;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+
+/**
+ * Offers a logger that writes to a print stream like {@link System#out}.
+ * 
+ * @version $Id$
+ * @since 2.0.9
+ */
+public class PrintStreamLogger
+    implements InvokerLogger
+{
+
+    /**
+     * The print stream to write to, never <code>null</code>.
+     */
+    private PrintStream out;
+
+    /**
+     * The threshold used to filter messages.
+     */
+    private int threshold;
+
+    /**
+     * Creates a new logger that writes to {@link System#out} and has a threshold of {@link #INFO}.
+     */
+    public PrintStreamLogger()
+    {
+        this( System.out, INFO );
+    }
+
+    /**
+     * Creates a new logger that writes to the specified print stream.
+     * 
+     * @param out The print stream to write to, must not be <code>null</code>.
+     * @param threshold The threshold for the logger.
+     */
+    public PrintStreamLogger( PrintStream out, int threshold )
+    {
+        if ( out == null )
+        {
+            throw new NullPointerException( "missing output stream" );
+        }
+        this.out = out;
+        setThreshold( threshold );
+    }
+
+    /**
+     * Writes the specified message and exception to the print stream.
+     * 
+     * @param level The priority level of the message.
+     * @param message The message to log, may be <code>null</code>.
+     * @param error The exception to log, may be <code>null</code>.
+     */
+    private void log( int level, String message, Throwable error )
+    {
+        if ( level > threshold )
+        {
+            // don't log when it doesn't match your threshold.
+            return;
+        }
+
+        if ( message == null && error == null )
+        {
+            // don't log when there's nothing to log.
+            return;
+        }
+
+        StringBuffer buffer = new StringBuffer();
+
+        switch ( level )
+        {
+            case ( DEBUG ):
+            {
+                buffer.append( "[DEBUG]" );
+                break;
+            }
+            case ( INFO ):
+            {
+                buffer.append( "[INFO]" );
+                break;
+            }
+            case ( WARN ):
+            {
+                buffer.append( "[WARN]" );
+                break;
+            }
+            case ( ERROR ):
+            {
+                buffer.append( "[ERROR]" );
+                break;
+            }
+            case ( FATAL ):
+            {
+                buffer.append( "[FATAL]" );
+                break;
+            }
+        }
+
+        buffer.append( ' ' );
+
+        if ( message != null )
+        {
+            buffer.append( message );
+        }
+
+        if ( error != null )
+        {
+            StringWriter writer = new StringWriter();
+            PrintWriter pWriter = new PrintWriter( writer );
+
+            error.printStackTrace( pWriter );
+
+            if ( message != null )
+            {
+                buffer.append( '\n' );
+            }
+
+            buffer.append( "Error:\n" );
+            buffer.append( writer.toString() );
+        }
+
+        out.println( buffer.toString() );
+    }
+
+    public void debug( String message )
+    {
+        log( DEBUG, message, null );
+    }
+
+    public void debug( String message, Throwable throwable )
+    {
+        log( DEBUG, message, throwable );
+    }
+
+    public void info( String message )
+    {
+        log( INFO, message, null );
+    }
+
+    public void info( String message, Throwable throwable )
+    {
+        log( INFO, message, throwable );
+    }
+
+    public void warn( String message )
+    {
+        log( WARN, message, null );
+    }
+
+    public void warn( String message, Throwable throwable )
+    {
+        log( WARN, message, throwable );
+    }
+
+    public void error( String message )
+    {
+        log( ERROR, message, null );
+    }
+
+    public void error( String message, Throwable throwable )
+    {
+        log( ERROR, message, throwable );
+    }
+
+    public void fatalError( String message )
+    {
+        log( FATAL, message, null );
+    }
+
+    public void fatalError( String message, Throwable throwable )
+    {
+        log( FATAL, message, throwable );
+    }
+
+    public boolean isDebugEnabled()
+    {
+        return threshold >= DEBUG;
+    }
+
+    public boolean isErrorEnabled()
+    {
+        return threshold >= ERROR;
+    }
+
+    public boolean isFatalErrorEnabled()
+    {
+        return threshold >= FATAL;
+    }
+
+    public boolean isInfoEnabled()
+    {
+        return threshold >= INFO;
+    }
+
+    public boolean isWarnEnabled()
+    {
+        return threshold >= WARN;
+    }
+
+    public int getThreshold()
+    {
+        return threshold;
+    }
+
+    public void setThreshold( int threshold )
+    {
+        this.threshold = threshold;
+    }
+
+}

Propchange: maven/shared/trunk/maven-invoker/src/main/java/org/apache/maven/shared/invoker/PrintStreamLogger.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/shared/trunk/maven-invoker/src/main/java/org/apache/maven/shared/invoker/PrintStreamLogger.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Modified: maven/shared/trunk/maven-invoker/src/main/java/org/apache/maven/shared/invoker/SystemOutHandler.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-invoker/src/main/java/org/apache/maven/shared/invoker/SystemOutHandler.java?rev=661996&r1=661995&r2=661996&view=diff
==============================================================================
--- maven/shared/trunk/maven-invoker/src/main/java/org/apache/maven/shared/invoker/SystemOutHandler.java (original)
+++ maven/shared/trunk/maven-invoker/src/main/java/org/apache/maven/shared/invoker/SystemOutHandler.java Sat May 31 03:50:38 2008
@@ -19,37 +19,31 @@
  * under the License.
  */
 
+/**
+ * Offers an output handler that writes to {@link System#out}.
+ * 
+ * @version $Id$
+ */
 public class SystemOutHandler
-    implements InvocationOutputHandler
+    extends PrintStreamHandler
 {
 
-    private boolean alwaysFlush;
-
+    /**
+     * Creates a new output handler.
+     */
     public SystemOutHandler()
     {
         this( false );
     }
 
+    /**
+     * Creates a new output handler.
+     * 
+     * @param alwaysFlush A flag whether the print stream should be flushed after each line.
+     */
     public SystemOutHandler( boolean alwaysFlush )
     {
-        this.alwaysFlush = alwaysFlush;
-    }
-
-    public void consumeLine( String line )
-    {
-        if ( line == null )
-        {
-            System.out.println();
-        }
-        else
-        {
-            System.out.println( line );
-        }
-
-        if ( alwaysFlush )
-        {
-            System.out.flush();
-        }
+        super( System.out, alwaysFlush );
     }
 
 }

Modified: maven/shared/trunk/maven-invoker/src/main/java/org/apache/maven/shared/invoker/SystemOutLogger.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-invoker/src/main/java/org/apache/maven/shared/invoker/SystemOutLogger.java?rev=661996&r1=661995&r2=661996&view=diff
==============================================================================
--- maven/shared/trunk/maven-invoker/src/main/java/org/apache/maven/shared/invoker/SystemOutLogger.java (original)
+++ maven/shared/trunk/maven-invoker/src/main/java/org/apache/maven/shared/invoker/SystemOutLogger.java Sat May 31 03:50:38 2008
@@ -19,169 +19,21 @@
  * under the License.
  */
 
-import java.io.PrintWriter;
-import java.io.StringWriter;
-
+/**
+ * Offers a logger that writes to {@link System#out}.
+ * 
+ * @version $Id$
+ */
 public class SystemOutLogger
-    implements InvokerLogger
+    extends PrintStreamLogger
 {
 
-    private int threshold = INFO;
-
-    public void debug( String message )
-    {
-        log( DEBUG, message, null );
-    }
-
-    private void log( int level, String message, Throwable error )
-    {
-        if ( level > threshold )
-        {
-            // don't log when it doesn't match your threshold.
-            return;
-        }
-
-        if ( message == null && error == null )
-        {
-            // don't log when there's nothing to log.
-            return;
-        }
-
-        StringBuffer buffer = new StringBuffer();
-
-        switch ( level )
-        {
-            case ( DEBUG ):
-            {
-                buffer.append( "[DEBUG]" );
-                break;
-            }
-            case ( INFO ):
-            {
-                buffer.append( "[INFO]" );
-                break;
-            }
-            case ( WARN ):
-            {
-                buffer.append( "[WARN]" );
-                break;
-            }
-            case ( ERROR ):
-            {
-                buffer.append( "[ERROR]" );
-                break;
-            }
-            case ( FATAL ):
-            {
-                buffer.append( "[FATAL]" );
-                break;
-            }
-        }
-
-        buffer.append( ' ' );
-
-        if ( message != null )
-        {
-            buffer.append( message );
-        }
-
-        if ( error != null )
-        {
-            StringWriter writer = new StringWriter();
-            PrintWriter pWriter = new PrintWriter( writer );
-
-            error.printStackTrace( pWriter );
-
-            if ( message != null )
-            {
-                buffer.append( '\n' );
-            }
-
-            buffer.append( "Error:\n" );
-            buffer.append( writer.toString() );
-        }
-
-        System.out.println( buffer.toString() );
-    }
-
-    public void debug( String message, Throwable throwable )
-    {
-        log( DEBUG, message, throwable );
-    }
-
-    public void error( String message )
-    {
-        log( ERROR, message, null );
-    }
-
-    public void error( String message, Throwable throwable )
-    {
-        log( ERROR, message, throwable );
-    }
-
-    public void fatalError( String message )
-    {
-        log( FATAL, message, null );
-    }
-
-    public void fatalError( String message, Throwable throwable )
-    {
-        log( FATAL, message, throwable );
-    }
-
-    public int getThreshold()
-    {
-        return threshold;
-    }
-
-    public void info( String message )
-    {
-        log( INFO, message, null );
-    }
-
-    public void info( String message, Throwable throwable )
-    {
-        log( INFO, message, throwable );
-    }
-
-    public boolean isDebugEnabled()
-    {
-        return threshold >= DEBUG;
-    }
-
-    public boolean isErrorEnabled()
-    {
-        return threshold >= ERROR;
-    }
-
-    public boolean isFatalErrorEnabled()
-    {
-        return threshold >= FATAL;
-    }
-
-    public boolean isInfoEnabled()
-    {
-        return threshold >= INFO;
-    }
-
-    public boolean isWarnEnabled()
-    {
-        return threshold >= WARN;
-    }
-
-    public void warn( String message )
-    {
-        log( WARN, message, null );
-    }
-
-    public void warn( String message, Throwable throwable )
-    {
-        log( WARN, message, throwable );
-    }
-
-    public void setThreshold( int threshold )
+    /**
+     * Creates a new logger with a threshold of {@link #INFO}.
+     */
+    public SystemOutLogger()
     {
-        this.threshold = threshold;
+        super( System.out, INFO );
     }
 
 }