maven-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bentm...@apache.org
Subject svn commit: r804164 - in /maven/components/trunk/maven-embedder/src/main/java/org/apache/maven/embedder: MavenEmbedderConsoleLogger.java MavenEmbedderFileLogger.java MavenEmbedderPrintStreamLogger.java
Date Fri, 14 Aug 2009 11:17:27 GMT
Author: bentmann
Date: Fri Aug 14 11:17:27 2009
New Revision: 804164

URL: http://svn.apache.org/viewvc?rev=804164&view=rev
Log:
o Fixed file logger to properly log exception stack traces

Added:
    maven/components/trunk/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedderPrintStreamLogger.java
  (with props)
Modified:
    maven/components/trunk/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedderConsoleLogger.java
    maven/components/trunk/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedderFileLogger.java

Modified: maven/components/trunk/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedderConsoleLogger.java
URL: http://svn.apache.org/viewvc/maven/components/trunk/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedderConsoleLogger.java?rev=804164&r1=804163&r2=804164&view=diff
==============================================================================
--- maven/components/trunk/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedderConsoleLogger.java
(original)
+++ maven/components/trunk/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedderConsoleLogger.java
Fri Aug 14 11:17:27 2009
@@ -1,4 +1,6 @@
-package org.apache.maven.embedder;/*
+package org.apache.maven.embedder;
+
+/*
  * 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
@@ -25,84 +27,12 @@
  * @author <a href="mailto:dev@avalon.codehaus.org">Avalon Development Team</a>
  */
 public final class MavenEmbedderConsoleLogger
-    extends AbstractMavenEmbedderLogger
+    extends MavenEmbedderPrintStreamLogger
 {
-    public void debug( String message,
-                       Throwable throwable )
-    {
-        if ( isDebugEnabled() )
-        {
-            System.out.print( "[DEBUG] " );
-            System.out.println( message );
-
-            if ( null != throwable )
-            {
-                throwable.printStackTrace( System.out );
-            }
-        }
-    }
-
-    public void info( String message,
-                      Throwable throwable )
-    {
-        if ( isInfoEnabled() )
-        {
-            System.out.print( "[INFO] " );
-            System.out.println( message );
-
-            if ( null != throwable )
-            {
-                throwable.printStackTrace( System.out );
-            }
-        }
-    }
 
-    public void warn( String message,
-                      Throwable throwable )
+    public MavenEmbedderConsoleLogger()
     {
-        if ( isWarnEnabled() )
-        {
-            System.out.print( "[WARNING] " );
-            System.out.println( message );
-
-            if ( null != throwable )
-            {
-                throwable.printStackTrace( System.out );
-            }
-        }
+        super( System.out );
     }
 
-    public void error( String message,
-                       Throwable throwable )
-    {
-        if ( isErrorEnabled() )
-        {
-            System.out.print( "[ERROR] " );
-            System.out.println( message );
-
-            if ( null != throwable )
-            {
-                throwable.printStackTrace( System.out );
-            }
-        }
-    }
-
-    public void fatalError( String message,
-                            Throwable throwable )
-    {
-        if ( isFatalErrorEnabled() )
-        {
-            System.out.print( "[ERROR] " );
-            System.out.println( message );
-
-            if ( null != throwable )
-            {
-                throwable.printStackTrace( System.out );
-            }
-        }
-    }
-
-    public void close()
-    {
-    }
 }

Modified: maven/components/trunk/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedderFileLogger.java
URL: http://svn.apache.org/viewvc/maven/components/trunk/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedderFileLogger.java?rev=804164&r1=804163&r2=804164&view=diff
==============================================================================
--- maven/components/trunk/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedderFileLogger.java
(original)
+++ maven/components/trunk/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedderFileLogger.java
Fri Aug 14 11:17:27 2009
@@ -20,122 +20,32 @@
  */
 
 import java.io.File;
-import java.io.FileWriter;
-import java.io.IOException;
-import java.io.PrintWriter;
+import java.io.FileNotFoundException;
+import java.io.PrintStream;
 
 /**
  * @author Jason van Zyl
  * @todo document the need to call close() once successfully constructed, otherwise file
handles can be leaked. Might be good to add a finalizer too, just in case.
  */
 public final class MavenEmbedderFileLogger
