| •1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
package com.github.weisj.jsvg.logging.impl; |
| 23 |
|
| 24 |
import java.util.Iterator; |
| 25 |
import java.util.ServiceLoader; |
| 26 |
import java.util.function.Supplier; |
| 27 |
import java.util.logging.Level; |
| 28 |
|
| 29 |
import org.jetbrains.annotations.NotNull; |
| 30 |
import org.jetbrains.annotations.Nullable; |
| 31 |
|
| 32 |
import com.github.weisj.jsvg.logging.LogManager; |
| 33 |
import com.github.weisj.jsvg.logging.Logger; |
| 34 |
|
| 35 |
import aQute.bnd.annotation.spi.ServiceConsumer; |
| 36 |
|
| 37 |
@ServiceConsumer(value = LogManager.class) |
| 38 |
@SuppressWarnings("SameNameButDifferent") |
| 39 |
public final class LogManagerImpl { |
| 40 |
private static final @NotNull LogManager logManager; |
| 41 |
|
| 42 |
private LogManagerImpl() {} |
| 43 |
|
| 44 |
static { |
| 45 |
Iterator<LogManager> managers = ServiceLoader.load(LogManager.class).iterator(); |
| 46 |
if (managers.hasNext()) { |
| 47 |
logManager = managers.next(); |
| 48 |
} else { |
| 49 |
logManager = new DefaultLogManager(); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
public static @NotNull LogManager logManager() { |
| 54 |
return logManager; |
| 55 |
} |
| 56 |
|
| 57 |
private static class DefaultLogManager implements LogManager { |
| 58 |
@Override |
| 59 |
public @NotNull Logger createLogger(@NotNull String name) { |
| 60 |
return new JavaUtilLoggerWrapper(java.util.logging.Logger.getLogger(name)); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
private static final class JavaUtilLoggerWrapper implements Logger { |
| 65 |
private final @NotNull java.util.logging.Logger logger; |
| 66 |
|
| 67 |
private JavaUtilLoggerWrapper(@NotNull java.util.logging.Logger logger) { |
| 68 |
this.logger = logger; |
| 69 |
} |
| 70 |
|
| 71 |
@Override |
| 72 |
public void log(Level level, @Nullable String message) { |
| 73 |
logger.log(new WrapperLogRecord(toJavaUtilLevel(level), message)); |
| 74 |
} |
| 75 |
|
| 76 |
@Override |
| 77 |
public void log(Level level, @Nullable String message, @NotNull Throwable e) { |
| 78 |
WrapperLogRecord logRecord = new WrapperLogRecord(toJavaUtilLevel(level), message); |
| 79 |
logRecord.setThrown(e); |
| 80 |
logger.log(logRecord); |
| 81 |
} |
| 82 |
|
| 83 |
@Override |
| 84 |
public void log(Level level, @NotNull Supplier<@Nullable String> messageSupplier) { |
| 85 |
java.util.logging.Level javaUtilLevel = toJavaUtilLevel(level); |
| 86 |
if (!logger.isLoggable(javaUtilLevel)) { |
| 87 |
return; |
| 88 |
} |
| 89 |
logger.log(new WrapperLogRecord(javaUtilLevel, messageSupplier.get())); |
| 90 |
} |
| 91 |
|
| 92 |
static java.util.logging.Level toJavaUtilLevel(@NotNull Logger.Level level) { |
| 93 |
switch (level) { |
| 94 |
case DEBUG: |
| 95 |
return java.util.logging.Level.FINE; |
| 96 |
case INFO: |
| 97 |
return java.util.logging.Level.INFO; |
| 98 |
case WARNING: |
| 99 |
return java.util.logging.Level.WARNING; |
| 100 |
case ERROR: |
| 101 |
return java.util.logging.Level.SEVERE; |
| 102 |
default: |
| 103 |
throw new IllegalArgumentException("Unknown log level: " + level); |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
private static final class WrapperLogRecord extends java.util.logging.LogRecord { |
| 109 |
private static final String DISPATCHER_CLASS_NAME = JavaUtilLoggerWrapper.class.getName(); |
| 110 |
private boolean needToInferCaller = true; |
| 111 |
|
| 112 |
public WrapperLogRecord(Level level, String msg) { |
| 113 |
super(level, msg); |
| 114 |
} |
| 115 |
|
| 116 |
@Override |
| 117 |
public String getSourceClassName() { |
| 118 |
if (needToInferCaller) { |
| 119 |
customInferCaller(); |
| 120 |
} |
| 121 |
return super.getSourceClassName(); |
| 122 |
} |
| 123 |
|
| 124 |
@Override |
| 125 |
public String getSourceMethodName() { |
| 126 |
if (needToInferCaller) { |
| 127 |
customInferCaller(); |
| 128 |
} |
| 129 |
return super.getSourceMethodName(); |
| 130 |
} |
| 131 |
|
| 132 |
private void customInferCaller() { |
| 133 |
needToInferCaller = false; |
| 134 |
new CallerFinder(DISPATCHER_CLASS_NAME) |
| 135 |
.findCaller() |
| 136 |
.ifPresent(st -> { |
| 137 |
setSourceClassName(st.getClassName()); |
| 138 |
setSourceMethodName(st.getMethodName()); |
| 139 |
}); |
| 140 |
} |
| 141 |
} |
| 142 |
} |