/* Capturing standard output and error in a log file
 * <P>
 * This from the good folks at Sun,
 * the Java Developers Connection.
 * If you don't subscribe you should -- it's free.
 * Quoting (with my valuable additions) from here >>>>>>>
 * <P>
 * This tip demonstrates how you can record in a log file everything you
 * print to standard out and standard error. This is especially useful 
 * if you deploy an application and your users encounter problems. You 
 * can have the users send you the log file for analysis.
 * <P>
 * A SaveOutput object is a PrintStream object that acts like a tee. 
 * Any characters it receives are forwarded to two places: the log file 
 * and the underlying print streams.
 * <P>
 * Although the Sun example doesn't show further elaborations,
 * we could output to a message window instead of (or plus) a log file,
 * and could support filtering of what lines make it to which places. */

import java.io.*;

/** This class just runs a demo */

public class LogDemo {

    public static void main (String[] args) {

/* These won't get in the log file. */

        System.out.println ("Here's is some stuff to stdout.");
        System.err.println ("Here's is some stuff to stderr.");

        try {

/* Start the log file. */

            SaveOutput.start ("log.txt");

/* Test it.
 * Note that the e.printStackTrace () will be logged. */

            System.out.println ("Here's is some LOG stuff to stdout.");
            System.err.println ("Here's is some LOG stuff to stderr.");
            System.out.println ("Let's throw an exception...");
	    throw new Exception ("My Exception!");
        }
	catch (Exception e) { e.printStackTrace (); }
	
/* Stop the log file and restore old setup. */

	finally { SaveOutput.stop (); }
    }
}
/* <IMG SRC="/cgi-bin/counter">*/
