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
35 public class SystemStreamLog
36 implements Log
37 {
38 private static final String EOL = System.getProperty( "line.separator" );
39
40 private int currentLevel = LEVEL_INFO;
41
42
43 public void setLogLevel( int level )
44 {
45 if ( level <= LEVEL_DEBUG )
46 {
47 currentLevel = LEVEL_DEBUG;
48 }
49 else if ( level <= LEVEL_INFO )
50 {
51 currentLevel = LEVEL_INFO;
52 }
53 else if ( level <= LEVEL_WARN )
54 {
55 currentLevel = LEVEL_WARN;
56 }
57 else if ( level <= LEVEL_ERROR )
58 {
59 currentLevel = LEVEL_ERROR;
60 }
61 else
62 {
63 currentLevel = LEVEL_DISABLED;
64 }
65 }
66
67
68 public void debug( CharSequence content )
69 {
70 if ( isDebugEnabled() )
71 {
72 print( "debug", content );
73 }
74 }
75
76
77 public void debug( CharSequence content, Throwable error )
78 {
79 if ( isDebugEnabled() )
80 {
81 print( "debug", content, error );
82 }
83 }
84
85
86 public void debug( Throwable error )
87 {
88 if ( isDebugEnabled() )
89 {
90 print( "debug", error );
91 }
92 }
93
94
95 public void info( CharSequence content )
96 {
97 if ( isInfoEnabled() )
98 {
99 print( "info", content );
100 }
101 }
102
103
104 public void info( CharSequence content, Throwable error )
105 {
106 if ( isInfoEnabled() )
107 {
108 print( "info", content, error );
109 }
110 }
111
112
113 public void info( Throwable error )
114 {
115 if ( isInfoEnabled() )
116 {
117 print( "info", error );
118 }
119 }
120
121
122 public void warn( CharSequence content )
123 {
124 if ( isWarnEnabled() )
125 {
126 print( "warn", content );
127 }
128 }
129
130
131 public void warn( CharSequence content, Throwable error )
132 {
133 if ( isWarnEnabled() )
134 {
135 print( "warn", content, error );
136 }
137 }
138
139
140 public void warn( Throwable error )
141 {
142 if ( isWarnEnabled() )
143 {
144 print( "warn", error );
145 }
146 }
147
148
149 public void error( CharSequence content )
150 {
151 if ( isErrorEnabled() )
152 {
153 System.err.println( "[error] " + content.toString() );
154 }
155 }
156
157
158 public void error( CharSequence content, Throwable error )
159 {
160 if ( isErrorEnabled() )
161 {
162 StringWriter sWriter = new StringWriter();
163 PrintWriter pWriter = new PrintWriter( sWriter );
164
165 error.printStackTrace( pWriter );
166
167 System.err.println( "[error] " + content.toString()
168 + EOL + EOL + sWriter.toString() );
169 }
170 }
171
172
173 public void error( Throwable error )
174 {
175 if ( isErrorEnabled() )
176 {
177 StringWriter sWriter = new StringWriter();
178 PrintWriter pWriter = new PrintWriter( sWriter );
179
180 error.printStackTrace( pWriter );
181
182 System.err.println( "[error] " + sWriter.toString() );
183 }
184 }
185
186
187 public boolean isDebugEnabled()
188 {
189 return ( currentLevel <= LEVEL_DEBUG );
190 }
191
192
193 public boolean isInfoEnabled()
194 {
195 return ( currentLevel <= LEVEL_INFO );
196 }
197
198
199 public boolean isWarnEnabled()
200 {
201 return ( currentLevel <= LEVEL_WARN );
202 }
203
204
205 public boolean isErrorEnabled()
206 {
207 return ( currentLevel <= LEVEL_ERROR );
208 }
209
210
211
212
213
214 private void print( String prefix, CharSequence content )
215 {
216 System.out.println( "[" + prefix + "] " + content.toString() );
217 }
218
219 private void print( String prefix, Throwable error )
220 {
221 StringWriter sWriter = new StringWriter();
222 PrintWriter pWriter = new PrintWriter( sWriter );
223
224 error.printStackTrace( pWriter );
225
226 System.out.println( "[" + prefix + "] " + sWriter.toString() );
227 }
228
229 private void print( String prefix, CharSequence content, Throwable error )
230 {
231 StringWriter sWriter = new StringWriter();
232 PrintWriter pWriter = new PrintWriter( sWriter );
233
234 error.printStackTrace( pWriter );
235
236 System.out.println( "[" + prefix + "] " + content.toString()
237 + EOL + EOL + sWriter.toString() );
238 }
239 }