fixed bug 403, added logging to tcp and email
[org.ibex.core.git] / src / org / ibex / util / Log.java
index d031dc7..1766be9 100644 (file)
@@ -9,6 +9,7 @@ package org.ibex.util;
 import org.ibex.js.*;
 import java.io.*;
 import java.util.*;
+import java.net.*;
 
 /** easy to use logger */
 public class Log {
@@ -19,6 +20,18 @@ public class Log {
     public static boolean logDates = false;
     public static Date lastDate = null;
 
+    public static PrintStream logstream = System.err;
+
+    public static void email(String address) { throw new Error("FIXME not supported"); }
+    public static void file(String filename) throws IOException {
+        // FIXME security
+        logstream = new PrintStream(new FileOutputStream(filename));
+    }
+    public static void tcp(String host, int port) throws IOException {
+        // FIXME security
+        logstream = new PrintStream(new Socket(InetAddress.getByName(host), port).getOutputStream());
+    }
+
     /** true iff nothing has yet been logged */
     public static boolean firstMessage = true;
 
@@ -35,10 +48,12 @@ public class Log {
     private static final int ECHO = -1;
 
     // the usual log4j levels, minus FAIL (we just throw an Error in that case)
-    private static final int DEBUG = 0;
-    private static final int INFO = 1;
-    private static final int WARN = 2;
-    private static final int ERROR = 3;
+    public static final int DEBUG = 0;
+    public static final int INFO = 1;
+    public static final int WARN = 2;
+    public static final int ERROR = 3;
+    public static final int SILENT = Integer.MAX_VALUE;
+    public static int level = INFO;
 
     private static final int BLUE = 34;
     private static final int GREEN = 32;
@@ -58,9 +73,10 @@ public class Log {
 
     private static String lastClassName = null;
     private static synchronized void log(Object o, Object message, int level) {
+        if (level > Log.level) return;
         if (firstMessage && !logDates) {
             firstMessage = false;
-            System.err.println(colorize(GREEN, false, "==========================================================================="));
+            logstream.println(colorize(GREEN, false, "==========================================================================="));
             diag(Log.class, "Logging enabled at " + new java.util.Date());
             if (color) diag(Log.class, "logging messages in " +
                 colorize(BLUE, true, "c") +
@@ -91,8 +107,8 @@ public class Log {
             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(colorize(GRAY, false, "=== " + now + " =========================================================="));
+                logstream.println();
+                logstream.println(colorize(GRAY, false, "=== " + now + " =========================================================="));
             }
             java.text.DateFormat df = new java.text.SimpleDateFormat("[EEE HH:mm:ss] ");
             classname = df.format(d) + classname;
@@ -112,7 +128,7 @@ public class Log {
                 while((s = br.readLine()) != null) m += s + "\n";
                 log(o, m.substring(0, m.length() - 1), level);
             } catch (IOException e) {
-                System.err.println(colorize(RED, true, "Logger: exception thrown by ByteArrayInputStream -- this should not happen"));
+                logstream.println(colorize(RED, true, "Logger: exception thrown by ByteArrayInputStream -- this should not happen"));
             }
             return;
         }
@@ -134,12 +150,12 @@ public class Log {
         }
 
         while(str.indexOf('\n') != -1) {
-            System.err.println(classname + colorize(levelcolor, bright, str.substring(0, str.indexOf('\n'))));
+            logstream.println(classname + colorize(levelcolor, bright, str.substring(0, str.indexOf('\n'))));
             classname = logDates ? "                " : "                      ";
             classname = colorize(GRAY,false,classname);
             str = str.substring(str.indexOf('\n') + 1);
         }
-        System.err.println(classname + colorize(levelcolor, bright, str));
+        logstream.println(classname + colorize(levelcolor, bright, str));
     }
 
     public static void recursiveLog(String indent, String name, Object o) throws JSExn {