From e33203c725fe48229d5286d476f17e4d42c425d6 Mon Sep 17 00:00:00 2001 From: megacz Date: Fri, 30 Jan 2004 07:43:28 +0000 Subject: [PATCH] 2003/12/29 03:23:59 darcs-hash:20040130074328-2ba56-fbf781cbc627d89919f627cd6fd7bcb828ad1fc7.gz --- src/org/xwt/util/Log.java | 94 +++++++++++++++++++++++++++++++-------------- 1 file changed, 65 insertions(+), 29 deletions(-) diff --git a/src/org/xwt/util/Log.java b/src/org/xwt/util/Log.java index 1eb61ef..0554f25 100644 --- a/src/org/xwt/util/Log.java +++ b/src/org/xwt/util/Log.java @@ -14,6 +14,7 @@ import java.util.*; public class Log { public static boolean on = true; + public static boolean color = true; public static boolean verbose = false; public static boolean logDates = false; public static Date lastDate = null; @@ -25,52 +26,71 @@ public class Log { public static void logJS(Object o, Object message) { logJS(message); } public static void logJS(Object message) { log(JS.getSourceName() + ":" + JS.getLine(), message); } - public static BufferedReader loggedReader(Reader r) { - // FIXME - return new BufferedReader(r); - /* - new BufferedReader(new FilterReader(new InputStreamReader(is)) { - public int read() throws IOException { - int i = super.read(); - if (Log.on) Log.log(this, "recv: " + ((char)i)); - return i; - } - public int read(char[] c, int off, int len) throws IOException { - int ret = super.read(c, off, len); - if (ret == -1) return ret; - String s; - BufferedReader br2 = new BufferedReader(new StringReader(new String(c, off, ret))); - while ((s = br2.readLine()) != null) Log.log(this, "recv: " + s); - return ret; - } - }); - */ - } - /** message can be a String or a Throwable */ public static synchronized void log(Object o, Object message) { + log(o, message, INFO); + } + + // these two logging levels serve ONLY to change the color; semantically they are the same as DEBUG + private static final int DIAGNOSTIC = -2; + private static final int ECHO = -1; + + // the usual log4j levels, minus FAIL (we just throw an Error in that case) + private static final int DEBUG = 0; + private static final int INFO = 1; + private static final int WARN = 2; + private static final int ERROR = 3; + + private static final int BLACK = 30; + private static final int BLUE = 34; + private static final int GREEN = 32; + private static final int CYAN = 36; + private static final int RED = 31; + private static final int PURPLE = 35; + private static final int BROWN = 33; + private static final int GRAY = 37; + + private static String color(int color, boolean bright, String s) { + return + "" + ((char)27) + "[" + (bright?1:0) + ";" + color + "m" + + s + + ((char)27) + "[0m"; + } + + private static String lastClassName = null; + private static synchronized void log(Object o, Object message, int level) { if (firstMessage && !logDates) { firstMessage = false; System.err.println("==========================================================================="); - log(Log.class, "Logging enabled at " + new java.util.Date()); + String incolor = color ? "in " + + color(BLUE, true, "c") + + color(RED, true, "o") + + color(CYAN, true, "l") + + color(GREEN, true, "o") + + color(PURPLE, true, "r") + " " : ""; + log(Log.class, "Logging enabled at " + new java.util.Date() + " " + incolor); } String classname; if (o instanceof Class) classname = ((Class)o).getName(); else if (o instanceof String) classname = (String)o; else classname = o.getClass().getName(); + + if (classname.equals(lastClassName)) classname = ""; + else lastClassName = classname; if (classname.indexOf('.') != -1) classname = classname.substring(classname.lastIndexOf('.') + 1); if (classname.length() > (logDates ? 14 : 20)) classname = classname.substring(0, (logDates ? 14 : 20)); while (classname.length() < (logDates ? 14 : 20)) classname = " " + classname; - classname = classname + ": "; + classname = classname + (classname.trim().length() == 0 ? " " : ": "); + classname = color(GRAY, true, 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 + " =========================================================="); + System.err.println(color(GRAY, false, "=== " + now + " ==========================================================")); } java.text.DateFormat df = new java.text.SimpleDateFormat("[EEE HH:mm:ss] "); classname = df.format(d) + classname; @@ -79,15 +99,18 @@ public class Log { if (message instanceof Throwable) { + if (level < ERROR) level = WARN; ByteArrayOutputStream baos = new ByteArrayOutputStream(); ((Throwable)message).printStackTrace(new PrintStream(baos)); byte[] b = baos.toByteArray(); BufferedReader br = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(b))); String s = null; try { - while((s = br.readLine()) != null) log(o, s); + String m = ""; + while((s = br.readLine()) != null) m += s + "\n"; + log(o, m, level); } catch (IOException e) { - System.err.println("Logger: exception thrown by ByteArrayInputStream -- this should not happen"); + System.err.println(color(RED, true, "Logger: exception thrown by ByteArrayInputStream -- this should not happen")); } return; } @@ -96,11 +119,24 @@ public class Log { while(str.indexOf('\t') != -1) str = str.substring(0, str.indexOf('\t')) + " " + str.substring(str.indexOf('\t') + 1); + classname = color(GRAY, false, classname); + int levelcolor = GRAY; + boolean bright = true; + switch (level) { + case DIAGNOSTIC: levelcolor = GREEN; bright = false; break; + case ECHO: levelcolor = BLUE; bright = true; break; + case DEBUG: levelcolor = BLACK; bright = true; break; + case INFO: levelcolor = GRAY; bright = false; break; + case WARN: levelcolor = BROWN; bright = false; break; + case ERROR: levelcolor = RED; bright = true; break; + } + while(str.indexOf('\n') != -1) { - System.err.println(classname + str.substring(0, str.indexOf('\n'))); + System.err.println(classname + color(levelcolor, bright, str.substring(0, str.indexOf('\n')))); + classname = logDates ? " " : " "; str = str.substring(str.indexOf('\n') + 1); } - System.err.println(classname + str); + System.err.println(classname + color(levelcolor, bright, str)); } public static void recursiveLog(String indent, String name, Object o) throws JSExn { -- 1.7.10.4