2003/12/13 00:38:34
[org.ibex.core.git] / src / org / xwt / util / Log.java
1 // Copyright (C) 2003 Adam Megacz <adam@xwt.org> all rights reserved.
2 //
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
6 // relink clause")
7
8 package org.xwt.util;
9 import org.xwt.js.*;
10 import java.io.*;
11 import java.util.*;
12
13 /** easy to use logger */
14 public class Log {
15
16     public static boolean on = true;
17     public static boolean verbose = false;
18     public static boolean logDates = false;
19     public static Date lastDate = null;
20
21     /** true iff nothing has yet been logged */
22     public static boolean firstMessage = true;
23
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); }
27
28     public static BufferedReader loggedReader(Reader r) {
29         // FIXME
30         return new BufferedReader(r);
31         /*
32             new BufferedReader(new FilterReader(new InputStreamReader(is)) {
33                     public int read() throws IOException {
34                         int i = super.read();
35                         if (Log.on) Log.log(this, "recv: " + ((char)i));
36                         return i;
37                     }
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;
41                         String s;
42                         BufferedReader br2 = new BufferedReader(new StringReader(new String(c, off, ret)));
43                         while ((s = br2.readLine()) != null) Log.log(this, "recv: " + s);
44                         return ret;
45                     }
46                 });
47         */
48     }
49
50     /** message can be a String or a Throwable */
51     public static synchronized void log(Object o, Object message) {
52         if (firstMessage && !logDates) {
53             firstMessage = false;
54             System.err.println("===========================================================================");
55             log(Log.class, "Logging enabled at " + new java.util.Date());
56         }
57
58         String classname;
59         if (o instanceof Class) classname = ((Class)o).getName();
60         else if (o instanceof String) classname = (String)o;
61         else classname = o.getClass().getName();
62         
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 + ": ";
67
68         if (logDates) {
69             Date d = new Date();
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);
72                 System.err.println();
73                 System.err.println("=== " + now + " ==========================================================");
74             }
75             java.text.DateFormat df = new java.text.SimpleDateFormat("[EEE HH:mm:ss] ");
76             classname = df.format(d) + classname;
77             lastDate = d;
78         }
79
80
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)));
86             String s = null;
87             try {
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");
91             }
92             return;
93         }
94
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);
98
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);
102         }
103         System.err.println(classname + str);
104     }
105
106     public static void recursiveLog(String indent, String name, Object o) throws JSExn {
107         if (!name.equals("")) name += " : ";
108
109         if (o == null) {
110             Log.logJS(indent + name + "<null>");
111
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));
117
118         } else if (o instanceof JS) {
119             Log.logJS(indent + name + "<object>");
120             JS s = (JS)o;
121             Enumeration e = s.keys();
122             while(e.hasMoreElements()) {
123                 Object key = e.nextElement();
124                 if (key != null)
125                     recursiveLog(indent + "  ", key.toString(),
126                                  (key instanceof Integer) ?
127                                  s.get(((Integer)key)) : s.get(key.toString()));
128             }
129         } else {
130             Log.logJS(indent + name + o);
131
132         }
133     }
134
135 }