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());
}
}
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;
}
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());
}
}
Aggregations