Преглед на файлове

Updated logger to use the new FatalErrorDialog

Updated FatalErrorDialog to terminate the application and show the appropriate error message


git-svn-id: http://svn.dmdirc.com/trunk@199 00569f92-eb28-0410-84fd-f71c24880f
tags/0.1
Gregory Holmes преди 17 години
родител
ревизия
2945115f5d

Двоични данни
dist/DMDirc.jar Целия файл


+ 0
- 31
dist/README.TXT Целия файл

@@ -1,31 +0,0 @@
1
-========================
2
-BUILD OUTPUT DESCRIPTION
3
-========================
4
-
5
-When you build an Java application project that has a main class, the IDE
6
-automatically copies all of the JAR
7
-files on the projects classpath to your projects dist/lib folder. The IDE
8
-also adds each of the JAR files to the Class-Path element in the application
9
-JAR files manifest file (MANIFEST.MF).
10
-
11
-To run the project from the command line, go to the dist folder and
12
-type the following:
13
-
14
-java -jar "DMDirc.jar" 
15
-
16
-To distribute this project, zip up the dist folder (including the lib folder)
17
-and distribute the ZIP file.
18
-
19
-Notes:
20
-
21
-* If two JAR files on the project classpath have the same name, only the first
22
-JAR file is copied to the lib folder.
23
-* If the classpath contains a folder of classes or resources, none of the
24
-classpath elements are copied to the dist folder.
25
-* If a library on the projects classpath also has a Class-Path element
26
-specified in the manifest,the content of the Class-Path element has to be on
27
-the projects runtime path.
28
-* To set a main class in a standard Java project, right-click the project node
29
-in the Projects window and choose Properties. Then click Run and enter the
30
-class name in the Main Class field. Alternatively, you can manually type the
31
-class name in the manifest Main-Class element.

Двоични данни
dist/lib/swing-layout-1.0.jar Целия файл


+ 7
- 4
src/uk/org/ownage/dmdirc/logger/DebugLevel.java Целия файл

@@ -22,9 +22,12 @@
22 22
 
23 23
 package uk.org.ownage.dmdirc.logger;
24 24
 
25
+/**
26
+ * Specific debug levels allowed by Logger
27
+ */
25 28
 public enum DebugLevel {        
26
-    NORMAL, 
27
-    FINE, 
28
-    FINER, 
29
-    FINEST
29
+    /**
30
+     * Normal debug messages
31
+     */
32
+    NORMAL
30 33
 }

+ 15
- 0
src/uk/org/ownage/dmdirc/logger/ErrorLevel.java Целия файл

@@ -22,9 +22,24 @@
22 22
 
23 23
 package uk.org.ownage.dmdirc.logger;
24 24
 
25
+/**
26
+ * Specific error levels allowed by Logger
27
+ */
25 28
 public enum ErrorLevel {        
29
+    /**
30
+     * Fatal error
31
+     */
26 32
     FATAL, 
33
+    /**
34
+     * Standard error
35
+     */
27 36
     ERROR, 
37
+    /**
38
+     * Warning error
39
+     */
28 40
     WARNING, 
41
+    /**
42
+     * Informational error
43
+     */
29 44
     INFO
30 45
 }

+ 18
- 0
src/uk/org/ownage/dmdirc/logger/LogLevel.java Целия файл

@@ -22,10 +22,28 @@
22 22
 
23 23
 package uk.org.ownage.dmdirc.logger;
24 24
 
25
+/**
26
+ * Specific log levels allowed by the Logger
27
+ */
25 28
 public enum LogLevel {        
29
+    /**
30
+     * Core log messages
31
+     */
26 32
     CORE, 
33
+    /**
34
+     * Command parser log messages
35
+     */
27 36
     COMMAND, 
37
+    /**
38
+     * Parser log messages
39
+     */
28 40
     PARSER, 
41
+    /**
42
+     * Plugin log messages
43
+     */
29 44
     PLUGIN, 
45
+    /**
46
+     * UI log messages
47
+     */
30 48
     UI
31 49
 }

+ 64
- 58
src/uk/org/ownage/dmdirc/logger/Logger.java Целия файл

@@ -35,124 +35,130 @@ import java.util.Date;
35 35
 import javax.swing.JDialog;
36 36
 import javax.swing.JOptionPane;
37 37
 import uk.org.ownage.dmdirc.Config;
