Paper/src/main/java/net/minecraft/server/ConsoleLogManager.java

108 Zeilen
4.0 KiB
Java

package net.minecraft.server;
import java.util.logging.ConsoleHandler;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
2011-03-23 16:39:25 +01:00
// CraftBukkit start
import java.io.File;
2011-03-23 16:39:25 +01:00
import java.util.logging.Handler;
import org.bukkit.craftbukkit.util.ShortConsoleLogFormatter;
import org.bukkit.craftbukkit.util.TerminalConsoleHandler;
2011-03-23 16:39:25 +01:00
// CraftBukkit end
2011-02-23 13:56:36 +01:00
public class ConsoleLogManager {
public static Logger a = Logger.getLogger("Minecraft");
2011-02-23 13:56:36 +01:00
public static Logger global = Logger.getLogger(""); // CraftBukkit
2012-01-14 23:02:10 +01:00
public ConsoleLogManager() {
}
2011-05-14 16:29:42 +02:00
// CraftBukkit - change of method signature!
public static void init(MinecraftServer server) {
ConsoleLogFormatter consolelogformatter = new ConsoleLogFormatter(server.options.has("log-strip-color")); // CraftBukkit - pass strip color option
a.setUseParentHandlers(false);
2011-02-23 13:56:36 +01:00
// CraftBukkit start
2011-03-31 22:40:00 +02:00
ConsoleHandler consolehandler = new TerminalConsoleHandler(server.reader);
2012-01-14 23:02:10 +01:00
for (Handler handler : global.getHandlers()) {
global.removeHandler(handler);
}
2011-03-31 22:40:00 +02:00
consolehandler.setFormatter(new ShortConsoleLogFormatter(server));
global.addHandler(consolehandler);
// CraftBukkit end
a.addHandler(consolehandler);
try {
// CraftBukkit start
2012-01-14 23:02:10 +01:00
String pattern = (String) server.options.valueOf("log-pattern");
// We have to parse the pattern ourself so we can create directories as needed (java #6244047)
String tmpDir = System.getProperty("java.io.tmpdir");
String homeDir = System.getProperty("user.home");
if (tmpDir == null) {
tmpDir = homeDir;
}
// We only care about parsing for directories, FileHandler can do file names by itself
File parent = new File(pattern).getParentFile();
String parentPath = parent.getPath();
StringBuilder fixedPattern = new StringBuilder();
int i = 0;
while (i < parentPath.length()) {
char ch = parentPath.charAt(i);
char ch2 = 0;
if (i + 1 < parentPath.length()) {
ch2 = Character.toLowerCase(pattern.charAt(i + 1));
}
if (ch == '%') {
if (ch2 == 'h') {
i += 2;
fixedPattern.append(homeDir);
continue;
} else if (ch2 == 't') {
i += 2;
fixedPattern.append(tmpDir);
continue;
} else if (ch2 == '%') {
// Even though we don't care about this we have to skip it to avoid matching %%t
i += 2;
fixedPattern.append("%%");
continue;
} else if (ch2 != 0) {
throw new java.io.IOException("log-pattern can only use %t and %h for directories, got %" + ch2);
}
}
fixedPattern.append(ch);
i++;
}
// Try to create needed parent directories
parent = new File(fixedPattern.toString());
if (parent != null) {
parent.mkdirs();
}
2012-01-14 23:02:10 +01:00
int limit = ((Integer) server.options.valueOf("log-limit")).intValue();
int count = ((Integer) server.options.valueOf("log-count")).intValue();
boolean append = ((Boolean) server.options.valueOf("log-append")).booleanValue();
FileHandler filehandler = new FileHandler(pattern, limit, count, append);
2012-01-12 19:43:34 +01:00
// CraftBukkit end
filehandler.setFormatter(consolelogformatter);
a.addHandler(filehandler);
2011-02-23 13:56:36 +01:00
global.addHandler(filehandler); // CraftBukkit
} catch (Exception exception) {
a.log(Level.WARNING, "Failed to log to server.log", exception);
}
}
2011-02-23 13:56:36 +01:00
}