3a54c4d65308c5566973fbbcf214af99b0e0e61d
[org.ibex.core.git] / src / org / xwt / util / Log.java
1 // Copyright 2002 Adam Megacz, see the COPYING file for licensing [LGPL]
2 package org.xwt.util;
3 import org.xwt.js.*;
4 import java.io.*;
5 import java.util.*;
6
7 /** easy to use logger */
8 public class Log {
9
10     public static boolean on = true;
11     public static boolean verbose = false;
12     public static boolean logDates = false;
13     public static Date lastDate = null;
14
15     /** true iff nothing has yet been logged */
16     public static boolean firstMessage = true;
17
18     /** log a message with the current JavaScript sourceName/line */
19     public static void logJS(Object o, Object message) { logJS(message); }
20     public static void logJS(Object message) {
21         JS.Thread current = org.xwt.js.JS.Thread.fromJavaThread(Thread.currentThread());
22         if (current == null) {
23             log("<none>", message);
24         } else {
25             log(current.getSourceName() + ":" + current.getLine(), message);
26         }
27     }
28
29     /** message can be a String or a Throwable */
30     public static synchronized void log(Object o, Object message) {
31
32         if (firstMessage && !logDates) {
33             firstMessage = false;
34             System.err.println("===========================================================================");
35             log(Log.class, "Logging enabled at " + new java.util.Date());
36         }
37
38         String classname;
39         if (o instanceof Class) classname = ((Class)o).getName();
40         else if (o instanceof String) classname = (String)o;
41         else classname = o.getClass().getName();
42         
43         if (classname.indexOf('.') != -1) classname = classname.substring(classname.lastIndexOf('.') + 1);
44         if (classname.length() > (logDates ? 14 : 20)) classname = classname.substring(0, (logDates ? 14 : 20));
45         while (classname.length() < (logDates ? 14 : 20)) classname = " " + classname;
46         classname = classname + ": ";
47
48         if (logDates) {
49             Date d = new Date();
50             if (lastDate == null || d.getYear() != lastDate.getYear() || d.getMonth() != lastDate.getMonth() || d.getDay() != lastDate.getDay()) {
51                 String now = new java.text.SimpleDateFormat("EEE dd MMM yyyy").format(d);
52                 System.err.println();
53                 System.err.println("=== " + now + " ==========================================================");
54             }
55             java.text.DateFormat df = new java.text.SimpleDateFormat("[EEE HH:mm:ss] ");
56             classname = df.format(d) + classname;
57             lastDate = d;
58         }
59
60         if (!(message instanceof Throwable)) System.err.println(classname + message);
61         else {
62             ByteArrayOutputStream baos = new ByteArrayOutputStream();
63             ((Throwable)message).printStackTrace(new PrintStream(baos));
64             byte[] b = baos.toByteArray();
65             BufferedReader br = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(b)));
66             String s = null;
67             try {
68                 while((s = br.readLine()) != null) {
69                     System.err.print(classname);
70                     for(int i=0; i<s.length(); i++)
71                         System.err.print(s.charAt(i) == '\t' ? "    " : ("" + s.charAt(i)));
72                     System.err.println();
73                 }
74             } catch (Exception e) { }
75
76         }
77     }
78
79 }