Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make log handler system thread safe #3800

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions src/main/java/ch/njol/skript/log/SkriptLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ public abstract class SkriptLogger {
@SuppressWarnings("null")
public final static Logger LOGGER = Bukkit.getServer() != null ? Bukkit.getLogger() : Logger.getLogger(Logger.GLOBAL_LOGGER_NAME); // cannot use Bukkit in tests

private final static HandlerList handlers = new HandlerList();
private final static ThreadLocal<HandlerList> handlerListThreadLocal = ThreadLocal.withInitial(HandlerList::new);

private static HandlerList getHandlers() {
return handlerListThreadLocal.get();
}

/**
* Shorthand for <tt>{@link #startLogHandler(LogHandler) startLogHandler}(new {@link RetainingLogHandler}());</tt>
Expand Down Expand Up @@ -98,23 +102,23 @@ public static ParseLogHandler startParseLogHandler() {
* @see RedirectingLogHandler
*/
public static <T extends LogHandler> T startLogHandler(final T h) {
handlers.add(h);
getHandlers().add(h);
return h;
}

static void removeHandler(final LogHandler h) {
if (!handlers.contains(h))
if (!getHandlers().contains(h))
return;
if (!h.equals(handlers.remove())) {
if (!h.equals(getHandlers().remove())) {
int i = 1;
while (!h.equals(handlers.remove()))
while (!h.equals(getHandlers().remove()))
i++;
LOGGER.severe("[Skript] " + i + " log handler" + (i == 1 ? " was" : "s were") + " not stopped properly! (at " + getCaller() + ") [if you're a server admin and you see this message please file a bug report at https://github.com/bensku/skript/issues if there is not already one]");
}
}

static boolean isStopped(final LogHandler h) {
return !handlers.contains(h);
return !getHandlers().contains(h);
}

@Nullable
Expand Down Expand Up @@ -167,7 +171,7 @@ public static void log(final @Nullable LogEntry entry) {
return;
if (Skript.testing() && node != null && node.debug())
System.out.print("---> " + entry.level + "/" + ErrorQuality.get(entry.quality) + ": " + entry.getMessage() + " ::" + LogEntry.findCaller());
for (final LogHandler h : handlers) {
for (final LogHandler h : getHandlers()) {
final LogResult r = h.log(entry);
switch (r) {
case CACHED:
Expand Down