fix ThreadPool bug
[org.ibex.util.git] / src / org / ibex / util / Log.java
index 2659b26..dc914c2 100644 (file)
@@ -18,6 +18,8 @@ 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
@@ -70,7 +72,7 @@ public class Log {
     }
     public static void clearnotes() { if (!notes) return; notebuf().setLength(0); }
 
-    private static final Basket.Map notebufs = new Basket.HashMap();
+    private static final Basket.Map notebufs = new Basket.Hash();
     public static StringBuffer notebuf() {
         StringBuffer ret = (StringBuffer)notebufs.get(Thread.currentThread());
         if (ret == null) {
@@ -120,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) {
@@ -138,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);
         }
@@ -198,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);