01: /*
02: * Copyright 2003 The Apache Software Foundation.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package velosurf.util;
18:
19: import java.io.IOException;
20: import java.io.Writer;
21:
22: import javax.servlet.ServletContext;
23:
24: /** This class implements a writer towards the servlet log.
25: *
26: * @author <a href=mailto:claude.brisson@gmail.com>Claude Brisson</a>
27: */
28: public class ServletLogWriter extends Writer {
29: /** build a new ServletLogWriter.
30: *
31: * @param log ServletContext
32: */
33: public ServletLogWriter(ServletContext log) {
34: this .log = log;
35: }
36:
37: /** write an array of chars to the servlet log.
38: *
39: * @param cbuf characters to write
40: * @param off offset in the array
41: * @param len number of characters to write
42: * @exception IOException thrown by underlying servlet logger
43: */
44: public void write(char[] cbuf, int off, int len) throws IOException {
45: // ignore \r\n & \n
46: if ((len == 2 && cbuf[off] == 13 && cbuf[off + 1] == 10)
47: || (len == 1 && cbuf[off] == 10))
48: return;
49: String s = new String(cbuf, off, len);
50: log.log(s);
51: }
52:
53: /** flush any pending output.
54: *
55: * @exception IOException thrown by underlying servlet logger
56: */
57: public void flush() throws IOException {
58: }
59:
60: /** close the writer.
61: *
62: * @exception IOException thrown by underlying servlet logger
63: */
64: public void close() throws IOException {
65: }
66:
67: /** the ServletContext object used to log.
68: */
69: private ServletContext log = null;
70: }
|