2003/12/24 21:41:05
[org.ibex.core.git] / src / org / xwt / util / Log.java
index fbed19c..1eb61ef 100644 (file)
@@ -1,4 +1,10 @@
-// Copyright 2003 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.*;
@@ -17,7 +23,29 @@ public class Log {
 
     /** 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(JSContext.getSourceName() + ":" + JSContext.getLine(), 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) {
@@ -49,26 +77,33 @@ 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) {
+    public static void recursiveLog(String indent, String name, Object o) throws JSExn {
         if (!name.equals("")) name += " : ";
 
         if (o == null) {