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.text.SimpleDateFormat;
022    import java.util.Date;
023    import java.io.*;
024    
025    /**
026     * Class used for logging/debugging
027     *
028     * @author Adam Olsen
029     * @version 1.0
030    **/
031    public class Logger
032    {
033            private static PrintWriter logOut = null;
034    
035            /**
036             * Sets the log file.  If no log file is set, it will not be written to
037             * @param file the log file location
038            */
039            public static void setLogFile( String file )
040            {
041                    try {
042                            File logFile = new File( file );
043                            logOut = new PrintWriter( new FileOutputStream( logFile ), true );
044                    }
045                    catch( IOException ex )
046                    {
047                            System.out.println( "Could not write to log file" );
048                    }
049            }
050    
051            /**
052             * Logs an exceptions stack trace if the log file is open
053             * @param ex the exception
054            */
055            public static void logException( Exception ex )
056            {
057                    if( System.getProperty( "debug" ) != null )
058                    {
059                            ex.printStackTrace();
060                    }
061    
062                    if( logOut != null )
063                    {
064                            try {
065                                    ex.printStackTrace( logOut );
066                            }
067                            catch( Exception e ) { }
068                    }
069            }
070    
071            /**
072             * Closes the log file if it's open
073            */
074            public static void closeLog()
075            {
076                    if( logOut != null )
077                    {
078                            try {
079                                    logOut.close();
080                            }
081                            catch( Exception ex ) { }
082                    }
083            }
084    
085            /**
086             * Outputs to the console only if the "debug" system property is set.
087             *
088             * @param message The message to output to the console
089            **/
090            public static void debug( String message )
091            {
092                    if( System.getProperty( "debug" ) != null )
093                    {
094                            com.valhalla.Logger.write( message );
095                    }
096    
097                    if( logOut != null )
098                    {
099                            SimpleDateFormat formatter = new SimpleDateFormat( "[yyyy-MM-dd @ HH:mm:ss]: " );
100                            String date = formatter.format( new Date() );
101    
102                            try {
103                                    logOut.println( date + message );
104                            }
105                            catch( Exception ex ) { }
106                    }
107            }
108    
109            /**
110             * Output to the console - include the date and time
111             *
112             * @param message The message to output to the console
113            **/
114            public static void write( String message )
115            {
116                    // get the current date/time and output it prettily to the console
117                    SimpleDateFormat formatter = new SimpleDateFormat( "[yyyy-MM-dd @ HH:mm:ss]: " );
118                    String date = formatter.format( new Date() );
119                    System.out.println( date + message );
120    
121            }
122    }