From e5ecb5b549585caea5b8e3becc9e7ac94d159022 Mon Sep 17 00:00:00 2001 From: Joel Dice Date: Tue, 15 Mar 2011 17:27:17 -0600 Subject: [PATCH] add optional avian.error.log system property This property may be used to specify a file name to use for printing stack traces for unhandled exceptions. The default is stderr. --- src/machine.cpp | 19 ++++++++++--------- src/machine.h | 16 ++++++++++++++++ 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/machine.cpp b/src/machine.cpp index 1f4e895148..7c1863c8f3 100644 --- a/src/machine.cpp +++ b/src/machine.cpp @@ -2258,6 +2258,7 @@ Machine::Machine(System* system, Heap* heap, Finder* bootFinder, referenceLock(0), shutdownLock(0), libraries(0), + errorLog(0), types(0), roots(0), finalizers(0), @@ -3888,19 +3889,19 @@ printTrace(Thread* t, object exception) for (object e = exception; e; e = throwableCause(t, e)) { if (e != exception) { - fprintf(stderr, "caused by: "); + fprintf(errorLog(t), "caused by: "); } - fprintf(stderr, "%s", &byteArrayBody + fprintf(errorLog(t), "%s", &byteArrayBody (t, className(t, objectClass(t, e)), 0)); if (throwableMessage(t, e)) { object m = throwableMessage(t, e); THREAD_RUNTIME_ARRAY(t, char, message, stringLength(t, m) + 1); stringChars(t, m, RUNTIME_ARRAY_BODY(message)); - fprintf(stderr, ": %s\n", RUNTIME_ARRAY_BODY(message)); + fprintf(errorLog(t), ": %s\n", RUNTIME_ARRAY_BODY(message)); } else { - fprintf(stderr, "\n"); + fprintf(errorLog(t), "\n"); } object trace = throwableTrace(t, e); @@ -3914,17 +3915,17 @@ printTrace(Thread* t, object exception) int line = t->m->processor->lineNumber (t, traceElementMethod(t, e), traceElementIp(t, e)); - fprintf(stderr, " at %s.%s ", class_, method); + fprintf(errorLog(t), " at %s.%s ", class_, method); switch (line) { case NativeLine: - fprintf(stderr, "(native)\n"); + fprintf(errorLog(t), "(native)\n"); break; case UnknownLine: - fprintf(stderr, "(unknown line)\n"); + fprintf(errorLog(t), "(unknown line)\n"); break; default: - fprintf(stderr, "(line %d)\n", line); + fprintf(errorLog(t), "(line %d)\n", line); } } } @@ -3934,7 +3935,7 @@ printTrace(Thread* t, object exception) } } - fflush(stderr); + fflush(errorLog(t)); } object diff --git a/src/machine.h b/src/machine.h index 4031093138..b784d6072a 100644 --- a/src/machine.h +++ b/src/machine.h @@ -1298,6 +1298,7 @@ class Machine { System::Monitor* referenceLock; System::Monitor* shutdownLock; System::Library* libraries; + FILE* errorLog; object types; object roots; object finalizers; @@ -3577,6 +3578,21 @@ methodClone(Thread* t, object method) methodCode(t, method)); } +inline FILE* +errorLog(Thread* t) +{ + if (t->m->errorLog == 0) { + const char* path = findProperty(t, "avian.error.log"); + if (path) { + t->m->errorLog = vm::fopen(path, "wb"); + } else { + t->m->errorLog = stderr; + } + } + + return t->m->errorLog; +} + } // namespace vm void