Merge from the pain train
[official-gcc.git] / libjava / java / util / logging / Level.java
blob5c3c22bf90664b39e810fcb860a48ded310c1bdb
1 /* Level.java -- a class for indicating logging levels
2 Copyright (C) 2002, 2005 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. */
39 package java.util.logging;
41 import java.io.Serializable;
42 import java.util.ResourceBundle;
44 /**
45 * A class for indicating logging levels. A number of commonly used
46 * levels is pre-defined (such as <code>java.util.logging.Level.INFO</code>),
47 * and applications should utilize those whenever possible. For specialized
48 * purposes, however, applications can sub-class Level in order to define
49 * custom logging levels.
51 * @author Sascha Brawer (brawer@acm.org)
53 public class Level implements Serializable
55 /* The integer values are the same as in the Sun J2SE 1.4.
56 * They have been obtained with a test program. In J2SE 1.4.1,
57 * Sun has amended the API documentation; these values are now
58 * publicly documented.
61 /**
62 * The <code>OFF</code> level is used as a threshold for filtering
63 * log records, meaning that no message should be logged.
65 * @see Logger#setLevel(java.util.logging.Level)
67 public static final Level OFF = new Level ("OFF", Integer.MAX_VALUE);
69 /**
70 * Log records whose level is <code>SEVERE</code> indicate a serious
71 * failure that prevents normal program execution. Messages at this
72 * level should be understandable to an inexperienced, non-technical
73 * end user. Ideally, they explain in simple words what actions the
74 * user can take in order to resolve the problem.
76 public static final Level SEVERE = new Level ("SEVERE", 1000);
79 /**
80 * Log records whose level is <code>WARNING</code> indicate a
81 * potential problem that does not prevent normal program execution.
82 * Messages at this level should be understandable to an
83 * inexperienced, non-technical end user. Ideally, they explain in
84 * simple words what actions the user can take in order to resolve
85 * the problem.
87 public static final Level WARNING = new Level ("WARNING", 900);
90 /**
91 * Log records whose level is <code>INFO</code> are used in purely
92 * informational situations that do not constitute serious errors or
93 * potential problems. In the default logging configuration, INFO
94 * messages will be written to the system console. For this reason,
95 * the INFO level should be used only for messages that are
96 * important to end users and system administrators. Messages at
97 * this level should be understandable to an inexperienced,
98 * non-technical user.
100 public static final Level INFO = new Level ("INFO", 800);
104 * Log records whose level is <code>CONFIG</code> are used for
105 * describing the static configuration, for example the windowing
106 * environment, the operating system version, etc.
108 public static final Level CONFIG = new Level ("CONFIG", 700);
112 * Log records whose level is <code>FINE</code> are typically used
113 * for messages that are relevant for developers using
114 * the component generating log messages. Examples include minor,
115 * recoverable failures, or possible inefficiencies.
117 public static final Level FINE = new Level ("FINE", 500);
121 * Log records whose level is <code>FINER</code> are intended for
122 * rather detailed tracing, for example entering a method, returning
123 * from a method, or throwing an exception.
125 public static final Level FINER = new Level ("FINER", 400);
129 * Log records whose level is <code>FINEST</code> are used for
130 * highly detailed tracing, for example to indicate that a certain
131 * point inside the body of a method has been reached.
133 public static final Level FINEST = new Level ("FINEST", 300);
137 * The <code>ALL</code> level is used as a threshold for filtering
138 * log records, meaning that every message should be logged.
140 * @see Logger#setLevel(java.util.logging.Level)
142 public static final Level ALL = new Level ("ALL", Integer.MIN_VALUE);
145 private static final Level[] knownLevels = {
146 ALL, FINEST, FINER, FINE, CONFIG, INFO, WARNING, SEVERE, OFF
151 * The name of the Level without localizing it, for example
152 * "WARNING".
154 private String name;
158 * The integer value of this <code>Level</code>.
160 private int value;
164 * The name of the resource bundle used for localizing the level
165 * name, or <code>null</code> if the name does not undergo
166 * localization.
168 private String resourceBundleName;
172 * Creates a logging level given a name and an integer value.
173 * It rarely is necessary to create custom levels,
174 * as most applications should be well served with one of the
175 * standard levels such as <code>Level.CONFIG</code>,
176 * <code>Level.INFO</code>, or <code>Level.FINE</code>.
178 * @param name the name of the level.
180 * @param value the integer value of the level. Please note
181 * that the Java<small><sup>TM</sup></small>
182 * Logging API does not specify integer
183 * values for standard levels (such as
184 * Level.FINE). Therefore, a custom
185 * level should pass an integer value that
186 * is calculated at run-time, e.g.
187 * <code>(Level.FINE.intValue() + Level.CONFIG.intValue())
188 * / 2</code> for a level between FINE and CONFIG.
190 protected Level(String name, int value)
192 this(name, value, null);
197 * Create a logging level given a name, an integer value and a name
198 * of a resource bundle for localizing the level name. It rarely
199 * is necessary to create custom levels, as most applications
200 * should be well served with one of the standard levels such as
201 * <code>Level.CONFIG</code>, <code>Level.INFO</code>, or
202 * <code>Level.FINE</code>.
204 * @param name the name of the level.
206 * @param value the integer value of the level. Please note
207 * that the Java<small><sup>TM</sup></small>
208 * Logging API does not specify integer
209 * values for standard levels (such as
210 * Level.FINE). Therefore, a custom
211 * level should pass an integer value that
212 * is calculated at run-time, e.g.
213 * <code>(Level.FINE.intValue() + Level.CONFIG.intValue())
214 * / 2</code> for a level between FINE and CONFIG.
216 * @param resourceBundleName the name of a resource bundle
217 * for localizing the level name, or <code>null</code>
218 * if the name does not need to be localized.
220 protected Level(String name, int value, String resourceBundleName)
222 this.name = name;
223 this.value = value;
224 this.resourceBundleName = resourceBundleName;
228 static final long serialVersionUID = -8176160795706313070L;
232 * Checks whether the Level has the same intValue as one of the
233 * pre-defined levels. If so, the pre-defined level object is
234 * returned.
236 * <br/>Since the resource bundle name is not taken into
237 * consideration, it is possible to resolve Level objects that have
238 * been de-serialized by another implementation, even if the other
239 * implementation uses a different resource bundle for localizing
240 * the names of pre-defined levels.
242 private Object readResolve()
244 for (int i = 0; i < knownLevels.length; i++)
245 if (value == knownLevels[i].intValue())
246 return knownLevels[i];
248 return this;
253 * Returns the name of the resource bundle used for localizing the
254 * level name.
256 * @return the name of the resource bundle used for localizing the
257 * level name, or <code>null</code> if the name does not undergo
258 * localization.
260 public String getResourceBundleName()
262 return resourceBundleName;
267 * Returns the name of the Level without localizing it, for example
268 * "WARNING".
270 public String getName()
272 return name;
277 * Returns the name of the Level after localizing it, for example
278 * "WARNUNG".
280 public String getLocalizedName()
282 String localizedName = null;
284 if (resourceBundleName != null)
288 ResourceBundle b = ResourceBundle.getBundle(resourceBundleName);
289 localizedName = b.getString(name);
291 catch (Exception _)
296 if (localizedName != null)
297 return localizedName;
298 else
299 return name;
304 * Returns the name of the Level without localizing it, for example
305 * "WARNING".
307 public final String toString()
309 return getName();
314 * Returns the integer value of the Level.
316 public final int intValue()
318 return value;
323 * Returns one of the standard Levels given either its name or its
324 * integer value. Custom subclasses of Level will not be returned
325 * by this method.
327 * @throws IllegalArgumentException if <code>name</code> is neither
328 * the name nor the integer value of one of the pre-defined standard
329 * logging levels.
331 * @throws NullPointerException if <code>name</code> is null.
334 public static Level parse(String name)
335 throws IllegalArgumentException
337 /* This will throw a NullPointerException if name is null,
338 * as required by the API specification.
340 name = name.intern();
342 for (int i = 0; i < knownLevels.length; i++)
344 if (name == knownLevels[i].name)
345 return knownLevels[i];
350 int num = Integer.parseInt(name);
351 for (int i = 0; i < knownLevels.length; i++)
352 if (num == knownLevels[i].value)
353 return knownLevels[i];
355 catch (NumberFormatException _)
359 String msg = "Not the name of a standard logging level: \"" + name + "\"";
360 throw new IllegalArgumentException(msg);
365 * Checks whether this Level's integer value is equal to that of
366 * another object.
368 * @return <code>true</code> if <code>other</code> is an instance of
369 * <code>java.util.logging.Level</code> and has the same integer
370 * value, <code>false</code> otherwise.
372 public boolean equals(Object other)
374 if (!(other instanceof Level))
375 return false;
377 return value == ((Level) other).value;
382 * Returns a hash code for this Level which is based on its numeric
383 * value.
385 public int hashCode()
387 return value;
392 * Determines whether or not this Level is one of the standard
393 * levels specified in the Logging API.
395 * <p>This method is package-private because it is not part
396 * of the logging API specification. However, an XMLFormatter
397 * is supposed to emit the numeric value for a custom log
398 * level, but the name for a pre-defined level. It seems
399 * cleaner to put this method to Level than to write some
400 * procedural code for XMLFormatter.
402 * @return <code>true</code> if this Level is a standard level,
403 * <code>false</code> otherwise.
405 final boolean isStandardLevel()
407 for (int i = 0; i < knownLevels.length; i++)
408 if (knownLevels[i] == this)
409 return true;
411 return false;