38
+import uk.org.ownage.dmdirc.ui.FatalErrorDialog;
38 39
 import uk.org.ownage.dmdirc.ui.MainFrame;
39 40
 
40 41
 /**
41
- *
42
- * @author greboid
42
+ * Logger class for an applications, provides logging, error logging and debug 
43
+ * logging
43 44
  */
44 45
 public class Logger {
45 46
     
47
+    /**
48
+     * Logfile Printwriter
49
+     */
46 50
     private static PrintWriter logWriter = null;
47 51
     
52
+    /**
53
+     * dialog used to report errors to the ui
54
+     */
48 55
     private static JDialog dialog;
49 56
     
57
+    /**
58
+     * JOptionPane used as part of some dialogs
59
+     */
50 60
     private static JOptionPane optionPane;
51 61
     
52
-    private static SimpleDateFormat format;
62
+    /**
63
+     * Date formatter, used for logging and displaying messages
64
+     */
65
+    private static SimpleDateFormat formatter;
53 66
     
54
-    /** Creates a new instance of Logger */
67
+    /**
68
+     * Prevents creation a new instance of Logger
69
+     */
55 70
     private Logger() {
56 71
     }
57 72
     
73
+    /**
74
+     * Record an error message for the application, notifying the user if appropriate
75
+     * @param level error level
76
+     * @param message Error message/cause
77
+     */
58 78
     public static void error(ErrorLevel level, String message) {
59 79
         if (logWriter == null ) createWriter();
60 80
         switch (level) {
61 81
             case FATAL:
62
-                logWriter.println(format.format(new Date())+": ERROR: "+level+" :"+message);
63
-                optionPane = new JOptionPane(message, JOptionPane.ERROR_MESSAGE,
64
-                        JOptionPane.DEFAULT_OPTION);
65
-                dialog = new JDialog(MainFrame.getMainFrame(), "Fatal Error",
66
-                        true);
67
-                dialog.setContentPane(optionPane);
68
-                dialog.setDefaultCloseOperation(
69
-                        JDialog.DO_NOTHING_ON_CLOSE);
70
-                dialog.addWindowListener(new WindowAdapter() {
71
-                    public void windowClosing(WindowEvent we) {
72
-                        System.exit(-1);
73
-                    }
74
-                });
75
-                optionPane.addPropertyChangeListener(
76
-                        new PropertyChangeListener() {
77
-                    public void propertyChange(PropertyChangeEvent e) {
78
-                        if (dialog.isVisible() && e.getSource() == optionPane) {
79
-                            System.exit(-1);
80
-                        }
81
-                    }
82
-                });
82
+                logWriter.println(formatter.format(new Date())+": ERROR: "+level+" :"+message);
83
+                dialog = new FatalErrorDialog(MainFrame.getMainFrame(), true, new String[]{message});
83 84
                 dialog.pack();
84 85
                 dialog.setLocationRelativeTo(MainFrame.getMainFrame());
85 86
                 dialog.setVisible(true);
86 87
                 break;
87 88
             default:
88
-                logWriter.println(format.format(new Date())+": ERROR: "+level+" :"+message);
89
-                System.err.println(format.format(new Date())+": ERROR: "+level+" :"+message);
89
+                logWriter.println(formatter.format(new Date())+": ERROR: "+level+" :"+message);
90
+                System.err.println(formatter.format(new Date())+": ERROR: "+level+" :"+message);
90 91
                 break;
91 92
         }
92 93
     }
93 94
     
95
+    /**
96
+     * Record an error message for the application, notifying the user if appropriate
97
+     * @param level error level
98
+     * @param exception Cause of error
99
+     */
94 100
     public static void error(ErrorLevel level, Exception exception) {
101
+        String[] message;
95 102
         if (logWriter == null ) createWriter();
96
-        StackTraceElement[] stacktrace = exception.getStackTrace();
103
+        StackTraceElement[] stackTrace = exception.getStackTrace();
97 104
         switch (level) {
98 105
             case FATAL:
99
-                logWriter.println(format.format(new Date())+": ERROR: "+level+" :"+exception.getMessage());
100
-                for (StackTraceElement traceElement: stacktrace) {
106
+                logWriter.println(formatter.format(new Date())+": ERROR: "+level+" :"+exception.getMessage());
107
+                message = new String[stackTrace.length+1];
108
+                message[0] = exception.getMessage();
109
+                int i = 1;
110
+                for (StackTraceElement traceElement: stackTrace) {
111
+                    message[i] = traceElement.toString();
101 112
                     logWriter.println("\t\t\t\t"+traceElement);
113
+                    i++;
102 114
                 }
103
-                optionPane = new JOptionPane(exception.getMessage(), JOptionPane.ERROR_MESSAGE,
104
-                        JOptionPane.DEFAULT_OPTION);
105
-                dialog = new JDialog(MainFrame.getMainFrame(), "Fatal Error",
106
-                        true);
107
-                dialog.setContentPane(optionPane);
108
-                dialog.setDefaultCloseOperation(
109
-                        JDialog.DO_NOTHING_ON_CLOSE);
110
-                dialog.addWindowListener(new WindowAdapter() {
111
-                    public void windowClosing(WindowEvent we) {
112
-                        System.exit(-1);
113
-                    }
114
-                });
115
-                optionPane.addPropertyChangeListener(
116
-                        new PropertyChangeListener() {
117
-                    public void propertyChange(PropertyChangeEvent e) {
118
-                        if (dialog.isVisible() && e.getSource() == optionPane) {
119
-                            System.exit(-1);
120
-                        }
121
-                    }
122
-                });
115
+                dialog = new FatalErrorDialog(MainFrame.getMainFrame(), true, message);
123 116
                 dialog.pack();
124 117
                 dialog.setLocationRelativeTo(MainFrame.getMainFrame());
125 118
                 dialog.setVisible(true);
126 119
                 break;
127 120
             default:
128
-                logWriter.println(format.format(new Date())+": ERROR: "+level+" :"+exception.getMessage());
129
-                for (StackTraceElement traceElement: stacktrace) {
121
+                logWriter.println(formatter.format(new Date())+": ERROR: "+level+" :"+exception.getMessage());
122
+                for (StackTraceElement traceElement: stackTrace) {
130 123
                     logWriter.println("\t\t\t\t"+traceElement);
131 124
                 }
132
-                System.err.println(format.format(new Date())+": ERROR: "+level+" :"+exception.getMessage());
125
+                System.err.println(formatter.format(new Date())+": ERROR: "+level+" :"+exception.getMessage());
133 126
                 break;
134 127
         }
135 128
     }
136 129
     
130
+    /**
131
+     * Record an debug message for the application, notifying the user if appropriate
132
+     * @param level debug level
133
+     * @param message Debug message
134
+     */
137 135
     public static void debug(DebugLevel level, String message) {
138 136
         if (logWriter == null ) createWriter();
139 137
         switch(level) {
140 138
             default:
141
-                System.out.println(format.format(new Date())+": DEBUG: "+level+" :"+message);
142
-                logWriter.println(format.format(new Date())+": DEBUG: "+level+" :"+message);
139
+                System.out.println(formatter.format(new Date())+": DEBUG: "+level+" :"+message);
140
+                logWriter.println(formatter.format(new Date())+": DEBUG: "+level+" :"+message);
143 141
                 break;
144 142
         }
145 143
     }
146 144
     
145
+    /**
146
+     * Record a log message for the application
147
+     * @param level log level
148
+     * @param message Log message
149
+     */
147 150
     public static void log(LogLevel level, String message) {
148 151
         if (logWriter == null ) createWriter();
149 152
         switch(level) {
150 153
             default:
151
-                logWriter.println(format.format(new Date())+": LOG: "+level+" :"+message);
154
+                logWriter.println(formatter.format(new Date())+": LOG: "+level+" :"+message);
152 155
                 break;
153 156
         }
154 157
     }
155 158
     
159
+    /**
160
+     * Initialises the logfile writer and date formatter
161
+     */
156 162
     private static void createWriter() {
157 163
         try {
158 164
             logWriter = new PrintWriter(new BufferedWriter(new FileWriter(Config.getConfigDir()+"errors.log")));
@@ -160,9 +166,9 @@ public class Logger {
160 166
             Logger.error(ErrorLevel.FATAL, "Oh god, i cant open the error log.");
161 167
         }
162 168
         if (Config.hasOption("logging", "dateFormat")) {
163
-            format = new SimpleDateFormat(Config.getOption("logging", "dateFormat"));
169
+            formatter = new SimpleDateFormat(Config.getOption("logging", "dateFormat"));
164 170
         } else {
165
-            format = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
171
+            formatter = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
166 172
         }
167 173
     }
168 174
 }

+ 6
- 0
src/uk/org/ownage/dmdirc/ui/FatalErrorDialog.form Целия файл

@@ -7,6 +7,9 @@
7 7
   <SyntheticProperties>
8 8
     <SyntheticProperty name="formSizePolicy" type="int" value="1"/>
9 9
   </SyntheticProperties>
10
+  <Events>
11
+    <EventHandler event="windowClosing" listener="java.awt.event.WindowListener" parameters="java.awt.event.WindowEvent" handler="formWindowClosing"/>
12
+  </Events>
10 13
   <AuxValues>
11 14
     <AuxValue name="FormSettings_generateMnemonicsCode" type="java.lang.Boolean" value="false"/>
12 15
     <AuxValue name="FormSettings_layoutCodeTarget" type="java.lang.Integer" value="2"/>
@@ -88,6 +91,9 @@
88 91
       <Properties>
89 92
         <Property name="text" type="java.lang.String" value="OK"/>
90 93
       </Properties>
94
+      <Events>
95
+        <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="jButton1ActionPerformed"/>
96
+      </Events>
91 97
     </Component>
92 98
   </SubComponents>
93 99
 </Form>

+ 29
- 3
src/uk/org/ownage/dmdirc/ui/FatalErrorDialog.java Целия файл

@@ -6,16 +6,23 @@
6 6
 
7 7
 package uk.org.ownage.dmdirc.ui;
8 8
 
9
+import java.awt.event.ActionEvent;
10
+import java.awt.event.ActionListener;
11
+import uk.org.ownage.dmdirc.logger.Logger;
12
+
9 13
 /**
10 14
  *
11 15
  * @author  chris
12 16
  */
13 17
 public class FatalErrorDialog extends javax.swing.JDialog {
14 18
     
15
-    /** Creates new form FatalErrorDialog */
16
-    public FatalErrorDialog(java.awt.Frame parent, boolean modal) {
19
+    public FatalErrorDialog(java.awt.Frame parent, boolean modal, String[] message) {
17 20
         super(parent, modal);
18 21
         initComponents();
22
+        for (String line: message) {
23
+            jTextArea1.append(line+"\r\n");
24
+        }
25
+        jTextArea1.setCaretPosition(0);
19 26
     }
20 27
     
21 28
     /** This method is called from within the constructor to
@@ -33,6 +40,12 @@ public class FatalErrorDialog extends javax.swing.JDialog {
33 40
         jButton1 = new javax.swing.JButton();
34 41
 
35 42
         setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
43
+        addWindowListener(new java.awt.event.WindowAdapter() {
44
+            public void windowClosing(java.awt.event.WindowEvent evt) {
45
+                formWindowClosing(evt);
46
+            }
47
+        });
48
+
36 49
         jLabel1.setFont(new java.awt.Font("Dialog", 1, 18));
37 50
         jLabel1.setText("We're sorry...");
38 51
 
@@ -46,6 +59,11 @@ public class FatalErrorDialog extends javax.swing.JDialog {
46 59
         jScrollPane1.setViewportView(jTextArea1);
47 60
 
48 61
         jButton1.setText("OK");
62
+        jButton1.addActionListener(new java.awt.event.ActionListener() {
63
+            public void actionPerformed(java.awt.event.ActionEvent evt) {
64
+                jButton1ActionPerformed(evt);
65
+            }
66
+        });
49 67
 
50 68
         org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
51 69
         getContentPane().setLayout(layout);
@@ -78,7 +96,15 @@ public class FatalErrorDialog extends javax.swing.JDialog {
78 96
         );
79 97
         pack();
80 98
     }// </editor-fold>//GEN-END:initComponents
81
-        
99
+
100
+    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
101
+      System.exit(-1);
102
+    }//GEN-LAST:event_formWindowClosing
103
+
104
+    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
105
+      System.exit(-1);
106
+    }//GEN-LAST:event_jButton1ActionPerformed
107
+    
82 108
     // Variables declaration - do not modify//GEN-BEGIN:variables
83 109
     private javax.swing.JButton jButton1;
84 110
     private javax.swing.JLabel jLabel1;

Loading…
Отказ
Запис