Skip to content
Snippets Groups Projects
Commit 52e27596 authored by Mick Jordan's avatar Mick Jordan
Browse files

Refactor log file handling for embedded mode, tagging log files with pid....

Refactor log file handling for embedded mode, tagging log files with pid. Invoke R_Suicide in embedded mode rather than return to console.
parent 5566f65c
No related branches found
No related tags found
No related merge requests found
......@@ -243,15 +243,12 @@ public class RCommand {
* logging the report will go to a file, so we print a message on
* the console as well.
*/
consoleHandler.println("internal error: " + e.getMessage() + " (see fastr_errors.log)");
RInternalError.reportError(e);
consoleHandler.println("internal error: " + e.getMessage() + " (see fastr_errors.log)");
} else {
/*
* This should never happen owing to earlier invariants of
* converting everything else to an RInternalError
*/
consoleHandler.println("unexpected internal error (" + e.getClass().getSimpleName() + "); " + e.getMessage());
// Something else, e.g. NPE
RInternalError.reportError(e);
consoleHandler.println("unexpected internal error (" + e.getClass().getSimpleName() + "); " + e.getMessage());
}
}
continue REPL;
......
......@@ -114,9 +114,12 @@ public class REmbedded {
runRmainloop(vm);
}
/**
* Upcalled from embedded mode to commit suicide.
*/
@SuppressWarnings("unused")
private static void R_Suicide(String msg) {
// TODO implement
Utils.exit(2);
}
}
......@@ -438,6 +438,10 @@ JNIEXPORT void JNICALL Java_com_oracle_truffle_r_runtime_ffi_jnr_JNI_1REmbed_nat
(*ptr_R_CleanUp)(x, y, z);
}
JNIEXPORT void JNICALL Java_com_oracle_truffle_r_runtime_ffi_jnr_JNI_1REmbed_nativeSuicide(JNIEnv *jniEnv, jclass c, jstring string) {
const char *cbuf = (*jniEnv)->GetStringUTFChars(jniEnv, string, NULL);
(*ptr_R_Suicide)(cbuf);
}
void uR_PolledEvents(void) {
unimplemented("R_PolledEvents");
......
......@@ -26,8 +26,11 @@ import java.io.FileDescriptor;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.file.Path;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.r.runtime.FastROptions;
import com.oracle.truffle.r.runtime.Utils;
import com.oracle.truffle.r.runtime.context.RContext;
import com.oracle.truffle.r.runtime.data.RPairList;
import com.oracle.truffle.r.runtime.data.RSymbol;
......@@ -58,7 +61,7 @@ public class RFFIUtils {
* In embedded mode can't trust that cwd is writeable, so output placed in /tmp. Also, tag with
* time in event of multiple concurrent instances (which happens with RStudio).
*/
private static final String tracePathPrefix = "/tmp/fastr_trace_nativecalls.log-";
private static final String TRACEFILE = "fastr_trace_nativecalls.log";
private static FileOutputStream traceFileStream;
private static PrintStream traceStream;
......@@ -68,9 +71,9 @@ public class RFFIUtils {
if (traceEnabled) {
if (RContext.isEmbedded()) {
if (traceStream == null) {
String tracePath = tracePathPrefix + Long.toString(System.currentTimeMillis());
Path tracePath = Utils.getLogPath(TRACEFILE);
try {
traceFileStream = new FileOutputStream(tracePath);
traceFileStream = new FileOutputStream(tracePath.toString());
traceStream = new PrintStream(traceFileStream);
} catch (IOException ex) {
System.err.println(ex.getMessage());
......
......@@ -29,13 +29,14 @@ import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.Date;
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.r.runtime.context.RContext;
/**
* This class is intended to be used for internal errors that do not correspond to R errors.
......@@ -149,7 +150,8 @@ public final class RInternalError extends Error {
System.err.println(verboseStackTrace);
}
if (FastROptions.PrintErrorStacktracesToFile.getBooleanValue()) {
try (BufferedWriter writer = Files.newBufferedWriter(FileSystems.getDefault().getPath(REnvVars.rHome(), "fastr_errors.log"), StandardCharsets.UTF_8, StandardOpenOption.APPEND,
Path logfile = Utils.getLogPath("fastr_errors.log");
try (BufferedWriter writer = Files.newBufferedWriter(logfile, StandardCharsets.UTF_8, StandardOpenOption.APPEND,
StandardOpenOption.CREATE)) {
writer.append(new Date().toString()).append('\n');
writer.append(out.toString()).append('\n');
......@@ -157,6 +159,9 @@ public final class RInternalError extends Error {
} catch (IOException e) {
e.printStackTrace();
}
if (RContext.isEmbedded()) {
Utils.rSuicide("FastR internal error");
}
}
}
}
......
......@@ -153,21 +153,17 @@ public final class Utils {
* polyglot context are.
*/
RPerfStats.report();
if (RContext.getInstance() != null && RContext.getInstance().getStartParams() != null && RContext.getInstance().getStartParams().getDebugInitFile()) {
throw new DebugExitException();
} else {
try {
/*
* This is not the proper way to dispose a PolyglotEngine, but it doesn't matter
* since we're going to System.exit anyway.
*/
RContext.getInstance().destroy();
} catch (Throwable t) {
// ignore
}
System.exit(status);
return null;
try {
/*
* This is not the proper way to dispose a PolyglotEngine, but it doesn't matter since
* we're going to System.exit anyway.
*/
RContext.getInstance().destroy();
} catch (Throwable t) {
// ignore
}
System.exit(status);
return null;
}
public static RuntimeException fail(String msg) {
......@@ -250,6 +246,17 @@ public final class Utils {
wdState().setCurrent(path);
}
/**
* Returns a {@link Path} for a log file with base name {@code fileName}, taking into account
* whether the system is running in embedded mode.
*/
public static Path getLogPath(String fileName) {
String root = RContext.isEmbedded() ? "/tmp" : REnvVars.rHome();
int pid = RFFIFactory.getRFFI().getBaseRFFI().getpid();
String baseName = RContext.isEmbedded() ? fileName + "-" + Integer.toString(pid) : fileName;
return FileSystems.getDefault().getPath(root, baseName);
}
/**
* Performs "~" expansion and also checks whether we need to take special case over relative
* paths due to the curwd having moved from the initial setting. In the latter case, if the path
......
......@@ -70,6 +70,7 @@ void testR_CleanUp(SA_TYPE x, int y, int z) {
void testR_Suicide(const char *msg) {
printf("testR_Suicide: %s\n",msg);
(ptr_stdR_Suicide(msg));
}
int testR_ReadConsole(const char *prompt, unsigned char *buf, int len, int h) {
......@@ -101,6 +102,7 @@ int main(int argc, char **argv) {
R_SetParams(Rp);
ptr_stdR_CleanUp = ptr_R_CleanUp;
ptr_R_CleanUp = &testR_CleanUp;
ptr_stdR_Suicide = ptr_R_Suicide;
ptr_R_Suicide = &testR_Suicide;
ptr_R_ReadConsole = &testR_ReadConsole;
ptr_R_WriteConsole = &testR_WriteConsole;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment