Search in sources :

Example 16 with TruffleLogger

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

the class LoggingTest method testInvalidId.

@Test
public void testInvalidId() {
    Context.Builder builder = newContextBuilder();
    TestHandler handler = new TestHandler();
    LoggingLanguageFirst.action = new BiPredicate<>() {

        @Override
        public boolean test(LoggingContext ctx, TruffleLogger[] defaultLoggers) {
            try {
                TruffleLogger.getLogger("LoggingTest-Invalid-Id");
                Assert.fail("Expected IllegalArgumentException");
            } catch (IllegalArgumentException iae) {
            // Expected exception
            }
            try {
                TruffleLogger.getLogger("LoggingTest-Invalid-Id", LoggingTest.class);
                Assert.fail("Expected IllegalArgumentException");
            } catch (IllegalArgumentException iae) {
            // Expected exception
            }
            try {
                TruffleLogger.getLogger("LoggingTest-Invalid-Id", "global");
                Assert.fail("Expected IllegalArgumentException");
            } catch (IllegalArgumentException iae) {
            // Expected exception
            }
            return false;
        }
    };
    try (Context ctx = builder.logHandler(handler).build()) {
        ctx.eval(LoggingLanguageFirst.ID, "");
    }
}
Also used : TruffleContext(com.oracle.truffle.api.TruffleContext) Context(org.graalvm.polyglot.Context) TruffleLogger(com.oracle.truffle.api.TruffleLogger) Test(org.junit.Test)

Example 17 with TruffleLogger

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

the class LoggingTest method testNoContextLoggingDefaultTruffleLogger.

@Test
public void testNoContextLoggingDefaultTruffleLogger() {
    TestHandler engineHandler = new TestHandler();
    try (Engine eng = newEngineBuilder().options(createLoggingOptions(LoggingLanguageFirst.ID, null, Level.FINE.toString(), ProxyInstrument.ID, null, Level.FINE.toString())).logHandler(engineHandler).build()) {
        AtomicReference<TruffleLogger> loggerRef = new AtomicReference<>();
        LoggingLanguageFirst.action = new BiPredicate<>() {

            @Override
            public boolean test(LoggingContext ctx, TruffleLogger[] loggers) {
                Assert.assertTrue(loggers.length > 0);
                loggerRef.set(loggers[0]);
                return true;
            }
        };
        try (Context ctx = newContextBuilder().engine(eng).build()) {
            ctx.eval(LoggingLanguageFirst.ID, "");
            try {
                loggerRef.get().log(Level.INFO, "Should not be logged.");
                Assert.assertFalse(assertionsEnabled());
            } catch (IllegalStateException e) {
            // Expected
            }
            List<Map.Entry<Level, String>> expectedInEngine = new ArrayList<>();
            expectedInEngine.addAll(createExpectedLog(LoggingLanguageFirst.ID, Level.FINE, Collections.emptyMap()));
            Assert.assertEquals(expectedInEngine, engineHandler.getLog());
        }
    }
}
Also used : TruffleContext(com.oracle.truffle.api.TruffleContext) Context(org.graalvm.polyglot.Context) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) TruffleLogger(com.oracle.truffle.api.TruffleLogger) Engine(org.graalvm.polyglot.Engine) Test(org.junit.Test)

Example 18 with TruffleLogger

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

the class LoggingTest method testContextBoundLoggerLoggersIdentitySingleContext.

@Test
public void testContextBoundLoggerLoggersIdentitySingleContext() {
    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.assertSame(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());
        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)

Example 19 with TruffleLogger

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

the class LoggingTest method testContextBoundLoggerNotEnteredContext.

