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 private boolean shouldFinalize = true;
41
42 private final Log log;
43
44 public FileLogger( File outputFile )
45 throws IOException
46 {
47 this( outputFile, null );
48 }
49
50 public FileLogger( File outputFile, Log log )
51 throws IOException
52 {
53 this.log = log;
54 stream = new PrintStream( new FileOutputStream( outputFile ) );
55
56 Runnable finalizer = new Runnable()
57 {
58 public void run()
59 {
60 try
61 {
62 finalize();
63 }
64 catch ( Throwable e )
65 {
66
67 }
68 }
69 };
70
71 Runtime.getRuntime().addShutdownHook( new Thread( finalizer ) );
72 }
73
74 public PrintStream getPrintStream()
75 {
76 return stream;
77 }
78
79 public void consumeLine( String line )
80 {
81 stream.println( line );
82 stream.flush();
83
84 if ( log != null )
85 {
86 log.info( line );
87 }
88 }
89
90 public void close()
91 {
92 if ( stream != null )
93 {
94 stream.flush();
95 }
96
97 IOUtil.close( stream );
98 }
99
100 public void finalize()
101 {
102 if ( shouldFinalize )
103 {
104 close();
105 }
106 }
107 }