| 1 | /** |
| 2 | * |
| 3 | */ |
| 4 | package net.sf.madmap; |
| 5 | |
| 6 | import java.io.BufferedWriter; |
| 7 | import java.io.File; |
| 8 | import java.io.FileOutputStream; |
| 9 | import java.io.OutputStreamWriter; |
| 10 | import javax.swing.text.*; |
| 11 | |
| 12 | /** |
| 13 | * @author ecaspole |
| 14 | * |
| 15 | */ |
| 16 | public class Logger implements ILogger { |
| 17 | |
| 18 | |
| 19 | public void myLog( MainWindow w, String s ) { |
| 20 | System.out.println( s ); |
| 21 | if ( ! MadmapMain.noGUI() ) { |
| 22 | String realOutput = s + "\n"; |
| 23 | w.log.append( realOutput ); |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | |
| 28 | /** |
| 29 | * @author ecaspole |
| 30 | * This is called from the GUI when Save is selected |
| 31 | */ |
| 32 | public void saveConsoleLog( MainWindow w, File f ) { |
| 33 | FileOutputStream fos; |
| 34 | OutputStreamWriter osr; |
| 35 | BufferedWriter bfr; |
| 36 | |
| 37 | Document doc = w.log.getDocument(); |
| 38 | int length = doc.getLength(); |
| 39 | try { |
| 40 | fos = new FileOutputStream( f ); |
| 41 | osr = new OutputStreamWriter( fos ); |
| 42 | bfr = new BufferedWriter( osr ); |
| 43 | |
| 44 | bfr.write( doc.getText(0, length) ); |
| 45 | |
| 46 | bfr.flush(); |
| 47 | bfr.close(); |
| 48 | } catch ( Exception ie ) { |
| 49 | System. out. println ( "Error opening file : Caught " + ie ); |
| 50 | ie.printStackTrace(); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | /* (non-Javadoc) |
| 55 | * @see net.sf.madmap.ILogger#getLog(net.sf.madmap.MainWindow) |
| 56 | */ |
| 57 | public String getLog( MainWindow w ) { |
| 58 | try { |
| 59 | Document doc = w.log.getDocument(); |
| 60 | return doc.getText(0, doc.getLength() ); |
| 61 | } catch ( Exception ie ) { |
| 62 | System. out. println ( "Error opening file : Caught " + ie ); |
| 63 | ie.printStackTrace(); |
| 64 | return null; |
| 65 | } |
| 66 | } |
| 67 | } |