2003/11/26 05:11:38
[org.ibex.core.git] / src / org / xwt / util / Log.java
index 81f89e1..269b2f2 100644 (file)
@@ -1,20 +1,27 @@
-// Copyright 2002 Adam Megacz, see the COPYING file for licensing [LGPL]
+// Copyright 2003 Adam Megacz, see the COPYING file for licensing [LGPL]
 package org.xwt.util;
+import org.xwt.js.*;
 import java.io.*;
+import java.util.*;
 
 /** easy to use logger */
 public class Log {
 
     public static boolean on = true;
     public static boolean verbose = false;
+    public static boolean logDates = false;
+    public static Date lastDate = null;
 
     /** true iff nothing has yet been logged */
     public static boolean firstMessage = true;
 
+    /** log a message with the current JavaScript sourceName/line */
+    public static void logJS(Object o, Object message) { logJS(message); }
+    public static void logJS(Object message) { log(JS.getSourceName() + ":" + JS.getLine(), message); }
+
     /** message can be a String or a Throwable */
     public static synchronized void log(Object o, Object message) {
-
-        if (firstMessage) {
+        if (firstMessage && !logDates) {
             firstMessage = false;
             System.err.println("===========================================================================");
             log(Log.class, "Logging enabled at " + new java.util.Date());
@@ -26,10 +33,22 @@ public class Log {
         else classname = o.getClass().getName();
         
         if (classname.indexOf('.') != -1) classname = classname.substring(classname.lastIndexOf('.') + 1);
-        if (classname.length() > 20) classname = classname.substring(0, 20);
-        while (classname.length() < 20) classname = " " + classname;
+        if (classname.length() > (logDates ? 14 : 20)) classname = classname.substring(0, (logDates ? 14 : 20));
+        while (classname.length() < (logDates ? 14 : 20)) classname = " " + classname;
         classname = classname + ": ";
 
+        if (logDates) {
+            Date d = new Date();
+            if (lastDate == null || d.getYear() != lastDate.getYear() || d.getMonth() != lastDate.getMonth() || d.getDay() != lastDate.getDay()) {
+                String now = new java.text.SimpleDateFormat("EEE dd MMM yyyy").format(d);
+                System.err.println();
+                System.err.println("=== " + now + " ==========================================================");
+            }
+            java.text.DateFormat df = new java.text.SimpleDateFormat("[EEE HH:mm:ss] ");
+            classname = df.format(d) + classname;
+            lastDate = d;
+        }
+
         if (!(message instanceof Throwable)) System.err.println(classname + message);
         else {
             ByteArrayOutputStream baos = new ByteArrayOutputStream();
@@ -49,4 +68,33 @@ public class Log {
         }
     }
 
+    public static void recursiveLog(String indent, String name, Object o) {
+        if (!name.equals("")) name += " : ";
+
+        if (o == null) {
+            Log.logJS(indent + name + "<null>");
+
+        } else if (o instanceof JSArray) {
+            Log.logJS(indent + name + "<array>");
+            JSArray na = (JSArray)o;
+            for(int i=0; i<na.length(); i++)
+                recursiveLog(indent + "  ", i + "", na.elementAt(i));
+
+        } else if (o instanceof JS) {
+            Log.logJS(indent + name + "<object>");
+            JS s = (JS)o;
+            Enumeration e = s.keys();
+            while(e.hasMoreElements()) {
+                Object key = e.nextElement();
+                if (key != null)
+                    recursiveLog(indent + "  ", key.toString(),
+                                 (key instanceof Integer) ?
+                                 s.get(((Integer)key)) : s.get(key.toString()));
+            }
+        } else {
+            Log.logJS(indent + name + o);
+
+        }
+    }
+
 }