Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
FileLogger |
|
| 1.5;1.5 | ||||
FileLogger$1 |
|
| 1.5;1.5 |
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.codehaus.plexus.util.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 1194996 2011-10-29 19:42:00Z olamy $ | |
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 | 8 | private boolean shouldFinalize = true; |
51 | ||
52 | /** | |
53 | * The optional mojo logger to additionally write messages to, can be <code>nulll</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 | 8 | this( outputFile, null ); |
67 | 8 | } |
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 | 8 | { |
79 | 8 | this.file = outputFile; |
80 | 8 | this.log = log; |
81 | ||
82 | 8 | outputFile.getParentFile().mkdirs(); |
83 | 8 | stream = new PrintStream( new FileOutputStream( outputFile ) ); |
84 | ||
85 | 8 | Runnable finalizer = new Runnable() |
86 | 8 | { |
87 | public void run() | |
88 | { | |
89 | try | |
90 | { | |
91 | 7 | finalize(); |
92 | } | |
93 | 0 | catch ( Throwable e ) |
94 | { | |
95 | // ignore | |
96 | 8 | } |
97 | 7 | } |
98 | }; | |
99 | ||
100 | 8 | Runtime.getRuntime().addShutdownHook( new Thread( finalizer ) ); |
101 | 8 | } |
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 | 0 | 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 | 8 | 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 | 16 | stream.println( line ); |
131 | 16 | stream.flush(); |
132 | ||
133 | 16 | if ( log != null ) |
134 | { | |
135 | 0 | log.info( line ); |
136 | } | |
137 | 16 | } |
138 | ||
139 | /** | |
140 | * Closes the underlying file stream. | |
141 | */ | |
142 | public void close() | |
143 | { | |
144 | 0 | if ( stream != null ) |
145 | { | |
146 | 0 | stream.flush(); |
147 | } | |
148 | ||
149 | 0 | IOUtil.close( stream ); |
150 | 0 | } |
151 | ||
152 | /** | |
153 | * Closes the underlying file stream. | |
154 | */ | |
155 | protected void finalize() | |
156 | { | |
157 | 0 | if ( shouldFinalize ) |
158 | { | |
159 | 0 | close(); |
160 | } | |
161 | 0 | } |
162 | } |