1 // Copyright (C) 2003 Adam Megacz <adam@ibex.org> all rights reserved.
3 // You may modify, copy, and redistribute this code under the terms of
4 // the GNU Library Public License version 2.1, with the exception of
5 // the portion of clause 6a after the semicolon (aka the "obnoxious
13 /** easy to use logger */
16 public static boolean on = System.getProperty("ibex.log.on", "true").equals("true");
17 public static boolean color = System.getProperty("ibex.log.color", "true").equals("true");
18 public static boolean verbose = System.getProperty("ibex.log.verbose", "false").equals("true");
19 public static boolean logDates = System.getProperty("ibex.log.dates", "false").equals("true");
20 public static boolean notes = System.getProperty("ibex.log.notes.on", "true").equals("true");
21 public static boolean stackTraces = System.getProperty("ibex.log.stackTraces", "true").equals("true");
22 public static int maximumNoteLength = Integer.parseInt(System.getProperty("ibex.log.notes.maximumLength", (1024 * 32)+""));
23 public static boolean rpc = false;
24 public static Date lastDate = null;
26 public static PrintStream logstream = System.err;
28 public static void flush() { logstream.flush(); }
29 public static void email(String address) { throw new Error("FIXME not supported"); }
30 public static void file(String filename) throws IOException {
32 logstream = new PrintStream(new FileOutputStream(filename));
34 public static void tcp(String host, int port) throws IOException {
36 logstream = new PrintStream(new Socket(InetAddress.getByName(host), port).getOutputStream());
39 private static Hashtable threadAnnotations = new Hashtable();
40 public static void setThreadAnnotation(String s) { threadAnnotations.put(Thread.currentThread(), s); }
43 * Notes can be used to attach log messages to the current thread
44 * if you're not sure you want them in the log just yet.
45 * Originally designed for retroactively logging socket-level
46 * conversations only if an error is encountered
48 public static void note(String s) {
50 StringBuffer notebuf = notebuf();
52 if (notebuf.length() > maximumNoteLength) {
54 notebuf.setLength(maximumNoteLength * 3 / 4);
58 public static void clearnotes() { if (!notes) return; notebuf().setLength(0); }
59 private static Hashtable notebufs = new Hashtable();
60 public static StringBuffer notebuf() {
61 StringBuffer ret = (StringBuffer)notebufs.get(Thread.currentThread());
63 ret = new StringBuffer(16 * 1024);
64 notebufs.put(Thread.currentThread(), ret);
69 /** true iff nothing has yet been logged */
70 public static boolean firstMessage = true;
72 /** message can be a String or a Throwable */
73 public static synchronized void echo(Object o, Object message) { log(o, message, ECHO); }
74 public static synchronized void diag(Object o, Object message) { log(o, message, DIAGNOSTIC); }
75 public static synchronized void debug(Object o, Object message) { log(o, message, DEBUG); }
76 public static synchronized void info(Object o, Object message) { log(o, message, INFO); }
77 public static synchronized void warn(Object o, Object message) { log(o, message, WARN); }
78 public static synchronized void error(Object o, Object message) { log(o, message, ERROR); }
80 // these two logging levels serve ONLY to change the color; semantically they are the same as DEBUG
81 private static final int DIAGNOSTIC = -2;
82 private static final int ECHO = -1;
84 // the usual log4j levels, minus FAIL (we just throw an Error in that case)
85 public static final int DEBUG = 0;
86 public static final int INFO = 1;
87 public static final int WARN = 2;
88 public static final int ERROR = 3;
89 public static final int SILENT = Integer.MAX_VALUE;
90 public static int level = INFO;
92 private static final int BLUE = 34;
93 private static final int GREEN = 32;
94 private static final int CYAN = 36;
95 private static final int RED = 31;
96 private static final int PURPLE = 35;
97 private static final int BROWN = 33;
98 private static final int GRAY = 37;
100 private static String colorize(int color, boolean bright, String s) {
101 if (!Log.color) return s;
103 "\033[40;" + (bright?"1;":"") + color + "m" +
108 private static String lastClassName = null;
109 private static synchronized void log(Object o, Object message, int level) {
110 if (level < Log.level) return;
111 if (firstMessage && !logDates) {
112 firstMessage = false;
113 logstream.println(colorize(GREEN, false, "==========================================================================="));
115 // FIXME later: causes problems with method pruning
116 //diag(Log.class, "Logging enabled at " + new java.util.Date());
118 if (color) diag(Log.class, "logging messages in " +
119 colorize(BLUE, true, "c") +
120 colorize(RED, true, "o") +
121 colorize(CYAN, true, "l") +
122 colorize(GREEN, true, "o") +
123 colorize(PURPLE, true, "r"));
127 if (o instanceof Class) {
128 classname = ((Class)o).getName();
129 if (classname.indexOf('.') != -1) classname = classname.substring(classname.lastIndexOf('.') + 1);
131 else if (o instanceof String) classname = (String)o;
132 else classname = o.getClass().getName();
134 if (classname.equals(lastClassName)) classname = "";
135 else lastClassName = classname;
137 if (classname.length() > (logDates ? 14 : 20)) classname = classname.substring(0, (logDates ? 14 : 20));
138 while (classname.length() < (logDates ? 14 : 20)) classname = " " + classname;
139 classname = classname + (classname.trim().length() == 0 ? " " : ": ");
140 classname = colorize(GRAY, true, classname);
141 classname = classname.replace('$', '.');
145 if (lastDate == null || d.getYear() != lastDate.getYear() || d.getMonth() != lastDate.getMonth() || d.getDay() != lastDate.getDay()) {
146 String now = new java.text.SimpleDateFormat("EEE dd MMM yyyy").format(d);
148 logstream.println(colorize(GRAY, false, "=== " + now + " =========================================================="));
150 java.text.DateFormat df = new java.text.SimpleDateFormat("[EEE HH:mm:ss] ");
151 classname = df.format(d) + classname;
155 String annot = (String)threadAnnotations.get(Thread.currentThread());
156 if (annot != null) classname += annot;
158 if (message instanceof Throwable) {
159 if (level < ERROR) level = WARN;
160 ByteArrayOutputStream baos = new ByteArrayOutputStream();
161 ((Throwable)message).printStackTrace(new PrintStream(baos));
162 if (notes && notebuf().length() > 0) {
163 PrintWriter pw = new PrintWriter(baos);
165 pw.println("Thread notes:");
166 pw.println(notebuf().toString());
170 byte[] b = baos.toByteArray();
171 BufferedReader br = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(b)));
176 while((s = br.readLine()) != null) m += s + "\n";
177 if (m.length() > 0) log(o, m.substring(0, m.length() - 1), level);
179 String m = br.readLine();
182 String s = br.readLine();
183 if (s == null) break;
184 if (s.indexOf('(') != -1) {
185 String shortened = s.substring(s.indexOf('(')+1);
186 shortened = shortened.substring(0, shortened.indexOf(')'));
187 m += " " + shortened;
188 if (ok > 1) m = m.substring(0, 78);
191 } while (m.length() < 78);
195 } catch (IOException e) {
196 // FEATURE: use org.ibex.io.Stream's here
197 logstream.println(colorize(RED, true, "Logger: exception thrown by ByteArrayInputStream;" +
198 " this should not happen"));
203 String str = message.toString();
204 if (str.indexOf('\n') != -1) lastClassName = "";
205 while(str.indexOf('\t') != -1)
206 str = str.substring(0, str.indexOf('\t')) + " " + str.substring(str.indexOf('\t') + 1);
208 classname = colorize(GRAY, false, classname);
209 int levelcolor = GRAY;
210 boolean bright = true;
212 case DIAGNOSTIC: levelcolor = GREEN; bright = false; break;
213 case ECHO: levelcolor = BLUE; bright = true; break;
214 case DEBUG: levelcolor = BROWN; bright = true; break;
215 case INFO: levelcolor = GRAY; bright = false; break;
216 case WARN: levelcolor = BROWN; bright = false; break;
217 case ERROR: levelcolor = RED; bright = true; break;
220 while(str.indexOf('\n') != -1) {
221 logstream.println(classname + colorize(levelcolor, bright, str.substring(0, str.indexOf('\n'))));
222 classname = logDates ? " " : " ";
223 classname = colorize(GRAY,false,classname);
224 str = str.substring(str.indexOf('\n') + 1);
226 logstream.println(classname + colorize(levelcolor, bright, str));