Skip to content

Commit

Permalink
fastr_errors.log should live somewhere else.
Browse files Browse the repository at this point in the history
  • Loading branch information
Miloslav Metelka committed May 24, 2018
1 parent 5874d3f commit bd11c78
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -193,25 +193,25 @@ private static void reportError(String errMsg, Throwable throwable, int contextI
}

String message = "An internal error occurred: \"" + errMsg + "\"\nPlease report an issue at https://github.com/oracle/fastr including the commands";
if (FastROptions.PrintErrorStacktracesToFile.getBooleanValue()) {
message += " and the error log file '" + Utils.getLogPath(getLogFileName(contextId)) + "'.";
} else {
message += ".";
}
if (FastROptions.PrintErrorStacktracesToFile.getBooleanValue()) {
Path logfile = Utils.getLogPath(getLogFileName(contextId));
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');
writer.append(verboseStackTrace).append("\n\n");
} catch (IOException e) {
e.printStackTrace();
if (logfile != null) {
message += " and the error log file '" + logfile + "'.";
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');
writer.append(verboseStackTrace).append("\n\n");
} catch (IOException e) {
e.printStackTrace();
}
}
System.err.println(message);
if (RContext.isEmbedded()) {
RSuicide.rSuicide("FastR internal error");
}
} else {
message += ".";
}
if (!FastROptions.PrintErrorStacktraces.getBooleanValue() && !FastROptions.PrintErrorStacktracesToFile.getBooleanValue()) {
System.err.println(message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,13 +249,13 @@ public static void updateCurwd(String path) {
* of the dirs is writable null is returned.
*/
public static Path getLogPath(String fileNamePrefix) {
String dir = RContext.isEmbedded() ? "/tmp" : System.getProperty("user.dir");
String dir = RContext.isEmbedded() ? System.getProperty("java.io.tmpdir") : System.getProperty("user.dir");
int dirId = 0;
int pid = RContext.getInitialPid();
String baseName = fileNamePrefix + "_pid" + Integer.toString(pid) + ".log";
while (true) {
Path path = FileSystems.getDefault().getPath(dir, baseName);
if (Files.isWritable(path.getParent())) {
if (Files.isWritable(path.getParent()) && (!Files.exists(path) || Files.isWritable(path))) {
return path;
}
switch (dirId) {
Expand All @@ -267,7 +267,7 @@ public static Path getLogPath(String fileNamePrefix) {
}
break;
case 1:
dir = "/tmp";
dir = System.getProperty("java.io.tmpdir");
break;
case 2:
dir = REnvVars.rHome();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,15 @@ public static synchronized void printStackTrace() {

private static void initTraceStream() {
Path tracePath = Utils.getLogPath(TRACEFILE);
try {
traceStream = new PrintWriter(tracePath.toString());
} catch (IOException ex) {
System.err.println(ex.getMessage());
if (tracePath != null) {
try {
traceStream = new PrintWriter(tracePath.toString());
} catch (IOException ex) {
System.err.println(ex.getMessage());
System.exit(1);
}
} else {
System.err.println("Cannot produce trace log file at any of the standard locations. Please check directory permissions.");
System.exit(1);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 3 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 3 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.oracle.truffle.r.test.library.utils;

import com.oracle.truffle.r.runtime.FastROptions;
import com.oracle.truffle.r.runtime.REnvVars;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import com.oracle.truffle.r.runtime.RInternalError;
import com.oracle.truffle.r.runtime.context.RContext;
import org.junit.Test;

import com.oracle.truffle.r.test.TestBase;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.PosixFilePermission;
import java.util.Set;

public class TestFastrErrorsLog extends TestBase {

@Test
public void testThrowInternalError() throws Exception {
boolean origValue = FastROptions.PrintErrorStacktracesToFile.getBooleanValue();
FastROptions.setValue(FastROptions.PrintErrorStacktracesToFile.name(), true);
try {
final String FASTR_ERRORS_LOG = "fastr_errors"; // Copy of
// RInternalError.FASTR_ERRORS_LOG
int pid = RContext.getInitialPid();
String baseName = FASTR_ERRORS_LOG + "_pid" + Integer.toString(pid) + ".log";
if (RContext.isEmbedded()) {
String dir1 = System.getProperty("java.io.tmpdir");
Path path1 = FileSystems.getDefault().getPath(dir1, baseName);
try {
assertFalse(Files.exists(path1));
reportError();
assertTrue(Files.exists(path1));
} finally {
try {
Files.delete(path1);
} catch (IOException ex) {
}
}
} else {
String dir1 = System.getProperty("user.dir");
Path path1 = FileSystems.getDefault().getPath(dir1, baseName);

String dir2 = System.getProperty("user.home");
Path path2 = FileSystems.getDefault().getPath(dir2, baseName);

String dir3 = System.getProperty("java.io.tmpdir");
Path path3 = FileSystems.getDefault().getPath(dir3, baseName);

String dir4 = REnvVars.rHome();
Path path4 = FileSystems.getDefault().getPath(dir4, baseName);

try {
// Intentionally not testing that the particular log file is not present yet
// to not depend on in which directory the test gets started.
// assertFalse(Files.exists(path1));
reportError();
assertTrue(Files.exists(path1));
setReadonly(path1, true);

reportError();
assertTrue(Files.exists(path2));
setReadonly(path2, true);

reportError();
assertTrue(Files.exists(path3));
setReadonly(path3, true);

reportError();
assertTrue(Files.exists(path4));
} finally {
try {
setReadonly(path1, false);
Files.delete(path1);
} catch (IOException ex) {
}
try {
setReadonly(path2, false);
Files.delete(path2);
} catch (IOException ex) {
}
try {
setReadonly(path3, false);
Files.delete(path3);
} catch (IOException ex) {
}
try {
Files.delete(path4);
} catch (IOException ex) {
}
}
}
} finally {
FastROptions.setValue(FastROptions.PrintErrorStacktracesToFile.name(), origValue);
}
}

private static final void setReadonly(Path path, boolean readonly) throws IOException {
Set<PosixFilePermission> perms = Files.getPosixFilePermissions(path);
if (readonly) {
perms.remove(PosixFilePermission.OWNER_WRITE);
} else {
perms.add(PosixFilePermission.OWNER_WRITE);
}
Files.setPosixFilePermissions(path, perms);
}

private static final void reportError() {
PrintStream temp = new PrintStream(new ByteArrayOutputStream());
PrintStream origErr = System.err;
System.setErr(temp);
RInternalError.reportError(new Throwable("Throwing expected error in test."));
System.setErr(origErr);
}

}

0 comments on commit bd11c78

Please sign in to comment.