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