X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;ds=sidebyside;f=src%2Forg%2Fxwt%2Futil%2FLog.java;h=ad266a0facf89b1e5e74929bd545210974d6ea6d;hb=16c24a73c1c1b2955db0bbbaf5a940215329bca1;hp=2e6d09e88941130839d323a06cf0a0a35e4a62e7;hpb=a31ea439cbb4e49894d8384c6c69e5fe8fae5619;p=org.ibex.core.git diff --git a/src/org/xwt/util/Log.java b/src/org/xwt/util/Log.java index 2e6d09e..ad266a0 100644 --- a/src/org/xwt/util/Log.java +++ b/src/org/xwt/util/Log.java @@ -1,4 +1,10 @@ -// Copyright 2002 Adam Megacz, see the COPYING file for licensing [LGPL] +// Copyright (C) 2003 Adam Megacz all rights reserved. +// +// You may modify, copy, and redistribute this code under the terms of +// the GNU Library Public License version 2.1, with the exception of +// the portion of clause 6a after the semicolon (aka the "obnoxious +// relink clause") + package org.xwt.util; import org.xwt.js.*; import java.io.*; @@ -8,6 +14,7 @@ import java.util.*; public class Log { public static boolean on = true; + public static boolean color = false; public static boolean verbose = false; public static boolean logDates = false; public static Date lastDate = null; @@ -15,62 +22,150 @@ public class Log { /** true iff nothing has yet been logged */ public static boolean firstMessage = true; - /** log a message with the current JavaScript sourceName/line */ - public static void logJS(Object o, Object message) { logJS(message); } - public static void logJS(Object message) { - JS.Thread current = org.xwt.js.JS.Thread.fromJavaThread(Thread.currentThread()); - if (current == null) { - log("", message); - } else { - log(current.getSourceName() + ":" + current.getLine(), message); - } + /** message can be a String or a Throwable */ + public static synchronized void echo(Object o, Object message) { log(o, message, ECHO); } + public static synchronized void diag(Object o, Object message) { log(o, message, DIAGNOSTIC); } + public static synchronized void debug(Object o, Object message) { log(o, message, DEBUG); } + public static synchronized void info(Object o, Object message) { log(o, message, INFO); } + public static synchronized void warn(Object o, Object message) { log(o, message, WARN); } + public static synchronized void error(Object o, Object message) { log(o, message, ERROR); } + + // 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 colorize(int color, boolean bright, String s) { + if (!Log.color) return s; + return + "\033[40;" + (bright?"1;":"") + color + "m" + + s + + "\033[0m"; } - /** message can be a String or a Throwable */ - public static synchronized void log(Object o, Object message) { + 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()); + System.err.println(colorize(GREEN, false, "===========================================================================")); + diag(Log.class, "Logging enabled at " + new java.util.Date()); + if (color) diag(Log.class, "logging messages in " + + colorize(BLUE, true, "c") + + colorize(RED, true, "o") + + colorize(CYAN, true, "l") + + colorize(GREEN, true, "o") + + colorize(PURPLE, true, "r")); } 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 = colorize(GRAY, true, classname); + classname = classname.replace('$', '.'); 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(colorize(GRAY, false, "=== " + now + " ==========================================================")); } java.text.DateFormat df = new java.text.SimpleDateFormat("[EEE HH:mm:ss] "); classname = df.format(d) + classname; lastDate = d; } - if (!(message instanceof Throwable)) System.err.println(classname + message); - else { + + 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) { - System.err.print(classname); - for(int i=0; i"); + + } else if (o instanceof JSArray) { + JS.log(indent + name + ""); + JSArray na = (JSArray)o; + for(int i=0; i"); + JS s = (JS)o; + Enumeration e = s.keys(); + while(e.hasMoreElements()) { + Object key = e.nextElement(); + if (key != null) + recursiveLog(indent + " ", key.toString(), + (key instanceof Integer) ? + s.get(((Integer)key)) : s.get(key.toString())); + } + } else { + JS.log(indent + name + o); } }