Debian Codegraph @

src/com/github/weisj/jsvg/logging/impl/LogManagerImpl.java

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 /*
2 * MIT License
3 *
4 * Copyright (c) 2025-2026 Jannis Weis
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
7 * associated documentation files (the "Software"), to deal in the Software without restriction,
8 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
9 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in all copies or
13 * substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
16 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
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 }