glenn 02/05/23 10:21:09 Added: util/java/org/apache/tomcat/util/log SystemLogHandler.java CaptureLog.java Log: Custome logger which can be used to capture System.out and System.err to a buffer for later use. Used by Tomcat 4 to capture this output when handling a request so any output can be sent to the webapp context log instead of catalina.out. Revision Changes Path 1.1 jakarta-tomcat-connectors/util/java/org/apache/tomcat/util/log/SystemLogHandler.java Index: SystemLogHandler.java =================================================================== /* * ==================================================================== * * The Apache Software License, Version 1.1 * * Copyright (c) 1999 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, if * any, must include the following acknowlegement: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowlegement may appear in the software itself, * if and wherever such third-party acknowlegements normally appear. * * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software * Foundation" must not be used to endorse or promote products derived * from this software without prior written permission. For written * permission, please contact [EMAIL PROTECTED] * * 5. Products derived from this software may not be called "Apache" * nor may "Apache" appear in their names without prior written * permission of the Apache Group. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * */ package org.apache.tomcat.util.log; import java.io.ByteArrayOutputStream; import java.io.Writer; import java.io.PrintStream; import java.io.PrintWriter; import java.io.FileWriter; import java.io.File; import java.io.OutputStreamWriter; import java.io.IOException; import java.io.StringWriter; import java.util.Hashtable; import java.util.Stack; /** * This helper class may be used to do sophisticated redirection of * System.out and System.err on a per Thread basis. * * A stack is implemented per Thread so that nested startCapture * and stopCapture can be used. * * @author Remy Maucherat * @author Glenn L. Nielsen */ public class SystemLogHandler extends PrintStream { // ----------------------------------------------------------- Constructors /** * Construct the handler to capture the output of the given steam. */ public SystemLogHandler(PrintStream wrapped) { super(wrapped); out = wrapped; } // ----------------------------------------------------- Instance Variables /** * Wrapped PrintStream. */ protected PrintStream out = null; /** * Thread <-> CaptureLog associations. */ protected static Hashtable logs = new Hashtable(); /** * Spare CaptureLog ready for reuse. */ protected static Stack reuse = new Stack(); // --------------------------------------------------------- Public Methods /** * Start capturing thread's output. */ public static void startCapture() { CaptureLog log = null; if (!reuse.isEmpty()) { try { log = (CaptureLog)reuse.pop(); } catch(Exception e) { } } if (log == null) { log = new CaptureLog(); } Stack stack = (Stack)logs.get(Thread.currentThread()); if (stack == null) { stack = new Stack(); logs.put(Thread.currentThread(), stack); } stack.push(log); } /** * Stop capturing thread's output and return captured data as a String. */ public static String stopCapture() { Stack stack = (Stack)logs.get(Thread.currentThread()); if (stack == null) { return null; } CaptureLog log = (CaptureLog)stack.pop(); if (log == null) { return null; } String capture = log.getCapture(); log.reset(); reuse.push(log); return capture; } // ------------------------------------------------------ Protected Methods /** * Find PrintStream to which the output must be written to. */ protected PrintStream findStream() { Stack stack = (Stack)logs.get(Thread.currentThread()); if (stack != null) { CaptureLog log = (CaptureLog)stack.peek(); if (log != null) { PrintStream ps = log.getStream(); if (ps != null) { return ps; } } } return out; } // ---------------------------------------------------- PrintStream Methods public void flush() { findStream().flush(); } public void close() { findStream().close(); } public boolean checkError() { return findStream().checkError(); } protected void setError() { //findStream().setError(); } public void write(int b) { findStream().write(b); } public void write(byte[] b) throws IOException { findStream().write(b); } public void write(byte[] buf, int off, int len) { findStream().write(buf, off, len); } public void print(boolean b) { findStream().print(b); } public void print(char c) { findStream().print(c); } public void print(int i) { findStream().print(i); } public void print(long l) { findStream().print(l); } public void print(float f) { findStream().print(f); } public void print(double d) { findStream().print(d); } public void print(char[] s) { findStream().print(s); } public void print(String s) { findStream().print(s); } public void print(Object obj) { findStream().print(obj); } public void println() { findStream().println(); } public void println(boolean x) { findStream().println(x); } public void println(char x) { findStream().println(x); } public void println(int x) { findStream().println(x); } public void println(long x) { findStream().println(x); } public void println(float x) { findStream().println(x); } public void println(double x) { findStream().println(x); } public void println(char[] x) { findStream().println(x); } public void println(String x) { findStream().println(x); } public void println(Object x) { findStream().println(x); } } 1.1 jakarta-tomcat-connectors/util/java/org/apache/tomcat/util/log/CaptureLog.java Index: CaptureLog.java =================================================================== /* * ==================================================================== * * The Apache Software License, Version 1.1 * * Copyright (c) 1999 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, if * any, must include the following acknowlegement: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowlegement may appear in the software itself, * if and wherever such third-party acknowlegements normally appear. * * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software * Foundation" must not be used to endorse or promote products derived * from this software without prior written permission. For written * permission, please contact [EMAIL PROTECTED] * * 5. Products derived from this software may not be called "Apache" * nor may "Apache" appear in their names without prior written * permission of the Apache Group. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * */ package org.apache.tomcat.util.log; import java.io.ByteArrayOutputStream; import java.io.Writer; import java.io.PrintStream; import java.io.PrintWriter; import java.io.FileWriter; import java.io.File; import java.io.OutputStreamWriter; import java.io.IOException; import java.io.StringWriter; import java.util.Hashtable; import java.util.Stack; /** * Per Thread System.err and System.out log capture data. * * @author Glenn L. Nielsen */ class CaptureLog { protected CaptureLog() { baos = new ByteArrayOutputStream(); ps = new PrintStream(baos); } private ByteArrayOutputStream baos; private PrintStream ps; protected PrintStream getStream() { return ps; } protected void reset() { baos.reset(); } protected String getCapture() { return baos.toString(); } }
-- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>