-    extends AbstractMavenEmbedderLogger
+    extends MavenEmbedderPrintStreamLogger
 {
-    private PrintWriter log;
 
     public MavenEmbedderFileLogger( File logFile )
     {
-        try
-        {
-            this.log = new PrintWriter( new FileWriter( logFile ) ); // platform encoding
-        }
-        catch ( IOException e )
-        {
-            // The client must make sure the file is valid.
-            // TODO: [BP] would just throwing the IOE be better? We can't just ignore it,
since that would give misleading NPE's later
-            throw new RuntimeException( "The embedder was unable to write to the specified
log file: " + logFile, e );
-        }
-    }
-
-    public void debug( String message,
-                       Throwable throwable )
-    {
-        if ( isDebugEnabled() )
-        {
-            print( "[DEBUG] " );
-            println( message );
-
-            if ( null != throwable )
-            {
-                throwable.printStackTrace( System.out );
-            }
-        }
-    }
-
-    public void info( String message,
-                      Throwable throwable )
-    {
-        if ( isInfoEnabled() )
-        {
-            print( "[INFO] " );
-            println( message );
-
-            if ( null != throwable )
-            {
-                throwable.printStackTrace( System.out );
-            }
-        }
-    }
-
-    public void warn( String message,
-                      Throwable throwable )
-    {
-        if ( isWarnEnabled() )
-        {
-            print( "[WARNING] " );
-            println( message );
-
-            if ( null != throwable )
-            {
-                throwable.printStackTrace( System.out );
-            }
-        }
+        super( openStream( logFile ) );
     }
 
-    public void error( String message,
-                       Throwable throwable )
+    private static PrintStream openStream( File logFile )
     {
-        if ( isErrorEnabled() )
+        try
         {
-            print( "[ERROR] " );
-            println( message );
-
-            if ( null != throwable )
-            {
-                throwable.printStackTrace( System.out );
-            }
+            return new PrintStream( logFile );
         }
-    }
-
-    public void fatalError( String message,
-                            Throwable throwable )
-    {
-        if ( isFatalErrorEnabled() )
+        catch ( FileNotFoundException e )
         {
-            print( "[ERROR] " );
-            println( message );
-
-            if ( null != throwable )
-            {
-                throwable.printStackTrace( System.out );
-            }
+            throw new IllegalArgumentException( "Cannot open specified log file " + logFile,
e );
         }
     }
 
-    protected void print( String message )
-    {
-        log.print( message );
-    }
-
-    protected void println( String message )
-    {
-        log.println( message );
-    }
-
-    public void close()
-    {
-        log.flush();
-
-        log.close();
-    }
 }

Added: maven/components/trunk/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedderPrintStreamLogger.java
URL: http://svn.apache.org/viewvc/maven/components/trunk/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedderPrintStreamLogger.java?rev=804164&view=auto
==============================================================================
--- maven/components/trunk/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedderPrintStreamLogger.java
(added)
+++ maven/components/trunk/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedderPrintStreamLogger.java
Fri Aug 14 11:17:27 2009
@@ -0,0 +1,137 @@
+package org.apache.maven.embedder;
+
+/*
+ * 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;
+
+/**
+ * Logs to a user-supplied {@link PrintStream}.
+ * 
+ * @author Benjamin Bentmann
+ */
+public class MavenEmbedderPrintStreamLogger
+    extends AbstractMavenEmbedderLogger
+{
+
+    private final PrintStream out;
+
+    private static final String FATAL_ERROR = "[FATAL] ";
+
+    private static final String ERROR = "[ERROR] ";
+
+    private static final String WARNING = "[WARNING] ";
+
+    private static final String INFO = "[INFO] ";
+
+    private static final String DEBUG = "[DEBUG] ";
+
+    public MavenEmbedderPrintStreamLogger( PrintStream out )
+    {
+        if ( out == null )
+        {
+            throw new IllegalArgumentException( "output stream missing" );
+        }
+
+        this.out = out;
+    }
+
+    public void debug( String message, Throwable throwable )
+    {
+        if ( isDebugEnabled() )
+        {
+            out.print( DEBUG );
+            out.println( message );
+
+            if ( null != throwable )
+            {
+                throwable.printStackTrace( out );
+            }
+        }
+    }
+
+    public void info( String message, Throwable throwable )
+    {
+        if ( isInfoEnabled() )
+        {
+            out.print( INFO );
+            out.println( message );
+
+            if ( null != throwable )
+            {
+                throwable.printStackTrace( out );
+            }
+        }
+    }
+
+    public void warn( String message, Throwable throwable )
+    {
+        if ( isWarnEnabled() )
+        {
+            out.print( WARNING );
+            out.println( message );
+
+            if ( null != throwable )
+            {
+                throwable.printStackTrace( out );
+            }
+        }
+    }
+
+    public void error( String message, Throwable throwable )
+    {
+        if ( isErrorEnabled() )
+        {
+            out.print( ERROR );
+            out.println( message );
+
+            if ( null != throwable )
+            {
+                throwable.printStackTrace( out );
+            }
+        }
+    }
+
+    public void fatalError( String message, Throwable throwable )
+    {
+        if ( isFatalErrorEnabled() )
+        {
+            out.print( FATAL_ERROR );
+            out.println( message );
+
+            if ( null != throwable )
+            {
+                throwable.printStackTrace( out );
+            }
+        }
+    }
+
+    public void close()
+    {
+        if ( out == System.out || out == System.err )
+        {
+            out.flush();
+        }
+        else
+        {
+            out.close();
+        }
+    }
+
+}

Propchange: maven/components/trunk/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedderPrintStreamLogger.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/components/trunk/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedderPrintStreamLogger.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision



Mime
View raw message