ErrorManager.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /* ErrorManager.java --
  2. A class for dealing with errors that a Handler encounters
  3. during logging
  4. Copyright (C) 2002, 2003 Free Software Foundation, Inc.
  5. This file is part of GNU Classpath.
  6. GNU Classpath is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10. GNU Classpath is distributed in the hope that it will be useful, but
  11. WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with GNU Classpath; see the file COPYING. If not, write to the
  16. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. 02110-1301 USA.
  18. Linking this library statically or dynamically with other modules is
  19. making a combined work based on this library. Thus, the terms and
  20. conditions of the GNU General Public License cover the whole
  21. combination.
  22. As a special exception, the copyright holders of this library give you
  23. permission to link this library with independent modules to produce an
  24. executable, regardless of the license terms of these independent
  25. modules, and to copy and distribute the resulting executable under
  26. terms of your choice, provided that you also meet, for each linked
  27. independent module, the terms and conditions of the license of that
  28. module. An independent module is a module which is not derived from
  29. or based on this library. If you modify this library, you may extend
  30. this exception to your version of the library, but you are not
  31. obligated to do so. If you do not wish to do so, delete this
  32. exception statement from your version. */
  33. package java.util.logging;
  34. /**
  35. * An <code>ErrorManager</code> deals with errors that a <code>Handler</code>
  36. * encounters while logging.
  37. *
  38. * @see Handler#setErrorManager(ErrorManager)
  39. *
  40. * @author Sascha Brawer (brawer@acm.org)
  41. */
  42. public class ErrorManager
  43. {
  44. /* The values have been taken from Sun's public J2SE 1.4 API
  45. * documentation.
  46. * See http://java.sun.com/j2se/1.4/docs/api/constant-values.html
  47. */
  48. /**
  49. * Indicates that there was a failure that does not readily
  50. * fall into any of the other categories.
  51. */
  52. public static final int GENERIC_FAILURE = 0;
  53. /**
  54. * Indicates that there was a problem upon writing to
  55. * an output stream.
  56. */
  57. public static final int WRITE_FAILURE = 1;
  58. /**
  59. * Indicates that there was a problem upon flushing
  60. * an output stream.
  61. */
  62. public static final int FLUSH_FAILURE = 2;
  63. /**
  64. * Indicates that there was a problem upon closing
  65. * an output stream.
  66. */
  67. public static final int CLOSE_FAILURE = 3;
  68. /**
  69. * Indicates that there was a problem upon opening
  70. * an output stream.
  71. */
  72. public static final int OPEN_FAILURE = 4;
  73. /**
  74. * Indicates that there was a problem upon formatting
  75. * the message of a log record.
  76. */
  77. public static final int FORMAT_FAILURE = 5;
  78. /**
  79. * Indicates whether the {@link #error} method of this ErrorManager
  80. * has ever been used.
  81. *
  82. * Declared volatile in order to correctly support the
  83. * double-checked locking idiom (once the revised Java Memory Model
  84. * gets adopted); see Classpath bug #2944.
  85. */
  86. private volatile boolean everUsed = false;
  87. public ErrorManager()
  88. {
  89. }
  90. /**
  91. * Reports an error that occured upon logging. The default implementation
  92. * emits the very first error to System.err, ignoring subsequent errors.
  93. *
  94. * @param message a message describing the error, or <code>null</code> if
  95. * there is no suitable description.
  96. *
  97. * @param ex an exception, or <code>null</code> if the error is not
  98. * related to an exception.
  99. *
  100. * @param errorCode one of the defined error codes, for example
  101. * <code>ErrorManager.CLOSE_FAILURE</code>.
  102. */
  103. public void error(String message, Exception ex, int errorCode)
  104. {
  105. if (everUsed)
  106. return;
  107. synchronized (this)
  108. {
  109. /* The double check is intentional. If the first check was
  110. * omitted, the monitor would have to be entered every time
  111. * error() method was called. If the second check was
  112. * omitted, the code below could be executed by multiple
  113. * threads simultaneously.
  114. *
  115. * This is the 'double-checked locking' idiom, which is broken
  116. * with the current version of the Java memory model. However,
  117. * we assume that JVMs will have adopted a revised version of
  118. * the Java Memory Model by the time GNU Classpath gains
  119. * widespread acceptance. See Classpath bug #2944.
  120. */
  121. if (everUsed)
  122. return;
  123. everUsed = true;
  124. }
  125. String codeMsg;
  126. switch (errorCode)
  127. {
  128. case GENERIC_FAILURE:
  129. codeMsg = "GENERIC_FAILURE";
  130. break;
  131. case WRITE_FAILURE:
  132. codeMsg = "WRITE_FAILURE";
  133. break;
  134. case FLUSH_FAILURE:
  135. codeMsg = "FLUSH_FAILURE";
  136. break;
  137. case CLOSE_FAILURE:
  138. codeMsg = "CLOSE_FAILURE";
  139. break;
  140. case OPEN_FAILURE:
  141. codeMsg = "OPEN_FAILURE";
  142. break;
  143. case FORMAT_FAILURE:
  144. codeMsg = "FORMAT_FAILURE";
  145. break;
  146. default:
  147. codeMsg = String.valueOf(errorCode);
  148. break;
  149. }
  150. System.err.println("Error upon logging: " + codeMsg);
  151. if ((message != null) && (message.length() > 0))
  152. System.err.println(message);
  153. if (ex != null)
  154. ex.printStackTrace();
  155. }
  156. }