1 package org.apache.maven.doxia.logging;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.PrintWriter;
23 import java.io.StringWriter;
24
25
26
27
28
29
30
31
32
33
34 public class SystemStreamLog
35 implements Log
36 {
37 private static final String EOL = System.getProperty( "line.separator" );
38
39 private int currentLevel = LEVEL_INFO;
40
41
42 public void setLogLevel( int level )
43 {
44 if ( level <= LEVEL_DEBUG )
45 {
46 currentLevel = LEVEL_DEBUG;
47 }
48 else if ( level <= LEVEL_INFO )
49 {
50 currentLevel = LEVEL_INFO;
51 }
52 else if ( level <= LEVEL_WARN )
53 {
54 currentLevel = LEVEL_WARN;
55 }
56 else if ( level <= LEVEL_ERROR )
57 {
58 currentLevel = LEVEL_ERROR;
59 }
60 else
61 {
62 currentLevel = LEVEL_DISABLED;
63 }
64 }
65
66
67
68
69
70
71 public void debug( CharSequence content )
72 {
73 if ( isDebugEnabled() )
74 {
75 print( "debug", content );
76 }
77 }
78
79
80 public void debug( CharSequence content, Throwable error )
81 {
82 if ( isDebugEnabled() )
83 {
84 print( "debug", content, error );
85 }
86 }
87
88
89 public void debug( Throwable error )
90 {
91 if ( isDebugEnabled() )
92 {
93 print( "debug", error );
94 }
95 }
96
97
98 public void info( CharSequence content )
99 {
100 if ( isInfoEnabled() )
101 {
102 print( "info", content );
103 }
104 }
105
106
107 public void info( CharSequence content, Throwable error )
108 {
109 if ( isInfoEnabled() )
110 {
111 print( "info", content, error );
112 }
113 }
114
115
116
117
118
119
120 public void info( Throwable error )
121 {
122 if ( isInfoEnabled() )
123 {
124 print( "info", error );
125 }
126 }
127
128
129
130
131
132
133 public void warn( CharSequence content )
134 {
135 if ( isWarnEnabled() )
136 {
137 print( "warn", content );
138 }
139 }
140
141
142 public void warn( CharSequence content, Throwable error )
143 {
144 if ( isWarnEnabled() )
145 {
146 print( "warn", content, error );
147 }
148 }
149
150
151 public void warn( Throwable error )
152 {
153 if ( isWarnEnabled() )
154 {
155 print( "warn", error );
156 }
157 }
158
159
160 public void error( CharSequence content )
161 {
162 if ( isErrorEnabled() )
163 {
164 System.err.println( "[error] " + content.toString() );
165 }
166 }
167
168
169 public void error( CharSequence content, Throwable error )
170 {
171 if ( isErrorEnabled() )
172 {
173 StringWriter sWriter = new StringWriter();
174 PrintWriter pWriter = new PrintWriter( sWriter );
175
176 error.printStackTrace( pWriter );
177
178 System.err.println( "[error] " + content.toString()
179 + EOL + EOL + sWriter.toString() );
180 }
181 }
182
183
184
185
186
187
188 public void error( Throwable error )
189 {
190 if ( isErrorEnabled() )
191 {
192 StringWriter sWriter = new StringWriter();
193 PrintWriter pWriter = new PrintWriter( sWriter );
194
195 error.printStackTrace( pWriter );
196
197 System.err.println( "[error] " + sWriter.toString() );
198 }
199 }
200
201
202
203
204
205
206 public boolean isDebugEnabled()
207 {
208 return ( currentLevel <= LEVEL_DEBUG );
209 }
210
211
212
213
214
215
216 public boolean isInfoEnabled()
217 {
218 return ( currentLevel <= LEVEL_INFO );
219 }
220
221
222
223
224
225
226 public boolean isWarnEnabled()
227 {
228 return ( currentLevel <= LEVEL_WARN );
229 }
230
231
232
233
234
235
236 public boolean isErrorEnabled()
237 {
238 return ( currentLevel <= LEVEL_ERROR );
239 }
240
241
242
243
244
245 private void print( String prefix, CharSequence content )
246 {
247 System.out.println( "[" + prefix + "] " + content.toString() );
248 }
249
250 private void print( String prefix, Throwable error )
251 {
252 StringWriter sWriter = new StringWriter();
253 PrintWriter pWriter = new PrintWriter( sWriter );
254
255 error.printStackTrace( pWriter );
256
257 System.out.println( "[" + prefix + "] " + sWriter.toString() );
258 }
259
260 private void print( String prefix, CharSequence content, Throwable error )
261 {
262 StringWriter sWriter = new StringWriter();
263 PrintWriter pWriter = new PrintWriter( sWriter );
264
265 error.printStackTrace( pWriter );
266
267 System.out.println( "[" + prefix + "] " + content.toString()
268 + EOL + EOL + sWriter.toString() );
269 }
270 }