/** A Debug class for which output can be easily switched on/off. * The method "print" will print a message, * only if "on" is set (default: true). * You can use setLog() to specify a file output (default: System.out). * Warning!: setLog(String) will trash any previously-existing file! */ class Debug { public static boolean on = true; protected static java.io.PrintStream dest = System.out; /** Warning: calling setLog(String) trashes any previously-existing file, * even if it was one created during this same run of the program. */ public static void setLog( java.io.PrintStream out ) { dest = out; } public static void setLog( String filename ) throws java.io.IOException { setLog( new java.io.PrintStream( new java.io.FileOutputStream( new java.io.File( filename ) ) ) ); } public static void print(String s) { if (on) dest.println(s); } /* Testing... */ public static void main( String[] args ) throws java.io.IOException { Debug.on = false; Debug.print( "A test which should not be printed." ); Debug.on = true; Debug.print( "A test which should be printed." ); // Switch output to a file. String testOutFile = "foo.out"; Debug.setLog( testOutFile ); Debug.on = false; Debug.print( "A test which should not be filed." ); Debug.on = true; Debug.print( "A test which should be filed." ); // Switch back to System.out: Debug.setLog( System.out ); Debug.print( "Bye." ); } }