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 20/** 21 * An error reporting facility for {@link Handler} implementations to record any 22 * error that may happen during logging. {@code Handlers} should report errors 23 * to an {@code ErrorManager}, instead of throwing exceptions, which would 24 * interfere with the log issuer's execution. 25 */ 26public class ErrorManager { 27 28 /** 29 * The error code indicating a failure that does not fit in any of the 30 * specific types of failures that follow. 31 */ 32 public static final int GENERIC_FAILURE = 0; 33 34 /** 35 * The error code indicating a failure when writing to an output stream. 36 */ 37 public static final int WRITE_FAILURE = 1; 38 39 /** 40 * The error code indicating a failure when flushing an output stream. 41 */ 42 public static final int FLUSH_FAILURE = 2; 43 44 /** 45 * The error code indicating a failure when closing an output stream. 46 */ 47 public static final int CLOSE_FAILURE = 3; 48 49 /** 50 * The error code indicating a failure when opening an output stream. 51 */ 52 public static final int OPEN_FAILURE = 4; 53 54 /** 55 * The error code indicating a failure when formatting the error messages. 56 */ 57 public static final int FORMAT_FAILURE = 5; 58 59 private static final String[] FAILURES = new String[] { "GENERIC_FAILURE", 60 "WRITE_FAILURE", "FLUSH_FAILURE", "CLOSE_FAILURE", "OPEN_FAILURE", 61 "FORMAT_FAILURE" }; 62 63 /** 64 * An indicator for determining if the error manager has been called at 65 * least once before. 66 */ 67 private boolean called; 68 69 /** 70 * Constructs an instance of {@code ErrorManager}. 71 */ 72 public ErrorManager() { 73 } 74 75 /** 76 * Reports an error using the given message, exception and error code. This 77 * implementation will write out the message to {@link System#err} on the 78 * first call and all subsequent calls are ignored. A subclass of this class 79 * should override this method. 80 * 81 * @param message 82 * the error message, which may be {@code null}. 83 * @param exception 84 * the exception associated with the error, which may be 85 * {@code null}. 86 * @param errorCode 87 * the error code that identifies the type of error; see the 88 * constant fields of this class for possible values. 89 */ 90 public void error(String message, Exception exception, int errorCode) { 91 synchronized (this) { 92 if (called) { 93 return; 94 } 95 called = true; 96 } 97 System.err.println(this.getClass().getName() + ": " + FAILURES[errorCode]); 98 if (message != null) { 99 System.err.println("Error message - " + message); 100 } 101 if (exception != null) { 102 System.err.println("Exception - " + exception); 103 } 104 } 105} 106