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.2.3 2005/12/21 22:28:16 tomdz Exp $
027: * @since OJB 1.0.4, 2005-apr-30
028: */
029: public class LoggerWrapperPrintWriter extends PrintWriter {
030:
031: private static final String LINESEP = System
032: .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: super (System.out); // dummy, must initialize stream
047: this .logger = logger;
048: this .level = level;
049: filterEverything = !logger.isEnabledFor(level);
050: }
051:
052: public LoggerWrapperPrintWriter(Logger logger) {
053: this (logger, DEFAULT_LEVEL);
054: }
055:
056: private void log(String s) {
057: switch (level) {
058: case Logger.FATAL:
059: logger.fatal(s);
060: break;
061:
062: case Logger.ERROR:
063: logger.error(s);
064: break;
065:
066: case Logger.WARN:
067: logger.warn(s);
068: break;
069:
070: case Logger.INFO:
071: logger.info(s);
072: break;
073:
074: case Logger.DEBUG:
075: logger.debug(s);
076: break;
077:
078: default:
079: throw new RuntimeException(
080: "Internal OJB fault. Logger API does not permit level "
081: + level);
082: }
083: }
084:
085: private void logLn(String s) {
086: if (s != null) {
087: log(s);
088: }
089: log(LINESEP);
090: }
091:
092: public void println() {
093: if (!filterEverything) {
094: logLn(null);
095: }
096: }
097:
098: public void print(char c) {
099: if (!filterEverything) {
100: log(new String(new char[] { c }));
101: }
102: }
103:
104: public void println(char c) {
105: if (!filterEverything) {
106: logLn(new String(new char[] { c }));
107: }
108: }
109:
110: public void print(double v) {
111: if (!filterEverything) {
112: log(Double.toString(v));
113: }
114: }
115:
116: public void println(double v) {
117: if (!filterEverything) {
118: logLn(Double.toString(v));
119: }
120: }
121:
122: public void print(float v) {
123: if (!filterEverything) {
124: log(Float.toString(v));
125: }
126: }
127:
128: public void println(float v) {
129: if (!filterEverything) {
130: logLn(Float.toString(v));
131: }
132: }
133:
134: public void print(int i) {
135: if (!filterEverything) {
136: log(Integer.toString(i));
137: }
138: }
139:
140: public void println(int i) {
141: if (!filterEverything) {
142: logLn(Integer.toString(i));
143: }
144: }
145:
146: public void print(long l) {
147: if (!filterEverything) {
148: log(Long.toString(l));
149: }
150: }
151:
152: public void println(long l) {
153: if (!filterEverything) {
154: logLn(Long.toString(l));
155: }
156: }
157:
158: public void print(boolean b) {
159: if (!filterEverything) {
160: log(BooleanUtils.toStringTrueFalse(b));
161: }
162: }
163:
164: public void println(boolean b) {
165: if (!filterEverything) {
166: logLn(BooleanUtils.toStringTrueFalse(b));
167: }
168: }
169:
170: public void print(char[] chars) {
171: if (!filterEverything) {
172: log(new String(chars));
173: }
174: }
175:
176: public void println(char[] chars) {
177: if (!filterEverything) {
178: logLn(new String(chars));
179: }
180: }
181:
182: public void print(Object o) {
183: if (!filterEverything && o != null) {
184: log(o.toString());
185: }
186: }
187:
188: public void println(Object o) {
189: if (!filterEverything && o != null) {
190: logLn(o.toString());
191: }
192: }
193:
194: public void print(String s) {
195: if (!filterEverything) {
196: log(s);
197: }
198: }
199:
200: public void println(String s) {
201: if (!filterEverything) {
202: logLn(s);
203: }
204: }
205:
206: public void write(int i) {
207: if (!filterEverything) {
208: print(i);
209: }
210: }
211:
212: public void write(String s) {
213: if (!filterEverything) {
214: print(s);
215: }
216: }
217:
218: public void write(char[] chars) {
219: if (!filterEverything) {
220: print(chars);
221: }
222: }
223:
224: public void write(char[] chars, int i, int i1) {
225: if (!filterEverything) {
226: log(new String(chars, i, i1));
227: }
228: }
229:
230: public void write(String s, int i, int i1) {
231: if (!filterEverything) {
232: log(s.substring(i, i1));
233: }
234: }
235:
236: }
|