1 | |
package org.apache.maven.shared.invoker; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.io.PrintStream; |
23 | |
import java.io.PrintWriter; |
24 | |
import java.io.StringWriter; |
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
public class PrintStreamLogger |
33 | |
implements InvokerLogger |
34 | |
{ |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
private PrintStream out; |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
private int threshold; |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
public PrintStreamLogger() |
50 | |
{ |
51 | 0 | this( System.out, INFO ); |
52 | 0 | } |
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
public PrintStreamLogger( PrintStream out, int threshold ) |
61 | 39 | { |
62 | 39 | if ( out == null ) |
63 | |
{ |
64 | 0 | throw new NullPointerException( "missing output stream" ); |
65 | |
} |
66 | 39 | this.out = out; |
67 | 39 | setThreshold( threshold ); |
68 | 39 | } |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
private void log( int level, String message, Throwable error ) |
78 | |
{ |
79 | 50 | if ( level > threshold ) |
80 | |
{ |
81 | |
|
82 | 13 | return; |
83 | |
} |
84 | |
|
85 | 37 | if ( message == null && error == null ) |
86 | |
{ |
87 | |
|
88 | 4 | return; |
89 | |
} |
90 | |
|
91 | 33 | StringBuffer buffer = new StringBuffer(); |
92 | |
|
93 | 33 | switch ( level ) |
94 | |
{ |
95 | |
case ( DEBUG ): |
96 | |
{ |
97 | 13 | buffer.append( "[DEBUG]" ); |
98 | 13 | break; |
99 | |
} |
100 | |
case ( INFO ): |
101 | |
{ |
102 | 4 | buffer.append( "[INFO]" ); |
103 | 4 | break; |
104 | |
} |
105 | |
case ( WARN ): |
106 | |
{ |
107 | 8 | buffer.append( "[WARN]" ); |
108 | 8 | break; |
109 | |
} |
110 | |
case ( ERROR ): |
111 | |
{ |
112 | 4 | buffer.append( "[ERROR]" ); |
113 | 4 | break; |
114 | |
} |
115 | |
case ( FATAL ): |
116 | |
{ |
117 | 4 | buffer.append( "[FATAL]" ); |
118 | |
break; |
119 | |
} |
120 | |
} |
121 | |
|
122 | 33 | buffer.append( ' ' ); |
123 | |
|
124 | 33 | if ( message != null ) |
125 | |
{ |
126 | 29 | buffer.append( message ); |
127 | |
} |
128 | |
|
129 | 33 | if ( error != null ) |
130 | |
{ |
131 | 8 | StringWriter writer = new StringWriter(); |
132 | 8 | PrintWriter pWriter = new PrintWriter( writer ); |
133 | |
|
134 | 8 | error.printStackTrace( pWriter ); |
135 | |
|
136 | 8 | if ( message != null ) |
137 | |
{ |
138 | 4 | buffer.append( '\n' ); |
139 | |
} |
140 | |
|
141 | 8 | buffer.append( "Error:\n" ); |
142 | 8 | buffer.append( writer.toString() ); |
143 | |
} |
144 | |
|
145 | 33 | out.println( buffer.toString() ); |
146 | 33 | } |
147 | |
|
148 | |
public void debug( String message ) |
149 | |
{ |
150 | 23 | log( DEBUG, message, null ); |
151 | 23 | } |
152 | |
|
153 | |
public void debug( String message, Throwable throwable ) |
154 | |
{ |
155 | 3 | log( DEBUG, message, throwable ); |
156 | 3 | } |
157 | |
|
158 | |
public void info( String message ) |
159 | |
{ |
160 | 2 | log( INFO, message, null ); |
161 | 2 | } |
162 | |
|
163 | |
public void info( String message, Throwable throwable ) |
164 | |
{ |
165 | 3 | log( INFO, message, throwable ); |
166 | 3 | } |
167 | |
|
168 | |
public void warn( String message ) |
169 | |
{ |
170 | 6 | log( WARN, message, null ); |
171 | 6 | } |
172 | |
|
173 | |
public void warn( String message, Throwable throwable ) |
174 | |
{ |
175 | 3 | log( WARN, message, throwable ); |
176 | 3 | } |
177 | |
|
178 | |
public void error( String message ) |
179 | |
{ |
180 | 2 | log( ERROR, message, null ); |
181 | 2 | } |
182 | |
|
183 | |
public void error( String message, Throwable throwable ) |
184 | |
{ |
185 | 3 | log( ERROR, message, throwable ); |
186 | 3 | } |
187 | |
|
188 | |
public void fatalError( String message ) |
189 | |
{ |
190 | 2 | log( FATAL, message, null ); |
191 | 2 | } |
192 | |
|
193 | |
public void fatalError( String message, Throwable throwable ) |
194 | |
{ |
195 | 3 | log( FATAL, message, throwable ); |
196 | 3 | } |
197 | |
|
198 | |
public boolean isDebugEnabled() |
199 | |
{ |
200 | 11 | return threshold >= DEBUG; |
201 | |
} |
202 | |
|
203 | |
public boolean isErrorEnabled() |
204 | |
{ |
205 | 5 | return threshold >= ERROR; |
206 | |
} |
207 | |
|
208 | |
public boolean isFatalErrorEnabled() |
209 | |
{ |
210 | 5 | return threshold >= FATAL; |
211 | |
} |
212 | |
|
213 | |
public boolean isInfoEnabled() |
214 | |
{ |
215 | 5 | return threshold >= INFO; |
216 | |
} |
217 | |
|
218 | |
public boolean isWarnEnabled() |
219 | |
{ |
220 | 5 | return threshold >= WARN; |
221 | |
} |
222 | |
|
223 | |
public int getThreshold() |
224 | |
{ |
225 | 1 | return threshold; |
226 | |
} |
227 | |
|
228 | |
public void setThreshold( int threshold ) |
229 | |
{ |
230 | 50 | this.threshold = threshold; |
231 | 50 | } |
232 | |
|
233 | |
} |