2 |
|
package uk.ac.ukc.iscream.util; |
3 |
|
|
4 |
|
//---IMPORTS--- |
5 |
+ |
import java.util.Date; |
6 |
+ |
import java.text.DateFormat; |
7 |
+ |
import java.util.Locale; |
8 |
|
|
9 |
|
/** |
10 |
|
* This class provides static methods to format the |
19 |
|
|
20 |
|
//---FINAL ATTRIBUTES--- |
21 |
|
|
22 |
+ |
/** |
23 |
+ |
* An array of names of verbosity levels. |
24 |
+ |
* Thus logging messages are now "classed" by the level |
25 |
+ |
*/ |
26 |
+ |
private final static String[] VERBOSITY_NAMES = {"FATAL ", "ERROR ", "WARNING", "SYSMSG ", "SYSINIT", "DEBUG "}; |
27 |
+ |
|
28 |
|
//---STATIC METHODS--- |
29 |
|
|
30 |
|
/** |
40 |
|
* @return an iscream standard name to be used as a toString() |
41 |
|
*/ |
42 |
|
public static String getName(String friendlyName, String className, String revision) { |
43 |
< |
if (friendlyName == null) { |
44 |
< |
return "{"+ className + "}(" + revision.substring(11, revision.length() - 2) + ")"; |
43 |
> |
if (friendlyName == null && className == null) { |
44 |
> |
return "{static(v" + revision.substring(11, revision.length() - 2) + ")}"; |
45 |
> |
} else if (friendlyName == null) { |
46 |
> |
return "{"+ tidyClassName(className) + "(v" + revision.substring(11, revision.length() - 2) + ")}"; |
47 |
> |
} else if (className == null) { |
48 |
> |
return friendlyName + "{static(v" + revision.substring(11, revision.length() - 2) + ")}"; |
49 |
> |
} else { |
50 |
> |
return friendlyName + "{"+ tidyClassName(className) + "(v" + revision.substring(11, revision.length() - 2) + ")}"; |
51 |
|
} |
52 |
< |
return friendlyName + "{"+ className + "}(" + revision.substring(11, revision.length() - 2) + ")"; |
52 |
> |
} |
53 |
> |
|
54 |
> |
/** |
55 |
> |
* This method generates a nicely formatted line for the log, |
56 |
> |
* including the date/time and the source of the message. The date |
57 |
> |
* and time are formatted using the DateFormat class, and the source |
58 |
> |
* class is formatted using the toString() method found in every |
59 |
> |
* source file. This is then prepended to the message and returned. |
60 |
> |
* |
61 |
> |
* @param source A string representation of the calling object. |
62 |
> |
* @param verbosity The verbosity of the message. |
63 |
> |
* @param message The message to be logged. |
64 |
> |
* @return The string to be written to the log. |
65 |
> |
*/ |
66 |
> |
public static String formatLogLine(String source, int verbosity, String message){ |
67 |
> |
String date = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM, Locale.UK).format(new Date()); |
68 |
> |
return "[" + date + "] [" + VERBOSITY_NAMES[verbosity] + "] " + source + ": " + message; |
69 |
> |
} |
70 |
> |
|
71 |
> |
/** |
72 |
> |
* If the class name begins with uk.ac.ukc.iscream |
73 |
> |
* this method will trim it off, otherwise it |
74 |
> |
* leaves the string unchanged. |
75 |
> |
* |
76 |
> |
* @param className the name of a class |
77 |
> |
* |
78 |
> |
* @return the tidy version of it |
79 |
> |
*/ |
80 |
> |
private static String tidyClassName(String className) { |
81 |
> |
if (className.startsWith("uk.ac.ukc.iscream")) { |
82 |
> |
return className.substring(18); |
83 |
> |
} |
84 |
> |
return className; |
85 |
|
} |
86 |
|
|
87 |
|
//---CONSTRUCTORS--- |