Skip to content

Commit

Permalink
m-m-m/ui-demo#2: workaround for TeaVM bug
Browse files Browse the repository at this point in the history
  • Loading branch information
hohwille committed Jun 23, 2024
1 parent 589ba8e commit 1444913
Showing 1 changed file with 141 additions and 126 deletions.
Original file line number Diff line number Diff line change
@@ -1,126 +1,141 @@
package io.github.mmm.scanner;

import java.util.function.Function;

import org.slf4j.Logger;
import org.slf4j.event.Level;

import io.github.mmm.base.text.AbstractTextFormatMessageHandler;
import io.github.mmm.base.text.TextFormatMessage;
import io.github.mmm.base.text.TextFormatMessageHandler;
import io.github.mmm.base.text.TextFormatMessageType;

/**
* Default implementation of {@link TextFormatMessageHandler}.
*/
@SuppressWarnings("exports")
public class SimpleTextFormatMessageHandler extends AbstractTextFormatMessageHandler {

/** Maps each {@link TextFormatMessageType} to its analog {@link Level}. */
public static final Function<TextFormatMessageType, Level> LOG_MAPPER_DEFAULT = new LogLevelMapper(false);

/**
* Like {@link #LOG_MAPPER_DEFAULT} but maps {@link TextFormatMessageType#INFO info messages} to log-level
* {@link Level#DEBUG debug}.
*/
public static final Function<TextFormatMessageType, Level> LOG_MAPPER_INFO_AS_DEBUG = new LogLevelMapper(true);

private static final SimpleTextFormatMessageHandler INSTANCE = new SimpleTextFormatMessageHandler(false,
AbstractCharStreamScanner.LOG, LOG_MAPPER_INFO_AS_DEBUG, false);

private final Logger logger;

private final Function<TextFormatMessageType, Level> logLevelMapper;

/**
* The constructor.
*
* @param throwOnError - {@code true} to throw a {@link RuntimeException} when an {@link TextFormatMessageType#ERROR
* error} {@link TextFormatMessage message} is {@link #handle(TextFormatMessage) handled}.
* @param logger the {@link Logger} used to log {@link TextFormatMessage}s. May be {@code null} to omit logging.
* @param logLevelMapper the {@link Function} to map {@link TextFormatMessageType} to the according log {@link Level}.
* @param collectMessages the flag for {@link #isCollectMessages()}.
*/
public SimpleTextFormatMessageHandler(boolean throwOnError, Logger logger,
Function<TextFormatMessageType, Level> logLevelMapper, boolean collectMessages) {

super(throwOnError, collectMessages);
this.logger = logger;
this.logLevelMapper = logLevelMapper;
}

private void log(TextFormatMessage message) {

if (this.logger == null) {
return;
}
Level level = this.logLevelMapper.apply(message.getType());
if (level != null) {
this.logger.atLevel(level).log("At line {} in column {}: {}", message.getLine(), message.getColumn(),
message.getText());
}
}

@Override
public TextFormatMessage handle(TextFormatMessage message) {

log(message);
return super.handle(message);
}

private static final class LogLevelMapper implements Function<TextFormatMessageType, Level> {

private final boolean logInfoOnDebug;

private LogLevelMapper(boolean logInfoOnDebug) {

super();
this.logInfoOnDebug = logInfoOnDebug;
}

@Override
public Level apply(TextFormatMessageType t) {

switch (t) {
case ERROR:
return Level.ERROR;
case WARNING:
return Level.WARN;
case INFO:
if (this.logInfoOnDebug) {
return Level.DEBUG;
}
return Level.INFO;
}
return null;
}
}

/**
* @return the singleton instance.
*/
public static SimpleTextFormatMessageHandler get() {

return INSTANCE;
}

/**
* @return a {@link SimpleTextFormatMessageHandler} that throws an exception in case of a
* {@link TextFormatMessageType#ERROR error} {@link TextFormatMessage message} and collects and logs other
* messages.
*/
public static SimpleTextFormatMessageHandler ofThrowErrors() {

return new SimpleTextFormatMessageHandler(true, AbstractCharStreamScanner.LOG, LOG_MAPPER_INFO_AS_DEBUG, true);
}

/**
* @return a {@link SimpleTextFormatMessageHandler} that throws an exception in case of a
* {@link TextFormatMessageType#ERROR error} {@link TextFormatMessage message} and collects other messages
* without any logging.
*/
public static SimpleTextFormatMessageHandler ofThrowErrorsNoLogging() {

return new SimpleTextFormatMessageHandler(true, null, null, true);
}
}
package io.github.mmm.scanner;

import java.util.function.Function;

import org.slf4j.Logger;
import org.slf4j.event.Level;

