1. /*
  2. * @(#)ErrorManager.java 1.8 04/02/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.util.logging;
  8. /**
  9. * ErrorManager objects can be attached to Handlers to process
  10. * any error that occur on a Handler during Logging.
  11. * <p>
  12. * When processing logging output, if a Handler encounters problems
  13. * then rather than throwing an Exception back to the issuer of
  14. * the logging call (who is unlikely to be interested) the Handler
  15. * should call its associated ErrorManager.
  16. */
  17. public class ErrorManager {
  18. private boolean reported = false;
  19. /*
  20. * We declare standard error codes for important categories of errors.
  21. */
  22. /**
  23. * GENERIC_FAILURE is used for failure that don't fit
  24. * into one of the other categories.
  25. */
  26. public final static int GENERIC_FAILURE = 0;
  27. /**
  28. * WRITE_FAILURE is used when a write to an output stream fails.
  29. */
  30. public final static int WRITE_FAILURE = 1;
  31. /**
  32. * FLUSH_FAILURE is used when a flush to an output stream fails.
  33. */
  34. public final static int FLUSH_FAILURE = 2;
  35. /**
  36. * CLOSE_FAILURE is used when a close of an output stream fails.
  37. */
  38. public final static int CLOSE_FAILURE = 3;
  39. /**
  40. * OPEN_FAILURE is used when an open of an output stream fails.
  41. */
  42. public final static int OPEN_FAILURE = 4;
  43. /**
  44. * FORMAT_FAILURE is used when formatting fails for any reason.
  45. */
  46. public final static int FORMAT_FAILURE = 5;
  47. /**
  48. * The error method is called when a Handler failure occurs.
  49. * <p>
  50. * This method may be overriden in subclasses. The default
  51. * behavior in this base class is that the first call is
  52. * reported to System.err, and subsequent calls are ignored.
  53. *
  54. * @param msg a descriptive string (may be null)
  55. * @param ex an exception (may be null)
  56. * @param code an error code defined in ErrorManager
  57. */
  58. public synchronized void error(String msg, Exception ex, int code) {
  59. if (reported) {
  60. // We only report the first error, to avoid clogging
  61. // the screen.
  62. return;
  63. }
  64. reported = true;
  65. String text = "java.util.logging.ErrorManager: " + code;
  66. if (msg != null) {
  67. text = text + ": " + msg;
  68. }
  69. System.err.println(text);
  70. if (ex != null) {
  71. ex.printStackTrace();
  72. }
  73. }
  74. }