Browse Source

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 years ago
parent
commit
2945115f5d

BIN
dist/DMDirc.jar View File


+ 0
- 31
dist/README.TXT View File

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.

BIN
dist/lib/swing-layout-1.0.jar View File


+ 7
- 4
src/uk/org/ownage/dmdirc/logger/DebugLevel.java View File

22
 
22
 
23
 package uk.org.ownage.dmdirc.logger;
23
 package uk.org.ownage.dmdirc.logger;
24
 
24
 
25
+/**
26
+ * Specific debug levels allowed by Logger
27
+ */
25
 public enum DebugLevel {        
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 View File

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

+ 18
- 0
src/uk/org/ownage/dmdirc/logger/LogLevel.java View File

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

+ 64
- 58
src/uk/org/ownage/dmdirc/logger/Logger.java View File

35
 import javax.swing.JDialog;
35
 import javax.swing.JDialog;
36
 import javax.swing.JOptionPane;
36
 import javax.swing.JOptionPane;
37
 import uk.org.ownage.dmdirc.Config;
37
 import uk.org.ownage.dmdirc.Config;
38
+import uk.org.ownage.dmdirc.ui.FatalErrorDialog;
38
 import uk.org.ownage.dmdirc.ui.MainFrame;
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
 public class Logger {
45
 public class Logger {
45
     
46
     
47
+    /**
48
+     * Logfile Printwriter
49
+     */
46
     private static PrintWriter logWriter = null;
50
     private static PrintWriter logWriter = null;
47
     
51
     
52
+    /**
53
+     * dialog used to report errors to the ui
54
+     */
48
     private static JDialog dialog;
55
     private static JDialog dialog;
49
     
56
     
57
+    /**
58
+     * JOptionPane used as part of some dialogs
59
+     */
50
     private static JOptionPane optionPane;
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
     private Logger() {
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
     public static void error(ErrorLevel level, String message) {
78
     public static void error(ErrorLevel level, String message) {
59
         if (logWriter == null ) createWriter();
79
         if (logWriter == null ) createWriter();
60
         switch (level) {
80
         switch (level) {
61
             case FATAL:
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
                 dialog.pack();
84
                 dialog.pack();
84
                 dialog.setLocationRelativeTo(MainFrame.getMainFrame());
85
                 dialog.setLocationRelativeTo(MainFrame.getMainFrame());
85
                 dialog.setVisible(true);
86
                 dialog.setVisible(true);
86
                 break;
87
                 break;
87
             default:
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
                 break;
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
     public static void error(ErrorLevel level, Exception exception) {
100
     public static void error(ErrorLevel level, Exception exception) {
101
+        String[] message;
95
         if (logWriter == null ) createWriter();
102
         if (logWriter == null ) createWriter();
96
-        StackTraceElement[] stacktrace = exception.getStackTrace();
103
+        StackTraceElement[] stackTrace = exception.getStackTrace();
97
         switch (level) {
104
         switch (level) {
98
             case FATAL:
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
                     logWriter.println("\t\t\t\t"+traceElement);
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
                 dialog.pack();
116
                 dialog.pack();
124
                 dialog.setLocationRelativeTo(MainFrame.getMainFrame());
117
                 dialog.setLocationRelativeTo(MainFrame.getMainFrame());
125
                 dialog.setVisible(true);
118
                 dialog.setVisible(true);
126
                 break;
119
                 break;
127
             default:
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
                     logWriter.println("\t\t\t\t"+traceElement);
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
                 break;
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
     public static void debug(DebugLevel level, String message) {
135
     public static void debug(DebugLevel level, String message) {
138
         if (logWriter == null ) createWriter();
136
         if (logWriter == null ) createWriter();
139
         switch(level) {
137
         switch(level) {
140
             default:
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
                 break;
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
     public static void log(LogLevel level, String message) {
150
     public static void log(LogLevel level, String message) {
148
         if (logWriter == null ) createWriter();
151
         if (logWriter == null ) createWriter();
149
         switch(level) {
152
         switch(level) {
150
             default:
153
             default:
151
-                logWriter.println(format.format(new Date())+": LOG: "+level+" :"+message);
154
+                logWriter.println(formatter.format(new Date())+": LOG: "+level+" :"+message);
152
                 break;
155
                 break;
153
         }
156
         }
154
     }
157
     }
155
     
158
     
159
+    /**
160
+     * Initialises the logfile writer and date formatter
161
+     */
156
     private static void createWriter() {
162
     private static void createWriter() {
157
         try {
163
         try {
158
             logWriter = new PrintWriter(new BufferedWriter(new FileWriter(Config.getConfigDir()+"errors.log")));
164
             logWriter = new PrintWriter(new BufferedWriter(new FileWriter(Config.getConfigDir()+"errors.log")));
160
             Logger.error(ErrorLevel.FATAL, "Oh god, i cant open the error log.");
166
             Logger.error(ErrorLevel.FATAL, "Oh god, i cant open the error log.");
161
         }
167
         }
162
         if (Config.hasOption("logging", "dateFormat")) {
168
         if (Config.hasOption("logging", "dateFormat")) {
163
-            format = new SimpleDateFormat(Config.getOption("logging", "dateFormat"));
169
+            formatter = new SimpleDateFormat(Config.getOption("logging", "dateFormat"));
164
         } else {
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 View File

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

+ 29
- 3
src/uk/org/ownage/dmdirc/ui/FatalErrorDialog.java View File

6
 
6
 
7
 package uk.org.ownage.dmdirc.ui;
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
  * @author  chris
15
  * @author  chris
12
  */
16
  */
13
 public class FatalErrorDialog extends javax.swing.JDialog {
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
         super(parent, modal);
20
         super(parent, modal);
18
         initComponents();
21
         initComponents();
22
+        for (String line: message) {
23
+            jTextArea1.append(line+"\r\n");
24
+        }
25
+        jTextArea1.setCaretPosition(0);
19
     }
26
     }
20
     
27
     
21
     /** This method is called from within the constructor to
28
     /** This method is called from within the constructor to
33
         jButton1 = new javax.swing.JButton();
40
         jButton1 = new javax.swing.JButton();
34
 
41
 
35
         setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
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
         jLabel1.setFont(new java.awt.Font("Dialog", 1, 18));
49
         jLabel1.setFont(new java.awt.Font("Dialog", 1, 18));
37
         jLabel1.setText("We're sorry...");
50
         jLabel1.setText("We're sorry...");
38
 
51
 
46
         jScrollPane1.setViewportView(jTextArea1);
59
         jScrollPane1.setViewportView(jTextArea1);
47
 
60
 
48
         jButton1.setText("OK");
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
         org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
68
         org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
51
         getContentPane().setLayout(layout);
69
         getContentPane().setLayout(layout);
78
         );
96
         );
79
         pack();
97
         pack();
80
     }// </editor-fold>//GEN-END:initComponents
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
     // Variables declaration - do not modify//GEN-BEGIN:variables
108
     // Variables declaration - do not modify//GEN-BEGIN:variables
83
     private javax.swing.JButton jButton1;
109
     private javax.swing.JButton jButton1;
84
     private javax.swing.JLabel jLabel1;
110
     private javax.swing.JLabel jLabel1;

Loading…
Cancel
Save