1 package org.apache.maven.shared.scriptinterpreter;
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.utils.io.IOUtil;
24
25 import java.io.File;
26 import java.io.FileOutputStream;
27 import java.io.IOException;
28 import java.io.PrintStream;
29
30 /**
31 * @version $Id: FileLogger.java 1389141 2012-09-23 20:29:01Z hboutemy $
32 */
33 public class FileLogger
34 implements ExecutionLogger
35 {
36
37 /**
38 * The path to the log file.
39 */
40 private File file;
41
42 /**
43 * The underlying file stream this logger writes to.
44 */
45 private PrintStream stream;
46
47 /**
48 * A flag whether the output stream should be closed during finalization of this logger.
49 */
50 private boolean shouldFinalize = true;
51
52 /**
53 * The optional mojo logger to additionally write messages to, can be <code>null</code>.
54 */
55 private final Log log;
56
57 /**
58 * Creates a new logger that writes to the specified file.
59 *
60 * @param outputFile The path to the output file, must not be <code>null</code>.
61 * @throws java.io.IOException If the output file could not be created.
62 */
63 public FileLogger( File outputFile )
64 throws IOException
65 {
66 this( outputFile, null );
67 }
68
69 /**
70 * Creates a new logger that writes to the specified file and optionally mirrors messages to the given mojo logger.
71 *
72 * @param outputFile The path to the output file, must not be <code>null</code>.
73 * @param log The mojo logger to additionally output messages to, may be <code>null</code> if not used.
74 * @throws java.io.IOException If the output file could not be created.
75 */
76 public FileLogger( File outputFile, Log log )
77 throws IOException
78 {
79 this.file = outputFile;
80 this.log = log;
81
82 outputFile.getParentFile().mkdirs();
83 stream = new PrintStream( new FileOutputStream( outputFile ) );
84
85 Runnable finalizer = new Runnable()
86 {
87 public void run()
88 {
89 try
90 {
91 finalize();
92 }
93 catch ( Throwable e )
94 {
95 // ignore
96 }
97 }
98 };
99
100 Runtime.getRuntime().addShutdownHook( new Thread( finalizer ) );
101 }
102
103 /**
104 * Gets the path to the output file.
105 *
106 * @return The path to the output file, never <code>null</code>.
107 */
108 public File getOutputFile()
109 {
110 return file;
111 }
112
113 /**
114 * Gets the underlying stream used to write message to the log file.
115 *
116 * @return The underlying stream used to write message to the log file, never <code>null</code>.
117 */
118 public PrintStream getPrintStream()
119 {
120 return stream;
121 }
122
123 /**
124 * Writes the specified line to the log file and optionally to the mojo logger.
125 *
126 * @param line The message to log.
127 */
128 public void consumeLine( String line )
129 {
130 stream.println( line );
131 stream.flush();
132
133 if ( log != null )
134 {
135 log.info( line );
136 }
137 }
138
139 /**
140 * Closes the underlying file stream.
141 */
142 public void close()
143 {
144 if ( stream != null )
145 {
146 stream.flush();
147 }
148
149 IOUtil.close( stream );
150 }
151
152 /**
153 * Closes the underlying file stream.
154 */
155 protected void finalize()
156 {
157 if ( shouldFinalize )
158 {
159 close();
160 }
161 }
162 }