2003/12/24 21:41:05
[org.ibex.core.git] / src / org / xwt / util / Log.java
index 71fb1ed..1eb61ef 100644 (file)
@@ -1,5 +1,12 @@
-// Copyright 2002 Adam Megacz, see the COPYING file for licensing [LGPL]
+// Copyright (C) 2003 Adam Megacz <adam@xwt.org> all rights reserved.
+//
+// You may modify, copy, and redistribute this code under the terms of
+// the GNU Library Public License version 2.1, with the exception of
+// the portion of clause 6a after the semicolon (aka the "obnoxious
+// relink clause")
+
 package org.xwt.util;
+import org.xwt.js.*;
 import java.io.*;
 import java.util.*;
 
@@ -14,9 +21,34 @@ public class Log {
     /** 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); }
+
+    public static BufferedReader loggedReader(Reader r) {
+        // FIXME
+        return new BufferedReader(r);
+        /*
+            new BufferedReader(new FilterReader(new InputStreamReader(is)) {
+                    public int read() throws IOException {
+                        int i = super.read();
+                        if (Log.on) Log.log(this, "recv: " + ((char)i));
+                        return i;
+                    }
+                    public int read(char[] c, int off, int len) throws IOException {
+                        int ret = super.read(c, off, len);
+                        if (ret == -1) return ret;
+                        String s;
+                        BufferedReader br2 = new BufferedReader(new StringReader(new String(c, off, ret)));
+                        while ((s = br2.readLine()) != null) Log.log(this, "recv: " + s);
+                        return ret;
+                    }
+                });
+        */
+    }
+
     /** message can be a String or a Throwable */
     public static synchronized void log(Object o, Object message) {
-
         if (firstMessage && !logDates) {
             firstMessage = false;
             System.err.println("===========================================================================");
@@ -45,21 +77,57 @@ public class Log {
             lastDate = d;
         }
 
-        if (!(message instanceof Throwable)) System.err.println(classname + message);
-        else {
+
+        if (message instanceof Throwable) {
             ByteArrayOutputStream baos = new ByteArrayOutputStream();
             ((Throwable)message).printStackTrace(new PrintStream(baos));
             byte[] b = baos.toByteArray();
             BufferedReader br = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(b)));
             String s = null;
             try {
-                while((s = br.readLine()) != null) {
-                    System.err.print(classname);
-                    for(int i=0; i<s.length(); i++)
-                        System.err.print(s.charAt(i) == '\t' ? "    " : ("" + s.charAt(i)));
-                    System.err.println();
-                }
-            } catch (Exception e) { }
+                while((s = br.readLine()) != null) log(o, s);
+            } catch (IOException e) {
+                System.err.println("Logger: exception thrown by ByteArrayInputStream -- this should not happen");
+            }
+            return;
+        }
+
+        String str = message.toString();
+        while(str.indexOf('\t') != -1)
+            str = str.substring(0, str.indexOf('\t')) + "    " + str.substring(str.indexOf('\t') + 1);
+
+        while(str.indexOf('\n') != -1) {
+            System.err.println(classname + str.substring(0, str.indexOf('\n')));
+            str = str.substring(str.indexOf('\n') + 1);
+        }
+        System.err.println(classname + str);
+    }
+
+    public static void recursiveLog(String indent, String name, Object o) throws JSExn {
+        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);
 
         }
     }