All computer source code presented on this page, unless it includes attribution to another author, is provided by Ed Halley under the Artistic License. Use such code freely and without any expectation of support. I would like to know if you make anything cool with the code, or need questions answered.

 * A type of Thread which has built-in facilities to switchable print streams.
 * <p>It's fairly common to want to add a number of calls to the
 * <tt>System.out.print()</tt> family of calls, especially during the
 * debugging phase of development.  This class offers built-in methods
 * for <tt>print()</tt>, as well as an analagous set of methods called
 * <tt>trace()</tt>.  Each set of methods can be enabled or disabled
 * centrally for all TracingThread instances.
public class TracingThread
        extends Thread
         * The stream for <tt>print()</tt> family of methods.
         * Defaults to <tt>System.out</tt> but can be set to any PrintStream.
         * Flushes after any call to any <tt>println()</tt> method.
         * May be set to <tt>null</tt> at any time.
    public static printout = System.out;
    public boolean printing() { return (printout != null); }

    public void println(String message)
        { print(message); println(); }
    public void printf(String format, Object[]args)
        if (printing())
                { printout.printf(format, args); printout.flush(); }
    public void print(String message)
        { if (printing()) printout.print(message); }
    public void print(double value)
        { if (printing()) printout.print(value); }
    public void print(int value)
        { if (printing()) printout.print(value); }
    public void println()
        { if (printing()) { printout.println(); printout.flush(); } }

         * The stream for <tt>trace()</tt> family of methods.
         * Defaults to <tt>null</tt> but can be set to any PrintStream.
         * This may be set to null at any time to disable output.
    public static traceout = null;
    public boolean tracing() { return (traceout != null); }
    public static boolean logging = false;
    protected StringBuffer line = new StringBuffer();

    public void traceln(String message)
        { trace(message); traceln(); }
    public void trace(double value)
        { if (tracing()) trace(Double.toString(value)); }
    public void trace(int value)
        { if (tracing()) trace(Integer.toString(value)); }
    public void trace(String message)
        { line.append(message); }
    public void traceln()
        if (tracing())
                if (logging)
                        traceout.print(new Datestamp().toString() + "| ");
                line = new StringBuffer();

     * A minor sleep of the thread, useful for exploring race conditions.
     * Equivalent to a Thread.sleep(100) but only if the <tt>traceout</tt>
     * member variable is non-null.  Ignores any exception of type
     * <tt>InterruptedException</tt> thrown during the sleep.
        public void pause()
                if (!tracing()) return;
                try { Thread.sleep(100); } catch (InterruptedException ex) { ; }


Contact Ed Halley by email at
Text, code, layout and artwork are Copyright © 1996-2013 Ed Halley.
Copying in whole or in part, with author attribution, is expressly allowed.
Any references to trademarks are illustrative and are controlled by their respective owners.
Make donations with PayPal - it's fast, free and secure!