@Test
public void testContextBoundLoggerNotEnteredContext() {
    TestHandler handler1 = new TestHandler();
    TestHandler handler2 = new TestHandler();
    AtomicReference<TruffleLogger> loggerRef = new AtomicReference<>();
    LoggingLanguageFirst.action = (ctx, loggers) -> {
        TruffleLogger logger = ctx.getEnv().getLogger(AbstractLoggingLanguage.LOGGER_NAMES[0]);
        Assert.assertNull(loggerRef.getAndSet(logger));
        LoggingLanguageFirst.action = null;
        return false;
    };
    try (Context ctx1 = newContextBuilder().options(createLoggingOptions(null, null, Level.FINEST.toString())).logHandler(handler1).build();
        Context ctx2 = newContextBuilder().options(createLoggingOptions(null, null, Level.FINEST.toString())).logHandler(handler2).build()) {
        ctx2.eval(LoggingLanguageFirst.ID, "");
        ctx1.enter();
        ctx1.eval(LoggingLanguageFirst.ID, "");
        try {
            TruffleLogger logger = loggerRef.get();
            Assert.assertNotNull(logger);
            logger.log(Level.FINEST, "bound");
        } finally {
            ctx1.leave();
        }
        Assert.assertEquals(createExpectedLog(LoggingLanguageFirst.ID, Level.FINEST, Collections.emptyMap()), handler1.getLog());
        Assert.assertEquals(Collections.singletonList(new AbstractMap.SimpleImmutableEntry<>(Level.FINEST, "bound")), handler2.getLog());
    }
}
Also used : TruffleContext(com.oracle.truffle.api.TruffleContext) Context(org.graalvm.polyglot.Context) TruffleLogger(com.oracle.truffle.api.TruffleLogger) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 20 with TruffleLogger

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

the class LoggingTest method testDefaultLevelMultipleContexts.

@Test
public void testDefaultLevelMultipleContexts() {
    String parentLoggerName = "testDefaultLevelMultipleContexts";
    String childLoggerName = String.format("%s.child", parentLoggerName);
    Map<String, Level> setLevelsMap = new HashMap<>();
    setLevelsMap.put(parentLoggerName, Level.FINE);
    setLevelsMap.put(childLoggerName, Level.SEVERE);
    Context.Builder builder = newContextBuilder();
    for (Map.Entry<String, Level> levelsMapEntry : setLevelsMap.entrySet()) {
        builder.options(createLoggingOptions(LoggingLanguageFirst.ID, levelsMapEntry.getKey(), levelsMapEntry.getValue().toString()));
    }
    TestHandler handler = new TestHandler();
    TruffleLogger parentLogger = TruffleLogger.getLogger(LoggingLanguageFirst.ID, parentLoggerName);
    TruffleLogger childLogger = TruffleLogger.getLogger(LoggingLanguageFirst.ID, childLoggerName);
    AbstractLoggingLanguage.action = (loggingContext, defaultLoggers) -> {
        parentLogger.log(Level.INFO, parentLogger.getName());
        childLogger.log(Level.INFO, childLogger.getName());
        return false;
    };
    try (Context ctx = builder.logHandler(handler).build()) {
        TestHandler handler2 = new TestHandler();
        try (Context ctx2 = newContextBuilder().logHandler(handler2).build()) {
            ctx.eval(LoggingLanguageFirst.ID, "");
            ctx2.eval(LoggingLanguageFirst.ID, "");
            List<Map.Entry<Level, String>> expected = Arrays.asList(new AbstractMap.SimpleEntry<>(Level.INFO, String.format("%s.%s", LoggingLanguageFirst.ID, parentLoggerName)));
            Assert.assertEquals(expected, handler.getLog());
            expected = new ArrayList<>();
            expected.add(new AbstractMap.SimpleEntry<>(Level.INFO, String.format("%s.%s", LoggingLanguageFirst.ID, parentLoggerName)));
            expected.add(new AbstractMap.SimpleEntry<>(Level.INFO, String.format("%s.%s", LoggingLanguageFirst.ID, childLoggerName)));
            Assert.assertEquals(expected, handler2.getLog());
        }
    }
}
Also used : TruffleContext(com.oracle.truffle.api.TruffleContext) Context(org.graalvm.polyglot.Context) HashMap(java.util.HashMap) AbstractMap(java.util.AbstractMap) TruffleLogger(com.oracle.truffle.api.TruffleLogger) Level(java.util.logging.Level) HashMap(java.util.HashMap) Map(java.util.Map) AbstractMap(java.util.AbstractMap) 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