Skip to content

Commit 803fecc

Browse files
committed
[GR-59492] System.out/err usage in PolyglotLoggers.
PullRequest: graal/19455
2 parents 274e2ce + 8416b27 commit 803fecc

File tree

7 files changed

+82
-28
lines changed

7 files changed

+82
-28
lines changed

substratevm/src/com.oracle.svm.truffle/src/com/oracle/svm/truffle/TruffleBaseFeature.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import java.util.function.Consumer;
6363
import java.util.stream.Stream;
6464

65+
import com.oracle.svm.core.log.Log;
6566
import org.graalvm.collections.Pair;
6667
import org.graalvm.home.HomeFinder;
6768
import org.graalvm.home.Version;
@@ -1477,6 +1478,17 @@ public Object transform(Object receiver, Object originalValue) {
14771478
}
14781479
}
14791480

1481+
@TargetClass(className = "com.oracle.truffle.polyglot.PolyglotEngineImpl", onlyWith = TruffleBaseFeature.IsEnabled.class)
1482+
final class Target_com_oracle_truffle_polyglot_PolyglotEngineImpl {
1483+
@Substitute
1484+
static void logFallback(String message) {
1485+
try (Log log = Log.log()) {
1486+
log.string(message.getBytes(StandardCharsets.UTF_8));
1487+
log.flush();
1488+
}
1489+
}
1490+
}
1491+
14801492
@TargetClass(className = "com.oracle.truffle.object.CoreLocations$DynamicObjectFieldLocation", onlyWith = TruffleBaseFeature.IsEnabled.class)
14811493
final class Target_com_oracle_truffle_object_CoreLocations_DynamicObjectFieldLocation {
14821494
@Alias @RecomputeFieldValue(kind = Kind.AtomicFieldUpdaterOffset) //

truffle/src/com.oracle.truffle.polyglot/src/com/oracle/truffle/polyglot/InstrumentCache.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
*/
4141
package com.oracle.truffle.polyglot;
4242

43-
import java.io.PrintStream;
4443
import java.util.ArrayList;
4544
import java.util.Collection;
4645
import java.util.Collections;
@@ -291,7 +290,6 @@ SandboxPolicy getSandboxPolicy() {
291290
}
292291

293292
private static void emitWarning(String message, Object... args) {
294-
PrintStream out = System.err;
295-
out.printf(message + "%n", args);
293+
PolyglotEngineImpl.logFallback(String.format(message + "%n", args));
296294
}
297295
}

truffle/src/com.oracle.truffle.polyglot/src/com/oracle/truffle/polyglot/InternalResourceRoots.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
import org.graalvm.nativeimage.ImageInfo;
4848
import org.graalvm.nativeimage.ProcessProperties;
4949

