Skip to content

Commit 77217f6

Browse files
author
Miloslav Metelka
committed
[GR-2798] Improvements of fastr_errors.log should live somewhere else.
PullRequest: fastr/1535
2 parents 6dd3866 + 89040f8 commit 77217f6

File tree

4 files changed

+169
-19
lines changed

4 files changed

+169
-19
lines changed

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

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -193,25 +193,27 @@ 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+
}
208+
} else {
209+
message += ". Cannot write error log file (tried current working directory, user home directory, FastR home directory).";
210210
}
211211
System.err.println(message);
212212
if (RContext.isEmbedded()) {
213213
RSuicide.rSuicide("FastR internal error");
214214
}
215+
} else {
216+
message += ".";
215217
}
216218
if (!FastROptions.PrintErrorStacktraces.getBooleanValue() && !FastROptions.PrintErrorStacktracesToFile.getBooleanValue()) {
217219
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 write trace log file (tried current working directory, user home directory, FastR home directory).");
98103
System.exit(1);
99104
}
100105
}
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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+
String fastrErrorsLog = "fastr_errors"; // Copy of RInternalError.FASTR_ERRORS_LOG
52+
int pid = RContext.getInitialPid();
53+
String baseName = fastrErrorsLog + "_pid" + Integer.toString(pid) + ".log";
54+
if (RContext.isEmbedded()) {
55+
String dir1 = System.getProperty("java.io.tmpdir");
56+
Path path1 = FileSystems.getDefault().getPath(dir1, baseName);
57+
try {
58+
assertFalse(Files.exists(path1));
59+
reportError();
60+
assertTrue(Files.exists(path1));
61+
} finally {
62+
try {
63+
Files.delete(path1);
64+
} catch (IOException ex) {
65+
}
66+
}
67+
} else {
68+
String dir1 = System.getProperty("user.dir");
69+
Path path1 = FileSystems.getDefault().getPath(dir1, baseName);
70+
71+
String dir2 = System.getProperty("user.home");
72+
Path path2 = FileSystems.getDefault().getPath(dir2, baseName);
73+
74+
String dir3 = System.getProperty("java.io.tmpdir");
75+
Path path3 = FileSystems.getDefault().getPath(dir3, baseName);
76+
77+
String dir4 = REnvVars.rHome();
78+
Path path4 = FileSystems.getDefault().getPath(dir4, baseName);
79+
80+
try {
81+
// Intentionally not testing that the particular log file is not present yet
82+
// to not depend on in which directory the test gets started.
83+
// assertFalse(Files.exists(path1));
84+
reportError();
85+
assertTrue(Files.exists(path1));
86+
setReadonly(path1, true);
87+
88+
reportError();
89+
assertTrue(Files.exists(path2));
90+
setReadonly(path2, true);
91+
92+
reportError();
93+
assertTrue(Files.exists(path3));
94+
setReadonly(path3, true);
95+
96+
reportError();
97+
assertTrue(Files.exists(path4));
98+
} finally {
99+
try {
100+
setReadonly(path1, false);
101+
Files.delete(path1);
102+
} catch (IOException ex) {
103+
}
104+
try {
105+
setReadonly(path2, false);
106+
Files.delete(path2);
107+
} catch (IOException ex) {
108+
}
109+
try {
110+
setReadonly(path3, false);
111+
Files.delete(path3);
112+
} catch (IOException ex) {
113+
}
114+
try {
115+
Files.delete(path4);
116+
} catch (IOException ex) {
117+
}
118+
}
119+
}
120+
} finally {
121+
FastROptions.setValue(FastROptions.PrintErrorStacktracesToFile.name(), origValue);
122+
}
123+
}
124+
125+
private static void setReadonly(Path path, boolean readonly) throws IOException {
126+
Set<PosixFilePermission> perms = Files.getPosixFilePermissions(path);
127+
if (readonly) {
128+
perms.remove(PosixFilePermission.OWNER_WRITE);
129+
} else {
130+
perms.add(PosixFilePermission.OWNER_WRITE);
131+
}
132+
Files.setPosixFilePermissions(path, perms);
133+
}
134+
135+
private static void reportError() {
136+
PrintStream temp = new PrintStream(new ByteArrayOutputStream());
137+
PrintStream origErr = System.err;
138+
System.setErr(temp);
139+
RInternalError.reportError(new Throwable("Throwing expected error in test."));
140+
System.setErr(origErr);
141+
}
142+
143+
}

0 commit comments

Comments
 (0)