FileDocCategorySizeDatePackage
Tee.javaAPI DocGlassfish v2 API4412Fri May 04 22:32:14 BST 2007com.sun.enterprise.util.io

Tee

public class Tee extends PrintStream
version
1.00 April 1, 2000
author
Byron Nevins

Fields Summary
static OutputStream
logfile
static PrintStream
oldStdout
static PrintStream
oldStderr
Constructors Summary
private Tee(PrintStream ps)

		super(ps);
	
Methods Summary
public static voidmain(java.lang.String[] args)

		try 
		{
			// Start capturing characters 
			//into the log file.
			Tee.start("log.txt");

			// Test it.
			System.out.println(
			"Here's is some stuff to stdout.");
			System.err.println(
			"Here's is some stuff to stderr.");
			System.out.println(
			"Let's throw an exception...");
			new Exception().printStackTrace();
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		} 
		finally 
		{
			// Stop capturing characters 
			//into the log file 
			// and restore old setup.
			Tee.stop();
		}
	
public static voidstart(java.lang.String f)

		// Save old settings.
		oldStdout = System.out;
		oldStderr = System.err;

		// Create/Open logfile.
		logfile = new PrintStream(new BufferedOutputStream(new FileOutputStream(f)));

		// Start redirecting the output.
		System.setOut(new Tee(System.out));
		System.setErr(new Tee(System.err));
	
public static voidstop()

		System.setOut(oldStdout);
		System.setErr(oldStderr);
		
		try 
		{
			logfile.close();
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
	
public voidwrite(int b)

		try 
		{
			logfile.write(b);
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
			setError();
		}
		super.write(b);
	
public voidwrite(byte[] buf, int off, int len)

		try 
		{
			logfile.write(buf, off, len);
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
			setError();
		}
		super.write(buf, off, len);