1 | |
package org.apache.maven.plugin.invoker; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
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 | |
|
33 | |
|
34 | |
public class FileLogger |
35 | |
implements InvocationOutputHandler |
36 | |
{ |
37 | |
|
38 | |
private PrintStream stream; |
39 | |
|
40 | 4 | private boolean shouldFinalize = true; |
41 | |
|
42 | |
private final Log log; |
43 | |
|
44 | |
public FileLogger( File outputFile ) |
45 | |
throws IOException |
46 | |
{ |
47 | 4 | this( outputFile, null ); |
48 | 4 | } |
49 | |
|
50 | |
public FileLogger( File outputFile, Log log ) |
51 | |
throws IOException |
52 | 4 | { |
53 | 4 | this.log = log; |
54 | 4 | stream = new PrintStream( new FileOutputStream( outputFile ) ); |
55 | |
|
56 | 4 | Runnable finalizer = new Runnable() |
57 | |
{ |
58 | 4 | public void run() |
59 | |
{ |
60 | |
try |
61 | |
{ |
62 | 2 | finalize(); |
63 | |
} |
64 | 0 | catch ( Throwable e ) |
65 | |
{ |
66 | |
|
67 | 2 | } |
68 | 2 | } |
69 | |
}; |
70 | |
|
71 | 4 | Runtime.getRuntime().addShutdownHook( new Thread( finalizer ) ); |
72 | 4 | } |
73 | |
|
74 | |
public PrintStream getPrintStream() |
75 | |
{ |
76 | 4 | return stream; |
77 | |
} |
78 | |
|
79 | |
public void consumeLine( String line ) |
80 | |
{ |
81 | 54 | stream.println( line ); |
82 | 54 | stream.flush(); |
83 | |
|
84 | 54 | if ( log != null ) |
85 | |
{ |
86 | 0 | log.info( line ); |
87 | |
} |
88 | 54 | } |
89 | |
|
90 | |
public void close() |
91 | |
{ |
92 | 4 | if ( stream != null ) |
93 | |
{ |
94 | 4 | stream.flush(); |
95 | |
} |
96 | |
|
97 | 4 | IOUtil.close( stream ); |
98 | 4 | } |
99 | |
|
100 | |
public void finalize() |
101 | |
{ |
102 | 0 | if ( shouldFinalize ) |
103 | |
{ |
104 | 0 | close(); |
105 | |
} |
106 | 0 | } |
107 | |
} |