Path: blob/master/src/java.logging/share/classes/java/util/logging/ErrorManager.java
41159 views
/*1* Copyright (c) 2001, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/242526package java.util.logging;2728/**29* ErrorManager objects can be attached to Handlers to process30* any error that occurs on a Handler during Logging.31* <p>32* When processing logging output, if a Handler encounters problems33* then rather than throwing an Exception back to the issuer of34* the logging call (who is unlikely to be interested) the Handler35* should call its associated ErrorManager.36*/3738public class ErrorManager {39private boolean reported = false;4041/**42* Create an {@code ErrorManager}.43*/44public ErrorManager() {}4546/*47* We declare standard error codes for important categories of errors.48*/4950/**51* GENERIC_FAILURE is used for failure that don't fit52* into one of the other categories.53*/54public static final int GENERIC_FAILURE = 0;55/**56* WRITE_FAILURE is used when a write to an output stream fails.57*/58public static final int WRITE_FAILURE = 1;59/**60* FLUSH_FAILURE is used when a flush to an output stream fails.61*/62public static final int FLUSH_FAILURE = 2;63/**64* CLOSE_FAILURE is used when a close of an output stream fails.65*/66public static final int CLOSE_FAILURE = 3;67/**68* OPEN_FAILURE is used when an open of an output stream fails.69*/70public static final int OPEN_FAILURE = 4;71/**72* FORMAT_FAILURE is used when formatting fails for any reason.73*/74public static final int FORMAT_FAILURE = 5;7576/**77* The error method is called when a Handler failure occurs.78* <p>79* This method may be overridden in subclasses. The default80* behavior in this base class is that the first call is81* reported to System.err, and subsequent calls are ignored.82*83* @param msg a descriptive string (may be null)84* @param ex an exception (may be null)85* @param code an error code defined in ErrorManager86*/87public synchronized void error(String msg, Exception ex, int code) {88if (reported) {89// We only report the first error, to avoid clogging90// the screen.91return;92}93reported = true;94String text = "java.util.logging.ErrorManager: " + code;95if (msg != null) {96text = text + ": " + msg;97}98System.err.println(text);99if (ex != null) {100ex.printStackTrace();101}102}103}104105106