2003/10/31 09:50:10
[org.ibex.core.git] / src / org / xwt / util / Log.java
index 81f89e1..eb456d1 100644 (file)
@@ -1,20 +1,34 @@
-// Copyright 2002 Adam Megacz, see the COPYING file for licensing [LGPL]
+// Copyright 2003 Adam Megacz, see the COPYING file for licensing [LGPL]
 package org.xwt.util;
+import org.xwt.js.JS;
 import java.io.*;
+import java.util.*;
 
 /** easy to use logger */
 public class Log {
 
     public static boolean on = true;
     public static boolean verbose = false;
+    public static boolean logDates = false;
+    public static Date lastDate = null;
 
     /** 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) {
+        JS.Thread current = org.xwt.js.JS.Thread.current();
+        if (current == null) {
+            log("<none>", message);
+        } else {
+            log(current.getSourceName() + ":" + current.getLine(), message);
+        }
+    }
+
     /** message can be a String or a Throwable */
     public static synchronized void log(Object o, Object message) {
-
-        if (firstMessage) {
+        if (firstMessage && !logDates) {
             firstMessage = false;
             System.err.println("===========================================================================");
             log(Log.class, "Logging enabled at " + new java.util.Date());
@@ -26,10 +40,22 @@ public class Log {
         else classname = o.getClass().getName();
         
         if (classname.indexOf('.') != -1) classname = classname.substring(classname.lastIndexOf('.') + 1);
-        if (classname.length() > 20) classname = classname.substring(0, 20);
-        while (classname.length() < 20) classname = " " + classname;
+        if (classname.length() > (logDates ? 14 : 20)) classname = classname.substring(0, (logDates ? 14 : 20));
+        while (classname.length() < (logDates ? 14 : 20)) classname = " " + classname;
         classname = classname + ": ";
 
+        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);
+                System.err.println();
+                System.err.println("=== " + now + " ==========================================================");
+            }
+            java.text.DateFormat df = new java.text.SimpleDateFormat("[EEE HH:mm:ss] ");
+            classname = df.format(d) + classname;
+            lastDate = d;
+        }
+
         if (!(message instanceof Throwable)) System.err.println(classname + message);
         else {
             ByteArrayOutputStream baos = new ByteArrayOutputStream();