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