001    /*
002     Copyright (C) 2003 Adam Olsen
003    
004     This program is free software; you can redistribute it and/or modify
005     it under the terms of the GNU General Public License as published by
006     the Free Software Foundation; either version 1, or (at your option)
007     any later version.
008    
009     This program is distributed in the hope that it will be useful,
010     but WITHOUT ANY WARRANTY; without even the implied warranty of
011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012     GNU General Public License for more details.
013    
014     You should have received a copy of the GNU General Public License
015     along with this program; if not, write to the Free Software
016     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
017     */
018    
019    package com.valhalla;
020    
021    import java.io.File;
022    import java.io.FileWriter;
023    import java.io.PrintWriter;
024    import java.text.SimpleDateFormat;
025    import java.util.Date;
026    
027    import javax.swing.SwingUtilities;
028    
029    import com.valhalla.gui.DebugWindow;
030    import com.valhalla.settings.Arguments;
031    
032    /**
033     * Class used for logging/debugging
034     *
035     * @author Adam Olsen
036     * @version 1.0
037     */
038    public class Logger {
039        private static DebugWindow d = new DebugWindow();
040    
041        private static PrintWriter out = null;
042    
043        private static FileWriter fw;
044    
045        /**
046         * Logs an exceptions stack trace if the log file is open
047         *
048         * @param e
049         *            the exception
050         */
051        public static void logException(Exception e) {
052            debug("An uncaught exception has occurred.  Stacktrace is below.");
053            debug("---------------------------------------------------------");
054            debug(e.toString());
055            StackTraceElement el[] = e.getStackTrace();
056            for (int i = 0; i < el.length; i++) {
057                debug(el[i].toString());
058            }
059            debug("---------------------------------------------------------");
060        }
061    
062        public static void setLogFile(String file) {
063            try {
064                File f = new File(file);
065                fw = new FileWriter(f,true);
066                out = new PrintWriter(fw, true);
067            } catch (Exception ex) {
068                debug("Could not open debug log: " + ex.getMessage());
069            }
070        }
071    
072        public static void closeLog() {
073            if (fw != null) {
074                try {
075                    fw.close();
076                } catch (Exception ex) {
077                    debug("Error closing debug log file: " + ex.getMessage());
078                }
079            }
080        }
081    
082        /**
083         * Outputs to the console only if the "debug" system property is set.
084         *
085         * @param message
086         *            The message to output to the console
087         */
088        public static void debug(final String message) {
089            // get the current date/time and output it prettily to the console
090            SimpleDateFormat formatter = new SimpleDateFormat("[HH:mm:ss]: ");
091            final String date = formatter.format(new Date());
092    
093            SwingUtilities.invokeLater(new Runnable() {
094                public void run() {
095                    d.append(date + message);
096                    if (Arguments.getInstance() != null
097                            && Arguments.getInstance().getBoolean("debug")) {
098                        if (!d.isVisible())
099                            d.setVisible(true);
100                    }
101                }
102            });
103    
104            if (out != null) {
105                out.println(date + message);
106            }
107    
108            System.out.println(date + message);
109        }
110    
111        public static void debug(Object message) {
112            debug(message + "");
113        }
114    
115        public static void debug(int num) {
116            debug(num + "");
117        }
118    
119        public static DebugWindow getDebugWindow() {
120            return d;
121        }
122    }