From b4cbe7980419f21ef890b407e6d7e6e8026ca71e Mon Sep 17 00:00:00 2001 From: adam Date: Wed, 11 Feb 2004 06:48:14 +0000 Subject: [PATCH] fixed bug 403, added logging to tcp and email darcs-hash:20040211064814-5007d-fa783bf5a3e19055d48711dac7f77e712f96e80b.gz --- src/org/ibex/Ibex.java | 1 + src/org/ibex/Main.java | 43 +++++++++++++++++++++++++------------- src/org/ibex/js/Interpreter.java | 4 +++- src/org/ibex/js/JS.java | 2 ++ src/org/ibex/plat/Win32.cc | 3 ++- src/org/ibex/util/Log.java | 36 ++++++++++++++++++++++--------- 6 files changed, 63 insertions(+), 26 deletions(-) diff --git a/src/org/ibex/Ibex.java b/src/org/ibex/Ibex.java index 3828b17..ed3cb9c 100644 --- a/src/org/ibex/Ibex.java +++ b/src/org/ibex/Ibex.java @@ -11,6 +11,7 @@ public final class Ibex extends JS.Cloneable { // FIXME remove this private final JS rr; + public Ibex(Stream rr) { this.rr = bless(rr); } public JS resolveString(String str, boolean permitAbsolute) throws JSExn { diff --git a/src/org/ibex/Main.java b/src/org/ibex/Main.java index 4a53a44..bfc2abd 100644 --- a/src/org/ibex/Main.java +++ b/src/org/ibex/Main.java @@ -3,6 +3,7 @@ package org.ibex; import java.net.*; import java.io.*; +import java.util.*; import org.ibex.js.*; import org.ibex.util.*; @@ -26,27 +27,41 @@ public class Main { public static Picture scarImage = null; public static void printUsage() { - System.err.println("Usage: ibex [-s] [-v] [-l /] source-location [initial-template]"); + System.err.println("Usage: ibex [-lawp] [ url | file | directory ]"); System.err.println(""); - System.err.println("Options:"); - System.err.println(" -v verbose logging (required for logging on Win32)"); - System.err.println(" -s [not yet supported]"); - System.err.println(" -l [not yet supported]"); - System.err.println(""); - System.err.println("Source-location is one of the following:"); - System.err.println(" - the path to an xwar file"); - System.err.println(" - the path to a directory to be used as an xwar"); - System.err.println(" - the http url of an xwar"); - System.err.println(""); - System.err.println("Initial-template is the path of the template to load; defaults to 'main'"); + System.err.println(" -l set logging level to { debug, info (default), warn, error, silent }"); + System.err.println(" -l rpc write log to a file on disk"); + System.err.println(" -l user@host email log to user@host"); + System.err.println(" -l host:port emit log to TCP socket"); + System.err.println(" -l write log to a file on disk"); + System.err.println(" -a check assertions"); + System.err.println(" -w reserved for libibex"); + System.err.println(" -p dump profiling information [not yet supported]"); Runtime.getRuntime().exit(-1); } - public static void main(String[] args) throws UnknownHostException, JSExn { + public static void main(String[] args) throws UnknownHostException, JSExn, IOException { int startargs = 0; while (true) { if (startargs > args.length - 1) printUsage(); - else if (args[startargs].equals("-v")) Log.verbose = true; + else if (args[startargs].equals("-a")) JS.checkAssertions = true; + else if (args[startargs].equals("-l")) { + startargs++; + StringTokenizer opts = new StringTokenizer(args[startargs], ","); + while(opts.hasMoreTokens()) { + String opt = opts.nextToken(); + if (opt.indexOf('@') != -1) Log.email(opt); + else if (opt.indexOf(':') != -1) + Log.tcp(opt.substring(0, opt.indexOf(':')), + Integer.parseInt(opt.substring(opt.indexOf(':') + 1))); + else if (opt.equals("debug")) Log.level = Log.DEBUG; + else if (opt.equals("info")) Log.level = Log.INFO; + else if (opt.equals("warn")) Log.level = Log.WARN; + else if (opt.equals("error")) Log.level = Log.ERROR; + else if (opt.equals("error")) Log.level = Log.SILENT; + else Log.file(opt); + } + } else break; startargs++; } diff --git a/src/org/ibex/js/Interpreter.java b/src/org/ibex/js/Interpreter.java index 07712aa..59b48c3 100644 --- a/src/org/ibex/js/Interpreter.java +++ b/src/org/ibex/js/Interpreter.java @@ -93,7 +93,9 @@ class Interpreter implements ByteCodes, Tokens { case DUP: stack.push(stack.peek()); break; case NEWSCOPE: scope = new JSScope(scope); break; case OLDSCOPE: scope = scope.getParentScope(); break; - case ASSERT: if (!JS.toBoolean(stack.pop())) throw je("ibex.assertion.failed" /*FEATURE: line number*/); break; + case ASSERT: + if (JS.checkAssertions && !JS.toBoolean(stack.pop())) + throw je("ibex.assertion.failed" /*FEATURE: line number*/); break; case BITNOT: stack.push(JS.N(~JS.toLong(stack.pop()))); break; case BANG: stack.push(JS.B(!JS.toBoolean(stack.pop()))); break; case NEWFUNCTION: stack.push(((JSFunction)arg)._cloneWithNewParentScope(scope)); break; diff --git a/src/org/ibex/js/JS.java b/src/org/ibex/js/JS.java index 378e125..9aa5606 100644 --- a/src/org/ibex/js/JS.java +++ b/src/org/ibex/js/JS.java @@ -9,6 +9,8 @@ import java.util.*; /** The minimum set of functionality required for objects which are manipulated by JavaScript */ public class JS extends org.ibex.util.BalancedTree { + public static boolean checkAssertions = false; + public static final Object METHOD = new Object(); public final JS unclone() { return _unclone(); } public Enumeration keys() throws JSExn { return entries == null ? emptyEnumeration : entries.keys(); } diff --git a/src/org/ibex/plat/Win32.cc b/src/org/ibex/plat/Win32.cc index a4d10fa..9834ce2 100644 --- a/src/org/ibex/plat/Win32.cc +++ b/src/org/ibex/plat/Win32.cc @@ -147,7 +147,8 @@ static unsigned char hand_cursor_and[32 * 4] = { }; void org::ibex::plat::Win32::natPreInit() { - if (org::ibex::util::Log::verbose) { + // Win32 throws stderr in the trash unless you designate your binary as a "console binary" + if (org::ibex::Log::logstream == java::lang::System::err) { AllocConsole(); freopen("CONOUT$", "w+t", stderr); } diff --git a/src/org/ibex/util/Log.java b/src/org/ibex/util/Log.java index d031dc7..1766be9 100644 --- a/src/org/ibex/util/Log.java +++ b/src/org/ibex/util/Log.java @@ -9,6 +9,7 @@ package org.ibex.util; import org.ibex.js.*; import java.io.*; import java.util.*; +import java.net.*; /** easy to use logger */ public class Log { @@ -19,6 +20,18 @@ public class Log { public static boolean logDates = false; public static Date lastDate = null; + public static PrintStream logstream = System.err; + + public static void email(String address) { throw new Error("FIXME not supported"); } + public static void file(String filename) throws IOException { + // FIXME security + logstream = new PrintStream(new FileOutputStream(filename)); + } + public static void tcp(String host, int port) throws IOException { + // FIXME security + logstream = new PrintStream(new Socket(InetAddress.getByName(host), port).getOutputStream()); + } + /** true iff nothing has yet been logged */ public static boolean firstMessage = true; @@ -35,10 +48,12 @@ public class Log { 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; + public static final int DEBUG = 0; + public static final int INFO = 1; + public static final int WARN = 2; + public static final int ERROR = 3; + public static final int SILENT = Integer.MAX_VALUE; + public static int level = INFO; private static final int BLUE = 34; private static final int GREEN = 32; @@ -58,9 +73,10 @@ public class Log { private static String lastClassName = null; private static synchronized void log(Object o, Object message, int level) { + if (level > Log.level) return; if (firstMessage && !logDates) { firstMessage = false; - System.err.println(colorize(GREEN, false, "===========================================================================")); + logstream.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") + @@ -91,8 +107,8 @@ public class Log { 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(colorize(GRAY, false, "=== " + now + " ==========================================================")); + logstream.println(); + logstream.println(colorize(GRAY, false, "=== " + now + " ==========================================================")); } java.text.DateFormat df = new java.text.SimpleDateFormat("[EEE HH:mm:ss] "); classname = df.format(d) + classname; @@ -112,7 +128,7 @@ public class Log { while((s = br.readLine()) != null) m += s + "\n"; log(o, m.substring(0, m.length() - 1), level); } catch (IOException e) { - System.err.println(colorize(RED, true, "Logger: exception thrown by ByteArrayInputStream -- this should not happen")); + logstream.println(colorize(RED, true, "Logger: exception thrown by ByteArrayInputStream -- this should not happen")); } return; } @@ -134,12 +150,12 @@ public class Log { } while(str.indexOf('\n') != -1) { - System.err.println(classname + colorize(levelcolor, bright, str.substring(0, str.indexOf('\n')))); + logstream.println(classname + colorize(levelcolor, bright, str.substring(0, str.indexOf('\n')))); classname = logDates ? " " : " "; classname = colorize(GRAY,false,classname); str = str.substring(str.indexOf('\n') + 1); } - System.err.println(classname + colorize(levelcolor, bright, str)); + logstream.println(classname + colorize(levelcolor, bright, str)); } public static void recursiveLog(String indent, String name, Object o) throws JSExn { -- 1.7.10.4