package de.ugoe.cs.util.console; import java.io.FileWriter; import java.io.IOException; import de.ugoe.cs.util.console.listener.IOutputListener; /** *
* Implements an {@link IOutputListener} for the {@link Console} that logs all * outputs in a file. This can be used to "pipe" the output-stream of the * console into a file. The advantage of using this mechanism for piping is that * the file will only contain the output stream. No errors, no commands, etc. *
* * @author Steffen Herbold * @version 1.0 */ public class FileOutputListener implements IOutputListener { /** ** Flag that ensures that only one log message is produced if the listener * breaks, e.g., because of a full hard disk/quota. *
*/ boolean failureLogged = false; /** ** Name of the output file. *
*/ String filename; /** ** Writer for the output. *
*/ FileWriter writer = null; /** ** Constructor. Creates a new FileOutputListener. *
* * @param filename * name and path of the file the listener writes to. */ public FileOutputListener(String filename) { this.filename = filename; } /** ** Starts the listener by opening the file and registering it with the * {@link Console}. *
*/ public void start() { try { writer = new FileWriter(filename); Console.getInstance().registerOutputListener(this); } catch (IOException e) { Console.printerrln("Failed to start FileOutputListener for file " + filename + ": " + e.getMessage()); } } /** ** Stops the listener by closing the file and removing itself from the * {@link Console}. *
*/ public void stop() { Console.getInstance().removeOutputListener(this); if( writer!=null ) { try { writer.close(); writer = null; } catch (IOException e) { Console.printerrln("Failed to close file " + filename + ": " + e.getMessage()); } } } /* * (non-Javadoc) * * @see * de.ugoe.cs.util.console.listener.IOutputListener#outputMsg(java.lang. * String) */ @Override public void outputMsg(String newMessage) { if( writer!=null ) { try { writer.write(newMessage); } catch (IOException e) { if (!failureLogged) { Console.printerrln("FileOutpustListener for file " + filename + " broken: " + e.getMessage()); failureLogged = true; } } } } /** ** Returns the name of the log file used by this listener. *
* * @return name of the log file */ public String getFilename() { return filename; } }