001package jmri.jmrit.logix;
002
003import java.io.BufferedWriter;
004import java.io.File;
005import java.io.FileNotFoundException;
006import java.io.FileOutputStream;
007import java.io.IOException;
008import java.io.OutputStreamWriter;
009import java.text.SimpleDateFormat;
010import java.util.Date;
011
012import javax.swing.JFileChooser;
013
014import jmri.util.FileUtil;
015import jmri.util.swing.JmriJOptionPane;
016import jmri.util.swing.TextFilter;
017
018class OpSessionLog {
019
020    private static final String OP_SESSION_LOG_ERROR = "Op session log error {}";
021    static BufferedWriter _outBuff;
022
023    private OpSessionLog() {
024    }
025
026    public static synchronized boolean makeLogFile(java.awt.Component parent) {
027
028        JFileChooser fileChooser = new jmri.util.swing.JmriJFileChooser(FileUtil.getUserFilesPath());
029        fileChooser.setDialogTitle(Bundle.getMessage("logSession"));
030        fileChooser.setFileFilter(new TextFilter());
031        int retVal = fileChooser.showDialog(parent, Bundle.getMessage("logFile"));
032        if (retVal != JFileChooser.APPROVE_OPTION) {
033            return false;
034        }
035
036        File file = fileChooser.getSelectedFile();
037        String fileName = file.getAbsolutePath();
038        String fileNameLC = fileName.toLowerCase();
039        if (!fileNameLC.endsWith(".txt")) {
040            fileName = fileName + ".txt";
041            file = new File(fileName);
042        }
043        // check for possible overwrite
044        if (file.exists()) {
045            if (JmriJOptionPane.showConfirmDialog(parent,
046                    Bundle.getMessage("overWritefile", fileName), Bundle.getMessage("QuestionTitle"),
047                    JmriJOptionPane.OK_CANCEL_OPTION, JmriJOptionPane.QUESTION_MESSAGE) != JmriJOptionPane.OK_OPTION) {
048                return false;
049            }
050        }
051
052        try {
053            _outBuff = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
054            writeHeader(fileName);
055        } catch (FileNotFoundException fnfe) {
056            JmriJOptionPane.showMessageDialog(parent, fnfe.getMessage(),
057                    Bundle.getMessage("WarningTitle"), JmriJOptionPane.WARNING_MESSAGE);
058            return false;
059        }
060        return true;
061    }
062
063    private static void writeHeader(String fileName) {
064        if (_outBuff==null) {
065            return;
066        }
067        try {
068            _outBuff.newLine();
069            _outBuff.append("\t\t\t");
070            _outBuff.append(fileName);
071            _outBuff.newLine();
072            _outBuff.append("\t\t\t");
073            SimpleDateFormat dateFormatter = new SimpleDateFormat("EEEE, MMMM d, yyyy");
074            _outBuff.append(dateFormatter.format(new Date()));
075            _outBuff.newLine();
076            _outBuff.newLine();
077            writeLn(Bundle.getMessage("startLog"));
078        } catch (IOException ioe) {
079            log.error(OP_SESSION_LOG_ERROR,ioe.getMessage());
080        }
081    }
082
083    public static synchronized void writeLn(String text) {
084        if (_outBuff==null) {
085            return;
086        }
087        try {
088            SimpleDateFormat dateFormatter = new SimpleDateFormat("  hh:mm:ss a   ");
089            _outBuff.append(dateFormatter.format(new Date()));
090            _outBuff.append(text);
091            _outBuff.newLine();
092        } catch (IOException ioe) {
093            log.error(OP_SESSION_LOG_ERROR,ioe.getMessage());
094        }
095    }
096
097    public static synchronized void flush() {
098        if (_outBuff==null) {
099            return;
100        }
101        try {
102            _outBuff.flush();
103        } catch (IOException ioe) {
104            log.error(OP_SESSION_LOG_ERROR,ioe.getMessage());
105        }
106    }
107
108    public static synchronized void close() {
109        if (_outBuff==null) {
110            return;
111        }
112        try {
113            writeLn(Bundle.getMessage("stopLog"));
114            _outBuff.flush();
115            _outBuff.close();
116        } catch (IOException ioe) {
117            log.error(OP_SESSION_LOG_ERROR,ioe.getMessage());
118        }
119    }
120
121    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(OpSessionLog.class);
122
123}