Search in sources :

Example 1 with TruffleLogger

use of com.oracle.truffle.api.TruffleLogger in project graal by oracle.

the class ContextPreInitializationTest method testEngineBoundLoggers.

@Test
public void testEngineBoundLoggers() throws Exception {
    String loggerName = "engine";
    String loggerLevelOptionName = String.format("log.%s.level", loggerName);
    setPatchable(FIRST);
    // In context pre-initialization there is no sdk Context to set log handler,
    // logging is done to System.err
    BaseLanguage.registerAction(ContextPreInitializationTestFirstLanguage.class, ActionKind.ON_INITIALIZE_CONTEXT, (env) -> {
        Object engine = TestAPIAccessor.engineAccess().getCurrentPolyglotEngine();
        TruffleLogger log = getEngineLogger(engine);
        log.log(Level.INFO, "preInit:info");
        log.log(Level.FINEST, "preInit:finest");
    });
    BaseLanguage.registerAction(ContextPreInitializationTestFirstLanguage.class, ActionKind.ON_PATCH_CONTEXT, (env) -> {
        Object engine = TestAPIAccessor.engineAccess().getCurrentPolyglotEngine();
        TruffleLogger log = getEngineLogger(engine);
        log.log(Level.INFO, "patch:info");
        log.log(Level.FINEST, "patch:finest");
    });
    final PrintStream origErr = System.err;
    final ByteArrayOutputStream preInitErr = new ByteArrayOutputStream();
    String origLogFile = System.getProperty("polyglot.log.file");
    try (PrintStream printStream = new PrintStream(preInitErr)) {
        System.setErr(printStream);
        System.setProperty("polyglot." + loggerLevelOptionName, "FINE");
        System.clearProperty("polyglot.log.file");
        doContextPreinitialize(FIRST);
    } finally {
        System.setErr(origErr);
        System.clearProperty("polyglot." + loggerLevelOptionName);
        if (origLogFile != null) {
            System.setProperty("polyglot.log.file", origLogFile);
        }
    }
    final String preInitLog = preInitErr.toString("UTF-8");
    assertTrue(preInitLog.contains("preInit:info"));
    assertFalse(preInitLog.contains("preInit:finest"));
    List<CountingContext> contexts = new ArrayList<>(emittedContexts);
    assertEquals(1, contexts.size());
    final CountingContext firstLangCtx = findContext(FIRST, contexts);
    assertNotNull(firstLangCtx);
    final TestHandler testHandler = new TestHandler(loggerName);
    try (Context ctx = Context.newBuilder().option(loggerLevelOptionName, "FINEST").logHandler(testHandler).build()) {
        Value res = ctx.eval(Source.create(FIRST, "test"));
        assertEquals("test", res.asString());
        Set<String> messages = testHandler.logs.stream().map(LogRecord::getMessage).collect(Collectors.toSet());
        assertTrue(messages.contains("patch:info"));
        assertTrue(messages.contains("patch:finest"));
    }
}
Also used : Context(org.graalvm.polyglot.Context) TruffleContext(com.oracle.truffle.api.TruffleContext) PrintStream(java.io.PrintStream) TruffleLogger(com.oracle.truffle.api.TruffleLogger) ArrayList(java.util.ArrayList) Value(org.graalvm.polyglot.Value) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 2 with TruffleLogger

use of com.oracle.truffle.api.TruffleLogger in project graal by oracle.

the class LoggingTest method testNoParameters.

@Test
public void testNoParameters() {
    String message = "Test{0}";
    AbstractLoggingLanguage.action = new BiPredicate<>() {

        @Override
        public boolean test(final LoggingContext context, final TruffleLogger[] loggers) {
            for (TruffleLogger logger : loggers) {
                logger.log(Level.WARNING, message);
                logger.log(Level.WARNING, () -> message);
                logger.logp(Level.WARNING, "C", "M", message);
                logger.logp(Level.WARNING, "C", "M", () -> message);
            }
            return false;
        }
    };
    final TestHandler handler = new TestHandler();
    try (Context ctx1 = newContextBuilder().logHandler(handler).build()) {
        ctx1.eval(LoggingLanguageFirst.ID, "");
        int loggedCount = 0;
        for (LogRecord r : handler.getRawLog()) {
            loggedCount++;
            Assert.assertEquals(message, r.getMessage());
            Assert.assertNull(r.getParameters());
        }
        Assert.assertEquals(4 * AbstractLoggingLanguage.LOGGER_NAMES.length, loggedCount);
    }
}
Also used : TruffleContext(com.oracle.truffle.api.TruffleContext) Context(org.graalvm.polyglot.Context) LogRecord(java.util.logging.LogRecord) TruffleLogger(com.oracle.truffle.api.TruffleLogger) Test(org.junit.Test)

Example 3 with TruffleLogger

use of com.oracle.truffle.api.TruffleLogger in project graal by oracle.

the class LoggingTest method assertLoggerOutput.

