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 verbose = false;
18 public static boolean logDates = false;
19 public static Date lastDate = null;
21 /** true iff nothing has yet been logged */
22 public static boolean firstMessage = true;
24 /** log a message with the current JavaScript sourceName/line */
25 public static void logJS(Object o, Object message) { logJS(message); }
26 public static void logJS(Object message) { log(JS.getSourceName() + ":" + JS.getLine(), message); }
28 /** message can be a String or a Throwable */
29 public static synchronized void log(Object o, Object message) {
30 if (firstMessage && !logDates) {
32 System.err.println("===========================================================================");
33 log(Log.class, "Logging enabled at " + new java.util.Date());
37 if (o instanceof Class) classname = ((Class)o).getName();
38 else if (o instanceof String) classname = (String)o;
39 else classname = o.getClass().getName();
41 if (classname.indexOf('.') != -1) classname = classname.substring(classname.lastIndexOf('.') + 1);
42 if (classname.length() > (logDates ? 14 : 20)) classname = classname.substring(0, (logDates ? 14 : 20));
43 while (classname.length() < (logDates ? 14 : 20)) classname = " " + classname;
44 classname = classname + ": ";
48 if (lastDate == null || d.getYear() != lastDate.getYear() || d.getMonth() != lastDate.getMonth() || d.getDay() != lastDate.getDay()) {
49 String now = new java.text.SimpleDateFormat("EEE dd MMM yyyy").format(d);
51 System.err.println("=== " + now + " ==========================================================");
53 java.text.DateFormat df = new java.text.SimpleDateFormat("[EEE HH:mm:ss] ");
54 classname = df.format(d) + classname;
58 if (!(message instanceof Throwable)) System.err.println(classname + message);
60 ByteArrayOutputStream baos = new ByteArrayOutputStream();
61 ((Throwable)message).printStackTrace(new PrintStream(baos));
62 byte[] b = baos.toByteArray();
63 BufferedReader br = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(b)));
66 while((s = br.readLine()) != null) {
67 System.err.print(classname);
68 for(int i=0; i<s.length(); i++)
69 System.err.print(s.charAt(i) == '\t' ? " " : ("" + s.charAt(i)));
72 } catch (Exception e) { }
77 public static void recursiveLog(String indent, String name, Object o) throws JSExn {
78 if (!name.equals("")) name += " : ";
81 Log.logJS(indent + name + "<null>");
83 } else if (o instanceof JSArray) {
84 Log.logJS(indent + name + "<array>");
85 JSArray na = (JSArray)o;
86 for(int i=0; i<na.length(); i++)
87 recursiveLog(indent + " ", i + "", na.elementAt(i));
89 } else if (o instanceof JS) {
90 Log.logJS(indent + name + "<object>");
92 Enumeration e = s.keys();
93 while(e.hasMoreElements()) {
94 Object key = e.nextElement();
96 recursiveLog(indent + " ", key.toString(),
97 (key instanceof Integer) ?
98 s.get(((Integer)key)) : s.get(key.toString()));
101 Log.logJS(indent + name + o);