Skip to content
Browse files

Replaced a zillion System.err.println() calls with proper logging usi…

…ng java.util.logging.Logger. The logging configuration must be provided by the user or reside on the classpath.
  • Loading branch information...
1 parent aa2446e commit 2947714291bccb89cd15aeac599c39f2107a4bfa @jetztgradnet jetztgradnet committed Jun 16, 2011
Showing with 607 additions and 650 deletions.
  1. +1 −1 src/main/java/erjang/ECons.java
  2. +2 −2 src/main/java/erjang/EHandle.java
  3. +19 −25 src/main/java/erjang/EModuleLoader.java
  4. +3 −0 src/main/java/erjang/ENative.java
  5. +4 −3 src/main/java/erjang/EPID.java
  6. +18 −18 src/main/java/erjang/EPeer.java
  7. +7 −11 src/main/java/erjang/EProc.java
  8. +29 −25 src/main/java/erjang/ERT.java
  9. +1 −1 src/main/java/erjang/ESeq.java
  10. +8 −12 src/main/java/erjang/ETask.java
  11. +0 −4 src/main/java/erjang/Erj.java
  12. +8 −14 src/main/java/erjang/ErjangCodeCache.java
  13. +5 −1 src/main/java/erjang/ErjangConfig.java
  14. +2 −2 src/main/java/erjang/Main.java
  15. +3 −2 src/main/java/erjang/beam/BIFUtil.java
  16. +7 −5 src/main/java/erjang/beam/CompilerVisitor.java
  17. +7 −5 src/main/java/erjang/beam/ErlangBeamDisLoader.java
  18. +25 −23 src/main/java/erjang/beam/ModuleAnalyzer.java
  19. +14 −11 src/main/java/erjang/beam/analysis/BeamTypeAnalysis.java
  20. +3 −1 src/main/java/erjang/beam/analysis/TypeMap.java
  21. +7 −5 src/main/java/erjang/beam/interpreter/AbstractInterpreter.java
  22. +27 −17 src/main/java/erjang/beam/interpreter/Interpreter.template
  23. +52 −54 src/main/java/erjang/beam/loader/BeamLoader.java
  24. +6 −2 src/main/java/erjang/beam/repr/ModuleRepr.java
  25. +95 −3 src/main/java/erjang/driver/EDriverInstance.java
  26. +10 −10 src/main/java/erjang/driver/EDriverTask.java
  27. +0 −2 src/main/java/erjang/driver/FDDriverInstance.java
  28. +3 −2 src/main/java/erjang/driver/IO.java
  29. +8 −9 src/main/java/erjang/driver/NIOSelector.java
  30. +0 −2 src/main/java/erjang/driver/efile/ClassPathResource.java
  31. +29 −25 src/main/java/erjang/driver/efile/EFile.java
  32. +13 −86 src/main/java/erjang/driver/inet_gethost/GetHostDriver.java
  33. +3 −1 src/main/java/erjang/driver/ram_file/RamFile.java
  34. +82 −173 src/main/java/erjang/driver/tcp_inet/TCPINet.java
  35. +5 −2 src/main/java/erjang/epmd/EPMDConnection.java
  36. +31 −6 src/main/java/erjang/epmd/EPMDServer.java
  37. +0 −31 src/main/java/erjang/epmd/Functions.java
  38. +14 −9 src/main/java/erjang/epmd/PacketConnection.java
  39. +13 −8 src/main/java/erjang/epmd/PacketServer.java
  40. +3 −2 src/main/java/erjang/m/crypto/Native.java
  41. +3 −1 src/main/java/erjang/m/erl_ddll/Native.java
  42. +2 −2 src/main/java/erjang/m/erlang/ErlBif.java
  43. +4 −1 src/main/java/erjang/m/erlang/ErlDist.java
  44. +20 −19 src/main/java/erjang/m/erlang/ErlPort.java
  45. +2 −2 src/main/java/erjang/m/erlang/ErlProc.java
  46. +3 −4 src/main/java/erjang/m/ets/ETable.java
  47. +6 −5 src/main/java/erjang/m/ets/Native.java
  48. +0 −1 src/main/java/erjang/util/Progress.java
