2003/12/07 09:57:15
[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     /** message can be a String or a Throwable */
29     public static synchronized void log(Object o, Object message) {
30         if (firstMessage && !logDates) {
31             firstMessage = false;
32             System.err.println("===========================================================================");
33             log(Log.class, "Logging enabled at " + new java.util.Date());
34         }
35
36         String classname;
37         if (o instanceof Class) classname = ((Class)o).getName();
38         else if (o instanceof String) classname = (String)o;
39         else classname = o.getClass().getName();
40         
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 + ": ";
45
46         if (logDates) {
47             Date d = new Date();
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);
50                 System.err.println();
51                 System.err.println("=== " + now + " ==========================================================");
52             }
53             java.text.DateFormat df = new java.text.SimpleDateFormat("[EEE HH:mm:ss] ");
54             classname = df.format(d) + classname;
55             lastDate = d;
56         }
57
58         if (!(message instanceof Throwable)) System.err.println(classname + message);
59         else {
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)));
64             String s = null;
65             try {
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)));
70                     System.err.println();
71                 }
72             } catch (Exception e) { }
73
74         }
75     }
76
77     public static void recursiveLog(String indent, String name, Object o) throws JSExn {
78         if (!name.equals("")) name += " : ";
79
80         if (o == null) {
81             Log.logJS(indent + name + "<null>");
82
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));
88
89         } else if (o instanceof JS) {
90             Log.logJS(indent + name + "<object>");
91             JS s = (JS)o;
92             Enumeration e = s.keys();
93             while(e.hasMoreElements()) {
94                 Object key = e.nextElement();
95                 if (key != null)
96                     recursiveLog(indent + "  ", key.toString(),
97                                  (key instanceof Integer) ?
98                                  s.get(((Integer)key)) : s.get(key.toString()));
99             }
100         } else {
101             Log.logJS(indent + name + o);
102
103         }
104     }
105
106 }