--- 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 2002/05/21 16:47:19 1.14 @@ -1,7 +1,30 @@ +/* + * i-scream central monitoring system + * http://www.i-scream.org.uk + * Copyright (C) 2000-2002 i-scream + * + * This program 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 + * of the License, or (at your option) any later version. + * + * This program 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 this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + //---PACKAGE DECLARATION--- -package uk.ac.ukc.iscream.util; +package uk.org.iscream.cms.server.util; //---IMPORTS--- +import java.util.Date; +import java.text.DateFormat; +import java.util.Locale; /** * This class provides static methods to format the @@ -9,13 +32,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.14 2002/05/21 16:47:19 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 +60,49 @@ 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).format(new Date()); + return "[" + date + "] [" + VERBOSITY_NAMES[verbosity] + "] " + source + ": " + message; + } + + /** + * If the class name begins with uk.org.iscream.cms.server + * 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) { + String prefix = "uk.org.iscream.cms.server"; + if (className.startsWith(prefix)) { + return className.substring(prefix.length()+1); + } + return className; } //---CONSTRUCTORS---