2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / java / util / logging / ErrorManager.java
blob7381a5227aca922797740d857903dea7e472116d
1 /* ErrorManager.java
2 -- a class for dealing with errors that a Handler encounters
3 during logging
5 Copyright (C) 2002, 2003 Free Software Foundation, Inc.
7 This file is part of GNU Classpath.
9 GNU Classpath is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
14 GNU Classpath is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GNU Classpath; see the file COPYING. If not, write to the
21 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 02111-1307 USA.
24 Linking this library statically or dynamically with other modules is
25 making a combined work based on this library. Thus, the terms and
26 conditions of the GNU General Public License cover the whole
27 combination.
29 As a special exception, the copyright holders of this library give you
30 permission to link this library with independent modules to produce an
31 executable, regardless of the license terms of these independent
32 modules, and to copy and distribute the resulting executable under
33 terms of your choice, provided that you also meet, for each linked
34 independent module, the terms and conditions of the license of that
35 module. An independent module is a module which is not derived from
36 or based on this library. If you modify this library, you may extend
37 this exception to your version of the library, but you are not
38 obligated to do so. If you do not wish to do so, delete this
39 exception statement from your version.
44 package java.util.logging;
46 /**
47 * An <code>ErrorManager</code> deals with errors that a <code>Handler</code>
48 * encounters while logging.
50 * @see Handler#setErrorManager(ErrorManager)
52 * @author Sascha Brawer (brawer@acm.org)
54 public class ErrorManager
56 /* The values have been taken from Sun's public J2SE 1.4 API
57 * documentation.
58 * See http://java.sun.com/j2se/1.4/docs/api/constant-values.html
61 /**
62 * Indicates that there was a failure that does not readily
63 * fall into any of the other categories.
65 public static final int GENERIC_FAILURE = 0;
68 /**
69 * Indicates that there was a problem upon writing to
70 * an output stream.
72 public static final int WRITE_FAILURE = 1;
75 /**
76 * Indicates that there was a problem upon flushing
77 * an output stream.
79 public static final int FLUSH_FAILURE = 2;
82 /**
83 * Indicates that there was a problem upon closing
84 * an output stream.
86 public static final int CLOSE_FAILURE = 3;
89 /**
90 * Indicates that there was a problem upon opening
91 * an output stream.
93 public static final int OPEN_FAILURE = 4;
96 /**
97 * Indicates that there was a problem upon formatting
98 * the message of a log record.
100 public static final int FORMAT_FAILURE = 5;
104 * Indicates whether the {@link #error} method of this ErrorManager
105 * has ever been used.
107 * Declared volatile in order to correctly support the
108 * double-checked locking idiom (once the revised Java Memory Model
109 * gets adopted); see Classpath bug #2944.
111 private volatile boolean everUsed = false;
114 public ErrorManager()
120 * Reports an error that occured upon logging. The default implementation
121 * emits the very first error to System.err, ignoring subsequent errors.
123 * @param message a message describing the error, or <code>null</code> if
124 * there is no suitable description.
126 * @param ex an exception, or <code>null</code> if the error is not
127 * related to an exception.
129 * @param errorCode one of the defined error codes, for example
130 * <code>ErrorManager.CLOSE_FAILURE</code>.
132 public void error(String message, Exception ex, int errorCode)
134 if (everUsed)
135 return;
137 synchronized (this)
139 /* The double check is intentional. If the first check was
140 * omitted, the monitor would have to be entered every time
141 * error() method was called. If the second check was
142 * omitted, the code below could be executed by multiple
143 * threads simultaneously.
145 * This is the 'double-checked locking' idiom, which is broken
146 * with the current version of the Java memory model. However,
147 * we assume that JVMs will have adopted a revised version of
148 * the Java Memory Model by the time GNU Classpath gains
149 * widespread acceptance. See Classpath bug #2944.
151 if (everUsed)
152 return;
154 everUsed = true;
157 String codeMsg;
158 switch (errorCode)
160 case GENERIC_FAILURE:
161 codeMsg = "GENERIC_FAILURE";
162 break;
164 case WRITE_FAILURE:
165 codeMsg = "WRITE_FAILURE";
166 break;
168 case FLUSH_FAILURE:
169 codeMsg = "FLUSH_FAILURE";
170 break;
172 case CLOSE_FAILURE:
173 codeMsg = "CLOSE_FAILURE";
174 break;
176 case OPEN_FAILURE:
177 codeMsg = "OPEN_FAILURE";
178 break;
180 case FORMAT_FAILURE:
181 codeMsg = "FORMAT_FAILURE";
182 break;
184 default:
185 codeMsg = String.valueOf(errorCode);
186 break;
189 System.err.println("Error upon logging: " + codeMsg);
190 if ((message != null) && (message.length() > 0))
191 System.err.println(message);
193 if (ex != null)
194 ex.printStackTrace();