View
2 src/main/java/erjang/ECons.java
@@ -194,7 +194,7 @@ public boolean collectIOList(List<ByteBuffer> out) {
// do nothing //
} else {
- System.err.println("head is "+head+"::"+head.getClass());
+ ERT.debug("head is "+head+"::"+head.getClass());
// not a well-formed iolist
return false;
View
4 src/main/java/erjang/EHandle.java
@@ -59,8 +59,8 @@ public int send(EHandle sender, EObject msg) throws Pausable {
task.mbox.put(msg);
return task.mbox.size();
} else {
- if (ERT.log.isLoggable(Level.FINE)) {
- ERT.log.fine("sending message to dead process/port ignored "+this+" ! "+msg);
+ if (ERT.ipclog.isLoggable(Level.FINE)) {
+ ERT.ipclog.fine("sending message to dead process/port ignored "+this+" ! "+msg);
}
return 0;
}
View
44 src/main/java/erjang/EModuleLoader.java
@@ -18,27 +18,21 @@
package erjang;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
import erjang.beam.BeamFileData;
import erjang.beam.BeamLoader;
-import erjang.beam.Compiler;
import erjang.beam.EUtil;
-
import erjang.beam.loader.ErjangBeamDisLoader;
import erjang.driver.efile.ClassPathResource;
-import erjang.driver.efile.EFile;
-
import erjang.util.Progress;
-import java.util.List;
-import java.util.ArrayList;
-
-import java.io.IOException;
-import java.io.FileNotFoundException;
-import java.io.File;
-import java.io.InputStream;
-
-import java.net.URL;
-
/** Handles the part of module loading that involves going from module name,
* or module name plus beam file name, to a BeamFileData representation;
* and chooses how to convert that into an EModule instance.
@@ -51,7 +45,7 @@
* - Module creation: From beam representation to executable module (EModule).
*/
class EModuleLoader {
- public static final boolean DEBUG_MODULE_LOAD = ErjangConfig.getBoolean("erjang.debug.load");
+ static final Logger log = Logger.getLogger("erjang.module.load");
final static BeamLoader beamParser = new ErjangBeamDisLoader();
@@ -87,20 +81,20 @@ public static EModule load_module(String moduleName, EBinary beamBin) throws IOE
after = System.currentTimeMillis();
loaded_module = load_compiled_module(moduleName, moduleClassLoader);
}
- if (DEBUG_MODULE_LOAD) {
+ if (log.isLoggable(Level.FINE)) {
long after_load = System.currentTimeMillis();
- System.err.print("[");
- System.err.print(moduleName);
- System.err.print(":");
- System.err.print(""+(after-before)+"ms");
- System.err.print(";"+(after_load-after)+"ms]");
if (use_interpreter)
acc_int_load += (after_load-after);
else
acc_load += (after_load-after);
- System.err.println("("+acc_load+")");
- Progress.done();
+ log.fine("["
+ + moduleName
+ + ":"
+ + ""+(after-before)+"ms"
+ + ";"+(after_load-after)+"ms]"
+ + "("+acc_load+")");
}
+ Progress.done();
return loaded_module;
}
@@ -146,8 +140,8 @@ private static void addLoadPaths(List<File> out, String path) {
@SuppressWarnings("unchecked")
public static EModule load_compiled_module(String mod, EModuleClassLoader loader) {
- if (DEBUG_MODULE_LOAD) {
- System.err.println("EML| load_compiled_module: "+mod+" @ "+loader);
+ if (log.isLoggable(Level.INFO)) {
+ log.info("EML| load_compiled_module: "+mod+" @ "+loader);
}
String internalName = erjang.beam.Compiler.moduleClassName(mod);
View
3 src/main/java/erjang/ENative.java
@@ -19,6 +19,8 @@
package erjang;
+import java.util.logging.Logger;
+
/**
* Base class for native code.
*
@@ -30,6 +32,7 @@
*
*/
public abstract class ENative {
+ protected static Logger log = Logger.getLogger("erjang.module");
/**
* if you want to put the BIFs in multiple class files, you can tell which
View
7 src/main/java/erjang/EPID.java
@@ -20,14 +20,15 @@
import java.io.IOException;
import java.util.Set;
+import java.util.logging.Logger;
import erjang.m.ets.EMatchContext;
import erjang.m.ets.EPattern;
import erjang.m.ets.ETermPattern;
public abstract class EPID extends EHandle {
-
+ static Logger log = Logger.getLogger("erjang.proc");
/**
*
*/
@@ -116,15 +117,15 @@ public static EPID make(EAtom node, int id, int serial, int creation) {
EPID res = EProc.find(id, serial);
if (res != null) return res;
- System.err.println("deadpid <"+id+"."+serial+"."+creation+">");
+ log.warning("deadpid <"+id+"."+serial+"."+creation+">");
// return DEADPID?
}
EAbstractNode peer = EPeer.get(node);
if (peer instanceof EPeer) {
return new EExternalPID((EPeer) peer, id, serial, creation);
} else {
- System.err.println("localnode="+ERT.getLocalNode().node+"; asking="+node);
+ log.fine("localnode="+ERT.getLocalNode().node+"; asking="+node);
// Presumably another node
// might be local with different name
return new EExternalPID(EPeer.get_or_create(node, creation, 0, 0), id, serial, creation);
View
36 src/main/java/erjang/EPeer.java
@@ -24,6 +24,8 @@
import java.util.LinkedList;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
+import java.util.logging.Level;
+import java.util.logging.Logger;
import erjang.driver.EDriverTask;
@@ -33,6 +35,7 @@
* This corresponds to a DistEntry in BEAM
*/
public class EPeer extends EAbstractNode {
+ static Logger log = Logger.getLogger("erjang.dist");
protected static final byte passThrough = (byte) 0x70;
protected static final byte distHeader = (byte) 131;
@@ -129,7 +132,7 @@ private void send_to_port(ByteBuffer[] ev, EHandle sender) throws Pausable {
close_and_finish(port);
}
} else {
- System.err.println("sending cast to dead task (port="+port+", this="+this+", sender="+sender+")");
+ log.warning("sending cast to dead task (port="+port+", this="+this+", sender="+sender+")");
}
}
@@ -165,7 +168,7 @@ public static EPeer get_or_create(EAtom node, int creation,
if (do_set_port) {
peer.setPort(port);
} else if (port != null) {
- System.err.println("Port already set for node (to "+peer.port+"); refraining from setting it again (to "+port+")");
+ log.warning("Port already set for node (to "+peer.port+"); refraining from setting it again (to "+port+")");
}
return peer;
@@ -200,9 +203,7 @@ public void net_message2(EInternalPort port, ByteBuffer hdr, ByteBuffer buf)
throws IOException, Pausable {
if (buf.remaining() == 0) {
- if (ERT.DEBUG_DIST) {
- System.err.println("received tick from " + this.node);
- }
+ log.fine("received tick from " + this.node);
return;
}
@@ -250,8 +251,8 @@ public void net_message2(EInternalPort port, ByteBuffer hdr, ByteBuffer buf)
break receive_loop;
}
- if (ERT.DEBUG_DIST) {
- System.err.println("received net_message " + tmp);
+ if (log.isLoggable(Level.FINE)) {
+ log.fine("received net_message " + tmp);
}
ESmall tag_sm;
@@ -291,8 +292,8 @@ public void net_message2(EInternalPort port, ByteBuffer hdr, ByteBuffer buf)
case SEND: { // {2, Cookie, ToPid}
EObject msg = ibuf.read_any();
- if (ERT.DEBUG_DIST)
- System.err.println(" payload: " + msg);
+ if (log.isLoggable(Level.FINE))
+ log.fine(" payload: " + msg);
EHandle dst = head.elm(3).testHandle();
if (dst == null)
throw new IOException("protocol error");
@@ -307,8 +308,8 @@ public void net_message2(EInternalPort port, ByteBuffer hdr, ByteBuffer buf)
// }
{
EObject msg = ibuf.read_any();
- if (ERT.DEBUG_DIST)
- System.err.println(" payload: " + msg);
+ if (log.isLoggable(Level.FINE))
+ log.fine(" payload: " + msg);
EAtom toname = head.elm(4).testAtom();
if (toname == null)
throw new IOException("protocol error");
@@ -413,8 +414,7 @@ private boolean process_distribution_header(EInternalPort port,
EInputStream ibuf) throws IOException {
EAtom[] atom_cache_refs;
- if (ERT.DEBUG_DIST)
- System.err.println("parsing distribuionHeader....");
+ log.fine("parsing distribuionHeader....");
int datum = ibuf.read1();
@@ -445,16 +445,16 @@ private boolean process_distribution_header(EInternalPort port,
boolean longAtoms = (flags[numberOfAtomCacheRefs] & 0x01) == 1;
if (longAtoms) {
- System.err.println("LONGATOMS!");
+ log.fine("LONGATOMS!");
}
for (int i = 0; i < numberOfAtomCacheRefs; i++) {
int segment_index = flags[i] & 7;
int index = ibuf.read1();
- if (ERT.DEBUG_DIST)
- System.err.print("cache[" + i + "] -> ref["
+ if (log.isLoggable(Level.FINE))
+ log.fine("cache[" + i + "] -> ref["
+ segment_index + "][" + index + "]");
if ((flags[i] & 8) == 8) {
@@ -477,8 +477,8 @@ private boolean process_distribution_header(EInternalPort port,
atom_cache_refs[i] = atom_cache[segment_index][index];
- if (ERT.DEBUG_DIST)
- System.err.println(" => " + atom_cache_refs[i]);
+ if (log.isLoggable(Level.FINE))
+ log.fine(" => " + atom_cache_refs[i]);
}
ibuf.setAtomCacheRefs(atom_cache_refs);
View
18 src/main/java/erjang/EProc.java
@@ -36,7 +36,7 @@
*/
public final class EProc extends ETask<EInternalPID> {
- static Logger log = Logger.getLogger(EProc.class.getName());
+ static Logger log = Logger.getLogger("erjang.proc");
public static final EObject TAIL_MARKER = new ETailMarker();
@@ -436,12 +436,9 @@ public void execute() throws Pausable {
}
}
- //System.out.println("proc "+this+" exited "+tmp);
-
result = am_normal;
} catch (NotImplemented e) {
- System.err.print("exiting "+self_handle()+" with: ");
log.log(Level.SEVERE, "[fail] exiting "+self_handle(), e);
result = e.reason();
death = e;
@@ -459,8 +456,7 @@ public void execute() throws Pausable {
} catch (Throwable e) {
- System.err.print("[java] exiting "+self_handle()+" with: ");
- e.printStackTrace();
+ log.log(Level.SEVERE, "[java] exiting "+self_handle()+" with: ", e);
ESeq erl_trace = ErlangError.decodeTrace(e.getStackTrace());
ETuple java_ex = ETuple.make(am_java_exception, EString
@@ -587,7 +583,7 @@ public EObject process_info(EObject spec) {
return new ETuple2(am_memory, ERT.box(50000));
} else {
- System.err.println("NotImplemented: process_info("+spec+")");
+ log.warning("NotImplemented: process_info("+spec+")");
throw new NotImplemented("process_info("+spec+")");
}
}
@@ -682,13 +678,13 @@ public static EInternalPID find(int id, int serial) {
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
- System.err.println("===== LIVE TASKS UPON EXIT");
+ log.warning("===== LIVE TASKS UPON EXIT");
for (EProc task : all_tasks.values()) {
- System.err.println("==" + task);
- System.err.println (task.fiber);
+ log.warning("==" + task);
+ log.warning(task.fiber.toString());
}
- System.err.println("=====");
+ log.warning("=====");
}
});
}
View
54 src/main/java/erjang/ERT.java
@@ -34,33 +34,33 @@
import kilim.Pausable;
import kilim.Task;
-import erjang.beam.Compiler;
import erjang.driver.Drivers;
import erjang.driver.EAsync;
import erjang.driver.EDriver;
import erjang.driver.EDriverTask;
import erjang.driver.efile.Posix;
-import erjang.m.erlang.ErlDist;
import erjang.m.java.JavaObject;
@Module(value = "erlang")
public class ERT {
- static Logger log = Logger.getLogger("erjang");
+ public static Logger log = Logger.getLogger("erjang");
+ static Logger ipclog = Logger.getLogger("erjang.ipc");
public static EAtom am_badsig = EAtom.intern("badsig");
public static EObject raise(EObject trace, EObject value) throws ErlangException {
- // System.err.println("raise "+trace);
+ log.warning("raise "+trace);
if (trace instanceof ErlangException.ExceptionAsObject) {
ErlangException etrace = ((ErlangException.ExceptionAsObject) trace).getException();
EAtom clazz = etrace.getExClass();
ESeq traz = etrace.getLazyTrace();
throw new ErlangRaise(clazz, value, traz);
} else if (trace==am_exit || trace==am_error || trace==am_throw) {
- System.err.println("Pre-R10-1 exception style is not supported.");
+ log.warning("Pre-R10-1 exception style is not supported.");
}
- new Throwable("bad argument to raise2: ("+value+", "+trace+")").printStackTrace(System.err);
+ Throwable error = new Throwable("bad argument to raise2: ("+value+", "+trace+")");
+ log.log(Level.WARNING, "bad argument to raise2: ("+value+", "+trace+")", error);
return am_badarg;
}
@@ -290,12 +290,6 @@ public static EAtom guard(boolean bool) {
public static final EAtom am_try_case_clause = EAtom
.intern("try_case_clause");
public static final EAtom am_if_clause = EAtom.intern("if_clause");
- public static final boolean DEBUG = false;
- public static final boolean DEBUG2 = false;
- public static final boolean DEBUG_WAIT = false;
- public static final boolean DEBUG_PORT = ErjangConfig.getBoolean("erjang.debug.port");
- public static final boolean DEBUG_INET = ErjangConfig.getBoolean("erjang.debug.inet");
- public static final boolean DEBUG_EFILE = false;
public static final EBinary EMPTY_BINARY = new EBinary(new byte[0]);
public static final ByteBuffer[] EMPTY_BYTEBUFFER_ARR = new ByteBuffer[0];
public static final ByteBuffer EMPTY_BYTEBUFFER = ByteBuffer.allocate(0);
@@ -311,8 +305,6 @@ public static EAtom guard(boolean bool) {
.intern("function_clause");
public static final EAtom am_ok = EAtom.intern("ok");
public static final EAtom am_noconnect = EAtom.intern("noconnect");
- public static final boolean DEVEL = true;
- public static final boolean DEBUG_DIST = false;
public static final EAtom am_latin1 = EAtom.intern("latin1");
public static final EAtom am_utf8 = EAtom.intern("utf8");
public static final EAtom am_unicode = EAtom.intern("unicode");
@@ -513,7 +505,7 @@ public static EObject send(EProc proc, EObject pid, EObject msg) throws Pausable
{
send_to_remote(proc, t, node_name, reg_name, msg, null);
} else { // PID was of a bad type.
- log.info("trying to send message to "+pid+" failed.");
+ ipclog.info("trying to send message to "+pid+" failed.");
throw badarg(pid, msg);
}
}
@@ -546,7 +538,7 @@ public static EObject send(EProc proc, final EObject pid, final EObject msg, EOb
{
return send_to_remote(proc, t, node_name, reg_name, msg, options);
} else { // PID was of a bad type.
- log.info("trying to send message to "+pid+" failed.");
+ ipclog.info("trying to send message to "+pid+" failed.");
throw badarg(pid, msg);
}
}
@@ -560,7 +552,9 @@ private static EObject send_to_remote(EProc proc, ETuple dest, EAtom node_name,
return am_ok; // Even if the process does not exist.
//TODO: Return 'noconnect' if options contain noconnect?...
} else { // We're talking to another node
- System.err.println("sending msg "+dest+" ! "+msg);
+ if (ipclog.isLoggable(Level.FINE)) {
+ ipclog.fine("sending msg "+dest+" ! "+msg);
+ }
EAbstractNode node = EPeer.get(node_name);
if (node == null) {
@@ -838,7 +832,7 @@ public static EObject loop_rec(EProc proc) {
int idx = proc.midx;
proc.in_receive = true;
EObject msg = proc.mbox.peek(idx);
- if (DEBUG_WAIT) System.err.println("WAIT| entered loop #"+idx+" message="+msg);
+ if (ipclog.isLoggable(Level.FINE)) ipclog.fine("WAIT| entered loop #"+idx+" message="+msg);
return msg;
}
@@ -860,11 +854,11 @@ public static boolean wait_timeout(EProc proc, EObject howlong)
try {
proc.check_exit();
- if (ERT.DEBUG_WAIT) System.err.println("WAIT| "+proc+" waits for messages for "+howlong+" ms");
+ if (ipclog.isLoggable(Level.FINE)) ipclog.fine("WAIT| "+proc+" waits for messages for "+howlong+" ms");
if (howlong == am_infinity) {
proc.mbox.untilHasMessages(proc.midx+1);
proc.check_exit();
- if (ERT.DEBUG_WAIT) System.err.println("WAIT| "+proc+" wakes up on message");
+ if (ipclog.isLoggable(Level.FINE)) ipclog.fine("WAIT| "+proc+" wakes up on message");
return true;
} else {
long now = System.currentTimeMillis();
@@ -889,11 +883,11 @@ public static boolean wait_timeout(EProc proc, EObject howlong)
} else {
- if (ERT.DEBUG_WAIT) System.err.println("WAIT| "+proc+" waiting for "+left+"ms for msg #"+(proc.midx+1));
+ if (ipclog.isLoggable(Level.FINE)) ipclog.fine("WAIT| "+proc+" waiting for "+left+"ms for msg #"+(proc.midx+1));
boolean res = proc.mbox
.untilHasMessages(proc.midx + 1, left);
proc.check_exit();
- if (ERT.DEBUG_WAIT) System.err.println("WAIT| "+proc+" wakes up "+(res?"on message" : "after timeout"));
+ if (ipclog.isLoggable(Level.FINE)) ipclog.fine("WAIT| "+proc+" wakes up "+(res?"on message" : "after timeout"));
return res;
}
@@ -907,17 +901,17 @@ public static boolean wait_timeout(EProc proc, EObject howlong)
public static void wait(EProc proc) throws Pausable {
try {
int idx = proc.midx + 1;
- if (DEBUG_WAIT) System.err.println("WAIT| "+proc+" waits for "+idx+" messages");
+ if (ipclog.isLoggable(Level.FINE)) ipclog.fine("WAIT| "+proc+" waits for "+idx+" messages");
proc.mbox.untilHasMessages(idx);
- if (DEBUG_WAIT) System.err.println("WAIT| "+proc+" wakes up after timeout; now has "+(idx));
+ if (ipclog.isLoggable(Level.FINE)) ipclog.fine("WAIT| "+proc+" wakes up after timeout; now has "+(idx));
} finally {
proc.in_receive = false;
}
}
/** message reception timed out, reset message index */
public static void timeout(EProc proc) {
- if (DEBUG_WAIT) System.err.println("WAIT| "+proc+" timed out");
+ if (ipclog.isLoggable(Level.FINE)) ipclog.fine("WAIT| "+proc+" timed out");
proc.midx = 0;
proc.timeout_start = 0L;
proc.in_receive = false;
@@ -1066,4 +1060,14 @@ public static File newFile(String file_name) {
return new File(Posix.getCWD(), file_name);
}
+ public static void debug(String text) {
+ log.fine(text);
+ }
+
+ public static void debug(boolean condition, String text) {
+ if (condition) {
+ debug(text);
+ }
+ }
+
}
View
2 src/main/java/erjang/ESeq.java
@@ -131,7 +131,7 @@ public EString testString() {
if (head == null) {
// BAD!
- System.err.println("null head in "+list.getClass());
+ ERT.log.warning("null head in "+list.getClass());
}
ESmall intval;
View
20 src/main/java/erjang/ETask.java
@@ -22,27 +22,23 @@
import java.util.HashMap;
import java.util.Map;
-import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.ConcurrentSkipListSet;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Level;
import java.util.logging.Logger;
-import com.trifork.clj_ds.IPersistentSet;
-import com.trifork.clj_ds.PersistentHashSet;
-
-import erjang.m.erlang.ErlProc;
-
import kilim.Mailbox;
import kilim.Pausable;
+import com.trifork.clj_ds.IPersistentSet;
+import com.trifork.clj_ds.PersistentHashSet;
+
/**
* An ETask is what is common for processes and open ports
*/
public abstract class ETask<H extends EHandle> extends kilim.Task {
- static Logger log = Logger.getLogger(ETask.class.getName());
+ static Logger log = Logger.getLogger("erjang.proc");
public static final EAtom am_normal = EAtom.intern("normal");
protected static final EAtom am_java_exception = EAtom
@@ -150,12 +146,12 @@ protected void do_proc_termination(EObject exit_reason) throws Pausable {
try {
handle.exit_signal(me, exit_reason, false);
} catch (Error e) {
- System.err.println("EXCEPTION IN EXIT HANDLER");
- e.printStackTrace();
+ log.severe("EXCEPTION IN EXIT HANDLER");
+ log.log(Level.FINE, "details: ", e);
throw e;
} catch (RuntimeException e) {
- System.err.println("EXCEPTION IN EXIT HANDLER");
- e.printStackTrace();
+ log.severe("EXCEPTION IN EXIT HANDLER");
+ log.log(Level.FINE, "details: ", e);
throw e;
}
}
View
4 src/main/java/erjang/Erj.java
@@ -18,10 +18,6 @@
package erjang;
-import java.io.File;
-import java.net.MalformedURLException;
-
-import erjang.beam.Compiler;
/**
*
View
22 src/main/java/erjang/ErjangCodeCache.java
@@ -18,30 +18,24 @@
package erjang;
-import java.net.URL;
-
-import java.io.IOException;
import java.io.File;
-import java.io.ByteArrayOutputStream;
-import java.io.ByteArrayInputStream;
-import java.io.InputStream;
import java.io.FileOutputStream;
-
-import java.util.Map;
-import java.util.HashMap;
+import java.io.IOException;
import java.util.Collections;
-import java.util.Queue;
+import java.util.HashMap;
import java.util.LinkedList;
-
+import java.util.Map;
+import java.util.Queue;
import java.util.jar.JarOutputStream;
+import java.util.logging.Logger;
import java.util.zip.ZipEntry;
-import erjang.beam.Compiler;
import erjang.beam.BeamLoader;
-import erjang.beam.JarClassRepo;
+import erjang.beam.Compiler;
import erjang.beam.RamClassRepo;
public class ErjangCodeCache {
+ static final Logger log = Logger.getLogger("erjang.beam.cache");
// Config:
static final String ERJ_CACHE_DIR;
static final boolean useAsyncPersisting;
@@ -203,7 +197,7 @@ static void persist(File file, RamClassRepo repo) {
jo.close();
tmpFile.renameTo(file);
} catch (IOException ioe) {
- System.err.println("Warning: Failed to store cached module in "+file);
+ log.warning("Warning: Failed to store cached module in "+file);
}
}
}
View
6 src/main/java/erjang/ErjangConfig.java
@@ -19,6 +19,8 @@
package erjang;
import java.util.Properties;
+import java.util.logging.Level;
+import java.util.logging.Logger;
import java.io.FileInputStream;
import java.io.IOException;
@@ -30,6 +32,7 @@
* Java property "erjang.configfile".
*/
public class ErjangConfig {
+ static final Logger log = Logger.getLogger("erjang.config");
static {
String configFileName = System.getProperty("erjang.configfile");
@@ -48,7 +51,8 @@
in.close();
}
} catch (IOException ioe) {
- System.err.println("Failed to load Erjang properties from "+configFileName+": "+ioe);
+ log.warning("Failed to load Erjang properties from "+configFileName+": "+ioe.getMessage());
+ log.log(Level.FINE, "details: ", ioe);
}
}
}
View
4 src/main/java/erjang/Main.java
@@ -82,7 +82,7 @@ private static String guess_erl_root() {
}
}
- System.err.println("Cannot find OTPROOT directory\n"
+ ERT.log.severe("Cannot find OTPROOT directory\n"
+ "Pass -root <dir>, or set environment variable.");
System.exit(-1);
@@ -161,7 +161,7 @@ public static void main(String[] args) throws Exception {
System.setProperty("erjang.path", erl_bootstrap_ebindir);
if (!(new File(erl_bootstrap_ebindir)).exists() && !erl_bootstrap_ebindir.startsWith(EFile.RESOURCE_PREFIX)) {
- System.err.println("No bootstrap classes at: "+erl_bootstrap_ebindir);
+ ERT.log.severe("No bootstrap classes at: "+erl_bootstrap_ebindir);
System.exit(1);
}
View
5 src/main/java/erjang/beam/BIFUtil.java
@@ -24,6 +24,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.logging.Level;
import org.objectweb.asm.Type;
@@ -291,8 +292,8 @@ private BuiltInFunction find_bif(Type[] parmTypes) {
for (Args a : args.generalize()) {
m = found.get(a);
if (m != null) {
- if (ERT.DEBUG2)
- System.err.println("missed opportunity erlang:"
+ if (ERT.log.isLoggable(Level.FINE))
+ ERT.log.fine("missed opportunity erlang:"
+ EAtom.intern(name) + "/" + parmTypes.length + " "
+ args + ", \n\tusing " + m);
View
12 src/main/java/erjang/beam/CompilerVisitor.java
@@ -33,6 +33,8 @@
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
+import java.util.logging.Level;
+import java.util.logging.Logger;
import kilim.Pausable;
import kilim.analysis.ClassInfo;
@@ -105,7 +107,7 @@
private final ClassVisitor cv;
private EAtom module_name;
private Type self_type;
-
+
private static final EObject ATOM_field_flags = EAtom.intern("field_flags");
private static final EObject ATOM_start = EAtom.intern("start");
@@ -606,8 +608,8 @@ public void visitEnd() {
EFun.ensure(arity);
if (is_exported) {
- if (ERT.DEBUG2)
- System.err.println("export " + module_name + ":"
+ if (ModuleAnalyzer.log.isLoggable(Level.FINE))
+ ModuleAnalyzer.log.fine("export " + module_name + ":"
+ fun_name + "/" + arity);
AnnotationVisitor an = fv.visitAnnotation(EXPORT_ANN_TYPE
.getDescriptor(), true);
@@ -3337,8 +3339,8 @@ private static void make_go2_method(ClassWriter cw, String outer_name,
int freevars, Type returnType, boolean isTailCall, boolean isPausable) {
if (isPausable) {
- if (ModuleAnalyzer.DEBUG_ANALYZE) {
- System.err.println
+ if (ModuleAnalyzer.log.isLoggable(Level.FINE)) {
+ ModuleAnalyzer.log.fine
("not generating go2 (pausable) for "+full_inner);
}
return;
View
12 src/main/java/erjang/beam/ErlangBeamDisLoader.java
@@ -20,6 +20,8 @@
import java.io.File;
import java.io.IOException;
+import java.util.logging.Level;
+import java.util.logging.Logger;
import com.ericsson.otp.erlang.OtpAuthException;
import com.ericsson.otp.erlang.OtpConnection;
@@ -35,6 +37,7 @@
import erjang.ETuple;
public class ErlangBeamDisLoader extends BeamLoader {
+ static Logger log = Logger.getLogger("erjang.beam");
/** For transition phase from external to internal beam file reader */
final boolean TRY_NATIVE_LOADER = true;
@@ -120,14 +123,13 @@ protected ETuple check(ETuple dis1, byte[] data) throws IOException {
return check(dis1, dis2);
}
protected ETuple check(ETuple dis1, ETuple dis2) {
- System.err.print("DB| loader-cmp: ");
+ log.fine("DB| loader-cmp: ");
boolean eq = false;
try {eq = dis1.equals(dis2);} catch (RuntimeException re) {
- System.err.println(re);
- re.printStackTrace(System.err);
+ log.log(Level.WARNING, "failed to compare tuples", re);
}
- if (eq) System.err.println("OK");
- else System.err.println("DIFF:\n"+dis1+"\nvs\n"+dis2);
+ if (eq) log.fine("OK");
+ else { if (log.isLoggable(Level.FINE)) { log.fine("DIFF:\n"+dis1+"\nvs\n"+dis2); }}
return dis1;
}
View
48 src/main/java/erjang/beam/ModuleAnalyzer.java
@@ -4,6 +4,8 @@
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
+import java.util.logging.Level;
+import java.util.logging.Logger;
import erjang.EAtom;
import erjang.EObject;
@@ -18,7 +20,7 @@
public class ModuleAnalyzer implements ModuleVisitor {
- public static final boolean DEBUG_ANALYZE = false;
+ static Logger log = Logger.getLogger("erjang.beam.analyze");
Map<Label, FunInfo> info = new HashMap<Label, FunInfo>();
static class FunInfo {
@@ -83,8 +85,8 @@ boolean propagate_one() {
if (!caller.is_pausable) {
effect = caller.is_pausable = true;
- if (DEBUG_ANALYZE) {
- System.err.println("propagate " +fun+ " -> " + caller);
+ if (log.isLoggable(Level.FINE)) {
+ log.fine("propagate " +fun+ " -> " + caller);
}
}
}
@@ -94,8 +96,8 @@ boolean propagate_one() {
if (!caller.call_is_pausable) {
effect = caller.call_is_pausable = true;
- if (DEBUG_ANALYZE) {
- System.err.println("propagate " +fun+ " -> " + caller);
+ if (log.isLoggable(Level.FINE)) {
+ log.fine("propagate " +fun+ " -> " + caller);
}
}
}
@@ -140,9 +142,9 @@ public void visitExport(EAtom fun, int arity, int entry) {
public void visitEnd() {
propagate();
- if (ERT.DEBUG2) {
+ if (log.isLoggable(Level.FINE)) {
for (Map.Entry<Label, FunInfo> e : info.entrySet()) {
- System.err.println(e.getValue());
+ log.fine(e.getValue().toString());
}
}
@@ -158,8 +160,8 @@ public void declareFunction(EAtom fun, int arity, int startLabel) {
public FunctionVisitor visitFunction(EAtom name, int arity,
final int startLabel) {
- if (DEBUG_ANALYZE) {
- System.err.println("== analyzing "+ModuleAnalyzer.this.name+":"+name+"/"+arity);
+ if (log.isLoggable(Level.FINE)) {
+ log.fine("== analyzing "+ModuleAnalyzer.this.name+":"+name+"/"+arity);
}
Label start = new Label(startLabel);
@@ -179,8 +181,8 @@ public void visitInsn(Insn insn) {
switch (op = insn.opcode()) {
case send: {
- if (DEBUG_ANALYZE && !self.is_pausable) {
- System.err.println("pausable: send");
+ if (log.isLoggable(Level.FINE) && !self.is_pausable) {
+ log.fine("pausable: send");
}
self.is_pausable = true;
@@ -243,8 +245,8 @@ public void visitInsn(Insn insn) {
}
case apply_last:
- if (DEBUG_ANALYZE && !self.call_is_pausable) {
- System.err.println("call_pausable: " + op);
+ if (log.isLoggable(Level.FINE) && !self.call_is_pausable) {
+ log.fine("call_pausable: " + op);
}
self.may_return_tail_marker = true;
@@ -267,8 +269,8 @@ public void visitInsn(Insn insn) {
}
}
- if (DEBUG_ANALYZE && !self.call_is_pausable) {
- System.err.println("call_pausable: " + op);
+ if (log.isLoggable(Level.FINE) && !self.call_is_pausable) {
+ log.fine("call_pausable: " + op);
}
self.may_return_tail_marker = true;
@@ -290,8 +292,8 @@ public void visitInsn(Insn insn) {
if (bif != null) {
- if (DEBUG_ANALYZE && !self.is_pausable && bif.isPausable()) {
- System.err.println("pausable: calls " + bif.javaMethod);
+ if (log.isLoggable(Level.FINE) && !self.is_pausable && bif.isPausable()) {
+ log.fine("pausable: calls " + bif.javaMethod);
}
@@ -300,8 +302,8 @@ public void visitInsn(Insn insn) {
} else if (op == BeamOpcode.call_ext) {
- if (DEBUG_ANALYZE && !self.is_pausable) {
- System.err.println("pausable: calls "+mod+":"+fun+"/"+arity);
+ if (log.isLoggable(Level.FINE) && !self.is_pausable) {
+ log.fine("pausable: calls "+mod+":"+fun+"/"+arity);
}
self.is_pausable = true;
@@ -313,8 +315,8 @@ public void visitInsn(Insn insn) {
case call_fun:
case wait:
case wait_timeout:
- if (DEBUG_ANALYZE && !self.is_pausable) {
- System.err.println("pausable: "+op);
+ if (log.isLoggable(Level.FINE) && !self.is_pausable) {
+ log.fine("pausable: "+op);
}
self.is_pausable = true;
@@ -343,8 +345,8 @@ public void visitInsn(Insn insn) {
self.is_pausable |= bif.isPausable();
- if (DEBUG_ANALYZE && self.is_pausable) {
- System.err.println("pausable: calls "+bif.javaMethod);
+ if (log.isLoggable(Level.FINE) && self.is_pausable) {
+ log.fine("pausable: calls "+bif.javaMethod);
}
}
View
25 src/main/java/erjang/beam/analysis/BeamTypeAnalysis.java
@@ -29,6 +29,8 @@
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;
+import java.util.logging.Level;
+import java.util.logging.Logger;
import org.objectweb.asm.Type;
@@ -83,6 +85,7 @@
import static erjang.beam.CodeAtoms.*;
public class BeamTypeAnalysis extends ModuleAdapter {
+ static final Logger log = Logger.getLogger("erjang.beam");
/**
*
*/
@@ -187,7 +190,7 @@ void live_analysis() {
}
} while (change);
- // System.err.println("live analysis for " + name + "/" + arity
+ // log.finer("live analysis for " + name + "/" + arity
// + " completed in " + iter + " iterations.");
}
@@ -265,7 +268,7 @@ public void visitEnd() {
if (lb.insns.get(0).opcode() == BeamOpcode.func_info) {
// ignore this
} else {
- System.err.println("UNREACHABLE " + lb.block_label);
+ log.fine("UNREACHABLE " + lb.block_label);
has_unreachable_code = true;
}
}
@@ -316,8 +319,8 @@ private void function_visit_end_aux(LabeledBlock block, int ext_label, BeamExcep
}
private void dump() {
- if (ERT.DEBUG2) {
- System.err.println("DUMPING " + name + "/" + arity);
+ if (log.isLoggable(Level.FINER)) {
+ log.finer("DUMPING " + name + "/" + arity);
for (Map.Entry<Integer, LabeledBlock> ent : lbs.entrySet()) {
ent.getValue().dump();
@@ -442,7 +445,7 @@ private void accept_2(BlockVisitor2 vis, BeamExceptionHandler exh) {
switch (opcode) {
case func_info: {
Insn.AAI insn = (Insn.AAI)insn_;
- // System.err.print("go: " + insn);
+ // log.finer("go: " + insn);
vis.visitInsn(opcode, insn.getExtFun());
break;
}
@@ -1417,11 +1420,11 @@ public void analyze() {
}
private void dump() {
- if (ERT.DEBUG2 == false) return;
+ if (!log.isLoggable(Level.FINER)) return;
next_insn: for (int i = 0; i < insns.size(); i++) {
- System.err.println(name + "(" + block_label + "):" + i
+ log.finer(name + "(" + block_label + "):" + i
+ " :: " + (map == null ? "?" : map[i]));
- System.err.println(" >> " + insns.get(i));
+ log.finer(" >> " + insns.get(i));
}
}
@@ -1777,7 +1780,7 @@ public void analyze0() {
case loop_rec_end:
case timeout: {
- // System.err.println(insn);
+ // log.finer(insn);
continue next_insn;
}
@@ -2035,7 +2038,7 @@ public void analyze0() {
lbv.merge_from(current);
} catch (Error e) {
- System.out.println("merge " + current + "\n | "
+ log.severe("merge " + current + "\n | "
+ lbv.initial + "\n FAILED");
throw e;
@@ -2360,7 +2363,7 @@ public void merge_from(TypeMap typeMap) {
if (new_types == initial) {
// ignore //
} else if (new_types.equals(initial)) {
- if (ERT.DEBUG) System.err.println("Missed TypeMap sharing opportunity");
+ if (log.isLoggable(Level.FINE)) log.fine("Missed TypeMap sharing opportunity");
} else {
// System.out.println("merge " + initial + "\n | " +
View
4 src/main/java/erjang/beam/analysis/TypeMap.java
@@ -20,6 +20,7 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
+import java.util.logging.Logger;
import org.objectweb.asm.Type;
@@ -30,6 +31,7 @@
import erjang.ETuple;
class TypeMap {
+ private static Logger log = Logger.getLogger("erjang.beam");
private static Type TYPE_EOBJECT = Type.getType(EObject.class);
private static Type TYPE_ETUPLE = Type.getType(ETuple.class);
private static String ETUPLE_NAME = TYPE_ETUPLE.getInternalName();
@@ -151,7 +153,7 @@ private TypeMap(IPersistentMap xregs, Type[] yregs, Type[] fregs, int stacksize,
this.exh = exh;
if (stacksize > 1023) {
- System.err.println("stacksize > 1023!");
+ log.warning("stacksize > 1023!");
}
}
View
12 src/main/java/erjang/beam/interpreter/AbstractInterpreter.java
@@ -21,6 +21,8 @@
import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
+import java.util.logging.Level;
+import java.util.logging.Logger;
import erjang.EModule;
import erjang.EModuleManager;
@@ -49,7 +51,7 @@
import kilim.Pausable;
public class AbstractInterpreter {
- static final boolean DEBUG = false;
+ static Logger log = Logger.getLogger("erjang.beam");
public static abstract class Encoder implements ModuleVisitor {
private EAtom moduleName;
@@ -76,16 +78,16 @@ public void visitExport(EAtom fun, int arity, int entryLabel) {
public void visitAttribute(EAtom att, EObject value) {}
public void visitEnd() {
- if (DEBUG) System.err.println("Interpreter code for module '"+moduleName+"':");
+ log.fine("Interpreter code for module '"+moduleName+"':");
for (Backpatch bp : backpatches) {
bp.patch(label_map.get(bp.label));
}
- if (DEBUG) {
+ if (log.isLoggable(Level.FINE)) {
for (int i=0; i<code.size(); i++) {
Insn insn = insn_start.get(i);
- System.err.println((insn!=null? "*" : " ") + i +
+ log.fine((insn!=null? "*" : " ") + i +
": " + (int)code.get(i) +
(insn!=null ? ("\t"+insn.toSymbolic().toString()) : ""));
}
@@ -98,7 +100,7 @@ public EModule toEModule() {
ValueJumpTable[] valueJumpTableArray = value_jump_tables.toArray(new ValueJumpTable[value_jump_tables.size()]);
ArityJumpTable[] arityJumpTableArray = arity_jump_tables.toArray(new ArityJumpTable[arity_jump_tables.size()]);
List<FunIDWithEntry> exports = convertExports(raw_exports);
- if (DEBUG) System.err.println("INT| Constructing module for "+moduleName.getName());
+ log.fine("INT| Constructing module for "+moduleName.getName());
return makeModule(moduleName.getName(),
codeArray, constArray,
valueJumpTableArray, arityJumpTableArray,
View
44 src/main/java/erjang/beam/interpreter/Interpreter.template
@@ -59,6 +59,9 @@ import erjang.beam.repr.ExtFun;
import kilim.Pausable;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
public class Interpreter extends AbstractInterpreter {
#ENUM#
public static final short ENSURE_REG_CAPACITY = MAX_OPCODE + 1;
@@ -112,7 +115,7 @@ public class Interpreter extends AbstractInterpreter {
int opcode_pos = emitPlaceholder();
insn_start.put(opcode_pos, insn);
- //System.err.println("@ "+opcode_pos+": "+insn.toSymbolic());
+ //log.finer("@ "+opcode_pos+": "+insn.toSymbolic());
switch (insn.opcode()) {
#ENCODE#
default: throw new Error("Unknown opcode: "+insn);
@@ -146,7 +149,7 @@ public class Interpreter extends AbstractInterpreter {
this.exports = exports;
this.imports = imports;
ext_funs = new EFun[imports.size()];
- if (DEBUG) System.err.println("INT| Constructed module for "+this.name);
+ log.fine("INT| Constructed module for "+this.name);
setup();
}
@@ -158,7 +161,7 @@ public class Interpreter extends AbstractInterpreter {
FunIDWithGuardedness imp_wg = imports.get(i);
FunID imp = imp_wg.fun;
boolean is_guard = imp_wg.is_guard;
-// System.err.println("INT| Import #"+i+": "+imp+" / "+is_guard);
+ log.finer("INT| Import #"+i+": "+imp+" / "+is_guard);
// If this is a BIF, resolve it right away:
BuiltInFunction bif =
@@ -177,7 +180,7 @@ public class Interpreter extends AbstractInterpreter {
int j=0;
for (FunIDWithEntry fi : exports) {
-// System.err.println("INT| Export #"+(j++)+": "+fi);
+ log.finer("INT| Export #"+(j++)+": "+fi);
EFun fun = EFun.get_fun_with_handler(fi.arity, new Function(fi.start_pc), getModuleClassLoader());
EModuleManager.add_export(this, fi, fun);
}
@@ -196,7 +199,9 @@ public class Interpreter extends AbstractInterpreter {
int argCnt = args.length;
EObject[] reg = getRegs(proc); //??
for (int i=0; i<argCnt; i++) {reg[i] = args[i];} //??
- //for (int i=0; i<argCnt; i++) System.err.println("INT| arg#"+i+"="+args[i]);
+ if (log.isLoggable(Level.FINER)) {
+ for (int i=0; i<argCnt; i++) log.finer("INT| arg#"+i+"="+args[i]);
+ }
return interpret(proc, start_pc, reg);
}
@@ -207,14 +212,16 @@ public class Interpreter extends AbstractInterpreter {
} else {
int argCnt = len;
for (int i=0; i<argCnt; i++) {reg[i] = args[i];} //??
- //for (int i=0; i<argCnt; i++) System.err.println("INT| arg#"+i+"="+args[i]);
+ if (log.isLoggable(Level.FINER)) {
+ for (int i=0; i<argCnt; i++) log.finer("INT| arg#"+i+"="+args[i]);
+ }
return interpret(proc, start_pc, reg);
}
}
/** Local call - with given PC and register array */
public EObject invoke_local(final EProc proc, final EObject[] reg, int argCnt, int pc) throws Pausable {
-// System.err.println("INT| invoking "+name+"@"+pc+"...");
+ log.fine("INT| invoking "+name+"@"+pc+"...");
return interpret(proc, pc, reg);
}
@@ -238,18 +245,19 @@ public class Interpreter extends AbstractInterpreter {
while (true) try {
while (true) {
-// System.err.print('¤');
if (pc >= code.length) {
throw new Error("Bad jump to: "+module_name()+"@"+pc+"; from_pc="+last_pc+"; op="+((int)code[last_pc]));
}
last_pc = pc;
final int opcode = code[pc++];
- //System.err.print("STACK| "); for (int i=0; i<=sp; i++) {System.err.print(" "+i+":"+stack[i]);} System.err.println();
-// System.err.println("INTP|"+proc.self_handle()+" (pc="+(pc-1)+"; sp="+sp+")"+opcode+" "+reg[0]);
+ if (log.isLoggable(Level.FINEST)) {
+ log.finest("STACK| "); for (int i=0; i<=sp; i++) {log.finest(" "+i+":"+stack[i]);} log.finest("");
+ }
+ log.finer("INTP|"+proc.self_handle()+" (pc="+(pc-1)+"; sp="+sp+")"+opcode+" "+reg[0]);
switch (opcode) {
case ENSURE_REG_CAPACITY: {
int max_x = code[pc++];
- System.err.println("INTP|"+proc.self_handle()+" Ensure reg capacity: "+max_x);
+ log.finer("INTP|"+proc.self_handle()+" Ensure reg capacity: "+max_x);
reg = ensureCapacity(reg, max_x);
} break;
@@ -282,10 +290,10 @@ public class Interpreter extends AbstractInterpreter {
continue;
} else throw(e);
} catch (RuntimeException e) {
- if (ERT.DEBUG) {
- System.err.println("DB| Error when interpreting "+module_name()+"@"+(pc-1)+"; opcode="+code[last_pc]+"; prefetched1="+prefetched1+"; prefetched2="+prefetched2+" : "+e);
- System.err.println("Code:");
- for (int i=Math.max(0,pc-15); i<Math.min(code.length,pc+5); i++) System.err.println(" "+i+": "+(int)code[i]);
+ if (log.isLoggable(Level.WARNING)) {
+ log.warning("DB| Error when interpreting "+module_name()+"@"+(pc-1)+"; opcode="+code[last_pc]+"; prefetched1="+prefetched1+"; prefetched2="+prefetched2+" : "+e);
+ log.warning("Code:");
+ for (int i=Math.max(0,pc-15); i<Math.min(code.length,pc+5); i++) log.warning(" "+i+": "+(int)code[i]);
}
throw e;
}
@@ -301,13 +309,15 @@ public class Interpreter extends AbstractInterpreter {
}
public EObject invoke(final EProc proc, final EObject[] args) throws Pausable {
-// System.err.println("INT| Closure invoked @ "+start_pc);
+ log.fine("INT| Closure invoked @ "+start_pc);
int argCnt = args.length;
int envCnt = env.length;
EObject[] reg = new EObject[1024]; //??
for (int i=0; i<argCnt; i++) {reg[i] = args[i];} //??
for (int i=0; i<envCnt; i++) {reg[argCnt+i] = env[i];}
- //for (int i=0; i<argCnt+envCnt; i++) System.err.println("INT| reg#"+i+"="+reg[i]);
+ if (log.isLoggable(Level.FINER)) {
+ for (int i=0; i<argCnt+envCnt; i++) log.finer("INT| reg#"+i+"="+reg[i]);
+ }
return interpret(proc, start_pc, reg);
}
}
View
106 src/main/java/erjang/beam/loader/BeamLoader.java
@@ -18,8 +18,6 @@
package erjang.beam.loader;
-import static erjang.beam.CodeAtoms.BEAM_FILE_ATOM;
-import static erjang.beam.CodeAtoms.FUNCTION_ATOM;
import static erjang.beam.CodeAtoms.START_ATOM;
import java.io.ByteArrayInputStream;
@@ -33,20 +31,22 @@
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
+import java.util.logging.Level;
+import java.util.logging.Logger;
import erjang.EAtom;
import erjang.EBinary;
import erjang.EInputStream;
import erjang.EObject;
import erjang.ESeq;
-import erjang.ESmall;
-import erjang.ETuple;
import erjang.beam.BeamOpcode;
-
+import erjang.beam.repr.AnonFun;
import erjang.beam.repr.CodeTables;
-import erjang.beam.repr.ModuleRepr;
+import erjang.beam.repr.ExtFun;
+import erjang.beam.repr.FunctionInfo;
import erjang.beam.repr.FunctionRepr;
import erjang.beam.repr.Insn;
+import erjang.beam.repr.ModuleRepr;
import erjang.beam.repr.Operands;
import erjang.beam.repr.Operands.AllocList;
import erjang.beam.repr.Operands.Atom;
@@ -62,13 +62,9 @@
import erjang.beam.repr.Operands.TableLiteral;
import erjang.beam.repr.Operands.XReg;
import erjang.beam.repr.Operands.YReg;
-import erjang.beam.repr.ExtFun;
-import erjang.beam.repr.AnonFun;
-import erjang.beam.repr.FunctionInfo;
public class BeamLoader extends CodeTables {
- static final boolean DEBUG = false;
- static final boolean DEBUG_ON_ERROR = true;
+ static Logger log = Logger.getLogger("erjang.beam");
/** For testing purposes. */
public static void main(String[] args) throws IOException {
@@ -296,7 +292,7 @@ public SectionMetadata readSectionHeader() throws IOException {
int tag;
try {
tag = in.read4BE();
- if (DEBUG) System.err.println("Reading section with tag "+toSymbolicTag(tag)+" at "+in.getPos());
+ if (log.isLoggable(Level.FINE)) log.fine("Reading section with tag "+toSymbolicTag(tag)+" at "+in.getPos());
} catch (EOFException eof) {
return null;
}
@@ -324,7 +320,7 @@ public void readSection(SectionMetadata section) throws IOException {
case C_INF: readCompilationInfoSection(); break;
case ABST: readASTSection(); break;
default:
- if (DEBUG) System.err.println("Unrecognized section tag: "+Integer.toHexString(section.tag));
+ if (log.isLoggable(Level.WARNING)) log.warning("Unrecognized section tag: "+Integer.toHexString(section.tag));
} // switch
} catch (Exception e) {
int relPos = in.getPos()-section.offset;
@@ -333,15 +329,17 @@ public void readSection(SectionMetadata section) throws IOException {
in.setPos(curPos-16);
byte[] d = new byte[64];
int ctxlen = in.read(d);
- if (DEBUG_ON_ERROR) {
- System.err.println("Context dump: ");
+ if (log.isLoggable(Level.SEVERE)) {
+ StringBuilder sb = new StringBuilder();
+ sb.append("Context dump: \n");
for (int i=0; i<ctxlen; i++) {
int byt = d[i] & 0xFF;
- if (byt<16) System.err.print("0");
- System.err.print(Integer.toHexString(byt & 0xFF));
- System.err.print(" ");
- if ((i+1) % 16 == 0 || (i+1)==ctxlen) System.err.println();
+ if (byt<16) sb.append("0");
+ sb.append(Integer.toHexString(byt & 0xFF));
+ sb.append(" ");
+ if ((i+1) % 16 == 0 || (i+1)==ctxlen) sb.append("\n");
}
+ log.severe(sb.toString());
}
} catch (Exception e2) {}
throw new IOException("Error occurred around "+relPos+"=0x"+Integer.toHexString(relPos)+" bytes into section "+Integer.toHexString(section.tag), e);
@@ -372,27 +370,27 @@ private String toSymbolicTag(int tag) {
public void readAtomSection() throws IOException {
- if (DEBUG) System.err.println("readAtomSection");
+ if (log.isLoggable(Level.FINE)) log.fine("readAtomSection");
int nAtoms = in.read4BE();
- if (DEBUG) System.err.println("Number of atoms: "+nAtoms);
+ if (log.isLoggable(Level.FINE)) log.fine("Number of atoms: "+nAtoms);
atoms = new EAtom[nAtoms];
for (int i=0; i<nAtoms; i++) {
String atom = readString(in.read1());
- if (DEBUG) System.err.println("- #"+(i+1)+": '"+atom+"'");
+ if (log.isLoggable(Level.FINE)) log.fine("- #"+(i+1)+": '"+atom+"'");
atoms[i] = EAtom.intern(atom);
}
}
public void readStringSection(int sectionLength) throws IOException {
- if (DEBUG) System.err.println("readStringSection");
+ if (log.isLoggable(Level.FINE)) log.fine("readStringSection");
stringpool = readBinary(sectionLength);
}
public void readExportSection() throws IOException {
- if (DEBUG) System.err.println("readExportSection");
+ if (log.isLoggable(Level.FINE)) log.fine("readExportSection");
int nExports = in.read4BE();
exports = new FunctionInfo[nExports];
- if (DEBUG) System.err.println("Number of exports: "+nExports);
+ if (log.isLoggable(Level.FINE)) log.fine("Number of exports: "+nExports);
EAtom mod = moduleName();
for (int i=0; i<nExports; i++) {
int fun_atom_nr = in.read4BE();
@@ -401,17 +399,17 @@ public void readExportSection() throws IOException {
EAtom fun = atom(fun_atom_nr);
exports[i] = new FunctionInfo(mod, fun, arity, label);
addFunctionAtLabel(exports[i]);
- if (DEBUG && atoms != null) {
- System.err.println("- #"+(i+1)+": "+atom(fun_atom_nr)+"/"+arity+" @ "+label);
+ if (log.isLoggable(Level.FINE) && atoms != null) {
+ log.fine("- #"+(i+1)+": "+atom(fun_atom_nr)+"/"+arity+" @ "+label);
}
}
}
public void readLocalFunctionSection() throws IOException {
- if (DEBUG) System.err.println("readLocalFunctionSection");
+ if (log.isLoggable(Level.FINE)) log.fine("readLocalFunctionSection");
int nLocals = in.read4BE();
localFunctions = new FunctionInfo[nLocals];
- if (DEBUG) System.err.println("Number of locals: "+nLocals);
+ if (log.isLoggable(Level.FINE)) log.fine("Number of locals: "+nLocals);
EAtom mod = moduleName();
for (int i=0; i<nLocals; i++) {
int fun_atom_nr = in.read4BE();
@@ -420,17 +418,17 @@ public void readLocalFunctionSection() throws IOException {
EAtom fun = atom(fun_atom_nr);
localFunctions[i] = new FunctionInfo(mod, fun, arity, label);
addFunctionAtLabel(localFunctions[i]);
- if (DEBUG && atoms != null) {
- System.err.println("- #"+(i+1)+": "+atom(fun_atom_nr)+"/"+arity+" @ "+label);
+ if (log.isLoggable(Level.FINE) && atoms != null) {
+ log.fine("- #"+(i+1)+": "+atom(fun_atom_nr)+"/"+arity+" @ "+label);
}
}
}
public void readFunctionSection() throws IOException {
- if (DEBUG) System.err.println("readFunctionSection");
+ if (log.isLoggable(Level.FINE)) log.fine("readFunctionSection");
int nFunctions = in.read4BE();
anonymousFuns = new AnonFun[nFunctions];
- if (DEBUG) System.err.println("Number of function descrs: "+nFunctions);
+ if (log.isLoggable(Level.FINE)) log.fine("Number of function descrs: "+nFunctions);
EAtom mod = moduleName();
for (int i=0; i<nFunctions; i++) {
int fun_atom_nr = in.read4BE();
@@ -443,76 +441,76 @@ public void readFunctionSection() throws IOException {
anonymousFuns[i] = new AnonFun(mod, fun, arity, label,
old_uniq, i, module_md5, index, free_vars);
- if (DEBUG && atoms != null) {
- System.err.println("- #"+(i+1)+": "+fun+"/"+arity+" @ "+label);
- System.err.println("--> occur:"+index+" free:"+free_vars+" $ "+old_uniq);
+ if (log.isLoggable(Level.FINE) && atoms != null) {
+ log.fine("- #"+(i+1)+": "+fun+"/"+arity+" @ "+label);
+ log.fine("--> occur:"+index+" free:"+free_vars+" $ "+old_uniq);
}
}
}
/** readImportSection
* Depends on atom table. */
public void readImportSection() throws IOException {
- if (DEBUG) System.err.println("readImportSection");
+ if (log.isLoggable(Level.FINE)) log.fine("readImportSection");
int nImports = in.read4BE();
- if (DEBUG) System.err.println("Number of imports: "+nImports);
+ if (log.isLoggable(Level.FINE)) log.fine("Number of imports: "+nImports);
externalFuns = new ExtFun[nImports];
for (int i=0; i<nImports; i++) {
int m_atm_no = in.read4BE();
int f_atm_no = in.read4BE();
int arity = in.read4BE();
EAtom mod = atom(m_atm_no), fun = atom(f_atm_no);
externalFuns[i] = new ExtFun(mod, fun, arity);
- if (DEBUG && atoms != null) {
- System.err.println("- #"+(i+1)+": "+mod+":"+fun+"/"+arity);
+ if (log.isLoggable(Level.FINE) && atoms != null) {
+ log.fine("- #"+(i+1)+": "+mod+":"+fun+"/"+arity);
}
}
}
public void readAttributeSection() throws IOException {
- if (DEBUG) System.err.println("readAttributeSection");
+ if (log.isLoggable(Level.FINE)) log.fine("readAttributeSection");
attributes = in.read_any();
- if (DEBUG) System.err.println("Attibutes: "+attributes);
+ if (log.isLoggable(Level.FINE)) log.fine("Attibutes: "+attributes);
}
public void readCompilationInfoSection() throws IOException {
- if (DEBUG) System.err.println("readCompilationInfoSection");
+ if (log.isLoggable(Level.FINE)) log.fine("readCompilationInfoSection");
compilation_info = in.read_any();
- if (DEBUG) System.err.println("Compilation info: "+compilation_info);
+ if (log.isLoggable(Level.FINE)) log.fine("Compilation info: "+compilation_info);
}
public void readASTSection() throws IOException {
- if (DEBUG) System.err.println("readASTSection");
+ if (log.isLoggable(Level.FINE)) log.fine("readASTSection");
if (!include_debug_info) return;
abstract_tree = in.read_any();
-// if (DEBUG) System.err.println("AST: "+abstract_tree);
+// if (log.isLoggable(Level.FINE)) log.fine("AST: "+abstract_tree);
}
public void readLiteralSection() throws IOException {
- if (DEBUG) System.err.println("readLiteralSection");
+ if (log.isLoggable(Level.FINE)) log.fine("readLiteralSection");
final byte[] buf = in.read_size_and_inflate();
final EInputStream is = new EInputStream(buf);
int nLiterals = is.read4BE();
- if (DEBUG) System.err.println("Number of literals: "+nLiterals);
+ if (log.isLoggable(Level.FINE)) log.fine("Number of literals: "+nLiterals);
literals = new EObject[nLiterals];
for (int i=0; i<nLiterals; i++) {
int lit_length = is.read4BE();
int pos_before_lit = is.getPos();
literals[i] = is.read_any();
- if (DEBUG) System.err.println("- #"+i+": "+literals[i]);
+ if (log.isLoggable(Level.FINE)) log.fine("- #"+i+": "+literals[i]);
int pos_after_lit = is.getPos();
assert(pos_after_lit == pos_before_lit + lit_length);
}
}
public void readCodeSection() throws IOException {
- if (DEBUG) System.err.println("readCodeSection");
+ if (log.isLoggable(Level.FINE)) log.fine("readCodeSection");
int flags = in.read4BE(); // Only 16 ever seen
int zero = in.read4BE(); // Only 0 ever seen
int highestOpcode = in.read4BE();
int labelCnt = in.read4BE();
int funCnt = in.read4BE();
- if (DEBUG) System.err.println("Code metrics: flags:"+flags+
+ if (log.isLoggable(Level.FINE)) log.fine("Code metrics: flags:"+flags+
", z:"+zero+
", hop:"+highestOpcode+
", L:"+labelCnt+
@@ -550,7 +548,7 @@ public Insn readInstruction() throws IOException {
case apply:
{
int i1 = readCodeInteger();
- if (DEBUG && opcode==BeamOpcode.label) System.err.println("DB| ### label "+i1+"###");
+ if (log.isLoggable(Level.FINE) && opcode==BeamOpcode.label) log.fine("DB| ### label "+i1+"###");
return new Insn.I(opcode, i1);
}
@@ -1231,12 +1229,12 @@ public Operand readOperand(int d1) throws IOException {
return new TableLiteral(literal(nr));
}
default:
- System.err.println("*** Unhandled extended operand tag: "+moretag);
+ log.warning("*** Unhandled extended operand tag: "+moretag);
} // switch
break;
}
default:
- System.err.println("*** Unhandled operand tag: "+tag);
+ log.warning("*** Unhandled operand tag: "+tag);
} // switch
return null;
}
View
8 src/main/java/erjang/beam/repr/ModuleRepr.java
@@ -21,6 +21,8 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
+import java.util.logging.Level;
+import java.util.logging.Logger;
import erjang.EAtom;
import erjang.EObject;
@@ -33,6 +35,8 @@
import erjang.beam.ModuleVisitor;
public class ModuleRepr implements BeamFileData {
+ static Logger log = Logger.getLogger("erjang.beam");
+
private EAtom moduleName;
private final CodeTables ct;
private final FunctionInfo[] exports;
@@ -63,8 +67,8 @@ public void accept(ModuleVisitor v) {
for (FunctionRepr fun : functions) fun.declare(v);
for (FunctionRepr fun : functions) fun.accept(v);
} catch (RuntimeException e) {
- System.err.println("ModuleRepr: Error in traversal: "+e);
- e.printStackTrace(System.err);
+ log.severe("ModuleRepr: Error in traversal: "+e.getMessage());
+ log.log(Level.FINE, "details: ", e);
throw(e);
} finally {
v.visitEnd();
View
98 src/main/java/erjang/driver/EDriverInstance.java
@@ -24,6 +24,8 @@
import java.nio.channels.SelectionKey;
import java.util.ArrayList;
import java.util.concurrent.locks.Lock;
+import java.util.logging.Level;
+import java.util.logging.Logger;
import kilim.Pausable;
import kilim.ReentrantLock;
@@ -49,6 +51,7 @@
*
*/
public abstract class EDriverInstance extends EDriverControl {
+ protected static Logger log = Logger.getLogger("erjang.driver");
EDriver driver;
protected EDriverTask task;
@@ -272,9 +275,7 @@ protected boolean driver_demonitor_process(ERef monitor) throws Pausable {
try {
return ErlProc.demonitor(task, monitor, ERT.NIL) == ERT.TRUE;
} catch (ErlangException e) {
- if (ERT.DEBUG_PORT) {
- e.printStackTrace();
- }
+ EDriverTask.log.log(Level.FINE, "demonitor", e);
return false;
}
}
@@ -464,6 +465,97 @@ protected void set_busy_port(boolean b) {
throw new erjang.NotImplemented();
}
+
+ public static void dump_buffer(ByteBuffer[] buffer) {
+ dump_buffer(log, null, buffer);
+ }
+
+ public static void dump_buffer(Logger log, String heading, ByteBuffer[] buffer) {
+ dump_buffer(log, Level.FINEST, heading, buffer);
+ }
+
+ public static void dump_buffer(Logger log, Level level, String heading, ByteBuffer[] buffer) {
+
+ if (!log.isLoggable(level)) return;
+
+ StringBuilder sb = new StringBuilder();
+ if (heading != null) {
+ sb.append(heading);
+ sb.append("\n");
+ }
+
+ sb.append(" vec[" + buffer.length + "]:: \n");
+
+ for (int i = 0; i < buffer.length; i++) {
+
+ ByteBuffer evp = buffer[i];