Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / java / util / logging / ErrorManager.java
blobbb481802bb63e455eac73c7b980dfc839fedc9ab
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.
6 This file is part of GNU Classpath.
8 GNU Classpath is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 GNU Classpath is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Classpath; see the file COPYING. If not, write to the
20 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 02111-1307 USA.
23 Linking this library statically or dynamically with other modules is
24 making a combined work based on this library. Thus, the terms and
25 conditions of the GNU General Public License cover the whole
26 combination.
28 As a special exception, the copyright holders of this library give you
29 permission to link this library with independent modules to produce an
30 executable, regardless of the license terms of these independent
31 modules, and to copy and distribute the resulting executable under
32 terms of your choice, provided that you also meet, for each linked
33 independent module, the terms and conditions of the license of that
34 module. An independent module is a module which is not derived from
35 or based on this library. If you modify this library, you may extend
36 this exception to your version of the library, but you are not
37 obligated to do so. If you do not wish to do so, delete this
38 exception statement from your version. */
41 package java.util.logging;
43 /**
44 * An <code>ErrorManager</code> deals with errors that a <code>Handler</code>
45 * encounters while logging.
47 * @see Handler#setErrorManager(ErrorManager)
49 * @author Sascha Brawer (brawer@acm.org)
51 public class ErrorManager
53 /* The values have been taken from Sun's public J2SE 1.4 API
54 * documentation.
55 * See http://java.sun.com/j2se/1.4/docs/api/constant-values.html
58 /**
59 * Indicates that there was a failure that does not readily
60 * fall into any of the other categories.
62 public static final int GENERIC_FAILURE = 0;
65 /**
66 * Indicates that there was a problem upon writing to
67 * an output stream.
69 public static final int WRITE_FAILURE = 1;
72 /**
73 * Indicates that there was a problem upon flushing
74 * an output stream.
76 public static final int FLUSH_FAILURE = 2;
79 /**
80 * Indicates that there was a problem upon closing
81 * an output stream.
83 public static final int CLOSE_FAILURE = 3;
86 /**
87 * Indicates that there was a problem upon opening
88 * an output stream.
90 public static final int OPEN_FAILURE = 4;
93 /**
94 * Indicates that there was a problem upon formatting
95 * the message of a log record.
97 public static final int FORMAT_FAILURE = 5;
101 * Indicates whether the {@link #error} method of this ErrorManager
102 * has ever been used.
104 * Declared volatile in order to correctly support the
105 * double-checked locking idiom (once the revised Java Memory Model
106 * gets adopted); see Classpath bug #2944.
108 private volatile boolean everUsed = false;
111 public ErrorManager()
117 * Reports an error that occured upon logging. The default implementation
118 * emits the very first error to System.err, ignoring subsequent errors.
120 * @param message a message describing the error, or <code>null</code> if
121 * there is no suitable description.
123 * @param ex an exception, or <code>null</code> if the error is not
124 * related to an exception.
126 * @param errorCode one of the defined error codes, for example
127 * <code>ErrorManager.CLOSE_FAILURE</code>.
129 public void error(String message, Exception ex, int errorCode)
131 if (everUsed)
132 return;
134 synchronized (this)
136 /* The double check is intentional. If the first check was
137 * omitted, the monitor would have to be entered every time
138 * error() method was called. If the second check was
139 * omitted, the code below could be executed by multiple
140 * threads simultaneously.
142 * This is the 'double-checked locking' idiom, which is broken
143 * with the current version of the Java memory model. However,
144 * we assume that JVMs will have adopted a revised version of
145 * the Java Memory Model by the time GNU Classpath gains
146 * widespread acceptance. See Classpath bug #2944.
148 if (everUsed)
149 return;
151 everUsed = true;
154 String codeMsg;
155 switch (errorCode)
157 case GENERIC_FAILURE:
158 codeMsg = "GENERIC_FAILURE";
159 break;
161 case WRITE_FAILURE:
162 codeMsg = "WRITE_FAILURE";
163 break;
165 case FLUSH_FAILURE:
166 codeMsg = "FLUSH_FAILURE";
167 break;
169 case CLOSE_FAILURE:
170 codeMsg = "CLOSE_FAILURE";
171 break;
173 case OPEN_FAILURE:
174 codeMsg = "OPEN_FAILURE";
175 break;
177 case FORMAT_FAILURE:
178 codeMsg = "FORMAT_FAILURE";
179 break;
181 default:
182 codeMsg = String.valueOf(errorCode);
183 break;
186 System.err.println("Error upon logging: " + codeMsg);
187 if ((message != null) && (message.length() > 0))
188 System.err.println(message);
190 if (ex != null)
191 ex.printStackTrace();