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