--- projects/cms/source/util/uk/org/iscream/cms/util/FormatName.java 2000/12/08 11:40:08 1.2 +++ projects/cms/source/util/uk/org/iscream/cms/util/FormatName.java 2001/02/26 00:46:17 1.7 @@ -2,6 +2,9 @@ package uk.ac.ukc.iscream.util; //---IMPORTS--- +import java.util.Date; +import java.text.DateFormat; +import java.util.Locale; /** * This class provides static methods to format the @@ -9,13 +12,19 @@ package uk.ac.ukc.iscream.util; * the various objects within the system to create * a toString String to send to the logger. * - * @author $Author: ajm $ - * @version $Id: FormatName.java,v 1.2 2000/12/08 11:40:08 ajm Exp $ + * @author $Author: tdb $ + * @version $Id: FormatName.java,v 1.7 2001/02/26 00:46:17 tdb Exp $ */ public class FormatName { //---FINAL ATTRIBUTES--- + /** + * An array of names of verbosity levels. + * Thus logging messages are now "classed" by the level + */ + private final static String[] VERBOSITY_NAMES = {"FATAL ", "ERROR ", "WARNING", "SYSMSG ", "SYSINIT", "DEBUG "}; + //---STATIC METHODS--- /** @@ -31,10 +40,48 @@ public class FormatName { * @return an iscream standard name to be used as a toString() */ public static String getName(String friendlyName, String className, String revision) { - if (friendlyName == null) { - return "{"+ className + "}(" + revision.substring(11, revision.length() - 2) + ")"; + if (friendlyName == null && className == null) { + return "{static(v" + revision.substring(11, revision.length() - 2) + ")}"; + } else if (friendlyName == null) { + return "{"+ tidyClassName(className) + "(v" + revision.substring(11, revision.length() - 2) + ")}"; + } else if (className == null) { + return friendlyName + "{static(v" + revision.substring(11, revision.length() - 2) + ")}"; + } else { + return friendlyName + "{"+ tidyClassName(className) + "(v" + revision.substring(11, revision.length() - 2) + ")}"; } - return friendlyName + "{"+ className + "}(" + revision.substring(11, revision.length() - 2) + ")"; + } + + /** + * This method generates a nicely formatted line for the log, + * including the date/time and the source of the message. The date + * and time are formatted using the DateFormat class, and the source + * class is formatted using the toString() method found in every + * source file. This is then prepended to the message and returned. + * + * @param source A string representation of the calling object. + * @param verbosity The verbosity of the message. + * @param message The message to be logged. + * @return The string to be written to the log. + */ + public static String formatLogLine(String source, int verbosity, String message){ + String date = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM, Locale.UK).format(new Date()); + return "[" + date + "] [" + VERBOSITY_NAMES[verbosity] + "] " + source + ": " + message; + } + + /** + * If the class name begins with uk.ac.ukc.iscream + * this method will trim it off, otherwise it + * leaves the string unchanged. + * + * @param className the name of a class + * + * @return the tidy version of it + */ + private static String tidyClassName(String className) { + if (className.startsWith("uk.ac.ukc.iscream")) { + return className.substring(18); + } + return className; } //---CONSTRUCTORS---