X-Git-Url: http://git.megacz.com/?p=org.ibex.core.git;a=blobdiff_plain;f=src%2Forg%2Fibex%2Futil%2FLog.java;h=467a339195d3b059d31011bf6fc927149f12030b;hp=9d03e6ea39b50c77dfacb9041d65b8377e1b01a0;hb=76982fccac3c46ccc47621dd22dc0c96b6b9cd62;hpb=c28ed126fbc51fb5794bb367f51a8755dc20140e diff --git a/src/org/ibex/util/Log.java b/src/org/ibex/util/Log.java index 9d03e6e..467a339 100644 --- a/src/org/ibex/util/Log.java +++ b/src/org/ibex/util/Log.java @@ -14,15 +14,18 @@ import java.net.*; /** easy to use logger */ public class Log { - public static boolean on = true; - public static boolean rpc = false; - public static boolean color = false; - public static boolean verbose = false; - public static boolean logDates = false; + public static boolean on = System.getProperty("ibex.log.on", "true").equals("true"); + public static boolean color = System.getProperty("ibex.log.color", "true").equals("true"); + public static boolean verbose = System.getProperty("ibex.log.verbose", "false").equals("true"); + public static boolean logDates = System.getProperty("ibex.log.dates", "false").equals("true"); + public static boolean notes = System.getProperty("ibex.log.notes.on", "true").equals("true"); + public static int maximumNoteLength = Integer.parseInt(System.getProperty("ibex.log.notes.maximumLength", (1024 * 32)+"")); + public static boolean rpc = false; public static Date lastDate = null; public static PrintStream logstream = System.err; + public static void flush() { logstream.flush(); } public static void email(String address) { throw new Error("FIXME not supported"); } public static void file(String filename) throws IOException { // FIXME security @@ -33,6 +36,36 @@ public class Log { logstream = new PrintStream(new Socket(InetAddress.getByName(host), port).getOutputStream()); } + private static Hashtable threadAnnotations = new Hashtable(); + public static void setThreadAnnotation(String s) { threadAnnotations.put(Thread.currentThread(), s); } + + /** + * Notes can be used to attach log messages to the current thread + * if you're not sure you want them in the log just yet. + * Originally designed for retroactively logging socket-level + * conversations only if an error is encountered + */ + public static void note(String s) { + if (!notes) return; + StringBuffer notebuf = notebuf(); + notebuf.append(s); + if (notebuf.length() > maximumNoteLength) { + notebuf.reverse(); + notebuf.setLength(maximumNoteLength * 3 / 4); + notebuf.reverse(); + } + } + public static void clearnotes() { if (!notes) return; notebuf().setLength(0); } + private static Hashtable notebufs = new Hashtable(); + private static StringBuffer notebuf() { + StringBuffer ret = (StringBuffer)notebufs.get(Thread.currentThread()); + if (ret == null) { + ret = new StringBuffer(16 * 1024); + notebufs.put(Thread.currentThread(), ret); + } + return ret; + } + /** true iff nothing has yet been logged */ public static boolean firstMessage = true; @@ -119,25 +152,38 @@ public class Log { lastDate = d; } + String annot = (String)threadAnnotations.get(Thread.currentThread()); + if (annot != null) classname += annot; if (message instanceof Throwable) { if (level < ERROR) level = WARN; ByteArrayOutputStream baos = new ByteArrayOutputStream(); ((Throwable)message).printStackTrace(new PrintStream(baos)); + if (notes && notebuf().length() > 0) { + PrintWriter pw = new PrintWriter(baos); + pw.println(); + pw.println("Thread notes:"); + pw.println(notebuf().toString()); + clearnotes(); + pw.flush(); + } byte[] b = baos.toByteArray(); BufferedReader br = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(b))); String s = null; try { String m = ""; while((s = br.readLine()) != null) m += s + "\n"; - log(o, m.substring(0, m.length() - 1), level); + if (m.length() > 0) log(o, m.substring(0, m.length() - 1), level); } catch (IOException e) { - logstream.println(colorize(RED, true, "Logger: exception thrown by ByteArrayInputStream -- this should not happen")); + // FEATURE: use org.ibex.io.Stream's here + logstream.println(colorize(RED, true, "Logger: exception thrown by ByteArrayInputStream; this should not happen")); } + lastClassName = ""; return; } String str = message.toString(); + if (str.indexOf('\n') != -1) lastClassName = ""; while(str.indexOf('\t') != -1) str = str.substring(0, str.indexOf('\t')) + " " + str.substring(str.indexOf('\t') + 1); @@ -162,7 +208,8 @@ public class Log { logstream.println(classname + colorize(levelcolor, bright, str)); } - public static void recursiveLog(String indent, String name, Object o) throws JSExn { + // FIXME: Update for new api + /*public static void recursiveLog(String indent, String name, Object o) throws JSExn { if (!name.equals("")) name += " : "; if (o == null) { @@ -189,6 +236,6 @@ public class Log { JS.log(indent + name + o); } - } + }*/ }