From d8b2070350f84ad2ee3152bad4dcdd6d6804f1e2 Mon Sep 17 00:00:00 2001 From: Benjamin Bentmann Date: Fri, 14 Aug 2009 11:17:27 +0000 Subject: [PATCH] o Fixed file logger to properly log exception stack traces git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@804164 13f79535-47bb-0310-9956-ffa450edef68 --- .../embedder/MavenEmbedderConsoleLogger.java | 84 +---------- .../embedder/MavenEmbedderFileLogger.java | 112 ++------------ .../MavenEmbedderPrintStreamLogger.java | 137 ++++++++++++++++++ 3 files changed, 155 insertions(+), 178 deletions(-) create mode 100644 maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedderPrintStreamLogger.java diff --git a/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedderConsoleLogger.java b/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedderConsoleLogger.java index 864d9848dc..93fde07bcf 100644 --- a/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedderConsoleLogger.java +++ b/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedderConsoleLogger.java @@ -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 @@ package org.apache.maven.embedder;/* * @author Avalon Development Team */ 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 MavenEmbedderConsoleLogger() + { + super( 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 ) - { - if ( isWarnEnabled() ) - { - System.out.print( "[WARNING] " ); - System.out.println( message ); - - if ( null != throwable ) - { - throwable.printStackTrace( 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() - { - } } diff --git a/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedderFileLogger.java b/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedderFileLogger.java index 057e2e3c19..76cbf5c5e9 100644 --- a/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedderFileLogger.java +++ b/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedderFileLogger.java @@ -20,122 +20,32 @@ package org.apache.maven.embedder; */ 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 ) + { + super( openStream( logFile ) ); + } + + private static PrintStream openStream( File logFile ) { try { - this.log = new PrintWriter( new FileWriter( logFile ) ); // platform encoding + return new PrintStream( logFile ); } - catch ( IOException e ) + catch ( FileNotFoundException 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 ); + throw new IllegalArgumentException( "Cannot open 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 ); - } - } - } - - public void error( String message, - Throwable throwable ) - { - if ( isErrorEnabled() ) - { - print( "[ERROR] " ); - println( message ); - - if ( null != throwable ) - { - throwable.printStackTrace( System.out ); - } - } - } - - public void fatalError( String message, - Throwable throwable ) - { - if ( isFatalErrorEnabled() ) - { - print( "[ERROR] " ); - println( message ); - - if ( null != throwable ) - { - throwable.printStackTrace( System.out ); - } - } - } - - protected void print( String message ) - { - log.print( message ); - } - - protected void println( String message ) - { - log.println( message ); - } - - public void close() - { - log.flush(); - - log.close(); - } } diff --git a/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedderPrintStreamLogger.java b/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedderPrintStreamLogger.java new file mode 100644 index 0000000000..2fdaee1ceb --- /dev/null +++ b/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedderPrintStreamLogger.java @@ -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(); + } + } + +}