50-
import java.io.PrintStream;
5150
import java.nio.file.InvalidPathException;
5251
import java.nio.file.Path;
5352
import java.nio.file.Paths;
@@ -333,14 +332,12 @@ static boolean isTraceInternalResourceEvents() {
333332

334333
static void logInternalResourceEvent(String message, Object... args) {
335334
if (isTraceInternalResourceEvents()) {
336-
PrintStream out = System.err;
337-
out.printf("[engine][resource] " + message + "%n", args);
335+
PolyglotEngineImpl.logFallback(String.format("[engine][resource] " + message + "%n", args));
338336
}
339337
}
340338

341339
private static void emitWarning(String message, Object... args) {
342-
PrintStream out = System.err;
343-
out.printf(message + "%n", args);
340+
PolyglotEngineImpl.logFallback(String.format(message + "%n", args));
344341
}
345342

346343
record Root(Path path, Kind kind, List<InternalResourceCache> caches) {

truffle/src/com.oracle.truffle.polyglot/src/com/oracle/truffle/polyglot/JDKSupport.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141
package com.oracle.truffle.polyglot;
4242

4343
import java.io.IOException;
44-
import java.io.PrintStream;
44+
import java.io.PrintWriter;
45+
import java.io.StringWriter;
4546
import java.lang.invoke.MethodHandle;
4647
import java.lang.invoke.MethodHandles;
4748
import java.lang.invoke.MethodType;
@@ -155,13 +156,15 @@ private static void performTruffleAttachLoadFailureAction(String reason, Throwab
155156
case "ignore" -> {
156157
}
157158
case "warn" -> {
158-
PrintStream err = System.err;
159-
err.println(formatErrorMessage(reason));
159+
PolyglotEngineImpl.logFallback(formatErrorMessage(reason));
160160
}
161161
case "diagnose" -> {
162-
PrintStream err = System.err;
163-
err.println(formatErrorMessage(reason));
164-
t.printStackTrace(err);
162+
StringWriter message = new StringWriter();
163+
try (PrintWriter err = new PrintWriter(message)) {
164+
err.println(formatErrorMessage(reason));
165+
t.printStackTrace(err);
166+
}
167+
PolyglotEngineImpl.logFallback(message.toString());
165168
}
166169
case "throw" -> throw new InternalError(formatErrorMessage(reason), t);
167170
default -> throw new IllegalArgumentException("Invalid polyglotimpl.AttachLibraryFailureAction system property value. Supported values are ignore, warn, diagnose, throw");

truffle/src/com.oracle.truffle.polyglot/src/com/oracle/truffle/polyglot/LanguageCache.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
package com.oracle.truffle.polyglot;
4242

4343
import java.io.IOException;
44-
import java.io.PrintStream;
4544
import java.lang.reflect.Method;
4645
import java.net.URL;
4746
import java.nio.file.NoSuchFileException;
@@ -657,8 +656,7 @@ SandboxPolicy getSandboxPolicy() {
657656
}
658657

659658
private static void emitWarning(String message, Object... args) {
660-
PrintStream out = System.err;
661-
out.printf(message + "%n", args);
659+
PolyglotEngineImpl.logFallback(String.format(message + "%n", args));
662660
}
663661

664662
private static final class HostLanguageProvider extends TruffleLanguageProvider {

truffle/src/com.oracle.truffle.polyglot/src/com/oracle/truffle/polyglot/PolyglotEngineImpl.java

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2361,16 +2361,19 @@ static void logCloseOnCollectedError(PolyglotEngineImpl engine, String reason, T
23612361
case Ignore -> {
23622362
}
23632363
case Print -> {
2364-
PrintStream errStream = System.err;
2365-
errStream.printf("""
2366-
[engine] WARNING: %s
2367-
To customize the behavior of this warning, use 'engine.CloseOnGCFailureAction' option or the 'polyglot.engine.CloseOnGCFailureAction' system property.
2368-
The accepted values are:
2369-
- Ignore: Do not print this warning.
2370-
- Print: Print this warning (default value).
2371-
- Throw: Throw an exception instead of printing this warning.
2372-
""", reason);
2373-
exception.printStackTrace(errStream);
2364+
StringWriter message = new StringWriter();
2365+
try (PrintWriter errWriter = new PrintWriter(message)) {
2366+
errWriter.printf("""
2367+
[engine] WARNING: %s
2368+
To customize the behavior of this warning, use 'engine.CloseOnGCFailureAction' option or the 'polyglot.engine.CloseOnGCFailureAction' system property.
2369+
The accepted values are:
2370+
- Ignore: Do not print this warning.
2371+
- Print: Print this warning (default value).
2372+
- Throw: Throw an exception instead of printing this warning.
2373+
""", reason);
2374+
exception.printStackTrace(errWriter);
2375+
}
2376+
logFallback(message.toString());
23742377
}
23752378
case Throw -> throw new RuntimeException(reason, exception);
23762379
}
@@ -2475,4 +2478,19 @@ or use the default runtime (no JIT compilation of polyglot code) by passing -Dtr
24752478
impl.getRootImpl().validateVirtualThreadCreation(getEngineOptionValues());
24762479
}
24772480

2481+
/**
2482+
* Logs a message when other logging mechanisms, such as {@link TruffleLogger} or the context's
2483+
* error stream, are unavailable. This can occur, for instance, in the event of a log handler
2484+
* failure.
2485+
* <p>
2486+
* On HotSpot, this method writes the message to {@code System.err}. When running on a native
2487+
* image, this method is substituted to delegate logging to the native image's
2488+
* {@link org.graalvm.nativeimage.LogHandler}.
2489+
*
2490+
* @param message the message to log
2491+
*/
2492+
static void logFallback(String message) {
2493+
PrintStream err = System.err;
2494+
err.println(message);
2495+
}
24782496
}

truffle/src/com.oracle.truffle.polyglot/src/com/oracle/truffle/polyglot/PolyglotLoggers.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
import java.util.Objects;
6060
import java.util.ResourceBundle;
6161
import java.util.Set;
62+
import java.util.concurrent.atomic.AtomicBoolean;
6263
import java.util.function.Function;
6364
import java.util.logging.ErrorManager;
6465
import java.util.logging.Formatter;
@@ -300,7 +301,7 @@ final void checkClosed() {
300301

301302
final synchronized void reportHandlerError(int errorKind, Throwable t) {
302303
if (errorManager == null) {
303-
errorManager = new ErrorManager();
304+
errorManager = new PolyglotErrorManager();
304305
}
305306
Exception exception;
306307
if (t instanceof Exception) {
@@ -785,4 +786,31 @@ public TruffleLogger apply(String loggerId) {
785786
return EngineAccessor.LANGUAGE.getLogger(loggerId, null, loggersCache);
786787
}
787788
}
789+
790+
private static final class PolyglotErrorManager extends ErrorManager {
791+
792+
private final AtomicBoolean reported = new AtomicBoolean();
793+
794+
PolyglotErrorManager() {
795+
}
796+
797+
@Override
798+
public void error(String msg, Exception ex, int code) {
799+
if (reported.getAndSet(true)) {
800+
return;
801+
}
802+
StringWriter content = new StringWriter();
803+
try (PrintWriter out = new PrintWriter(content)) {
804+
String text = "java.util.logging.ErrorManager: " + code;
805+
if (msg != null) {
806+
text = text + ": " + msg;
807+
}
808+
out.println(text);
809+
if (ex != null) {
810+
ex.printStackTrace(out);
811+
}
812+
}
813+
PolyglotEngineImpl.logFallback(content.toString());
814+
}
815+
}
788816
}

0 commit comments

Comments
 (0)