1 // Copyright (C) 2003 Adam Megacz <adam@xwt.org> all rights reserved.
3 // You may modify, copy, and redistribute this code under the terms of
4 // the GNU Library Public License version 2.1, with the exception of
5 // the portion of clause 6a after the semicolon (aka the "obnoxious
13 /** easy to use logger */
16 public static boolean on = true;
17 public static boolean color = true;
18 public static boolean verbose = false;
19 public static boolean logDates = false;
20 public static Date lastDate = null;
22 /** true iff nothing has yet been logged */
23 public static boolean firstMessage = true;
25 /** message can be a String or a Throwable */
26 public static synchronized void echo(Object o, Object message) { log(o, message, ECHO); }
27 public static synchronized void diag(Object o, Object message) { log(o, message, DIAGNOSTIC); }
28 public static synchronized void debug(Object o, Object message) { log(o, message, DEBUG); }
29 public static synchronized void info(Object o, Object message) { log(o, message, INFO); }
30 public static synchronized void warn(Object o, Object message) { log(o, message, WARN); }
31 public static synchronized void error(Object o, Object message) { log(o, message, ERROR); }
33 // these two logging levels serve ONLY to change the color; semantically they are the same as DEBUG
34 private static final int DIAGNOSTIC = -2;
35 private static final int ECHO = -1;
37 // the usual log4j levels, minus FAIL (we just throw an Error in that case)
38 private static final int DEBUG = 0;
39 private static final int INFO = 1;
40 private static final int WARN = 2;
41 private static final int ERROR = 3;
43 private static final int BLACK = 30;
44 private static final int BLUE = 34;
45 private static final int GREEN = 32;
46 private static final int CYAN = 36;
47 private static final int RED = 31;
48 private static final int PURPLE = 35;
49 private static final int BROWN = 33;
50 private static final int GRAY = 37;
52 private static String color(int color, boolean bright, String s) {
53 if (!Log.color) return s;
55 "" + ((char)27) + "[" + (bright?1:0) + ";" + color + "m" +
60 private static String lastClassName = null;
61 private static synchronized void log(Object o, Object message, int level) {
62 if (firstMessage && !logDates) {
64 System.err.println(color(GREEN, false, "==========================================================================="));
65 diag(Log.class, "Logging enabled at " + new java.util.Date());
66 if (color) diag(Log.class, "logging messages in " +
67 color(BLUE, true, "c") +
68 color(RED, true, "o") +
69 color(CYAN, true, "l") +
70 color(GREEN, true, "o") +
71 color(PURPLE, true, "r"));
75 if (o instanceof Class) classname = ((Class)o).getName();
76 else if (o instanceof String) classname = (String)o;
77 else classname = o.getClass().getName();
79 if (classname.equals(lastClassName)) classname = "";
80 else lastClassName = classname;
82 if (classname.indexOf('.') != -1) classname = classname.substring(classname.lastIndexOf('.') + 1);
83 if (classname.length() > (logDates ? 14 : 20)) classname = classname.substring(0, (logDates ? 14 : 20));
84 while (classname.length() < (logDates ? 14 : 20)) classname = " " + classname;
85 classname = classname + (classname.trim().length() == 0 ? " " : ": ");
86 classname = color(GRAY, true, classname);
90 if (lastDate == null || d.getYear() != lastDate.getYear() || d.getMonth() != lastDate.getMonth() || d.getDay() != lastDate.getDay()) {
91 String now = new java.text.SimpleDateFormat("EEE dd MMM yyyy").format(d);
93 System.err.println(color(GRAY, false, "=== " + now + " =========================================================="));
95 java.text.DateFormat df = new java.text.SimpleDateFormat("[EEE HH:mm:ss] ");
96 classname = df.format(d) + classname;
101 if (message instanceof Throwable) {
102 if (level < ERROR) level = WARN;
103 ByteArrayOutputStream baos = new ByteArrayOutputStream();
104 ((Throwable)message).printStackTrace(new PrintStream(baos));
105 byte[] b = baos.toByteArray();
106 BufferedReader br = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(b)));
110 while((s = br.readLine()) != null) m += s + "\n";
112 } catch (IOException e) {
113 System.err.println(color(RED, true, "Logger: exception thrown by ByteArrayInputStream -- this should not happen"));
118 String str = message.toString();
119 while(str.indexOf('\t') != -1)
120 str = str.substring(0, str.indexOf('\t')) + " " + str.substring(str.indexOf('\t') + 1);
122 classname = color(GRAY, false, classname);
123 int levelcolor = GRAY;
124 boolean bright = true;
126 case DIAGNOSTIC: levelcolor = GREEN; bright = false; break;
127 case ECHO: levelcolor = BLUE; bright = true; break;
128 case DEBUG: levelcolor = BLACK; bright = true; break;
129 case INFO: levelcolor = GRAY; bright = false; break;
130 case WARN: levelcolor = BROWN; bright = false; break;
131 case ERROR: levelcolor = RED; bright = true; break;
134 while(str.indexOf('\n') != -1) {
135 System.err.println(classname + color(levelcolor, bright, str.substring(0, str.indexOf('\n'))));
136 classname = logDates ? " " : " ";
137 str = str.substring(str.indexOf('\n') + 1);
139 System.err.println(classname + color(levelcolor, bright, str));
142 public static void recursiveLog(String indent, String name, Object o) throws JSExn {
143 if (!name.equals("")) name += " : ";
146 JS.log(indent + name + "<null>");
148 } else if (o instanceof JSArray) {
149 JS.log(indent + name + "<array>");
150 JSArray na = (JSArray)o;
151 for(int i=0; i<na.length(); i++)
152 recursiveLog(indent + " ", i + "", na.elementAt(i));
154 } else if (o instanceof JS) {
155 JS.log(indent + name + "<object>");
157 Enumeration e = s.keys();
158 while(e.hasMoreElements()) {
159 Object key = e.nextElement();
161 recursiveLog(indent + " ", key.toString(),
162 (key instanceof Integer) ?
163 s.get(((Integer)key)) : s.get(key.toString()));
166 JS.log(indent + name + o);