ErrorManager.java revision 2ad60cfc28e14ee8f0bb038720836a4696c478ad
1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package java.util.logging; 19 20import org.apache.harmony.logging.internal.nls.Messages; 21 22/** 23 * <p> 24 * An error reporting facility for {@link Handler} implementations to record any 25 * error that may happen during logging. <code>Handlers</code> should report 26 * errors to an <code>ErrorManager</code>, instead of throwing exceptions, 27 * which would interfere with the log issuer's execution. 28 * </p> 29 */ 30public class ErrorManager { 31 32 /** 33 * The error code indicating a failure that does not fit in any of the 34 * specific types of failures that follow. 35 */ 36 public static final int GENERIC_FAILURE = 0; 37 38 /** 39 * The error code indicating a failure when writing to an output stream. 40 */ 41 public static final int WRITE_FAILURE = 1; 42 43 /** 44 * The error code indicating a failure when flushing an output stream. 45 */ 46 public static final int FLUSH_FAILURE = 2; 47 48 /** 49 * The error code indicating a failure when closing an output stream. 50 */ 51 public static final int CLOSE_FAILURE = 3; 52 53 /** 54 * The error code indicating a failure when opening an output stream. 55 */ 56 public static final int OPEN_FAILURE = 4; 57 58 /** 59 * The error code indicating a failure when formatting the error messages. 60 */ 61 public static final int FORMAT_FAILURE = 5; 62 63 @SuppressWarnings("nls") 64 private static final String[] FAILURES = new String[] { "GENERIC_FAILURE", 65 "WRITE_FAILURE", "FLUSH_FAILURE", "CLOSE_FAILURE", "OPEN_FAILURE", 66 "FORMAT_FAILURE" }; 67 68 /** 69 * An indicator for determining if the error manager has been called at 70 * least once before. 71 */ 72 private boolean called; 73 74 /** 75 * Constructs an instance of <code>ErrorManager</code>. 76 */ 77 public ErrorManager() { 78 super(); 79 } 80 81 /** 82 * <p> 83 * Reports an error using the given message, exception and error code. This 84 * implementation will write out the message to {@link System#err} on the 85 * first call and all subsequent calls are ignored. A subclass of this class 86 * should override this method. 87 * </p> 88 * 89 * @param message 90 * The error message, which may be <code>null</code>. 91 * @param exception 92 * The exception associated with the error, which may be 93 * <code>null</code>. 94 * @param errorCode 95 * The error code that identifies the type of error; see the 96 * constant fields on this class. 97 */ 98 public void error(String message, Exception exception, int errorCode) { 99 synchronized (this) { 100 if (called) { 101 return; 102 } 103 called = true; 104 } 105 System.err.println(this.getClass().getName() 106 + ": " + FAILURES[errorCode]); //$NON-NLS-1$ 107 if (message != null) { 108 // logging.1E=Error message - {0} 109 System.err.println(Messages.getString("logging.1E", message)); //$NON-NLS-1$ 110 } 111 if (exception != null) { 112 // logging.1F=Exception - {0} 113 System.err.println(Messages.getString("logging.1F", exception)); //$NON-NLS-1$ 114 } 115 } 116} 117