2002/05/28 18:30:30
[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 java.io.*;
4 import java.util.*;
5
6 /** easy to use logger */
7 public class Log {
8
9     public static boolean on = true;
10     public static boolean verbose = false;
11     public static boolean logDates = false;
12     public static Date lastDate = null;
13
14     /** true iff nothing has yet been logged */
15     public static boolean firstMessage = true;
16
17     /** message can be a String or a Throwable */
18     public static synchronized void log(Object o, Object message) {
19
20         if (firstMessage && !logDates) {
21             firstMessage = false;
22             System.err.println("===========================================================================");
23             log(Log.class, "Logging enabled at " + new java.util.Date());
24         }
25
26         String classname;
27         if (o instanceof Class) classname = ((Class)o).getName();
28         else if (o instanceof String) classname = (String)o;
29         else classname = o.getClass().getName();
30         
31         if (classname.indexOf('.') != -1) classname = classname.substring(classname.lastIndexOf('.') + 1);
32         if (classname.length() > (logDates ? 14 : 20)) classname = classname.substring(0, (logDates ? 14 : 20));
33         while (classname.length() < (logDates ? 14 : 20)) classname = " " + classname;
34         classname = classname + ": ";
35
36         if (logDates) {
37             Date d = new Date();
38             if (lastDate == null || d.getYear() != lastDate.getYear() || d.getMonth() != lastDate.getMonth() || d.getDay() != lastDate.getDay()) {
39                 String now = new java.text.SimpleDateFormat("EEE dd MMM yyyy").format(d);
40                 System.err.println();
41                 System.err.println("=== " + now + " ==========================================================");
42             }
43             java.text.DateFormat df = new java.text.SimpleDateFormat("[EEE HH:mm:ss] ");
44             classname = df.format(d) + classname;
45             lastDate = d;
46         }
47
48         if (!(message instanceof Throwable)) System.err.println(classname + message);
49         else {
50             ByteArrayOutputStream baos = new ByteArrayOutputStream();
51             ((Throwable)message).printStackTrace(new PrintStream(baos));
52             byte[] b = baos.toByteArray();
53             BufferedReader br = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(b)));
54             String s = null;
55             try {
56                 while((s = br.readLine()) != null) {
57                     System.err.print(classname);
58                     for(int i=0; i<s.length(); i++)
59                         System.err.print(s.charAt(i) == '\t' ? "    " : ("" + s.charAt(i)));
60                     System.err.println();
61                 }
62             } catch (Exception e) { }
63
64         }
65     }
66
67 }