From 26de9c334dbd26cba03d47205d4ffbe9b24942af Mon Sep 17 00:00:00 2001 From: Eric Scharff Date: Thu, 27 Sep 2007 12:42:06 -0600 Subject: [PATCH 1/5] Add a default logging handler, in case the user doesn't provide any. --- classpath/java/util/logging/Logger.java | 61 ++++++++++++++++++++++++- test/Logging.java | 12 +++-- 2 files changed, 67 insertions(+), 6 deletions(-) diff --git a/classpath/java/util/logging/Logger.java b/classpath/java/util/logging/Logger.java index e0635ee520..76295efaae 100644 --- a/classpath/java/util/logging/Logger.java +++ b/classpath/java/util/logging/Logger.java @@ -6,7 +6,11 @@ import java.util.List; public class Logger { private final String name; - private static final ArrayList handlers = new ArrayList(); + private static final ArrayList handlers; + static { + handlers = new ArrayList(); + handlers.add(new DefaultHandler()); + } public static Logger getLogger(String name) { return new Logger(name); @@ -17,7 +21,7 @@ public class Logger { } public List getHandlers() { - return handlers; + return new ArrayList(handlers); } public void addHandler(Handler handler) { @@ -64,4 +68,57 @@ public class Logger { public void setLevel(Level level) { // Currently ignored } + + private static class DefaultHandler extends Handler { + private static final int NAME_WIDTH = 18; + private static final int METHOD_WIDTH = 20; + private static final int LEVEL_WIDTH = 8; + + public Object clone() { return this; } + public void close() { } + public void flush() { } + + private void maybeLogThrown(StringBuilder sb, Throwable t) { + if (t != null) { + sb.append("\nCaused by: "); + sb.append(t.getClass().getName()); + sb.append(": "); + sb.append(t.getMessage()); + sb.append('\n'); + + for (StackTraceElement elt : t.getStackTrace()) { + sb.append('\t'); + sb.append(elt.getClassName()); + sb.append('.'); + sb.append(elt.getMethodName()); + sb.append("(line"); + sb.append(':'); + sb.append(elt.getLineNumber()); + sb.append(')'); + sb.append('\n'); + } + maybeLogThrown(sb, t.getCause()); + } + } + + private void indent(StringBuilder sb, int amount) { + do { + sb.append(' '); + } while (--amount > 0); + } + + public void publish(LogRecord r) { + StringBuilder sb = new StringBuilder(); + sb.append(r.getLoggerName()); + indent(sb, NAME_WIDTH - r.getLoggerName().length()); + sb.append(r.getSourceMethodName()); + indent(sb, METHOD_WIDTH - r.getSourceMethodName().length()); + sb.append(r.getLevel().getName()); + indent(sb, LEVEL_WIDTH - r.getLevel().getName().length()); + sb.append(r.getMessage()); + maybeLogThrown(sb, r.getThrown()); + System.out.println(sb.toString()); + } + } + } diff --git a/test/Logging.java b/test/Logging.java index 6805e43f2b..c6b2073366 100644 --- a/test/Logging.java +++ b/test/Logging.java @@ -92,11 +92,15 @@ public class Logging { throw new Exception("Started here"); } + private static final boolean useCustomHandler = true; public static void main(String args[]) { - Logger root = Logger.getLogger(""); - root.addHandler(new MyHandler()); - for (Handler h : root.getHandlers()) root.removeHandler(h); - root.addHandler(new MyHandler()); + if (useCustomHandler) { + Logger root = Logger.getLogger(""); + root.addHandler(new MyHandler()); + for (Handler h : root.getHandlers()) root.removeHandler(h); + root.addHandler(new MyHandler()); + } + Logging me = new Logging(); me.run(); } From 9621679d2a3c8a63ac29167cc65c24f3872cd3a5 Mon Sep 17 00:00:00 2001 From: Eric Scharff Date: Thu, 27 Sep 2007 12:43:20 -0600 Subject: [PATCH 2/5] Cosmetic tweaks to the default logger --- classpath/java/util/logging/Logger.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/classpath/java/util/logging/Logger.java b/classpath/java/util/logging/Logger.java index 76295efaae..927be42175 100644 --- a/classpath/java/util/logging/Logger.java +++ b/classpath/java/util/logging/Logger.java @@ -70,8 +70,8 @@ public class Logger { } private static class DefaultHandler extends Handler { - private static final int NAME_WIDTH = 18; - private static final int METHOD_WIDTH = 20; + private static final int NAME_WIDTH = 14; + private static final int METHOD_WIDTH = 15; private static final int LEVEL_WIDTH = 8; public Object clone() { return this; } From 101b0c3b0eefaf89c445cbb93d1013ec465b72c8 Mon Sep 17 00:00:00 2001 From: Eric Scharff Date: Thu, 27 Sep 2007 15:05:55 -0600 Subject: [PATCH 3/5] Support printing of null references --- classpath/java/io/PrintStream.java | 2 +- classpath/java/lang/String.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/classpath/java/io/PrintStream.java b/classpath/java/io/PrintStream.java index 1e21d86ed3..37c141bb03 100644 --- a/classpath/java/io/PrintStream.java +++ b/classpath/java/io/PrintStream.java @@ -23,7 +23,7 @@ public class PrintStream extends OutputStream { } public void print(Object o) { - print(o.toString()); + print(String.valueOf(o)); } public void print(char c) { diff --git a/classpath/java/lang/String.java b/classpath/java/lang/String.java index 3c72ee92e3..283e4b7248 100644 --- a/classpath/java/lang/String.java +++ b/classpath/java/lang/String.java @@ -379,7 +379,7 @@ public final class String implements Comparable { public native String intern(); public static String valueOf(Object s) { - return s.toString(); + return s == null ? "null" : s.toString(); } public static String valueOf(boolean v) { From 2ae6aa7ddf694cac9c56ce99777d6ab529872f1d Mon Sep 17 00:00:00 2001 From: Eric Scharff Date: Thu, 27 Sep 2007 15:06:56 -0600 Subject: [PATCH 4/5] Fix System.arraycopy when the source and dest are the same --- classpath/java/util/ArrayList.java | 2 +- src/builtin.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/classpath/java/util/ArrayList.java b/classpath/java/util/ArrayList.java index fa0813805c..abb5ea28cc 100644 --- a/classpath/java/util/ArrayList.java +++ b/classpath/java/util/ArrayList.java @@ -70,7 +70,7 @@ public class ArrayList implements List { public void add(int index, T element) { size = Math.max(size+1, index+1); grow(); - System.arraycopy(array, index, array, index+1, size-index); + System.arraycopy(array, index, array, index+1, size-index-1); array[index] = element; } diff --git a/src/builtin.cpp b/src/builtin.cpp index 44209e5f4c..a9a84e8fdf 100644 --- a/src/builtin.cpp +++ b/src/builtin.cpp @@ -446,7 +446,7 @@ Java_java_lang_System_arraycopy { uint8_t* sbody = &cast(s, 2 * BytesPerWord); uint8_t* dbody = &cast(d, 2 * BytesPerWord); - if (src == dst) { + if (s == d) { memmove(dbody + (dstOffset * elementSize), sbody + (srcOffset * elementSize), length * elementSize); From 0efc4988373260de1065c7c81eb8e1e10c7d78c7 Mon Sep 17 00:00:00 2001 From: Eric Scharff Date: Thu, 27 Sep 2007 15:21:39 -0600 Subject: [PATCH 5/5] Another place where printing null should be permitted --- classpath/java/io/PrintStream.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classpath/java/io/PrintStream.java b/classpath/java/io/PrintStream.java index 37c141bb03..43cc6e3860 100644 --- a/classpath/java/io/PrintStream.java +++ b/classpath/java/io/PrintStream.java @@ -46,7 +46,7 @@ public class PrintStream extends OutputStream { } public void println(Object o) { - println(o.toString()); + println(String.valueOf(o)); } public void println(char c) {