Skip to content

Commit

Permalink
Revert async logging fix (not needed)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lauriichan committed Apr 26, 2021
1 parent d5eecad commit 88ee197
Showing 1 changed file with 10 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

import net.sourcewriters.minecraft.vcompat.reflection.VersionControl;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.craftbukkit.Main;
import org.bukkit.craftbukkit.libs.jline.console.ConsoleReader;
Expand All @@ -22,10 +21,7 @@
import java.awt.Color;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.function.BiConsumer;

public class BukkitLogger implements ILogger {
Expand All @@ -41,8 +37,6 @@ private static ConsoleReader getReader() {

private final boolean ansiSupported, jLine;

private final List<String> queue = Collections.synchronizedList(new ArrayList<>());

private final HashSet<String> colorMessage = new HashSet<>();
private final LogTypeMap typeMap = new LogTypeMap();
private final Writer writer;
Expand All @@ -56,8 +50,11 @@ private static ConsoleReader getReader() {
private String format = "[%date%/%time%] [Server thread/%plugin% | %type%]: %message%";

public BukkitLogger(Plugin plugin) {
Bukkit.getScheduler().runTaskTimer(plugin, this::streamDequeue, 0, 12);
this.plugin = plugin.getName();
this(plugin.getName());
}

public BukkitLogger(String plugin) {
this.plugin = plugin;
this.reader = getReader();
this.writer = reader.getOutput();
this.colored = this.ansiSupported = reader.getTerminal().isAnsiSupported();
Expand Down Expand Up @@ -249,55 +246,28 @@ public BukkitLogger log(LogType type, String message) {
return this;
}

private BukkitLogger sendColored(LogType type, String format, String message) throws IOException {
public BukkitLogger sendColored(LogType type, String format, String message) throws IOException {
boolean colorMessage = this.colorMessage.contains(type.getId());
String ansi;
if (custom != null) {
boolean color = (ansi = type.asColorString(false)).length() != 0;
custom.accept(true, format.replace("%type%", ansi + type.getName() + (color ? ANSI_RESET : "")).replace("%message%",
(colorMessage ? ansi : "") + LoggingColors.format(message, color)) + (color ? ANSI_RESET : ""));
}
return queueStream(format.replace("%type%", (ansi = type.asColorString(true)) + type.getName().toUpperCase() + ANSI_RESET)
return sendStream(format.replace("%type%", (ansi = type.asColorString(true)) + type.getName().toUpperCase() + ANSI_RESET)
.replace("%message%", (colorMessage ? ansi : "") + LoggingColors.format(message, true)) + ANSI_RESET);
}

private BukkitLogger sendUncolored(LogType type, String format, String message) throws IOException {
public BukkitLogger sendUncolored(LogType type, String format, String message) throws IOException {
message = format.replace("%type%", type.getName().toUpperCase()).replace("%message%", LoggingColors.format(message, false));
if (custom != null) {
custom.accept(true, message);
}
return queueStream(message);
}

private BukkitLogger queueStream(String message) throws IOException {
message += System.getProperty("line.separator");
if (Bukkit.isPrimaryThread()) {
return sendStreamDequeue(message);
}
queue.add(message);
return this;
}

private void streamDequeue() {
if (!queue.isEmpty()) {
String[] messages = queue.toArray(new String[queue.size()]);
queue.clear();
for (String message : messages) {
try {
sendStream(message);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

private BukkitLogger sendStreamDequeue(String message) throws IOException {
streamDequeue();
return sendStream(message);
}

private BukkitLogger sendStream(String message) throws IOException {
public BukkitLogger sendStream(String message) throws IOException {
message += System.getProperty("line.separator");
if (jLine) {
reader.flush();
writer.write("\b\b" + message);
Expand Down

0 comments on commit 88ee197

Please sign in to comment.