|
34 | 34 | import java.io.ByteArrayOutputStream;
|
35 | 35 | import java.io.IOException;
|
36 | 36 | import java.io.OutputStream;
|
| 37 | +import java.io.PrintStream; |
37 | 38 | import java.lang.reflect.Method;
|
38 | 39 | import java.nio.file.Paths;
|
39 | 40 | import java.util.ArrayList;
|
|
46 | 47 | import java.util.Map;
|
47 | 48 | import java.util.Set;
|
48 | 49 | import java.util.concurrent.atomic.AtomicInteger;
|
| 50 | +import java.util.logging.Handler; |
| 51 | +import java.util.logging.LogRecord; |
49 | 52 | import org.graalvm.options.OptionCategory;
|
50 | 53 | import org.graalvm.options.OptionDescriptors;
|
51 | 54 | import org.graalvm.options.OptionKey;
|
@@ -852,6 +855,59 @@ public void testTemporaryEngine() throws Exception {
|
852 | 855 | assertEquals(1, firstLangCtx.disposeThreadCount);
|
853 | 856 | }
|
854 | 857 |
|
| 858 | + @Test |
| 859 | + public void testLogging() throws Exception { |
| 860 | + setPatchable(FIRST); |
| 861 | + // In context pre-initialization there is no sdk Context to set log handler, |
| 862 | + // logging is done to System.out |
| 863 | + final PrintStream origOut = System.out; |
| 864 | + final ByteArrayOutputStream preInitOut = new ByteArrayOutputStream(); |
| 865 | + try (PrintStream printStream = new PrintStream(preInitOut)) { |
| 866 | + System.setOut(printStream); |
| 867 | + System.setProperty("polyglot.log.engine.level", "FINE"); |
| 868 | + doContextPreinitialize(FIRST); |
| 869 | + } finally { |
| 870 | + System.setOut(origOut); |
| 871 | + System.getProperties().remove("polyglot.log.engine.level"); |
| 872 | + } |
| 873 | + final String preInitLog = preInitOut.toString("UTF-8"); |
| 874 | + assertTrue(preInitLog.contains("Pre-initialized context for language: ContextPreInitializationFirst")); |
| 875 | + List<CountingContext> contexts = new ArrayList<>(emittedContexts); |
| 876 | + assertEquals(1, contexts.size()); |
| 877 | + final CountingContext firstLangCtx = findContext(FIRST, contexts); |
| 878 | + assertNotNull(firstLangCtx); |
| 879 | + assertEquals(1, firstLangCtx.createContextCount); |
| 880 | + assertEquals(1, firstLangCtx.initializeContextCount); |
| 881 | + assertEquals(0, firstLangCtx.patchContextCount); |
| 882 | + assertEquals(0, firstLangCtx.disposeContextCount); |
| 883 | + assertEquals(0, firstLangCtx.initializeThreadCount); |
| 884 | + assertEquals(0, firstLangCtx.disposeThreadCount); |
| 885 | + final TestHandler testHandler = new TestHandler(); |
| 886 | + final Context ctx = Context.newBuilder().option("log.engine.level", "FINE").logHandler(testHandler).build(); |
| 887 | + Value res = ctx.eval(Source.create(FIRST, "test")); |
| 888 | + assertEquals("test", res.asString()); |
| 889 | + assertEquals(1, testHandler.logs.size()); |
| 890 | + assertEquals(FIRST, testHandler.logs.get(0).getParameters()[0]); |
| 891 | + assertEquals("Successfully patched context of language: {0}", testHandler.logs.get(0).getMessage()); |
| 892 | + contexts = new ArrayList<>(emittedContexts); |
| 893 | + assertEquals(1, contexts.size()); |
| 894 | + assertEquals(1, firstLangCtx.createContextCount); |
| 895 | + assertEquals(1, firstLangCtx.initializeContextCount); |
| 896 | + assertEquals(1, firstLangCtx.patchContextCount); |
| 897 | + assertEquals(0, firstLangCtx.disposeContextCount); |
| 898 | + assertEquals(1, firstLangCtx.initializeThreadCount); |
| 899 | + assertEquals(0, firstLangCtx.disposeThreadCount); |
| 900 | + ctx.close(); |
| 901 | + contexts = new ArrayList<>(emittedContexts); |
| 902 | + assertEquals(1, contexts.size()); |
| 903 | + assertEquals(1, firstLangCtx.createContextCount); |
| 904 | + assertEquals(1, firstLangCtx.initializeContextCount); |
| 905 | + assertEquals(1, firstLangCtx.patchContextCount); |
| 906 | + assertEquals(1, firstLangCtx.disposeContextCount); |
| 907 | + assertEquals(1, firstLangCtx.initializeThreadCount); |
| 908 | + assertEquals(1, firstLangCtx.disposeThreadCount); |
| 909 | + } |
| 910 | + |
855 | 911 | private static void resetSystemPropertiesOptions() {
|
856 | 912 | System.getProperties().remove("polyglot.engine.PreinitializeContexts");
|
857 | 913 | System.getProperties().remove(SYS_OPTION1_KEY);
|
@@ -1097,4 +1153,21 @@ protected void initializeContext(CountingContext context) throws Exception {
|
1097 | 1153 | @TruffleLanguage.Registration(id = INTERNAL, name = INTERNAL, version = "1.0", mimeType = INTERNAL, internal = true)
|
1098 | 1154 | public static final class ContextPreInitializationTestInternalLanguage extends BaseLanguage {
|
1099 | 1155 | }
|
| 1156 | + |
| 1157 | + private static final class TestHandler extends Handler { |
| 1158 | + final List<LogRecord> logs = new ArrayList<>(); |
| 1159 | + |
| 1160 | + @Override |
| 1161 | + public void publish(LogRecord record) { |
| 1162 | + logs.add(record); |
| 1163 | + } |
| 1164 | + |
| 1165 | + @Override |
| 1166 | + public void flush() { |
| 1167 | + } |
| 1168 | + |
| 1169 | + @Override |
| 1170 | + public void close() throws SecurityException { |
| 1171 | + } |
| 1172 | + } |
1100 | 1173 | }
|
0 commit comments