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 this( System.out, INFO );
52 }
53
54
55
56
57
58
59
60 public PrintStreamLogger( PrintStream out, int threshold )
61 {
62 if ( out == null )
63 {
64 throw new NullPointerException( "missing output stream" );
65 }
66 this.out = out;
67 setThreshold( threshold );
68 }
69
70
71
72
73
74
75
76
77 private void log( int level, String message, Throwable error )
78 {
79 if ( level > threshold )
80 {
81
82 return;
83 }
84
85 if ( message == null && error == null )
86 {
87
88 return;
89 }
90
91 StringBuffer buffer = new StringBuffer();
92
93 switch ( level )
94 {
95 case ( DEBUG ):
96 {
97 buffer.append( "[DEBUG]" );
98 break;
99 }
100 case ( INFO ):
101 {
102 buffer.append( "[INFO]" );
103 break;
104 }
105 case ( WARN ):
106 {
107 buffer.append( "[WARN]" );
108 break;
109 }
110 case ( ERROR ):
111 {
112 buffer.append( "[ERROR]" );
113 break;
114 }
115 case ( FATAL ):
116 {
117 buffer.append( "[FATAL]" );
118 break;
119 }
120 }
121
122 buffer.append( ' ' );
123
124 if ( message != null )
125 {
126 buffer.append( message );
127 }
128
129 if ( error != null )
130 {
131 StringWriter writer = new StringWriter();
132 PrintWriter pWriter = new PrintWriter( writer );
133
134 error.printStackTrace( pWriter );
135
136 if ( message != null )
137 {
138 buffer.append( '\n' );
139 }
140
141 buffer.append( "Error:\n" );
142 buffer.append( writer.toString() );
143 }
144
145 out.println( buffer.toString() );
146 }
147
148 public void debug( String message )
149 {
150 log( DEBUG, message, null );
151 }
152
153 public void debug( String message, Throwable throwable )
154 {
155 log( DEBUG, message, throwable );
156 }
157
158 public void info( String message )
159 {
160 log( INFO, message, null );
161 }
162
163 public void info( String message, Throwable throwable )
164 {
165 log( INFO, message, throwable );
166 }
167
168 public void warn( String message )
169 {
170 log( WARN, message, null );
171 }
172
173 public void warn( String message, Throwable throwable )
174 {
175 log( WARN, message, throwable );
176 }
177
178 public void error( String message )
179 {
180 log( ERROR, message, null );
181 }
182
183 public void error( String message, Throwable throwable )
184 {
185 log( ERROR, message, throwable );
186 }
187
188 public void fatalError( String message )
189 {
190 log( FATAL, message, null );
191 }
192
193 public void fatalError( String message, Throwable throwable )
194 {
195 log( FATAL, message, throwable );
196 }
197
198 public boolean isDebugEnabled()
199 {
200 return threshold >= DEBUG;
201 }
202
203 public boolean isErrorEnabled()
204 {
205 return threshold >= ERROR;
206 }
207
208 public boolean isFatalErrorEnabled()
209 {
210 return threshold >= FATAL;
211 }
212
213 public boolean isInfoEnabled()
214 {
215 return threshold >= INFO;
216 }
217
218 public boolean isWarnEnabled()
219 {
220 return threshold >= WARN;
221 }
222
223 public int getThreshold()
224 {
225 return threshold;
226 }
227
228 public void setThreshold( int threshold )
229 {
230 this.threshold = threshold;
231 }
232
233 }