private static void assertLoggerOutput(String expected, Consumer<TruffleLogger> log) {
    AbstractLoggingLanguage.action = new BiPredicate<>() {

        @Override
        public boolean test(final LoggingContext context, final TruffleLogger[] loggers) {
            log.accept(loggers[0]);
            return false;
        }
    };
    final ByteArrayOutputStream logStream = new ByteArrayOutputStream();
    try (Context ctx1 = Context.newBuilder().logHandler(logStream).build()) {
        ctx1.eval(LoggingLanguageFirst.ID, "");
        final String output = new String(logStream.toByteArray());
        Assert.assertTrue(output, output.indexOf(expected) > 0);
    }
}
Also used : TruffleContext(com.oracle.truffle.api.TruffleContext) Context(org.graalvm.polyglot.Context) TruffleLogger(com.oracle.truffle.api.TruffleLogger) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 4 with TruffleLogger

use of com.oracle.truffle.api.TruffleLogger in project graal by oracle.

the class LoggingTest method testParametersObjects.

@Test
public void testParametersObjects() {
    AbstractLoggingLanguage.action = new BiPredicate<>() {

        @Override
        public boolean test(final LoggingContext context, final TruffleLogger[] loggers) {
            for (TruffleLogger logger : loggers) {
                logger.log(Level.WARNING, "Parameters", new LoggingLanguageObject("passed"));
            }
            return false;
        }
    };
    final TestHandler handler1 = new TestHandler();
    try (Context ctx1 = newContextBuilder().logHandler(handler1).build()) {
        ctx1.eval(LoggingLanguageFirst.ID, "");
        boolean logged = false;
        for (LogRecord r : handler1.getRawLog()) {
            logged = true;
            Assert.assertEquals("Parameters", r.getMessage());
            Assert.assertArrayEquals(new Object[] { "passed" }, r.getParameters());
        }
        Assert.assertTrue(logged);
    }
}
Also used : TruffleContext(com.oracle.truffle.api.TruffleContext) Context(org.graalvm.polyglot.Context) LogRecord(java.util.logging.LogRecord) TruffleLogger(com.oracle.truffle.api.TruffleLogger) Test(org.junit.Test)

Example 5 with TruffleLogger

use of com.oracle.truffle.api.TruffleLogger in project graal by oracle.

the class LoggingTest method testContextBoundLoggerLoggersIdentityMultipleContexts.

@Test
public void testContextBoundLoggerLoggersIdentityMultipleContexts() {
    TestHandler handler = new TestHandler();
    Map<String, TruffleLogger> loggersByName = new HashMap<>();
    LoggingLanguageFirst.action = (ctx, loggers) -> {
        boolean firstRun = loggersByName.isEmpty();
        for (String loggerName : AbstractLoggingLanguage.LOGGER_NAMES) {
            TruffleLogger logger = ctx.env.getLogger(loggerName);
            Assert.assertNotNull(logger);
            if (firstRun) {
                loggersByName.put(loggerName, logger);
            } else {
                Assert.assertNotNull(loggersByName.get(loggerName));
                Assert.assertNotSame(loggersByName.get(loggerName), logger);
            }
        }
        return false;
    };
    try (Context ctx = newContextBuilder().options(createLoggingOptions(null, null, Level.FINEST.toString())).logHandler(handler).build()) {
        ctx.eval(LoggingLanguageFirst.ID, "");
    }
    Assert.assertFalse(loggersByName.isEmpty());
    try (Context ctx = newContextBuilder().options(createLoggingOptions(null, null, Level.FINEST.toString())).logHandler(handler).build()) {
        ctx.eval(LoggingLanguageFirst.ID, "");
    }
}
Also used : TruffleContext(com.oracle.truffle.api.TruffleContext) Context(org.graalvm.polyglot.Context) HashMap(java.util.HashMap) TruffleLogger(com.oracle.truffle.api.TruffleLogger) Test(org.junit.Test)

Aggregations

TruffleLogger (com.oracle.truffle.api.TruffleLogger)23 TruffleContext (com.oracle.truffle.api.TruffleContext)16 Context (org.graalvm.polyglot.Context)16 Test (org.junit.Test)15 ArrayList (java.util.ArrayList)4 Level (java.util.logging.Level)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 HashMap (java.util.HashMap)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 LogRecord (java.util.logging.LogRecord)3 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)2 TruffleInstrument (com.oracle.truffle.api.instrumentation.TruffleInstrument)2 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)2 PrintStream (java.io.PrintStream)2 AbstractMap (java.util.AbstractMap)2 OptionValues (org.graalvm.options.OptionValues)2 Engine (org.graalvm.polyglot.Engine)2 Value (org.graalvm.polyglot.Value)2 CompilerDirectives (com.oracle.truffle.api.CompilerDirectives)1 InstrumentClientInstrumenter (com.oracle.truffle.api.instrumentation.InstrumentationHandler.InstrumentClientInstrumenter)1