Skip to content

Commit f14be98

Browse files
committed
improved logging of exceptions from delegate calls
1 parent 84e1d84 commit f14be98

File tree

1 file changed

+131
-37
lines changed

1 file changed

+131
-37
lines changed

graalpython/org.graalvm.python.embedding/src/org/graalvm/python/embedding/utils/VirtualFileSystemImpl.java

Lines changed: 131 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
import java.io.IOException;
4949
import java.io.InputStream;
5050
import java.io.InputStreamReader;
51+
import java.io.PrintWriter;
52+
import java.io.StringWriter;
5153
import java.net.URI;
5254
import java.nio.ByteBuffer;
5355
import java.nio.channels.SeekableByteChannel;
@@ -98,6 +100,13 @@ final class VirtualFileSystemImpl implements FileSystem, AutoCloseable {
98100
consoleHandler.setFormatter(new SimpleFormatter() {
99101
@Override
100102
public synchronized String format(LogRecord lr) {
103+
if (lr.getThrown() != null) {
104+
StringWriter sw = new StringWriter();
105+
PrintWriter pw = new PrintWriter(sw);
106+
lr.getThrown().printStackTrace(pw);
107+
pw.close();
108+
return String.format("%s: %s\n%s", lr.getLevel().getName(), lr.getMessage(), sw.toString());
109+
}
101110
return String.format("%s: %s\n", lr.getLevel().getName(), lr.getMessage());
102111
}
103112
});
@@ -620,12 +629,12 @@ public void checkAccess(Path p, Set<? extends AccessMode> modes, LinkOption... l
620629

621630
Path path = toAbsolutePathInternal(p);
622631
if (!pathIsInVfs(path)) {
623-
boolean passed = false;
624632
try {
625633
delegate.checkAccess(path, modes, linkOptions);
626-
passed = true;
627-
} finally {
628-
finest("VFS.checkAccess delegated '%s' %s and ", path, passed ? "passed" : "did not pass");
634+
finest("VFS.checkAccess delegated '%s'", path);
635+
} catch (Throwable t) {
636+
finest(t, "VFS.checkAccess delegated '%s'", path);
637+
throw t;
629638
}
630639
} else {
631640
if (modes.contains(AccessMode.WRITE)) {
@@ -647,11 +656,12 @@ public void createDirectory(Path d, FileAttribute<?>... attrs) throws IOExceptio
647656

648657
Path dir = toAbsolutePathInternal(d);
649658
if (!pathIsInVfs(dir)) {
650-
boolean passed = false;
651659
try {
652660
delegate.createDirectory(dir, attrs);
653-
} finally {
654-
finest("VFS.createDirectory delegated '%s' %s", dir, passed ? "passed" : "did not pass");
661+
finest("VFS.createDirectory delegated '%s'", dir);
662+
} catch (Throwable t) {
663+
finest(t, "VFS.createDirectory delegated '%s'", dir);
664+
throw t;
655665
}
656666
} else {
657667
throw securityException("VFS.createDirectory", String.format("read-only filesystem, create directory not supported '%s'", dir));
@@ -664,11 +674,12 @@ public void delete(Path p) throws IOException {
664674

665675
Path path = toAbsolutePathInternal(p);
666676
if (!pathIsInVfs(path)) {
667-
boolean passed = false;
668677
try {
669678
delegate.delete(path);
670-
} finally {
671-
finest("VFS.delete delegated '%s' %s", path, passed ? "passed" : "did not pass");
679+
finest("VFS.delete delegated '%s'", path);
680+
} catch (Throwable t) {
681+
finest(t, "VFS.delete delegated '%s'", path);
682+
throw t;
672683
}
673684
} else {
674685
throw securityException("VFS.delete", String.format("read-only filesystem, delete not supported: '%s'", path));
@@ -683,11 +694,13 @@ public SeekableByteChannel newByteChannel(Path p, Set<? extends OpenOption> opti
683694

684695
Path path = toAbsolutePathInternal(p);
685696
if (!pathIsInVfs(path)) {
686-
boolean passed = false;
687697
try {
688-
return delegate.newByteChannel(path, options, attrs);
689-
} finally {
690-
finest("VFS.newByteChannel delegated '%s' %s", path, passed ? "passed" : "did not pass");
698+
SeekableByteChannel ret = delegate.newByteChannel(path, options, attrs);
699+
finest("VFS.newByteChannel delegated '%s'", path);
700+
return ret;
701+
} catch (Throwable t) {
702+
finest(t, "VFS.newByteChannel delegated '%s'", path);
703+
throw t;
691704
}
692705
}
693706

@@ -775,11 +788,13 @@ public DirectoryStream<Path> newDirectoryStream(Path d, DirectoryStream.Filter<?
775788
Objects.requireNonNull(d);
776789
Path dir = toAbsolutePathInternal(d);
777790
if (!pathIsInVfs(dir)) {
778-
boolean passed = false;
779791
try {
780-
return delegate.newDirectoryStream(dir, filter);
781-
} finally {
782-
finest("VFS.newDirectoryStream delegated '%s' %s", dir, passed ? "passed" : "did not pass");
792+
DirectoryStream<Path> ret = delegate.newDirectoryStream(dir, filter);
793+
finest("VFS.newDirectoryStream delegated '%s'", d);
794+
return ret;
795+
} catch (Throwable t) {
796+
finest(t, "VFS.newDirectoryStream delegated '%s'", d);
797+
throw t;
783798
}
784799
}
785800
Objects.requireNonNull(filter);
@@ -855,9 +870,14 @@ public Path toRealPath(Path p, LinkOption... linkOptions) throws IOException {
855870
Path path = toAbsolutePathInternal(p);
856871
boolean pathIsInVFS = pathIsInVfs(path);
857872
if (!pathIsInVFS) {
858-
Path ret = delegate.toRealPath(path, linkOptions);
859-
finest("VFS.toRealPath delegated '%s' -> '%s'", path, ret);
860-
return ret;
873+
try {
874+
Path ret = delegate.toRealPath(path, linkOptions);
875+
finest("VFS.toRealPath delegated '%s' -> '%s'", path, ret);
876+
return ret;
877+
} catch (Throwable t) {
878+
finest(t, "VFS.toRealPath delegated '%s'", path);
879+
throw t;
880+
}
861881
} else {
862882
Path result = path;
863883
if (shouldExtract(path)) {
@@ -878,9 +898,14 @@ public Map<String, Object> readAttributes(Path p, String attributes, LinkOption.
878898

879899
Path path = toAbsolutePathInternal(p);
880900
if (!pathIsInVfs(path)) {
881-
Map<String, Object> ret = delegate.readAttributes(path, attributes, options);
882-
finest("VFS.readAttributes delegated '%s' -> '%s'", path, ret);
883-
return ret;
901+
try {
902+
Map<String, Object> ret = delegate.readAttributes(path, attributes, options);
903+
finest("VFS.readAttributes delegated '%s' -> '%s'", path, ret);
904+
return ret;
905+
} catch (Throwable t) {
906+
finest(t, "VFS.readAttributes delegated '%s'", path);
907+
throw t;
908+
}
884909
}
885910
BaseEntry entry = getEntry(path);
886911
if (entry == null) {
@@ -933,7 +958,13 @@ public void setCurrentWorkingDirectory(Path d) {
933958
}
934959
} else {
935960
if (delegate != null) {
936-
delegate.setCurrentWorkingDirectory(dir);
961+
try {
962+
delegate.setCurrentWorkingDirectory(dir);
963+
finest("VFS.setCurrentWorkingDirectory delegated '%s'", d);
964+
} catch (Throwable t) {
965+
finest(t, "VFS.setCurrentWorkingDirectory delegated '%s'", d);
966+
throw t;
967+
}
937968
} else {
938969
// allow so that we can resolve relative paths pointing from real FS to VFS
939970
// {cwd}/real/fs/../ ... /../vfs_root
@@ -952,10 +983,16 @@ public void copy(Path s, Path t, CopyOption... options) throws IOException {
952983
if (pathIsInVfs(target)) {
953984
throw securityException("VFS.move", String.format("read-only filesystem, can't copy '%s' to '%s'", source, target));
954985
} else {
955-
if (allowHostIO == READ_WRITE && pathIsInVfs(source)) {
956-
FileSystem.super.copy(source, target, options);
957-
} else {
958-
delegate.copy(source, target, options);
986+
try {
987+
if (allowHostIO == READ_WRITE && pathIsInVfs(source)) {
988+
FileSystem.super.copy(source, target, options);
989+
} else {
990+
delegate.copy(source, target, options);
991+
}
992+
finest("VFS.copy delegated '%s' '%s'", source, target);
993+
} catch (Throwable thr) {
994+
finest(thr, "VFS.copy delegated '%s' '%s'", source, target);
995+
throw thr;
959996
}
960997
}
961998
}
@@ -967,7 +1004,12 @@ public void move(Path s, Path t, CopyOption... options) throws IOException {
9671004
Path source = toAbsolutePathInternal(s);
9681005
Path target = toAbsolutePathInternal(t);
9691006
if (!pathIsInVfs(source) && !pathIsInVfs(target)) {
970-
delegate.move(source, target, options);
1007+
try {
1008+
delegate.move(source, target, options);
1009+
} catch (Throwable thr) {
1010+
finest(thr, "VFS.move delegated '%s' '%s'", source, target);
1011+
throw thr;
1012+
}
9711013
} else {
9721014
throw securityException("VFS.move", String.format("read-only filesystem, can't move '%s' to '%s'", source, target));
9731015
}
@@ -978,7 +1020,14 @@ public Charset getEncoding(Path p) {
9781020
Objects.requireNonNull(p);
9791021
Path path = toAbsolutePathInternal(p);
9801022
if (!pathIsInVfs(path)) {
981-
return delegate.getEncoding(path);
1023+
try {
1024+
Charset ret = delegate.getEncoding(path);
1025+
finest("VFS.getEncoding delegated '%s'", path);
1026+
return ret;
1027+
} catch (Throwable t) {
1028+
finest(t, "VFS.getEncoding delegated '%s'", path);
1029+
throw t;
1030+
}
9821031
} else {
9831032
return null;
9841033
}
@@ -991,7 +1040,13 @@ public void createSymbolicLink(Path l, Path t, FileAttribute<?>... attrs) throws
9911040
Path link = toAbsolutePathInternal(l);
9921041
Path target = toAbsolutePathInternal(t);
9931042
if (!pathIsInVfs(link) && !pathIsInVfs(target)) {
994-
delegate.createSymbolicLink(link, target, attrs);
1043+
try {
1044+
delegate.createSymbolicLink(link, target, attrs);
1045+
finest("VFS.createSymbolicLink delegated '%s' '%s'", link, target);
1046+
} catch (Throwable thr) {
1047+
finest(thr, "VFS.createSymbolicLink delegated '%s' '%s'", link, target);
1048+
throw thr;
1049+
}
9951050
} else {
9961051
throw securityException("VFS.createSymbolicLink", String.format("read-only filesystem, can't create symbolic link from '%s' to '%s'", link, target));
9971052
}
@@ -1004,7 +1059,13 @@ public void createLink(Path l, Path e) throws IOException {
10041059
Path link = toAbsolutePathInternal(l);
10051060
Path existing = toAbsolutePathInternal(e);
10061061
if (!pathIsInVfs(link) && !pathIsInVfs(existing)) {
1007-
delegate.createLink(link, existing);
1062+
try {
1063+
delegate.createLink(link, existing);
1064+
finest("VFS.createLink delegated '%s' '%s'", link, existing);
1065+
} catch (Throwable thr) {
1066+
finest(thr, "VFS.createLink delegated '%s' '%s'", link, existing);
1067+
throw thr;
1068+
}
10081069
} else {
10091070
throw securityException("VFS.createLink", String.format("read-only filesystem, can't create link '%s' to '%s'", link, existing));
10101071
}
@@ -1015,7 +1076,14 @@ public Path readSymbolicLink(Path l) throws IOException {
10151076
Objects.requireNonNull(l);
10161077
Path link = toAbsolutePathInternal(l);
10171078
if (!pathIsInVfs(link)) {
1018-
return delegate.readSymbolicLink(link);
1079+
try {
1080+
Path ret = delegate.readSymbolicLink(link);
1081+
finest("VFS.readSymbolicLink delegated '%s' '%s'", link, ret);
1082+
return ret;
1083+
} catch (Throwable t) {
1084+
finest(t, "VFS.readSymbolicLink delegated '%s'", link);
1085+
throw t;
1086+
}
10191087
} else {
10201088
throw securityException("VFS.readSymbolicLink", String.format("reading symbolic links in VirtualFileSystem not supported %s", link));
10211089
}
@@ -1026,7 +1094,13 @@ public void setAttribute(Path p, String attribute, Object value, LinkOption... o
10261094
Objects.requireNonNull(p);
10271095
Path path = toAbsolutePathInternal(p);
10281096
if (!pathIsInVfs(path)) {
1029-
delegate.setAttribute(path, attribute, value, options);
1097+
try {
1098+
delegate.setAttribute(path, attribute, value, options);
1099+
finest("VFS.setAttribute delegated '%s' '%s'", path, attribute);
1100+
} catch (Throwable t) {
1101+
finest(t, "VFS.setAttribute delegated '%s' '%s'", path, attribute);
1102+
throw t;
1103+
}
10301104
} else {
10311105
throw securityException("VFS.setAttribute", String.format("read-only filesystem, can't set attribute '%s' for '%s", attribute, p));
10321106
}
@@ -1037,14 +1111,28 @@ public String getMimeType(Path p) {
10371111
Objects.requireNonNull(p);
10381112
Path path = toAbsolutePathInternal(p);
10391113
if (!pathIsInVfs(path)) {
1040-
return delegate.getMimeType(path);
1114+
try {
1115+
String ret = delegate.getMimeType(path);
1116+
finest("VFS.getMimeType delegated '%s' '%s", path, ret);
1117+
return ret;
1118+
} catch (Throwable t) {
1119+
finest(t, "VFS.getMimeType delegated '%s'", path);
1120+
throw t;
1121+
}
10411122
}
10421123
return null;
10431124
}
10441125

10451126
@Override
10461127
public Path getTempDirectory() {
1047-
return delegate.getTempDirectory();
1128+
try {
1129+
Path ret = delegate.getTempDirectory();
1130+
finest("VFS.getTempDirectory delegated '%s'", ret);
1131+
return ret;
1132+
} catch (Throwable t) {
1133+
finest(t, "VFS.getTempDirectory delegated");
1134+
throw t;
1135+
}
10481136
}
10491137

10501138
private static void warn(String msgFormat, Object... args) {
@@ -1071,6 +1159,12 @@ private static void finest(String msgFormat, Object... args) {
10711159
}
10721160
}
10731161

1162+
private static void finest(Throwable t, String msgFormat, Object... args) {
1163+
if (LOGGER.isLoggable(Level.FINEST)) {
1164+
LOGGER.log(Level.FINEST, String.format(msgFormat, args), t);
1165+
}
1166+
}
1167+
10741168
private static SecurityException securityException(String from, String msg) {
10751169
finer("%s %s", from, msg);
10761170
throw new SecurityException(msg);

0 commit comments

Comments
 (0)