Changeset 88


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

add logging to text file

Location:
bbtracker/trunk/src/org/bbtracker/mobile
Files:
10 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; 
  • bbtracker/trunk/src/org/bbtracker/mobile/FileTrackStore.java

    r83 r88  
    3434                                        result.addElement(new FileTrackStoreEntry(name, date, fileUrl)); 
    3535                                } catch (final IOException e) { 
    36                                         BBTracker.log("Failed to load info from " + file); 
    37                                         BBTracker.log(e); 
     36                                        BBTracker.log(this, e, "loading info from " + file); 
    3837                                } finally { 
    3938                                        if (din != null) { 
     
    4140                                                        din.close(); 
    4241                                                } catch (final IOException e) { 
    43                                                         BBTracker.log(e); 
     42                                                        BBTracker.log(this, e); 
    4443                                                } 
    4544                                        } 
     
    5049                        return entries; 
    5150                } catch (final IOException e) { 
    52                         BBTracker.log(e); 
     51                        BBTracker.log(this, e, "loading track list"); 
    5352                        throw new TrackStoreException(e); 
    5453                } finally { 
     
    5857                                } catch (final IOException e) { 
    5958                                        // can't do anything about it 
    60                                         BBTracker.log(e); 
     59                                        BBTracker.log(this, e); 
    6160                                } 
    6261                        } 
     
    7372                        track.writeToStream(dout); 
    7473                } catch (final IOException e) { 
    75                         BBTracker.log(e); 
     74                        BBTracker.log(this, e, "saving track"); 
    7675                        throw new TrackStoreException(e); 
    7776                } finally { 
     
    8180                                } catch (final IOException e) { 
    8281                                        // can't do anything about it 
    83                                         BBTracker.log(e); 
    8482                                } 
    8583                        } 
     
    8987                                } catch (final IOException e) { 
    9088                                        // can't do anything about it 
    91                                         BBTracker.log(e); 
     89                                        BBTracker.log(this, e); 
    9290                                } 
    9391                        } 
     
    173171                                connection.delete(); 
    174172                        } catch (final IOException e) { 
    175                                 BBTracker.log(e); 
     173                                BBTracker.log(this, e, "deleting track"); 
    176174                                throw new TrackStoreException("Failed to delete track: " + e.getMessage()); 
    177175                        } 
     
    185183                                return track; 
    186184                        } catch (final IOException e) { 
    187                                 BBTracker.log(e); 
     185                                BBTracker.log(this, e, "loading track"); 
    188186                                throw new TrackStoreException("Failed to load track: " + e.getMessage()); 
    189187                        } finally { 
     
    193191                                        } catch (final IOException e) { 
    194192                                                // can't do anything about it 
    195                                                 BBTracker.log(e); 
     193                                                BBTracker.log(this, e); 
    196194                                        } 
    197195                                } 
  • bbtracker/trunk/src/org/bbtracker/mobile/IconManager.java

    r61 r88  
    9191                                icon = Image.createImage(resourceName); 
    9292                        } catch (final IOException e) { 
    93                                 BBTracker.log("Couldn't read \"" + resourceName + "\": " + e.getMessage()); 
     93                                BBTracker.log(this, "Couldn't read \"" + resourceName + "\": " + e.getMessage()); 
    9494                                icon = NO_ICON; 
    9595                        } 
  • bbtracker/trunk/src/org/bbtracker/mobile/RMSTrackStore.java

    r73 r88  
    3737                                        dis.close(); 
    3838                                } catch (final IOException e) { 
    39                                         BBTracker.log(e); 
     39                                        BBTracker.log(this, e); 
    4040                                } 
    4141                                i++; 
     
    5555                        return new TrackStoreEntry[0]; 
    5656                } catch (final RecordStoreException e) { 
    57                         BBTracker.log(e); 
     57                        BBTracker.log(this, e, "listing tracks"); 
    5858                        throw new TrackStoreException(e); 
    5959                } finally { 
  • bbtracker/trunk/src/org/bbtracker/mobile/TrackManager.java

    r81 r88  
    280280                                break; 
    281281                        } catch (final TrackStoreException e) { 
    282                                 BBTracker.log(e); 
     282                                BBTracker.log(this, e, "saving track"); 
    283283                                final String msg = e.getMessage(); 
    284284                                error = error == null ? msg : error + "\n" + msg; 
     
    305305                } catch (final IllegalArgumentException e) { 
    306306                        provider.setLocationListener(locationListener, -1, -1, -1); 
    307                         BBTracker.log(e); 
     307                        BBTracker.log(this, e); 
    308308                } 
    309309        } 
     
    432432                                        state = oldState; 
    433433                                } catch (final LocationException e) { 
    434                                         BBTracker.log(e); 
     434                                        BBTracker.log(this, e); 
    435435                                        BBTracker.getTimer().schedule(this, DELAY_PER_LEVEL * gpsRecoveryEscalation); 
    436436                                        fireStateChanged(); 
     
    438438                        } 
    439439                } 
    440  
    441440        } 
    442441} 
  • bbtracker/trunk/src/org/bbtracker/mobile/gui/AboutForm.java

    r61 r88  
    2727 
    2828public class AboutForm extends Form implements CommandListener { 
     29        private final Command backCommand; 
     30 
     31        private final Command activeDebugCommand; 
     32 
     33        private final Command deactiveDebugCommand; 
     34 
    2935        public AboutForm() { 
    3036                super("About " + BBTracker.getName()); 
     
    3339                                "bbTracker is released under the GNU General Public License v2. See http://www.gnu.org/licenses/.")); 
    3440                append("Icons have been taken (and sometimes modified) from the Tango Project (http://tango-project.org/) and the Human Icon Theme (Copyright 2004-2006 Canonical Ltd.). Both projects release their icons under the Creative Commons Attribution-ShareAlike 2.5 license. Any modifications I did on those icons are released under the same license."); 
    35                 addCommand(new Command("Back", Command.BACK, 0)); 
     41                backCommand = new Command("Back", Command.BACK, 0); 
     42                activeDebugCommand = new Command("Activate Debug Log", Command.SCREEN, 1); 
     43                deactiveDebugCommand = new Command("Deactivate Debug Log", Command.SCREEN, 1); 
     44                addCommand(backCommand); 
     45                updateDebugCommands(); 
    3646                setCommandListener(this); 
    3747        } 
    3848 
     49        private void updateDebugCommands() { 
     50                removeCommand(activeDebugCommand); 
     51                removeCommand(deactiveDebugCommand); 
     52                addCommand(BBTracker.isLogActive() ? deactiveDebugCommand : activeDebugCommand); 
     53        } 
     54 
    3955        public void commandAction(final Command command, final Displayable source) { 
    40                 BBTracker.getInstance().showMainCanvas(); 
     56                if (command == backCommand) { 
     57                        BBTracker.getInstance().showMainCanvas(); 
     58                } else if (command == deactiveDebugCommand) { 
     59                        BBTracker.setLogActive(false); 
     60                        updateDebugCommands(); 
     61                } else if (command == activeDebugCommand) { 
     62                        BBTracker.setLogActive(true); 
     63                        updateDebugCommands(); 
     64                } 
    4165        } 
    4266 
  • bbtracker/trunk/src/org/bbtracker/mobile/gui/MainCanvas.java

    r85 r88  
    292292                                nextDisplayable = this; 
    293293                        } else { 
    294                                 BBTracker.log("Unknown command: " + command + " <" + command.getLabel() + "/" + command.getLongLabel() + 
    295                                                 ">"); 
     294                                BBTracker.log(this, "Unknown command: " + command + " <" + command.getLabel() + "/" + 
     295                                                command.getLongLabel() + ">"); 
    296296                                nextDisplayable = this; 
    297297                        } 
  • bbtracker/trunk/src/org/bbtracker/mobile/gui/OptionsForm.java

    r82 r88  
    194194                        } catch (final NumberFormatException e) { 
    195195                                // should not happen 
    196                                 BBTracker.log(e); 
     196                                BBTracker.log(this, e, "parsing sampleInterval: " + sampleField.getString()); 
    197197                        } 
    198198                        pref.setStartAction(startTypeGroup.getSelectedIndex()); 
    199199                        pref.setTrackDirectory(directoryField.getString()); 
     200                        BBTracker.initLog(); 
    200201 
    201202                        for (int i = 0; i < Preferences.EXPORT_FORMATS.length; i++) { 
  • bbtracker/trunk/src/org/bbtracker/mobile/gui/StatusTile.java

    r62 r88  
    8989                twoLineLayout = fitsTwoLineLayout(width); 
    9090                if (!twoLineLayout && width < ((MARGIN + latWidth) * 2 + MINIMAL_GAP)) { 
    91                         BBTracker.log("onResize: Setting Font size to small, because even three lines overlap!"); 
     91                        BBTracker.log(this, "onResize: Setting Font size to small, because even three lines overlap!"); 
    9292                        setFontSize(Font.SIZE_SMALL); 
    9393                } 
     
    199199                        lineCount = 3; 
    200200                        if (!fitsThreeLineLayout(width)) { 
    201                                 BBTracker.log("getPreferredHeight: Setting Font size to small, because even three lines overlap!"); 
     201                                BBTracker 
     202                                                .log(this, "getPreferredHeight: Setting Font size to small, because even three lines overlap!"); 
    202203                                setFontSize(Font.SIZE_SMALL); 
    203204                        } 
  • bbtracker/trunk/src/org/bbtracker/mobile/gui/TracksForm.java

    r75 r88  
    107107                                loadEntries(); 
    108108                        } catch (final TrackStoreException e) { 
    109                                 BBTracker.log(e); 
     109                                BBTracker.log(this, e, "deleting track and listing tracks"); 
    110110                                final Alert alert = new Alert("Couldn't delete Track.", "The track " + tse.getName() + 
    111111                                                " couldn't be deleted: " + e.getMessage(), null, AlertType.INFO); 
     
    125125                                track = tse.loadTrack(); 
    126126                        } catch (final TrackStoreException e) { 
    127                                 BBTracker.log(e); 
     127                                BBTracker.log(this, e, "loading track"); 
    128128                                final Alert alert = new Alert("Couldn't load Track.", "The track " + tse.getName() + 
    129129                                                " couldn't be loaded: " + e.getMessage(), null, AlertType.INFO); 
Note: See TracChangeset for help on using the changeset viewer.