Skip to content

Commit bd11c78

Browse files
author
Miloslav Metelka
committed
fastr_errors.log should live somewhere else.
1 parent 5874d3f commit bd11c78

File tree

4 files changed

+168
-19
lines changed

4 files changed

+168
-19
lines changed

com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/RInternalError.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -193,25 +193,25 @@ private static void reportError(String errMsg, Throwable throwable, int contextI
193193
}
194194

195195
String message = "An internal error occurred: \"" + errMsg + "\"\nPlease report an issue at https://github.com/oracle/fastr including the commands";
196-
if (FastROptions.PrintErrorStacktracesToFile.getBooleanValue()) {
197-
message += " and the error log file '" + Utils.getLogPath(getLogFileName(contextId)) + "'.";
198-
} else {
199-
message += ".";
200-
}
201196
if (FastROptions.PrintErrorStacktracesToFile.getBooleanValue()) {
202197
Path logfile = Utils.getLogPath(getLogFileName(contextId));
203-
try (BufferedWriter writer = Files.newBufferedWriter(logfile, StandardCharsets.UTF_8, StandardOpenOption.APPEND,
204-
StandardOpenOption.CREATE)) {
205-
writer.append(new Date().toString()).append('\n');
206-
writer.append(out.toString()).append('\n');
207-
writer.append(verboseStackTrace).append("\n\n");
208-
} catch (IOException e) {
209-
e.printStackTrace();
198+
if (logfile != null) {
199+
message += " and the error log file '" + logfile + "'.";
200+
try (BufferedWriter writer = Files.newBufferedWriter(logfile, StandardCharsets.UTF_8, StandardOpenOption.APPEND,
201+
StandardOpenOption.CREATE)) {
202+
writer.append(new Date().toString()).append('\n');
203+
writer.append(out.toString()).append('\n');
204+
writer.append(verboseStackTrace).append("\n\n");
205+
} catch (IOException e) {
206+
e.printStackTrace();
207+
}
210208
}
211209
System.err.println(message);
212210
if (RContext.isEmbedded()) {
213211
RSuicide.rSuicide("FastR internal error");
214212
}
213+
} else {
214+
message += ".";
215215
}
216216
if (!FastROptions.PrintErrorStacktraces.getBooleanValue() && !FastROptions.PrintErrorStacktracesToFile.getBooleanValue()) {
217217
System.err.println(message);

com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/Utils.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,13 +249,13 @@ public static void updateCurwd(String path) {
249249
* of the dirs is writable null is returned.
250250
*/
251251
public static Path getLogPath(String fileNamePrefix) {
252-
String dir = RContext.isEmbedded() ? "/tmp" : System.getProperty("user.dir");
252+
String dir = RContext.isEmbedded() ? System.getProperty("java.io.tmpdir") : System.getProperty("user.dir");
253253
int dirId = 0;
254254
int pid = RContext.getInitialPid();
255255
String baseName = fileNamePrefix + "_pid" + Integer.toString(pid) + ".log";
256256
while (true) {
257257
Path path = FileSystems.getDefault().getPath(dir, baseName);
258-
if (Files.isWritable(path.getParent())) {
258+
if (Files.isWritable(path.getParent()) && (!Files.exists(path) || Files.isWritable(path))) {
259259
return path;
260260
}
261261
switch (dirId) {
@@ -267,7 +267,7 @@ public static Path getLogPath(String fileNamePrefix) {
267267
}
268268
break;
269269
case 1:
270-
dir = "/tmp";
270+
dir = System.getProperty("java.io.tmpdir");
271271
break;
272272
case 2:
273273
dir = REnvVars.rHome();

com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/ffi/RFFILog.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,15 @@ public static synchronized void printStackTrace() {
9191

9292
private static void initTraceStream() {
9393
Path tracePath = Utils.getLogPath(TRACEFILE);
94-
try {
95-
traceStream = new PrintWriter(tracePath.toString());
96-
} catch (IOException ex) {
97-
System.err.println(ex.getMessage());
94+
if (tracePath != null) {
95+
try {
96+
traceStream = new PrintWriter(tracePath.toString());
97+
} catch (IOException ex) {
98+
System.err.println(ex.getMessage());
99+
System.exit(1);
100+
}
101+
} else {
102+
System.err.println("Cannot produce trace log file at any of the standard locations. Please check directory permissions.");
98103
System.exit(1);
99104
}
100105
}
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/*
2+
* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 3 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 3 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 3 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
package com.oracle.truffle.r.test.library.utils;
24+
25+
import com.oracle.truffle.r.runtime.FastROptions;
26+
import com.oracle.truffle.r.runtime.REnvVars;
27+
import static org.junit.Assert.assertFalse;
28+
import static org.junit.Assert.assertTrue;
29+
30+
import com.oracle.truffle.r.runtime.RInternalError;
31+
import com.oracle.truffle.r.runtime.context.RContext;
32+
import org.junit.Test;
33+
34+
import com.oracle.truffle.r.test.TestBase;
35+
import java.io.ByteArrayOutputStream;
36+
import java.io.IOException;
37+
import java.io.PrintStream;
38+
import java.nio.file.FileSystems;
39+
import java.nio.file.Files;
40+
import java.nio.file.Path;
41+
import java.nio.file.attribute.PosixFilePermission;
42+
import java.util.Set;
43+
44+
public class TestFastrErrorsLog extends TestBase {
45+
46+
@Test
47+
public void testThrowInternalError() throws Exception {
48+
boolean origValue = FastROptions.PrintErrorStacktracesToFile.getBooleanValue();
49+
FastROptions.setValue(FastROptions.PrintErrorStacktracesToFile.name(), true);
50+
try {
51+
final String FASTR_ERRORS_LOG = "fastr_errors"; // Copy of
52+
// RInternalError.FASTR_ERRORS_LOG
53+
int pid = RContext.getInitialPid();
54+
String baseName = FASTR_ERRORS_LOG + "_pid" + Integer.toString(pid) + ".log";
55+
if (RContext.isEmbedded()) {
56+
String dir1 = System.getProperty("java.io.tmpdir");
57+
Path path1 = FileSystems.getDefault().getPath(dir1, baseName);
58+
try {
59+
assertFalse(Files.exists(path1));
60+
reportError();
61+
assertTrue(Files.exists(path1));
62+
} finally {
63+
try {
64+
Files.delete(path1);
65+
} catch (IOException ex) {
66+
}
67+
}
68+
} else {
69+
String dir1 = System.getProperty("user.dir");
70+
Path path1 = FileSystems.getDefault().getPath(dir1, baseName);
71+
72+
String dir2 = System.getProperty("user.home");
73+
Path path2 = FileSystems.getDefault().getPath(dir2, baseName);
74+
75+
String dir3 = System.getProperty("java.io.tmpdir");
76+
Path path3 = FileSystems.getDefault().getPath(dir3, baseName);
77+
78+
String dir4 = REnvVars.rHome();
79+
Path path4 = FileSystems.getDefault().getPath(dir4, baseName);
80+
81+
try {
82+
// Intentionally not testing that the particular log file is not present yet
83+
// to not depend on in which directory the test gets started.
84+
// assertFalse(Files.exists(path1));
85+
reportError();
86+
assertTrue(Files.exists(path1));
87+
setReadonly(path1, true);
88+
89+
reportError();
90+
assertTrue(Files.exists(path2));
91+
setReadonly(path2, true);
92+
93+
reportError();
94+
assertTrue(Files.exists(path3));
95+
setReadonly(path3, true);
96+
97+
reportError();
98+
assertTrue(Files.exists(path4));
99+
} finally {
100+
try {
101+
setReadonly(path1, false);
102+
Files.delete(path1);
103+
} catch (IOException ex) {
104+
}
105+
try {
106+
setReadonly(path2, false);
107+
Files.delete(path2);
108+
} catch (IOException ex) {
109+
}
110+
try {
111+
setReadonly(path3, false);
112+
Files.delete(path3);
113+
} catch (IOException ex) {
114+
}
115+
try {
116+
Files.delete(path4);
117+
} catch (IOException ex) {
118+
}
119+
}
120+
}
121+
} finally {
122+
FastROptions.setValue(FastROptions.PrintErrorStacktracesToFile.name(), origValue);
123+
}
124+
}
125+
126+
private static final void setReadonly(Path path, boolean readonly) throws IOException {
127+
Set<PosixFilePermission> perms = Files.getPosixFilePermissions(path);
128+
if (readonly) {
129+
perms.remove(PosixFilePermission.OWNER_WRITE);
130+
} else {
131+
perms.add(PosixFilePermission.OWNER_WRITE);
132+
}
133+
Files.setPosixFilePermissions(path, perms);
134+
}
135+
136+
private static final void reportError() {
137+
PrintStream temp = new PrintStream(new ByteArrayOutputStream());
138+
PrintStream origErr = System.err;
139+
System.setErr(temp);
140+
RInternalError.reportError(new Throwable("Throwing expected error in test."));
141+
System.setErr(origErr);
142+
}
143+
144+
}

0 commit comments

Comments
 (0)