package jdk.incubator.http.internal.hpack;
import jdk.incubator.http.internal.common.Utils;
import jdk.incubator.http.internal.hpack.HPACK.Logger.Level;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.function.Supplier;
import static java.lang.String.format;
import static java.util.stream.Collectors.joining;
import static jdk.incubator.http.internal.hpack.HPACK.Logger.Level.EXTRA;
import static jdk.incubator.http.internal.hpack.HPACK.Logger.Level.NONE;
import static jdk.incubator.http.internal.hpack.HPACK.Logger.Level.NORMAL;
public final class HPACK {
private static final RootLogger LOGGER;
private static final Map<String, Level> logLevels =
Map.of("NORMAL", NORMAL, "EXTRA", EXTRA);
static {
String PROPERTY = "jdk.internal.httpclient.hpack.log.level";
String value = AccessController.doPrivileged(
(PrivilegedAction<String>) () -> System.getProperty(PROPERTY));
if (value == null) {
LOGGER = new RootLogger(NONE);
} else {
String upperCasedValue = value.toUpperCase();
Level l = logLevels.get(upperCasedValue);
if (l == null) {
LOGGER = new RootLogger(NONE);
LOGGER.log(System.Logger.Level.INFO,
() -> format("%s value '%s' not recognized (use %s); logging disabled",
PROPERTY, value, logLevels.keySet().stream().collect(joining(", "))));
} else {
LOGGER = new RootLogger(l);
LOGGER.log(System.Logger.Level.DEBUG,
() -> format("logging level %s", l));
}
}
}
public static Logger getLogger() {
return LOGGER;
}
private HPACK() { }
public static class Logger implements System.Logger {
public enum Level {
NONE(0, System.Logger.Level.OFF),
NORMAL(1, System.Logger.Level.DEBUG),
EXTRA(2, System.Logger.Level.TRACE);
private final int level;
final System.Logger.Level systemLevel;
Level(int i, System.Logger.Level system) {
level = i;
systemLevel = system;
}
public final boolean implies(Level other) {
return this.level >= other.level;
}
}
private final String name;
private final Level level;
private final String path;
private final System.Logger logger;
private Logger(String path, String name, Level level) {
this(path, name, level, null);
}
private Logger(String p, String name, Level level, System.Logger logger) {
this.path = p;
this.name = name;
this.level = level;
this.logger = Utils.getHpackLogger(path::toString, level.systemLevel);
}
public final String getName() {
return name;
}
@Override
public boolean isLoggable(System.Logger.Level level) {
return logger.isLoggable(level);
}
@Override
public void log(System.Logger.Level level, ResourceBundle bundle, String msg, Throwable thrown) {
logger.log(level, bundle, msg,thrown);
}
@Override
public void log(System.Logger.Level level, ResourceBundle bundle, String format, Object... params) {
logger.log(level, bundle, format, params);
}
public boolean isLoggable(Level level) {
return this.level.implies(level);
}
public void log(Level level, Supplier<String> s) {
if (this.level.implies(level)) {
logger.log(level.systemLevel, s);
}
}
public Logger subLogger(String name) {
return new Logger(path + "/" + name, name, level);
}
}
private static final class RootLogger extends Logger {
protected RootLogger(Level level) {
super("hpack", "hpack", level);
}
}
}