import io.github.mmm.base.text.AbstractTextFormatMessageHandler;
import io.github.mmm.base.text.TextFormatMessage;
import io.github.mmm.base.text.TextFormatMessageHandler;
import io.github.mmm.base.text.TextFormatMessageType;

/**
* Default implementation of {@link TextFormatMessageHandler}.
*/
@SuppressWarnings("exports")
public class SimpleTextFormatMessageHandler extends AbstractTextFormatMessageHandler {

/** Maps each {@link TextFormatMessageType} to its analog {@link Level}. */
public static final Function<TextFormatMessageType, Level> LOG_MAPPER_DEFAULT = new LogLevelMapper(false);

/**
* Like {@link #LOG_MAPPER_DEFAULT} but maps {@link TextFormatMessageType#INFO info messages} to log-level
* {@link Level#DEBUG debug}.
*/
public static final Function<TextFormatMessageType, Level> LOG_MAPPER_INFO_AS_DEBUG = new LogLevelMapper(true);

private static final SimpleTextFormatMessageHandler INSTANCE = new SimpleTextFormatMessageHandler(false,
AbstractCharStreamScanner.LOG, LOG_MAPPER_INFO_AS_DEBUG, false);

private final Logger logger;

private final Function<TextFormatMessageType, Level> logLevelMapper;

/**
* The constructor.
*
* @param throwOnError - {@code true} to throw a {@link RuntimeException} when an {@link TextFormatMessageType#ERROR
* error} {@link TextFormatMessage message} is {@link #handle(TextFormatMessage) handled}.
* @param logger the {@link Logger} used to log {@link TextFormatMessage}s. May be {@code null} to omit logging.
* @param logLevelMapper the {@link Function} to map {@link TextFormatMessageType} to the according log {@link Level}.
* @param collectMessages the flag for {@link #isCollectMessages()}.
*/
public SimpleTextFormatMessageHandler(boolean throwOnError, Logger logger,
Function<TextFormatMessageType, Level> logLevelMapper, boolean collectMessages) {

super(throwOnError, collectMessages);
this.logger = logger;
this.logLevelMapper = logLevelMapper;
}

private void log(TextFormatMessage message) {

if (this.logger == null) {
return;
}
Level level = this.logLevelMapper.apply(message.getType());
if (level != null) {
String logMsg = "At line {} in column {}: {}";
// Workaround for https://github.com/konsoletyper/teavm/issues/918
// this.logger.atLevel(level).log(logMsg, message.getLine(), message.getColumn(), message.getText());
switch (level) {
case INFO:
this.logger.info(logMsg, message.getLine(), message.getColumn(), message.getText());
break;
case WARN:
this.logger.warn(logMsg, message.getLine(), message.getColumn(), message.getText());
break;
case ERROR:
this.logger.error(logMsg, message.getLine(), message.getColumn(), message.getText());
break;
default:
this.logger.warn(logMsg, message.getLine(), message.getColumn(), message.getText());
break;
}
}
}

@Override
public TextFormatMessage handle(TextFormatMessage message) {

log(message);
return super.handle(message);
}

private static final class LogLevelMapper implements Function<TextFormatMessageType, Level> {

private final boolean logInfoOnDebug;

private LogLevelMapper(boolean logInfoOnDebug) {

super();
this.logInfoOnDebug = logInfoOnDebug;
}

@Override
public Level apply(TextFormatMessageType t) {

switch (t) {
case ERROR:
return Level.ERROR;
case WARNING:
return Level.WARN;
case INFO:
if (this.logInfoOnDebug) {
return Level.DEBUG;
}
return Level.INFO;
}
return null;
}
}

/**
* @return the singleton instance.
*/
public static SimpleTextFormatMessageHandler get() {

return INSTANCE;
}

/**
* @return a {@link SimpleTextFormatMessageHandler} that throws an exception in case of a
* {@link TextFormatMessageType#ERROR error} {@link TextFormatMessage message} and collects and logs other
* messages.
*/
public static SimpleTextFormatMessageHandler ofThrowErrors() {

return new SimpleTextFormatMessageHandler(true, AbstractCharStreamScanner.LOG, LOG_MAPPER_INFO_AS_DEBUG, true);
}

/**
* @return a {@link SimpleTextFormatMessageHandler} that throws an exception in case of a
* {@link TextFormatMessageType#ERROR error} {@link TextFormatMessage message} and collects other messages
* without any logging.
*/
public static SimpleTextFormatMessageHandler ofThrowErrorsNoLogging() {

return new SimpleTextFormatMessageHandler(true, null, null, true);
}
}

0 comments on commit 1444913

Please sign in to comment.