2003/12/24 10:14:44
[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 int lastDate = 0;
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             Calendar c = GregorianCalendar.getInstance();
70             Date d = c.getTime();
71             int change = c.get(Calendar.DAY_OF_YEAR) + c.get(Calendar.YEAR);
72
73             if (change != lastDate) {
74                 lastDate = change;
75                 String now = new java.text.SimpleDateFormat("EEE dd MMM yyyy").format(d);
76                 System.err.println();
77                 System.err.println("=== " + now + " ==========================================================");
78             }
79             java.text.DateFormat df = new java.text.SimpleDateFormat("[EEE HH:mm:ss] ");
80             classname = df.format(d) + classname;
81         }
82
83
84         if (message instanceof Throwable) {
85             ByteArrayOutputStream baos = new ByteArrayOutputStream();
86             ((Throwable)message).printStackTrace(new PrintStream(baos));
87             byte[] b = baos.toByteArray();
88             BufferedReader br = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(b)));
89             String s = null;
90             try {
91                 while((s = br.readLine()) != null) log(o, s);
92             } catch (IOException e) {
93                 System.err.println("Logger: exception thrown by ByteArrayInputStream -- this should not happen");
94             }
95             return;
96         }
97
98         String str = message.toString();
99         while(str.indexOf('\t') != -1)
100             str = str.substring(0, str.indexOf('\t')) + "    " + str.substring(str.indexOf('\t') + 1);
101
102         while(str.indexOf('\n') != -1) {
103             System.err.println(classname + str.substring(0, str.indexOf('\n')));
104             str = str.substring(str.indexOf('\n') + 1);
105         }
106         System.err.println(classname + str);
107     }
108
109     public static void recursiveLog(String indent, String name, Object o) throws JSExn {
110         if (!name.equals("")) name += " : ";
111
112         if (o == null) {
113             Log.logJS(indent + name + "<null>");
114
115         } else if (o instanceof JSArray) {
116             Log.logJS(indent + name + "<array>");
117             JSArray na = (JSArray)o;
118             for(int i=0; i<na.length(); i++)
119                 recursiveLog(indent + "  ", i + "", na.elementAt(i));
120
121         } else if (o instanceof JS) {
122             Log.logJS(indent + name + "<object>");
123             JS s = (JS)o;
124             Enumeration e = s.keys();
125             while(e.hasMoreElements()) {
126                 Object key = e.nextElement();
127                 if (key != null)
128                     recursiveLog(indent + "  ", key.toString(),
129                                  (key instanceof Integer) ?
130                                  s.get(((Integer)key)) : s.get(key.toString()));
131             }
132         } else {
133             Log.logJS(indent + name + o);
134
135         }
136     }
137
138 }