001 package org.apache.ojb.broker.util.logging;
002
003 /* Copyright 2002-2005 The Apache Software Foundation
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 import java.io.PrintWriter;
019
020 import org.apache.commons.lang.BooleanUtils;
021
022 /**
023 * Extremely simple piggyback for OJB Logger interface to provide PrintWriter dito.
024 *
025 * @author <a href="mailto:mkalen@apache.org">Martin Kalén</a>
026 * @version CVS $Id: LoggerWrapperPrintWriter.java,v 1.1 2007-08-24 22:17:32 ewestfal Exp $
027 * @since OJB 1.0.4, 2005-apr-30
028 */
029 public class LoggerWrapperPrintWriter extends PrintWriter
030 {
031
032 private static final String LINESEP = System.getProperty("line.separator");
033 private static final int DEFAULT_LEVEL = Logger.INFO;
034
035 private final Logger logger;
036 private final int level;
037 private final boolean filterEverything;
038
039 /**
040 * Construct a new PrintWriter piggyback for the specified OJB logger.
041 * @param logger the logger to which all PrintWriter events should be sent to
042 * @param level the log level for PrintWriter events, can only be specified
043 * as a single level since ther is no priority concept in PrintWriter API
044 */
045 public LoggerWrapperPrintWriter(Logger logger, int level)
046 {
047 super(System.out); // dummy, must initialize stream
048 this.logger = logger;
049 this.level = level;
050 filterEverything = !logger.isEnabledFor(level);
051 }
052
053 public LoggerWrapperPrintWriter(Logger logger)
054 {
055 this(logger, DEFAULT_LEVEL);
056 }
057
058 private void log(String s)
059 {
060 switch (level)
061 {
062 case Logger.FATAL:
063 logger.fatal(s);
064 break;
065
066 case Logger.ERROR:
067 logger.error(s);
068 break;
069
070 case Logger.WARN:
071 logger.warn(s);
072 break;
073
074 case Logger.INFO:
075 logger.info(s);
076 break;
077
078 case Logger.DEBUG:
079 logger.debug(s);
080 break;
081
082 default:
083 throw new RuntimeException("Internal OJB fault. Logger API does not permit level "
084 + level);
085 }
086 }
087
088 private void logLn(String s)
089 {
090 if (s != null)
091 {
092 log(s);
093 }
094 log(LINESEP);
095 }
096
097
098 public void println()
099 {
100 if (!filterEverything)
101 {
102 logLn(null);
103 }
104 }
105
106 public void print(char c)
107 {
108 if (!filterEverything)
109 {
110 log(new String(new char[]{c}));
111 }
112 }
113
114 public void println(char c)
115 {
116 if (!filterEverything)
117 {
118 logLn(new String(new char[]{c}));
119 }
120 }
121
122 public void print(double v)
123 {
124 if (!filterEverything)
125 {
126 log(Double.toString(v));
127 }
128 }
129
130 public void println(double v)
131 {
132 if (!filterEverything)
133 {
134 logLn(Double.toString(v));
135 }
136 }
137
138 public void print(float v)
139 {
140 if (!filterEverything)
141 {
142 log(Float.toString(v));
143 }
144 }
145
146 public void println(float v)
147 {
148 if (!filterEverything)
149 {
150 logLn(Float.toString(v));
151 }
152 }
153
154 public void print(int i)
155 {
156 if (!filterEverything)
157 {
158 log(Integer.toString(i));
159 }
160 }
161
162 public void println(int i)
163 {
164 if (!filterEverything)
165 {
166 logLn(Integer.toString(i));
167 }
168 }
169
170 public void print(long l)
171 {
172 if (!filterEverything)
173 {
174 log(Long.toString(l));
175 }
176 }
177
178 public void println(long l)
179 {
180 if (!filterEverything)
181 {
182 logLn(Long.toString(l));
183 }
184 }
185
186 public void print(boolean b)
187 {
188 if (!filterEverything)
189 {
190 log(BooleanUtils.toStringTrueFalse(b));
191 }
192 }
193
194 public void println(boolean b)
195 {
196 if (!filterEverything)
197 {
198 logLn(BooleanUtils.toStringTrueFalse(b));
199 }
200 }
201
202 public void print(char[] chars)
203 {
204 if (!filterEverything)
205 {
206 log(new String(chars));
207 }
208 }
209
210 public void println(char[] chars)
211 {
212 if (!filterEverything)
213 {
214 logLn(new String(chars));
215 }
216 }
217
218 public void print(Object o)
219 {
220 if (!filterEverything && o != null)
221 {
222 log(o.toString());
223 }
224 }
225
226 public void println(Object o)
227 {
228 if (!filterEverything && o != null)
229 {
230 logLn(o.toString());
231 }
232 }
233
234 public void print(String s)
235 {
236 if (!filterEverything)
237 {
238 log(s);
239 }
240 }
241
242 public void println(String s)
243 {
244 if (!filterEverything)
245 {
246 logLn(s);
247 }
248 }
249
250 public void write(int i)
251 {
252 if (!filterEverything)
253 {
254 print(i);
255 }
256 }
257
258 public void write(String s)
259 {
260 if (!filterEverything)
261 {
262 print(s);
263 }
264 }
265
266 public void write(char[] chars)
267 {
268 if (!filterEverything)
269 {
270 print(chars);
271 }
272 }
273
274 public void write(char[] chars, int i, int i1)
275 {
276 if (!filterEverything)
277 {
278 log(new String(chars, i, i1));
279 }
280 }
281
282 public void write(String s, int i, int i1)
283 {
284 if (!filterEverything)
285 {
286 log(s.substring(i, i1));
287 }
288 }
289
290 }