fix ThreadPool bug
[org.ibex.util.git] / src / org / ibex / util / Log.java
index aef3ba2..dc914c2 100644 (file)
@@ -1,18 +1,33 @@
-// Copyright (C) 2003 Adam Megacz <adam@ibex.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")
+// Copyright 2000-2005 the Contributors, as shown in the revision logs.
+// Licensed under the Apache Public Source License 2.0 ("the License").
+// You may not use this file except in compliance with the License.
 
 package org.ibex.util;
-import org.ibex.js.*;
-import java.io.*;
+
 import java.util.*;
-import java.net.*;
 
-/** easy to use logger */
+import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
+import java.io.PrintWriter;
+import java.io.FileOutputStream;
+import java.io.InputStreamReader;
+import java.io.IOException;
+import java.net.Socket;
+import java.net.InetAddress;
+import java.text.SimpleDateFormat;
+
+// FEATURE: logging exceptions should automatically unwrap exceptions
+
+/** Easy to use logger.
+ * 
+ * @author adam@ibex.org
+ */
 public class Log {
+    private static final SimpleDateFormat formatDate = new SimpleDateFormat("EEE dd MMM yyyy");
+    private static final SimpleDateFormat formatTime = new SimpleDateFormat("[EEE HH:mm:ss] ");
+    private static final Hashtable threadAnnotations = new Hashtable();
 
     public static boolean on            = System.getProperty("ibex.log.on", "true").equals("true");
     public static boolean color         = System.getProperty("ibex.log.color", "true").equals("true");
@@ -22,7 +37,7 @@ public class Log {
     public static boolean stackTraces   = System.getProperty("ibex.log.stackTraces", "true").equals("true");
     public static int maximumNoteLength = Integer.parseInt(System.getProperty("ibex.log.notes.maximumLength", (1024 * 32)+""));
     public static boolean rpc           = false;
-    public static Date lastDate = null;
+    public static int lastDay = -1;
 
     public static PrintStream logstream = System.err;
 
@@ -37,7 +52,6 @@ public class Log {
         logstream = new PrintStream(new Socket(InetAddress.getByName(host), port).getOutputStream());
     }
 
-    private static Hashtable threadAnnotations = new Hashtable();
     public static void setThreadAnnotation(String s) { threadAnnotations.put(Thread.currentThread(), s); }
 
     /** 
@@ -57,7 +71,8 @@ public class Log {
         }
     }
     public static void clearnotes() { if (!notes) return; notebuf().setLength(0); }
-    private static Hashtable notebufs = new Hashtable();
+
+    private static final Basket.Map notebufs = new Basket.Hash();
     public static StringBuffer notebuf() {
         StringBuffer ret = (StringBuffer)notebufs.get(Thread.currentThread());
         if (ret == null) {
@@ -107,6 +122,14 @@ public class Log {
     }
 
     private static String lastClassName = null;
+
+    public static void printStackTrace(Object o, int level) {
+        try {
+            throw new Exception("just printing a stack trace; no real problem");
+        } catch (Exception e) {
+            log(o, e, level);
+        }
+    }
     private static synchronized void log(Object o, Object message, int level) {
         if (level < Log.level) return;
         if (firstMessage && !logDates) {
@@ -125,7 +148,8 @@ public class Log {
         }
 
         String classname;
-        if (o instanceof Class) {
+        if (o == null) classname = "";
+        else if (o instanceof Class) {
             classname = ((Class)o).getName();
             if (classname.indexOf('.') != -1) classname = classname.substring(classname.lastIndexOf('.') + 1);
         }
@@ -142,15 +166,14 @@ public class Log {
         classname = classname.replace('$', '.');
 
         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);
+            Calendar cal = Calendar.getInstance();
+            if (lastDay < 0 || lastDay != cal.get(Calendar.DAY_OF_YEAR)) {
+                lastDay = cal.get(Calendar.DAY_OF_YEAR);
+                String now = formatDate.format(cal.getTime());
                 logstream.println();
-                logstream.println(colorize(GRAY, false, "=== " + now + " =========================================================="));
+                logstream.println(colorize(GREEN, false, "=== " + now + " =========================================================="));
             }
-            java.text.DateFormat df = new java.text.SimpleDateFormat("[EEE HH:mm:ss] ");
-            classname = df.format(d) + classname;
-            lastDate = d;
+            classname = formatTime.format(cal.getTime()) + classname;
         }
 
         String annot = (String)threadAnnotations.get(Thread.currentThread());
@@ -186,7 +209,7 @@ public class Log {
                             String shortened = s.substring(s.indexOf('(')+1);
                             shortened = shortened.substring(0, shortened.indexOf(')'));
                             m += " " + shortened;
-                            if (ok > 1) m = m.substring(0, 78);
+                            if (ok > 1) m = m.substring(0, Math.min(m.length(), 78));
                             ok++;
                         }
                     } while (m.length() < 78);
@@ -227,33 +250,4 @@ public class Log {
         logstream.println(classname + colorize(levelcolor, bright, str));
     }
 
-    public static void recursiveLog(String indent, String name, Object o) throws JSExn {
-        if (!name.equals("")) name += " : ";
-
-        if (o == null) {
-            JS.log(indent + name + "<null>");
-
-        } else if (o instanceof JSArray) {
-            JS.log(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) {
-            JS.log(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 {
-            JS.log(indent + name + o);
-
-        }
-    }
-
 }