FileDocCategorySizeDatePackage
SystemLogHandler.javaAPI DocApache Tomcat 6.0.145951Fri Jul 20 04:20:36 BST 2007org.apache.tomcat.util.log

SystemLogHandler

public class SystemLogHandler extends PrintStream
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

Fields Summary
protected PrintStream
out
Wrapped PrintStream.
protected static ThreadLocal
logs
Thread <-> CaptureLog associations.
protected static Stack
reuse
Spare CaptureLog ready for reuse.
Constructors Summary
public SystemLogHandler(PrintStream wrapped)
Construct the handler to capture the output of the given steam.

        super(wrapped);
        out = wrapped;
    
Methods Summary
public booleancheckError()

        return findStream().checkError();
    
public voidclose()

        findStream().close();
    
protected java.io.PrintStreamfindStream()
Find PrintStream to which the output must be written to.

        Stack stack = (Stack)logs.get();
        if (stack != null && !stack.isEmpty()) {
            CaptureLog log = (CaptureLog)stack.peek();
            if (log != null) {
                PrintStream ps = log.getStream();
                if (ps != null) {
                    return ps;
                }
            }
        }
        return out;
    
public voidflush()

        findStream().flush();
    
public voidprint(boolean b)

        findStream().print(b);
    
public voidprint(char c)

        findStream().print(c);
    
public voidprint(int i)

        findStream().print(i);
    
public voidprint(long l)

        findStream().print(l);
    
public voidprint(float f)

        findStream().print(f);
    
public voidprint(double d)

        findStream().print(d);
    
public voidprint(char[] s)

        findStream().print(s);
    
public voidprint(java.lang.String s)

        findStream().print(s);
    
public voidprint(java.lang.Object obj)

        findStream().print(obj);
    
public voidprintln()

        findStream().println();
    
public voidprintln(boolean x)

        findStream().println(x);
    
public voidprintln(char x)

        findStream().println(x);
    
public voidprintln(int x)

        findStream().println(x);
    
public voidprintln(long x)

        findStream().println(x);
    
public voidprintln(float x)

        findStream().println(x);
    
public voidprintln(double x)

        findStream().println(x);
    
public voidprintln(char[] x)

        findStream().println(x);
    
public voidprintln(java.lang.String x)

        findStream().println(x);
    
public voidprintln(java.lang.Object x)

        findStream().println(x);
    
protected voidsetError()

        //findStream().setError();
    
public static voidstartCapture()
Start capturing thread's output.



    // --------------------------------------------------------- Public Methods


             
        
        CaptureLog log = null;
        if (!reuse.isEmpty()) {
            try {
                log = (CaptureLog)reuse.pop();
            } catch (EmptyStackException e) {
                log = new CaptureLog();
            }
        } else {
            log = new CaptureLog();
        }
        Stack stack = (Stack)logs.get();
        if (stack == null) {
            stack = new Stack();
            logs.set(stack);
        }
        stack.push(log);
    
public static java.lang.StringstopCapture()
Stop capturing thread's output and return captured data as a String.

        Stack stack = (Stack)logs.get();
        if (stack == null || stack.isEmpty()) {
            return null;
        }
        CaptureLog log = (CaptureLog)stack.pop();
        if (log == null) {
            return null;
        }
        String capture = log.getCapture();
        log.reset();
        reuse.push(log);
        return capture;
    
public voidwrite(byte[] b)

        findStream().write(b);
    
public voidwrite(byte[] buf, int off, int len)

        findStream().write(buf, off, len);
    
public voidwrite(int b)

        findStream().write(b);