123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673 |
- /* LogRecord.java --
- A class for the state associated with individual logging events
- Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
- This file is part of GNU Classpath.
- GNU Classpath is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2, or (at your option)
- any later version.
- GNU Classpath is distributed in the hope that it will be useful, but
- WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with GNU Classpath; see the file COPYING. If not, write to the
- Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
- 02110-1301 USA.
- Linking this library statically or dynamically with other modules is
- making a combined work based on this library. Thus, the terms and
- conditions of the GNU General Public License cover the whole
- combination.
- As a special exception, the copyright holders of this library give you
- permission to link this library with independent modules to produce an
- executable, regardless of the license terms of these independent
- modules, and to copy and distribute the resulting executable under
- terms of your choice, provided that you also meet, for each linked
- independent module, the terms and conditions of the license of that
- module. An independent module is a module which is not derived from
- or based on this library. If you modify this library, you may extend
- this exception to your version of the library, but you are not
- obligated to do so. If you do not wish to do so, delete this
- exception statement from your version. */
- package java.util.logging;
- import java.util.ResourceBundle;
- /**
- * A <code>LogRecord</code> contains the state for an individual
- * event to be logged.
- *
- * <p>As soon as a LogRecord instance has been handed over to the
- * logging framework, applications should not manipulate it anymore.
- *
- * @author Sascha Brawer (brawer@acm.org)
- */
- public class LogRecord
- implements java.io.Serializable
- {
- /**
- * The severity level of this <code>LogRecord</code>.
- */
- private Level level;
- /**
- * The sequence number of this <code>LogRecord</code>.
- */
- private long sequenceNumber;
- /**
- * The name of the class that issued the logging request, or
- * <code>null</code> if this information could not be obtained.
- */
- private String sourceClassName;
- /**
- * The name of the method that issued the logging request, or
- * <code>null</code> if this information could not be obtained.
- */
- private String sourceMethodName;
- /**
- * The message for this <code>LogRecord</code> before
- * any localization or formatting.
- */
- private String message;
- /**
- * An identifier for the thread in which this <code>LogRecord</code>
- * was created. The identifier is not necessarily related to any
- * thread identifiers used by the operating system.
- */
- private int threadID;
- /**
- * The time when this <code>LogRecord</code> was created,
- * in milliseconds since the beginning of January 1, 1970.
- */
- private long millis;
- /**
- * The Throwable associated with this <code>LogRecord</code>, or
- * <code>null</code> if the logged event is not related to an
- * exception or error.
- */
- private Throwable thrown;
- /**
- * The name of the logger where this <code>LogRecord</code> has
- * originated, or <code>null</code> if this <code>LogRecord</code>
- * does not originate from a <code>Logger</code>.
- */
- private String loggerName;
- /**
- * The name of the resource bundle used for localizing log messages,
- * or <code>null</code> if no bundle has been specified.
- */
- private String resourceBundleName;
- private transient Object[] parameters;
- private transient ResourceBundle bundle;
- /**
- * Constructs a <code>LogRecord</code> given a severity level and
- * an unlocalized message text. In addition, the sequence number,
- * creation time (as returned by <code>getMillis()</code>) and
- * thread ID are assigned. All other properties are set to
- * <code>null</code>.
- *
- * @param level the severity level, for example <code>Level.WARNING</code>.
- *
- * @param message the message text (which will be used as key
- * for looking up the localized message text
- * if a resource bundle has been associated).
- */
- public LogRecord(Level level, String message)
- {
- this.level = level;
- this.message = message;
- this.millis = System.currentTimeMillis();
- /* A subclass of java.lang.Thread could override hashCode(),
- * in which case the result would not be guaranteed anymore
- * to be unique among all threads. While System.identityHashCode
- * is not necessarily unique either, it at least cannot be
- * overridden by user code. However, is might be a good idea
- * to use something better for generating thread IDs.
- */
- this.threadID = System.identityHashCode(Thread.currentThread());
- sequenceNumber = allocateSeqNum();
- }
- /**
- * Determined with the serialver tool of the Sun J2SE 1.4.
- */
- static final long serialVersionUID = 5372048053134512534L;
- private void readObject(java.io.ObjectInputStream in)
- throws java.io.IOException, java.lang.ClassNotFoundException
- {
- in.defaultReadObject();
- /* We assume that future versions will be downwards compatible,
- * so we can ignore the versions.
- */
- byte majorVersion = in.readByte();
- byte minorVersion = in.readByte();
- int numParams = in.readInt();
- if (numParams >= 0)
- {
- parameters = new Object[numParams];
- for (int i = 0; i < numParams; i++)
- parameters[i] = in.readObject();
- }
- }
- /**
- * @serialData The default fields, followed by a major byte version
- * number, followed by a minor byte version number, followed by
- * information about the log record parameters. If
- * <code>parameters</code> is <code>null</code>, the integer -1 is
- * written, otherwise the length of the <code>parameters</code>
- * array (which can be zero), followed by the result of calling
- * {@link Object#toString() toString()} on the parameter (or
- * <code>null</code> if the parameter is <code>null</code>).
- *
- * <p><strong>Specification Note:</strong> The Javadoc for the
- * Sun reference implementation does not specify the version
- * number. FIXME: Reverse-engineer the JDK and file a bug
- * report with Sun, asking for amendment of the specification.
- */
- private void writeObject(java.io.ObjectOutputStream out)
- throws java.io.IOException
- {
- out.defaultWriteObject();
- /* Major, minor version number: The Javadoc for J2SE1.4 does not
- * specify the values.
- */
- out.writeByte(0);
- out.writeByte(0);
- if (parameters == null)
- out.writeInt(-1);
- else
- {
- out.writeInt(parameters.length);
- for (int i = 0; i < parameters.length; i++)
- {
- if (parameters[i] == null)
- out.writeObject(null);
- else
- out.writeObject(parameters[i].toString());
- }
- }
- }
- /**
- * Returns the name of the logger where this <code>LogRecord</code>
- * has originated.
- *
- * @return the name of the source {@link Logger}, or
- * <code>null</code> if this <code>LogRecord</code>
- * does not originate from a <code>Logger</code>.
- */
- public String getLoggerName()
- {
- return loggerName;
- }
- /**
- * Sets the name of the logger where this <code>LogRecord</code>
- * has originated.
- *
- * <p>As soon as a <code>LogRecord</code> has been handed over
- * to the logging framework, applications should not modify it
- * anymore. Therefore, this method should only be called on
- * freshly constructed LogRecords.
- *
- * @param name the name of the source logger, or <code>null</code> to
- * indicate that this <code>LogRecord</code> does not
- * originate from a <code>Logger</code>.
- */
- public void setLoggerName(String name)
- {
- loggerName = name;
- }
- /**
- * Returns the resource bundle that is used when the message
- * of this <code>LogRecord</code> needs to be localized.
- *
- * @return the resource bundle used for localization,
- * or <code>null</code> if this message does not need
- * to be localized.
- */
- public ResourceBundle getResourceBundle()
- {
- return bundle;
- }
- /**
- * Sets the resource bundle that is used when the message
- * of this <code>LogRecord</code> needs to be localized.
- *
- * <p>As soon as a <code>LogRecord</code> has been handed over
- * to the logging framework, applications should not modify it
- * anymore. Therefore, this method should only be called on
- * freshly constructed LogRecords.
- *
- * @param bundle the resource bundle to be used, or
- * <code>null</code> to indicate that this
- * message does not need to be localized.
- */
- public void setResourceBundle(ResourceBundle bundle)
- {
- this.bundle = bundle;
- /* FIXME: Is there a way to infer the name
- * of a resource bundle from a ResourceBundle object?
- */
- this.resourceBundleName = null;
- }
- /**
- * Returns the name of the resource bundle that is used when the
- * message of this <code>LogRecord</code> needs to be localized.
- *
- * @return the name of the resource bundle used for localization,
- * or <code>null</code> if this message does not need
- * to be localized.
- */
- public String getResourceBundleName()
- {
- return resourceBundleName;
- }
- /**
- * Sets the name of the resource bundle that is used when the
- * message of this <code>LogRecord</code> needs to be localized.
- *
- * <p>As soon as a <code>LogRecord</code> has been handed over
- * to the logging framework, applications should not modify it
- * anymore. Therefore, this method should only be called on
- * freshly constructed LogRecords.
- *
- * @param name the name of the resource bundle to be used, or
- * <code>null</code> to indicate that this message
- * does not need to be localized.
- */
- public void setResourceBundleName(String name)
- {
- resourceBundleName = name;
- bundle = null;
- try
- {
- if (resourceBundleName != null)
- bundle = ResourceBundle.getBundle(resourceBundleName);
- }
- catch (java.util.MissingResourceException _)
- {
- }
- }
- /**
- * Returns the level of the LogRecord.
- *
- * <p>Applications should be aware of the possibility that the
- * result is not necessarily one of the standard logging levels,
- * since the logging framework allows to create custom subclasses
- * of <code>java.util.logging.Level</code>. Therefore, filters
- * should perform checks like <code>theRecord.getLevel().intValue()
- * == Level.INFO.intValue()</code> instead of <code>theRecord.getLevel()
- * == Level.INFO</code>.
- */
- public Level getLevel()
- {
- return level;
- }
- /**
- * Sets the severity level of this <code>LogRecord</code> to a new
- * value.
- *
- * <p>As soon as a <code>LogRecord</code> has been handed over
- * to the logging framework, applications should not modify it
- * anymore. Therefore, this method should only be called on
- * freshly constructed LogRecords.
- *
- * @param level the new severity level, for example
- * <code>Level.WARNING</code>.
- */
- public void setLevel(Level level)
- {
- this.level = level;
- }
- /**
- * The last used sequence number for any LogRecord.
- */
- private static long lastSeqNum;
- /**
- * Allocates a sequence number for a new LogRecord. This class
- * method is only called by the LogRecord constructor.
- */
- private static synchronized long allocateSeqNum()
- {
- lastSeqNum += 1;
- return lastSeqNum;
- }
- /**
- * Returns the sequence number of this <code>LogRecord</code>.
- */
- public long getSequenceNumber()
- {
- return sequenceNumber;
- }
- /**
- * Sets the sequence number of this <code>LogRecord</code> to a new
- * value.
- *
- * <p>As soon as a <code>LogRecord</code> has been handed over
- * to the logging framework, applications should not modify it
- * anymore. Therefore, this method should only be called on
- * freshly constructed LogRecords.
- *
- * @param seqNum the new sequence number.
- */
- public void setSequenceNumber(long seqNum)
- {
- this.sequenceNumber = seqNum;
- }
- /**
- * Returns the name of the class where the event being logged
- * has had its origin. This information can be passed as
- * parameter to some logging calls, and in certain cases, the
- * logging framework tries to determine an approximation
- * (which may or may not be accurate).
- *
- * @return the name of the class that issued the logging request,
- * or <code>null</code> if this information could not
- * be obtained.
- */
- public String getSourceClassName()
- {
- if (sourceClassName != null)
- return sourceClassName;
- /* FIXME: Should infer this information from the call stack. */
- return null;
- }
- /**
- * Sets the name of the class where the event being logged
- * has had its origin.
- *
- * <p>As soon as a <code>LogRecord</code> has been handed over
- * to the logging framework, applications should not modify it
- * anymore. Therefore, this method should only be called on
- * freshly constructed LogRecords.
- *
- * @param sourceClassName the name of the class that issued the
- * logging request, or <code>null</code> to indicate that
- * this information could not be obtained.
- */
- public void setSourceClassName(String sourceClassName)
- {
- this.sourceClassName = sourceClassName;
- }
- /**
- * Returns the name of the method where the event being logged
- * has had its origin. This information can be passed as
- * parameter to some logging calls, and in certain cases, the
- * logging framework tries to determine an approximation
- * (which may or may not be accurate).
- *
- * @return the name of the method that issued the logging request,
- * or <code>null</code> if this information could not
- * be obtained.
- */
- public String getSourceMethodName()
- {
- if (sourceMethodName != null)
- return sourceMethodName;
- /* FIXME: Should infer this information from the call stack. */
- return null;
- }
- /**
- * Sets the name of the method where the event being logged
- * has had its origin.
- *
- * <p>As soon as a <code>LogRecord</code> has been handed over
- * to the logging framework, applications should not modify it
- * anymore. Therefore, this method should only be called on
- * freshly constructed LogRecords.
- *
- * @param sourceMethodName the name of the method that issued the
- * logging request, or <code>null</code> to indicate that
- * this information could not be obtained.
- */
- public void setSourceMethodName(String sourceMethodName)
- {
- this.sourceMethodName = sourceMethodName;
- }
- /**
- * Returns the message for this <code>LogRecord</code> before
- * any localization or parameter substitution.
- *
- * <p>A {@link Logger} will try to localize the message
- * if a resource bundle has been associated with this
- * <code>LogRecord</code>. In this case, the logger will call
- * <code>getMessage()</code> and use the result as the key
- * for looking up the localized message in the bundle.
- * If no bundle has been associated, or if the result of
- * <code>getMessage()</code> is not a valid key in the
- * bundle, the logger will use the raw message text as
- * returned by this method.
- *
- * @return the message text, or <code>null</code> if there
- * is no message text.
- */
- public String getMessage()
- {
- return message;
- }
- /**
- * Sets the message for this <code>LogRecord</code>.
- *
- * <p>A <code>Logger</code> will try to localize the message
- * if a resource bundle has been associated with this
- * <code>LogRecord</code>. In this case, the logger will call
- * <code>getMessage()</code> and use the result as the key
- * for looking up the localized message in the bundle.
- * If no bundle has been associated, or if the result of
- * <code>getMessage()</code> is not a valid key in the
- * bundle, the logger will use the raw message text as
- * returned by this method.
- *
- * <p>It is possible to set the message to either an empty String or
- * <code>null</code>, although this does not make the the message
- * very helpful to human users.
- *
- * @param message the message text (which will be used as key
- * for looking up the localized message text
- * if a resource bundle has been associated).
- */
- public void setMessage(String message)
- {
- this.message = message;
- }
- /**
- * Returns the parameters to the log message.
- *
- * @return the parameters to the message, or <code>null</code> if
- * the message has no parameters.
- */
- public Object[] getParameters()
- {
- return parameters;
- }
- /**
- * Sets the parameters to the log message.
- *
- * <p>As soon as a <code>LogRecord</code> has been handed over
- * to the logging framework, applications should not modify it
- * anymore. Therefore, this method should only be called on
- * freshly constructed LogRecords.
- *
- * @param parameters the parameters to the message, or <code>null</code>
- * to indicate that the message has no parameters.
- */
- public void setParameters(Object[] parameters)
- {
- this.parameters = parameters;
- }
- /**
- * Returns an identifier for the thread in which this
- * <code>LogRecord</code> was created. The identifier is not
- * necessarily related to any thread identifiers used by the
- * operating system.
- *
- * @return an identifier for the source thread.
- */
- public int getThreadID()
- {
- return threadID;
- }
- /**
- * Sets the identifier indicating in which thread this
- * <code>LogRecord</code> was created. The identifier is not
- * necessarily related to any thread identifiers used by the
- * operating system.
- *
- * <p>As soon as a <code>LogRecord</code> has been handed over
- * to the logging framework, applications should not modify it
- * anymore. Therefore, this method should only be called on
- * freshly constructed LogRecords.
- *
- * @param threadID the identifier for the source thread.
- */
- public void setThreadID(int threadID)
- {
- this.threadID = threadID;
- }
- /**
- * Returns the time when this <code>LogRecord</code> was created.
- *
- * @return the time of creation in milliseconds since the beginning
- * of January 1, 1970.
- */
- public long getMillis()
- {
- return millis;
- }
- /**
- * Sets the time when this <code>LogRecord</code> was created.
- *
- * <p>As soon as a <code>LogRecord</code> has been handed over
- * to the logging framework, applications should not modify it
- * anymore. Therefore, this method should only be called on
- * freshly constructed LogRecords.
- *
- * @param millis the time of creation in milliseconds since the
- * beginning of January 1, 1970.
- */
- public void setMillis(long millis)
- {
- this.millis = millis;
- }
- /**
- * Returns the Throwable associated with this <code>LogRecord</code>,
- * or <code>null</code> if the logged event is not related to an exception
- * or error.
- */
- public Throwable getThrown()
- {
- return thrown;
- }
- /**
- * Associates this <code>LogRecord</code> with an exception or error.
- *
- * <p>As soon as a <code>LogRecord</code> has been handed over
- * to the logging framework, applications should not modify it
- * anymore. Therefore, this method should only be called on
- * freshly constructed LogRecords.
- *
- * @param thrown the exception or error to associate with, or
- * <code>null</code> if this <code>LogRecord</code>
- * should be made unrelated to an exception or error.
- */
- public void setThrown(Throwable thrown)
- {
- this.thrown = thrown;
- }
- }
|