You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Logger.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright (c) 2006-2007 Chris Smith, Shane Mc Cormack, Gregory Holmes
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package uk.org.ownage.dmdirc.logger;
  23. import java.awt.event.WindowAdapter;
  24. import java.awt.event.WindowEvent;
  25. import java.beans.PropertyChangeEvent;
  26. import java.beans.PropertyChangeListener;
  27. import java.io.BufferedWriter;
  28. import java.io.FileWriter;
  29. import java.io.IOException;
  30. import java.io.PrintWriter;
  31. import java.text.SimpleDateFormat;
  32. import java.util.Date;
  33. import javax.swing.JDialog;
  34. import javax.swing.JOptionPane;
  35. import uk.org.ownage.dmdirc.Config;
  36. import uk.org.ownage.dmdirc.ui.MainFrame;
  37. /**
  38. *
  39. * @author greboid
  40. */
  41. public class Logger {
  42. private static PrintWriter logWriter = null;
  43. private static JDialog dialog;
  44. private static JOptionPane optionPane;
  45. private static SimpleDateFormat format;
  46. /** Creates a new instance of Logger */
  47. private Logger() {
  48. }
  49. public static void error(ErrorLevel level, String message) {
  50. if (logWriter == null ) createWriter();
  51. switch (level) {
  52. case FATAL:
  53. logWriter.println(format.format(new Date())+": ERROR: "+": "+level+" :"+message);
  54. optionPane = new JOptionPane(message, JOptionPane.ERROR_MESSAGE,
  55. JOptionPane.DEFAULT_OPTION);
  56. dialog = new JDialog(MainFrame.getMainFrame(), "Fatal Error",
  57. true);
  58. dialog.setContentPane(optionPane);
  59. dialog.setDefaultCloseOperation(
  60. JDialog.DO_NOTHING_ON_CLOSE);
  61. dialog.addWindowListener(new WindowAdapter() {
  62. public void windowClosing(WindowEvent we) {
  63. System.exit(-1);
  64. }
  65. });
  66. optionPane.addPropertyChangeListener(
  67. new PropertyChangeListener() {
  68. public void propertyChange(PropertyChangeEvent e) {
  69. if (dialog.isVisible() && e.getSource() == optionPane) {
  70. System.exit(-1);
  71. }
  72. }
  73. });
  74. dialog.pack();
  75. dialog.setLocationRelativeTo(MainFrame.getMainFrame());
  76. dialog.setVisible(true);
  77. break;
  78. default:
  79. logWriter.println(format.format(new Date())+": ERROR: "+": "+level+" :"+message);
  80. break;
  81. }
  82. }
  83. public static void error(ErrorLevel level, Exception exception) {
  84. if (logWriter == null ) createWriter();
  85. StackTraceElement[] stacktrace = exception.getStackTrace();
  86. switch (level) {
  87. case FATAL:
  88. logWriter.println(format.format(new Date())+": ERROR: "+": "+level+" :"+exception.getMessage());
  89. for (StackTraceElement traceElement: stacktrace) {
  90. logWriter.println("\t\t\t\t"+traceElement);
  91. }
  92. optionPane = new JOptionPane(exception.getMessage(), JOptionPane.ERROR_MESSAGE,
  93. JOptionPane.DEFAULT_OPTION);
  94. dialog = new JDialog(MainFrame.getMainFrame(), "Fatal Error",
  95. true);
  96. dialog.setContentPane(optionPane);
  97. dialog.setDefaultCloseOperation(
  98. JDialog.DO_NOTHING_ON_CLOSE);
  99. dialog.addWindowListener(new WindowAdapter() {
  100. public void windowClosing(WindowEvent we) {
  101. System.exit(-1);
  102. }
  103. });
  104. optionPane.addPropertyChangeListener(
  105. new PropertyChangeListener() {
  106. public void propertyChange(PropertyChangeEvent e) {
  107. if (dialog.isVisible() && e.getSource() == optionPane) {
  108. System.exit(-1);
  109. }
  110. }
  111. });
  112. dialog.pack();
  113. dialog.setLocationRelativeTo(MainFrame.getMainFrame());
  114. dialog.setVisible(true);
  115. break;
  116. default:
  117. logWriter.println(format.format(new Date())+": ERROR: "+": "+level+" :"+exception.getMessage());
  118. for (StackTraceElement traceElement: stacktrace) {
  119. logWriter.println("\t\t\t\t"+traceElement);
  120. }
  121. break;
  122. }
  123. }
  124. public static void debug(DebugLevel level, String message) {
  125. if (logWriter == null ) createWriter();
  126. switch(level) {
  127. default:
  128. System.out.println(format.format(new Date())+": DEBUG: "+": "+level+" :"+message);
  129. logWriter.println(format.format(new Date())+": DEBUG: "+": "+level+" :"+message);
  130. break;
  131. }
  132. }
  133. public static void log(LogLevel level, String message) {
  134. if (logWriter == null ) createWriter();
  135. switch(level) {
  136. default:
  137. logWriter.println(format.format(new Date())+": LOG: "+": "+level+" :"+message);
  138. break;
  139. }
  140. }
  141. private static void createWriter() {
  142. try {
  143. logWriter = new PrintWriter(new BufferedWriter(new FileWriter(Config.getConfigDir()+"errors.log")));
  144. } catch (IOException ex) {
  145. Logger.error(ErrorLevel.FATAL, "Oh god, i cant open the error log.");
  146. }
  147. if (Config.hasOption("logging", "dateFormat")) {
  148. format = new SimpleDateFormat(Config.getOption("logging", "dateFormat"));
  149. } else {
  150. format = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
  151. }
  152. }
  153. }