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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 com.dmdirc.logger;
  23. import com.dmdirc.Config;
  24. import com.dmdirc.ui.dialogs.error.FatalErrorDialog;
  25. import java.io.File;
  26. import java.io.FileNotFoundException;
  27. import java.io.FileOutputStream;
  28. import java.io.IOException;
  29. import java.io.OutputStream;
  30. import java.io.PrintWriter;
  31. import java.util.Date;
  32. /**
  33. * Logger class for the application.
  34. */
  35. public final class Logger {
  36. /** ProgramError folder. */
  37. private static File errorDir;
  38. /** Prevent instantiation of a new instance of Logger. */
  39. private Logger() {
  40. //Ignore
  41. }
  42. /**
  43. * Called when a user correctable error occurs.
  44. *
  45. * @param level Severity of the error
  46. * @param message Brief error description
  47. */
  48. public static void userError(final ErrorLevel level,
  49. final String message) {
  50. error(level, message, null, false);
  51. }
  52. /**
  53. * Called when a non user correctable error occurs, the error will be
  54. * logged and optionally sent to the developers.
  55. *
  56. * @param level Severity of the error
  57. * @param message Brief error description
  58. * @param exception Cause of error
  59. */
  60. public static void appError(final ErrorLevel level,
  61. final String message, final Throwable exception) {
  62. error(level, message, exception, true);
  63. }
  64. /**
  65. * Handles an error in the program.
  66. *
  67. * @param level Severity of the error
  68. * @param message Brief error description
  69. * @param exception Cause of error
  70. * @param sendable Whether the error is sendable
  71. */
  72. private static void error(final ErrorLevel level,
  73. final String message, final Throwable exception,
  74. final boolean sendable) {
  75. final ProgramError error = createError(level, message, exception);
  76. if (!sendable) {
  77. error.setStatus(ErrorStatus.NOT_APPLICABLE);
  78. }
  79. if (sendable && Config.getOptionBool("general", "submitErrors")) {
  80. ErrorManager.getErrorManager().sendError(error);
  81. }
  82. if (level == ErrorLevel.FATAL) {
  83. if (!Config.getOptionBool("general", "submitErrors")) {
  84. error.setStatus(ErrorStatus.FINISHED);
  85. }
  86. new FatalErrorDialog(error);
  87. return;
  88. }
  89. ErrorManager.getErrorManager().addError(error);
  90. }
  91. /**
  92. * Creates a new ProgramError from the supplied information, and writes
  93. * the error to a file.
  94. *
  95. * @param level Error level
  96. * @param message Error message
  97. * @param exception Error cause
  98. *
  99. * @return ProgramError encapsulating the supplied information
  100. */
  101. private static ProgramError createError(final ErrorLevel level,
  102. final String message, final Throwable exception) {
  103. final String[] trace;
  104. final StackTraceElement[] traceElements;
  105. if (exception == null) {
  106. trace = new String[0];
  107. } else {
  108. traceElements = exception.getStackTrace();
  109. trace = new String[traceElements.length + 1];
  110. trace[0] = exception.toString();
  111. for (int i = 0; i < traceElements.length; i++) {
  112. trace[i + 1] = traceElements[i].toString();
  113. }
  114. }
  115. final ProgramError error = new ProgramError(level, message, trace,
  116. new Date(System.currentTimeMillis()));
  117. writeError(error);
  118. return error;
  119. }
  120. /**
  121. * Writes the specified error to a file.
  122. *
  123. * @param error ProgramError to write to a file.
  124. */
  125. private static void writeError(final ProgramError error) {
  126. final PrintWriter out = new PrintWriter(createNewErrorFile(error), true);
  127. out.println("Date:" + error.getDate());
  128. out.println("Level: " + error.getLevel());
  129. out.println("Description: " + error.getMessage());
  130. out.println("Details:");
  131. final String[] trace = error.getTrace();
  132. for (String traceLine : trace) {
  133. out.println('\t' + traceLine);
  134. }
  135. out.close();
  136. }
  137. /**
  138. * Creates a new file for an error and returns the output stream.
  139. *
  140. * @param error Error to create file for
  141. *
  142. * @return BufferedOutputStream to write to the error file
  143. */
  144. @SuppressWarnings("PMD.SystemPrintln")
  145. private static synchronized OutputStream createNewErrorFile(final ProgramError error) {
  146. if (errorDir == null) {
  147. errorDir = new File(Config.getConfigDir() + "errors");
  148. if (!errorDir.exists()) {
  149. errorDir.mkdirs();
  150. }
  151. }
  152. final File errorFile = new File(errorDir, "" + error.getLevel() + "-"
  153. + error.getDate().getTime() + ".log");
  154. if (errorFile.exists()) {
  155. boolean rename = false;
  156. while (!rename) {
  157. rename = errorFile.renameTo(new File(errorFile.getAbsolutePath() + '_'));
  158. }
  159. }
  160. try {
  161. errorFile.createNewFile();
  162. } catch (IOException ex) {
  163. System.err.println("Error creating new file: ");
  164. ex.printStackTrace(System.err);
  165. return new NullOutputStream();
  166. }
  167. try {
  168. return new FileOutputStream(errorFile);
  169. } catch (FileNotFoundException ex) {
  170. System.err.println("Error creating new stream: ");
  171. ex.printStackTrace(System.err);
  172. return new NullOutputStream();
  173. }
  174. }
  175. }