1 package org.apache.maven.plugin.invoker; 2 3 /* 4 * Licensed to the Apache Software Foundation (ASF) under one 5 * or more contributor license agreements. See the NOTICE file 6 * distributed with this work for additional information 7 * regarding copyright ownership. The ASF licenses this file 8 * to you under the Apache License, Version 2.0 (the 9 * "License"); you may not use this file except in compliance 10 * with the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, 15 * software distributed under the License is distributed on an 16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 * KIND, either express or implied. See the License for the 18 * specific language governing permissions and limitations 19 * under the License. 20 */ 21 22 import org.apache.maven.plugin.logging.Log; 23 import org.apache.maven.shared.invoker.InvocationOutputHandler; 24 import org.codehaus.plexus.util.IOUtil; 25 26 import java.io.File; 27 import java.io.FileOutputStream; 28 import java.io.IOException; 29 import java.io.PrintStream; 30 31 /** 32 * @version $Id: FileLogger.java 684034 2008-08-08 18:20:31Z bentmann $ 33 */ 34 class FileLogger 35 implements InvocationOutputHandler 36 { 37 38 /** 39 * The path to the log file. 40 */ 41 private File file; 42 43 /** 44 * The underlying file stream this logger writes to. 45 */ 46 private PrintStream stream; 47 48 /** 49 * A flag whether the output stream should be closed during finalization of this logger. 50 */ 51 private boolean shouldFinalize = true; 52 53 /** 54 * The optional mojo logger to additionally write messages to, can be <code>nulll</code>. 55 */ 56 private final Log log; 57 58 /** 59 * Creates a new logger that writes to the specified file. 60 * 61 * @param outputFile The path to the output file, must not be <code>null</code>. 62 * @throws IOException If the output file could not be created. 63 */ 64 public FileLogger( File outputFile ) 65 throws IOException 66 { 67 this( outputFile, null ); 68 } 69 70 /** 71 * Creates a new logger that writes to the specified file and optionally mirrors messages to the given mojo logger. 72 * 73 * @param outputFile The path to the output file, must not be <code>null</code>. 74 * @param log The mojo logger to additionally output messages to, may be <code>null</code> if not used. 75 * @throws IOException If the output file could not be created. 76 */ 77 public FileLogger( File outputFile, Log log ) 78 throws IOException 79 { 80 this.file = outputFile; 81 this.log = log; 82 83 outputFile.getParentFile().mkdirs(); 84 stream = new PrintStream( new FileOutputStream( outputFile ) ); 85 86 Runnable finalizer = new Runnable() 87 { 88 public void run() 89 { 90 try 91 { 92 finalize(); 93 } 94 catch ( Throwable e ) 95 { 96 // ignore 97 } 98 } 99 }; 100 101 Runtime.getRuntime().addShutdownHook( new Thread( finalizer ) ); 102 } 103 104 /** 105 * Gets the path to the output file. 106 * 107 * @return The path to the output file, never <code>null</code>. 108 */ 109 public File getOutputFile() 110 { 111 return file; 112 } 113 114 /** 115 * Gets the underlying stream used to write message to the log file. 116 * 117 * @return The underlying stream used to write message to the log file, never <code>null</code>. 118 */ 119 public PrintStream getPrintStream() 120 { 121 return stream; 122 } 123 124 /** 125 * Writes the specified line to the log file and optionally to the mojo logger. 126 * 127 * @param line The message to log. 128 */ 129 public void consumeLine( String line ) 130 { 131 stream.println( line ); 132 stream.flush(); 133 134 if ( log != null ) 135 { 136 log.info( line ); 137 } 138 } 139 140 /** 141 * Closes the underlying file stream. 142 */ 143 public void close() 144 { 145 if ( stream != null ) 146 { 147 stream.flush(); 148 } 149 150 IOUtil.close( stream ); 151 } 152 153 /** 154 * Closes the underlying file stream. 155 */ 156 protected void finalize() 157 { 158 if ( shouldFinalize ) 159 { 160 close(); 161 } 162 } 163 }