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 public static BufferedReader loggedReader(Reader r) {
30 return new BufferedReader(r);
32 new BufferedReader(new FilterReader(new InputStreamReader(is)) {
33 public int read() throws IOException {
35 if (Log.on) Log.log(this, "recv: " + ((char)i));
38 public int read(char[] c, int off, int len) throws IOException {
39 int ret = super.read(c, off, len);
40 if (ret == -1) return ret;
42 BufferedReader br2 = new BufferedReader(new StringReader(new String(c, off, ret)));
43 while ((s = br2.readLine()) != null) Log.log(this, "recv: " + s);
50 /** message can be a String or a Throwable */
51 public static synchronized void log(Object o, Object message) {
52 if (firstMessage && !logDates) {
54 System.err.println("===========================================================================");
55 log(Log.class, "Logging enabled at " + new java.util.Date());
59 if (o instanceof Class) classname = ((Class)o).getName();
60 else if (o instanceof String) classname = (String)o;
61 else classname = o.getClass().getName();
63 if (classname.indexOf('.') != -1) classname = classname.substring(classname.lastIndexOf('.') + 1);
64 if (classname.length() > (logDates ? 14 : 20)) classname = classname.substring(0, (logDates ? 14 : 20));
65 while (classname.length() < (logDates ? 14 : 20)) classname = " " + classname;
66 classname = classname + ": ";
70 if (lastDate == null || d.getYear() != lastDate.getYear() || d.getMonth() != lastDate.getMonth() || d.getDay() != lastDate.getDay()) {
71 String now = new java.text.SimpleDateFormat("EEE dd MMM yyyy").format(d);
73 System.err.println("=== " + now + " ==========================================================");
75 java.text.DateFormat df = new java.text.SimpleDateFormat("[EEE HH:mm:ss] ");
76 classname = df.format(d) + classname;
81 if (message instanceof Throwable) {
82 ByteArrayOutputStream baos = new ByteArrayOutputStream();
83 ((Throwable)message).printStackTrace(new PrintStream(baos));
84 byte[] b = baos.toByteArray();
85 BufferedReader br = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(b)));
88 while((s = br.readLine()) != null) log(o, s);
89 } catch (IOException e) {
90 System.err.println("Logger: exception thrown by ByteArrayInputStream -- this should not happen");
95 String str = message.toString();
96 while(str.indexOf('\t') != -1)
97 str = str.substring(0, str.indexOf('\t')) + " " + str.substring(str.indexOf('\t') + 1);
99 while(str.indexOf('\n') != -1) {
100 System.err.println(classname + str.substring(0, str.indexOf('\n')));
101 str = str.substring(str.indexOf('\n') + 1);
103 System.err.println(classname + str);
106 public static void recursiveLog(String indent, String name, Object o) throws JSExn {
107 if (!name.equals("")) name += " : ";
110 Log.logJS(indent + name + "<null>");
112 } else if (o instanceof JSArray) {
113 Log.logJS(indent + name + "<array>");
114 JSArray na = (JSArray)o;
115 for(int i=0; i<na.length(); i++)
116 recursiveLog(indent + " ", i + "", na.elementAt(i));
118 } else if (o instanceof JS) {
119 Log.logJS(indent + name + "<object>");
121 Enumeration e = s.keys();
122 while(e.hasMoreElements()) {
123 Object key = e.nextElement();
125 recursiveLog(indent + " ", key.toString(),
126 (key instanceof Integer) ?
127 s.get(((Integer)key)) : s.get(key.toString()));
130 Log.logJS(indent + name + o);