Ignore:
Timestamp:
08/23/07 22:15:34 (5 years ago)
Author:
saua
Message:

add logging to text file

File:
1 edited

Legend:

Unmodified
Added
Removed
  • bbtracker/trunk/src/org/bbtracker/mobile/BBTracker.java

    r75 r88  
    1818package org.bbtracker.mobile; 
    1919 
     20import java.io.OutputStream; 
     21import java.io.PrintStream; 
     22import java.util.Date; 
    2023import java.util.Timer; 
    2124 
     25import javax.microedition.io.Connector; 
     26import javax.microedition.io.file.FileConnection; 
    2227import javax.microedition.lcdui.Alert; 
    2328import javax.microedition.lcdui.AlertType; 
     
    3439import org.bbtracker.mobile.TrackStore.TrackStoreException; 
    3540import org.bbtracker.mobile.gui.MainCanvas; 
     41import org.bbtracker.mobile.gui.OptionsForm; 
    3642import org.bbtracker.mobile.gui.TrackNameForm; 
    37 import org.bbtracker.mobile.gui.OptionsForm; 
    3843import org.bbtracker.mobile.gui.TracksForm; 
    3944 
     
    4651 
    4752        private static BBTracker instance; 
     53 
     54        private static PrintStream logStream; 
    4855 
    4956        private final TrackManager trackManager; 
     
    8087                        nonFatal(e, "Initializing Location Provider", mainCanvas); 
    8188                } 
     89 
     90                initLog(); 
    8291        } 
    8392 
    8493        public void shutdown(final boolean destroy) { 
     94                log(this, "shutdown " + destroy); 
    8595                if (trackManager != null) { 
    8696                        trackManager.shutdown(); 
     
    94104                        notifyDestroyed(); 
    95105                } 
     106                if (logStream != null) { 
     107                        logStream.close(); 
     108                        logStream = null; 
     109                } 
    96110        } 
    97111 
     
    121135 
    122136        public static void nonFatal(final Throwable t, final String action, final Displayable next) { 
    123                 log(t); 
     137                log(BBTracker.class, t, "non-fatal " + action); 
    124138                final Alert alert = new Alert("Non-fatal Exception", "Non-fatal Exception while " + action + ": " + 
    125139                                t.getMessage(), null, AlertType.WARNING); 
     
    128142 
    129143        public static void fatal(final Throwable t, final String action) { 
    130                 log(t); 
     144                log(BBTracker.class, t, "fatal " + action); 
    131145                final BBTracker i = getInstance(); 
    132146                i.shutdown(false); 
     
    152166        } 
    153167 
    154         public static void log(final Throwable e) { 
     168        public static void initLog() { 
     169                if (logStream != null) { 
     170                        return; 
     171                } 
     172                final String dirName = Preferences.getInstance().getTrackDirectory(); 
     173                final String logUrl = "file:///" + dirName + "debug.txt"; 
     174                try { 
     175                        final FileConnection fileConnection = (FileConnection) Connector.open(logUrl); 
     176                        if (!(fileConnection.exists() && fileConnection.canWrite())) { 
     177                                fileConnection.close(); 
     178                                return; 
     179                        } 
     180                        final OutputStream out = fileConnection.openOutputStream(); 
     181                        logStream = new PrintStream(out); 
     182                } catch (final Throwable e) { 
     183                        log(BBTracker.class, e, "opening " + logUrl); 
     184                } 
     185        } 
     186 
     187        public static void setLogActive(final boolean logActive) { 
     188                if (!logActive && logStream != null) { 
     189                        logStream.close(); 
     190                        logStream = null; 
     191                } 
     192 
     193                final String dirName = Preferences.getInstance().getTrackDirectory(); 
     194                final String logUrl = "file:///" + dirName + "debug.txt"; 
     195                try { 
     196                        final FileConnection fileConnection = (FileConnection) Connector.open(logUrl); 
     197                        if (logActive) { 
     198                                if (!fileConnection.exists()) { 
     199                                        fileConnection.create(); 
     200                                } 
     201                                final OutputStream out = fileConnection.openOutputStream(); 
     202                                logStream = new PrintStream(out); 
     203                        } else { 
     204                                if (fileConnection.exists()) { 
     205                                        fileConnection.delete(); 
     206                                        fileConnection.close(); 
     207                                } 
     208                        } 
     209                } catch (final Throwable e) { 
     210                        log(BBTracker.class, e, "opening " + logUrl); 
     211                } 
     212        } 
     213 
     214        public static boolean isLogActive() { 
     215                return logStream != null; 
     216        } 
     217 
     218        public static void log(final Object source, final Throwable e) { 
     219                log(source, "Exception: " + e.toString()); 
     220                // this is only useful for debugging in the emulator 
    155221                e.printStackTrace(); 
    156222        } 
    157223 
    158         public static void log(final String m) { 
    159                 System.err.println(m); 
     224        public static void log(final Object source, final Throwable e, final String message) { 
     225                log(source, "Exception <" + message + ">: " + e.toString()); 
     226                // this is only useful for debugging in the emulator 
     227                e.printStackTrace(); 
     228        } 
     229 
     230        public static void log(final Object source, final String m) { 
     231                final String line = new Date() + ": [" + source + "] " + m; 
     232                System.err.println(line); 
     233                if (logStream != null) { 
     234                        logStream.println(line); 
     235                } 
    160236        } 
    161237 
     
    165241 
    166242        protected void pauseApp() { 
     243                log(this, "pauseApp"); 
    167244        } 
    168245 
    169246        protected void startApp() throws MIDletStateChangeException { 
     247                log(this, firstStart ? "first startApp" : "startApp"); 
    170248                if (firstStart) { 
    171249                        firstStart = false; 
Note: See TracChangeset for help on using the changeset viewer.