2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / java / util / logging / Level.java
blobd8987f6b5813fa2203f12fe9158a27b5de020b67
1 /* Level.java -- a class for indicating logging levels
2 Copyright (C) 2002 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version.
41 package java.util.logging;
43 import java.util.ResourceBundle;
46 /**
47 * A class for indicating logging levels. A number of commonly used
48 * levels is pre-defined (such as <code>java.util.logging.Level.INFO</code>),
49 * and applications should utilize those whenever possible. For specialized
50 * purposes, however, applications can sub-class Level in order to define
51 * custom logging levels.
53 * @author Sascha Brawer <brawer@acm.org>
55 public class Level
56 implements java.io.Serializable
58 /* The integer values are the same as in the Sun J2SE 1.4.
59 * They have been obtained with a test program. In J2SE 1.4.1,
60 * Sun has amended the API documentation; these values are now
61 * publicly documented.
64 /**
65 * The <code>OFF</code> level is used as a threshold for filtering
66 * log records, meaning that no message should be logged.
68 * @see Logger#setLevel(java.util.logging.Level)
70 public static final Level OFF = new Level ("OFF", Integer.MAX_VALUE);
72 /**
73 * Log records whose level is <code>SEVERE</code> indicate a serious
74 * failure that prevents normal program execution. Messages at this
75 * level should be understandable to an inexperienced, non-technical
76 * end user. Ideally, they explain in simple words what actions the
77 * user can take in order to resolve the problem.
79 public static final Level SEVERE = new Level ("SEVERE", 1000);
82 /**
83 * Log records whose level is <code>WARNING</code> indicate a
84 * potential problem that does not prevent normal program execution.
85 * Messages at this level should be understandable to an
86 * inexperienced, non-technical end user. Ideally, they explain in
87 * simple words what actions the user can take in order to resolve
88 * the problem.
90 public static final Level WARNING = new Level ("WARNING", 900);
93 /**
94 * Log records whose level is <code>INFO</code> are used in purely
95 * informational situations that do not constitute serious errors or
96 * potential problems. In the default logging configuration, INFO
97 * messages will be written to the system console. For this reason,
98 * the INFO level should be used only for messages that are
99 * important to end users and system administrators. Messages at
100 * this level should be understandable to an inexperienced,
101 * non-technical user.
103 public static final Level INFO = new Level ("INFO", 800);
107 * Log records whose level is <code>CONFIG</code> are used for
108 * describing the static configuration, for example the windowing
109 * environment, the operating system version, etc.
111 public static final Level CONFIG = new Level ("CONFIG", 700);
115 * Log records whose level is <code>FINE</code> are typically used
116 * for messages that are relevant for developers using
117 * the component generating log messages. Examples include minor,
118 * recoverable failures, or possible inefficiencies.
120 public static final Level FINE = new Level ("FINE", 500);
124 * Log records whose level is <code>FINER</code> are intended for
125 * rather detailed tracing, for example entering a method, returning
126 * from a method, or throwing an exception.
128 public static final Level FINER = new Level ("FINER", 400);
132 * Log records whose level is <code>FINEST</code> are used for
133 * highly detailed tracing, for example to indicate that a certain
134 * point inside the body of a method has been reached.
136 public static final Level FINEST = new Level ("FINEST", 300);
140 * The <code>ALL</code> level is used as a threshold for filtering
141 * log records, meaning that every message should be logged.
143 * @see Logger#setLevel(java.util.logging.Level)
145 public static final Level ALL = new Level ("ALL", Integer.MIN_VALUE);
148 private static final Level[] knownLevels = {
149 ALL, FINEST, FINER, FINE, CONFIG, INFO, WARNING, SEVERE, OFF
154 * The name of the Level without localizing it, for example
155 * "WARNING".
157 private String name;
161 * The integer value of this <code>Level</code>.
163 private int value;
167 * The name of the resource bundle used for localizing the level
168 * name, or <code>null</code> if the name does not undergo
169 * localization.
171 private String resourceBundleName;
175 * Creates a logging level given a name and an integer value.
176 * It rarely is necessary to create custom levels,
177 * as most applications should be well served with one of the
178 * standard levels such as <code>Level.CONFIG</code>,
179 * <code>Level.INFO</code>, or <code>Level.FINE</code>.
181 * @param name the name of the level.
183 * @param value the integer value of the level. Please note
184 * that the Java<small><sup>TM</sup></small>
185 * Logging API does not specify integer
186 * values for standard levels (such as
187 * Level.FINE). Therefore, a custom
188 * level should pass an integer value that
189 * is calculated at run-time, e.g.
190 * <code>(Level.FINE.intValue() + Level.CONFIG.intValue())
191 * / 2</code> for a level between FINE and CONFIG.
193 protected Level(String name, int value)
195 this(name, value, null);
200 * Create a logging level given a name, an integer value and a name
201 * of a resource bundle for localizing the level name. It rarely
202 * is necessary to create custom levels, as most applications
203 * should be well served with one of the standard levels such as
204 * <code>Level.CONFIG</code>, <code>Level.INFO</code>, or
205 * <code>Level.FINE</code>.
207 * @param name the name of the level.
209 * @param value the integer value of the level. Please note
210 * that the Java<small><sup>TM</sup></small>
211 * Logging API does not specify integer
212 * values for standard levels (such as
213 * Level.FINE). Therefore, a custom
214 * level should pass an integer value that
215 * is calculated at run-time, e.g.
216 * <code>(Level.FINE.intValue() + Level.CONFIG.intValue())
217 * / 2</code> for a level between FINE and CONFIG.
219 * @param resourceBundleName the name of a resource bundle
220 * for localizing the level name, or <code>null</code>
221 * if the name does not need to be localized.
223 protected Level(String name, int value, String resourceBundleName)
225 this.name = name;
226 this.value = value;
227 this.resourceBundleName = resourceBundleName;
231 static final long serialVersionUID = -8176160795706313070L;
235 * Checks whether the Level has the same intValue as one of the
236 * pre-defined levels. If so, the pre-defined level object is
237 * returned.
239 * <br/>Since the resource bundle name is not taken into
240 * consideration, it is possible to resolve Level objects that have
241 * been de-serialized by another implementation, even if the other
242 * implementation uses a different resource bundle for localizing
243 * the names of pre-defined levels.
245 private Object readResolve()
247 for (int i = 0; i < knownLevels.length; i++)
248 if (value == knownLevels[i].intValue())
249 return knownLevels[i];
251 return this;
256 * Returns the name of the resource bundle used for localizing the
257 * level name.
259 * @return the name of the resource bundle used for localizing the
260 * level name, or <code>null</code> if the name does not undergo
261 * localization.
263 public String getResourceBundleName()
265 return resourceBundleName;
270 * Returns the name of the Level without localizing it, for example
271 * "WARNING".
273 public String getName()
275 return name;
280 * Returns the name of the Level after localizing it, for example
281 * "WARNUNG".
283 public String getLocalizedName()
285 String localizedName = null;
287 if (resourceBundleName != null)
291 ResourceBundle b = ResourceBundle.getBundle(resourceBundleName);
292 localizedName = b.getString(name);
294 catch (Exception _)
299 if (localizedName != null)
300 return localizedName;
301 else
302 return name;
307 * Returns the name of the Level without localizing it, for example
308 * "WARNING".
310 public final String toString()
312 return getName();
317 * Returns the integer value of the Level.
319 public final int intValue()
321 return value;
326 * Returns one of the standard Levels given either its name or its
327 * integer value. Custom subclasses of Level will not be returned
328 * by this method.
330 * @throws IllegalArgumentException if <code>name</code> is neither
331 * the name nor the integer value of one of the pre-defined standard
332 * logging levels.
334 * @throws NullPointerException if <code>name</code> is null.
337 public static Level parse(String name)
338 throws IllegalArgumentException
340 /* This will throw a NullPointerException if name is null,
341 * as required by the API specification.
343 name = name.intern();
345 for (int i = 0; i < knownLevels.length; i++)
347 if (name == knownLevels[i].name)
348 return knownLevels[i];
353 int num = Integer.parseInt(name);
354 for (int i = 0; i < knownLevels.length; i++)
355 if (num == knownLevels[i].value)
356 return knownLevels[i];
358 catch (NumberFormatException _)
362 String msg = "Not the name of a standard logging level: \"" + name + "\"";
363 throw new IllegalArgumentException(msg);
368 * Checks whether this Level's integer value is equal to that of
369 * another object.
371 * @return <code>true</code> if <code>other</code> is an instance of
372 * <code>java.util.logging.Level</code> and has the same integer
373 * value, <code>false</code> otherwise.
375 public boolean equals(Object other)
377 if (!(other instanceof Level))
378 return false;
380 return value == ((Level) other).value;
385 * Returns a hash code for this Level which is based on its numeric
386 * value.
388 public int hashCode()
390 return value;
395 * Determines whether or not this Level is one of the standard
396 * levels specified in the Logging API.
398 * <p>This method is package-private because it is not part
399 * of the logging API specification. However, an XMLFormatter
400 * is supposed to emit the numeric value for a custom log
401 * level, but the name for a pre-defined level. It seems
402 * cleaner to put this method to Level than to write some
403 * procedural code for XMLFormatter.
405 * @return <code>true</code> if this Level is a standard level,
406 * <code>false</code> otherwise.
408 final boolean isStandardLevel()
410 for (int i = 0; i < knownLevels.length; i++)
411 if (knownLevels[i] == this)
412 return true;
414 return false;