Search in sources :

Example 21 with TruffleLogger

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

the class LoggingTest method testContextBoundLoggerMultipleContexts.

@Test
public void testContextBoundLoggerMultipleContexts() {
    TestHandler handler1 = new TestHandler();
    TestHandler handler2 = new TestHandler();
    LoggingLanguageFirst.action = (ctx, loggers) -> {
        for (Level level : AbstractLoggingLanguage.LOGGER_LEVELS) {
            for (String loggerName : AbstractLoggingLanguage.LOGGER_NAMES) {
                TruffleLogger logger = ctx.env.getLogger(loggerName);
                logger.log(level, logger.getName());
            }
        }
        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()) {
        ctx1.eval(LoggingLanguageFirst.ID, "");
        ctx2.eval(LoggingLanguageFirst.ID, "");
        Assert.assertEquals(createExpectedLog(LoggingLanguageFirst.ID, Level.FINEST, Collections.emptyMap()), handler1.getLog());
        Assert.assertEquals(createExpectedLog(LoggingLanguageFirst.ID, Level.FINEST, Collections.emptyMap()), handler2.getLog());
    }
}
Also used : TruffleContext(com.oracle.truffle.api.TruffleContext) Context(org.graalvm.polyglot.Context) TruffleLogger(com.oracle.truffle.api.TruffleLogger) Level(java.util.logging.Level) Test(org.junit.Test)

Example 22 with TruffleLogger

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

the class PolyglotEngineImpl method getEngineLogger.

TruffleLogger getEngineLogger() {
    TruffleLogger result = this.engineLogger;
    if (result == null) {
        synchronized (this.lock) {
            result = this.engineLogger;
            if (result == null) {
                result = this.engineLoggerSupplier.apply(OPTION_GROUP_ENGINE);
                Object logger = EngineAccessor.LANGUAGE.getLoggerCache(result);
                LoggerCache loggerCache = (LoggerCache) EngineAccessor.LANGUAGE.getLoggersSPI(logger);
                loggerCache.setOwner(this);
                if (!logLevels.isEmpty()) {
                    EngineAccessor.LANGUAGE.configureLoggers(this, logLevels, logger);
                }
                this.engineLogger = result;
            }
        }
    }
    return result;
}
Also used : TruffleLogger(com.oracle.truffle.api.TruffleLogger) LoggerCache(com.oracle.truffle.polyglot.PolyglotLoggers.LoggerCache)

Example 23 with TruffleLogger

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

the class ContextPreInitializationTest method testContextBoundLogger.

@Test
public void testContextBoundLogger() throws Exception {
    String logger1Name = String.format("%s.bound", FIRST);
    String logger1LevelOptionName = String.format("log.%s.level", logger1Name);
    String logger2Name = String.format("%s.bound2", FIRST);
    String logger2LevelOptionName = String.format("log.%s.level", logger2Name);
    AtomicReference<TruffleLogger> loggerRef = new AtomicReference<>();
    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) -> {
        env.getLogger("bound").log(Level.INFO, "bound:preInit:info");
        env.getLogger("bound").log(Level.FINEST, "bound:preInit:finest");
        TruffleLogger log = env.getLogger("bound2");
        loggerRef.set(log);
        log.log(Level.INFO, "bound2:preInit:info");
        log.log(Level.FINEST, "bound2:preInit:finest");
    });
    BaseLanguage.registerAction(ContextPreInitializationTestFirstLanguage.class, ActionKind.ON_PATCH_CONTEXT, (env) -> {
        env.getLogger("bound").log(Level.INFO, "bound:patch:info");
        env.getLogger("bound").log(Level.FINEST, "bound:patch:finest");
        TruffleLogger log = loggerRef.get();
        Assert.assertNotNull(log);
        log.log(Level.INFO, "bound2:patch:info");
        log.log(Level.FINEST, "bound2: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." + logger1LevelOptionName, "FINE");
        System.setProperty("polyglot." + logger2LevelOptionName, "FINE");
        System.clearProperty("polyglot.log.file");
        doContextPreinitialize(FIRST);
    } finally {
        System.setErr(origErr);
        System.clearProperty("polyglot." + logger1LevelOptionName);
        System.clearProperty("polyglot." + logger2LevelOptionName);
        if (origLogFile != null) {
            System.setProperty("polyglot.log.file", origLogFile);
        }
    }
    final String preInitLog = preInitErr.toString("UTF-8");
    assertTrue(preInitLog.contains("bound:preInit:info"));
    assertTrue(preInitLog.contains("bound2:preInit:info"));
    assertFalse(preInitLog.contains("bound:preInit:finest"));
    assertFalse(preInitLog.contains("bound2: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(logger1Name, logger2Name);
    try (Context ctx = Context.newBuilder().option(logger1LevelOptionName, "FINEST").option(logger2LevelOptionName, "FINEST").logHandler(testHandler).build()) {
        Value res = ctx.eval(Source.create(FIRST, "test"));
        assertEquals("test", res.asString());
        assertEquals(4, testHandler.logs.size());
        assertEquals("bound:patch:info", testHandler.logs.get(0).getMessage());
        assertEquals("bound:patch:finest", testHandler.logs.get(1).getMessage());
        assertEquals("bound2:patch:info", testHandler.logs.get(2).getMessage());
        assertEquals("bound2:patch:finest", testHandler.logs.get(3).getMessage());
    }
}
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) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteArrayOutputStream(java.io.ByteArrayOutputStream) 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