From 49739bab76c71da5c4adc417e02087aa77330a11 Mon Sep 17 00:00:00 2001 From: sk89q Date: Fri, 5 Nov 2010 22:37:16 -0700 Subject: [PATCH] Added basic command logging and logging to file. --- src/WorldEdit.java | 2 +- src/WorldEditListener.java | 49 +++++++++++++++++++- src/com/sk89q/worldedit/LogFormat.java | 64 ++++++++++++++++++++++++++ 3 files changed, 113 insertions(+), 2 deletions(-) create mode 100644 src/com/sk89q/worldedit/LogFormat.java diff --git a/src/WorldEdit.java b/src/WorldEdit.java index bb532b063..571c15097 100644 --- a/src/WorldEdit.java +++ b/src/WorldEdit.java @@ -33,7 +33,7 @@ public class WorldEdit extends Plugin { /** * Logger. */ - private static final Logger logger = Logger.getLogger("Minecraft"); + private static final Logger logger = Logger.getLogger("Minecraft.WorldEdit"); /** * WorldEditLibrary instance. */ diff --git a/src/WorldEditListener.java b/src/WorldEditListener.java index 7c958de72..779b343a8 100644 --- a/src/WorldEditListener.java +++ b/src/WorldEditListener.java @@ -23,6 +23,8 @@ import java.util.Set; import java.util.HashSet; import java.util.logging.Level; import java.util.logging.Logger; +import java.util.logging.Handler; +import java.util.logging.FileHandler; import java.io.*; import com.sk89q.worldedit.*; import com.sk89q.worldedit.blocks.*; @@ -39,7 +41,7 @@ public class WorldEditListener extends PluginListener { /** * Logger. */ - private static final Logger logger = Logger.getLogger("Minecraft"); + private static final Logger logger = Logger.getLogger("Minecraft.WorldEdit"); /** * Default list of allowed block types. @@ -90,6 +92,10 @@ public class WorldEditListener extends PluginListener { * Max radius for commands that use a radius. */ private int maxRadius = -1; + /** + * Indicates whether commands should be logged to the console. + */ + private boolean logComands = false; /** * Construct an instance of the plugin. @@ -293,6 +299,11 @@ public class WorldEditListener extends PluginListener { WorldEditSession session, EditSession editSession, String[] split) throws WorldEditException { + if (logComands) { + logger.log(Level.INFO, "WorldEdit: " + player.getName() + ": " + + joinString(split, " ")); + } + // Jump to the first free position if (split[0].equalsIgnoreCase("/unstuck")) { checkArgs(split, 0, 0, split[0]); @@ -1405,6 +1416,24 @@ public class WorldEditListener extends PluginListener { String type = properties.getString("shell-save-type", "").trim(); shellSaveType = type.equals("") ? null : type; + + logComands = properties.getBoolean("log-commands", false); + + String logFile = properties.getString("log-file", ""); + if (!logFile.equals("")) { + try { + FileHandler handler = new FileHandler(logFile, true); + handler.setFormatter(new LogFormat()); + logger.addHandler(handler); + } catch (IOException e) { + logger.log(Level.WARNING, "Could not use log file " + logFile + ": " + + e.getMessage()); + } + } else { + for (Handler handler : logger.getHandlers()) { + logger.removeHandler(handler); + } + } } /** @@ -1443,4 +1472,22 @@ public class WorldEditListener extends PluginListener { return session; } } + + /** + * Joins a string from an array of strings. + * + * @param str + * @param delimiter + * @return + */ + private static String joinString(String[] str, String delimiter) { + if (str.length == 0) { + return ""; + } + StringBuilder buffer = new StringBuilder(str[0]); + for (int i = 1; i < str.length; i++) { + buffer.append(delimiter).append(str[i]); + } + return buffer.toString(); + } } diff --git a/src/com/sk89q/worldedit/LogFormat.java b/src/com/sk89q/worldedit/LogFormat.java new file mode 100644 index 000000000..bb295e6c7 --- /dev/null +++ b/src/com/sk89q/worldedit/LogFormat.java @@ -0,0 +1,64 @@ +// $Id$ +/* + * WorldEdit + * Copyright (C) 2010 sk89q + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . +*/ + +package com.sk89q.worldedit; + +import java.util.logging.Formatter; +import java.util.logging.LogRecord; +import java.util.logging.Level; +import java.io.PrintWriter; +import java.io.StringWriter; + +/** + * Used for formatting. + * + * @author sk89q + */ +public class LogFormat extends Formatter { + public String format(LogRecord record) { + StringBuilder text = new StringBuilder(); + Level level = record.getLevel(); + + if (level == Level.FINEST) { + text.append("[FINEST] "); + } else if (level == Level.FINER) { + text.append("[FINER] "); + } else if (level == Level.FINE) { + text.append("[FINE] "); + } else if (level == Level.INFO) { + text.append("[INFO] "); + } else if (level == Level.WARNING) { + text.append("[WARNING] "); + } else if (level == Level.SEVERE) { + text.append("[SEVERE] "); + } + + text.append(record.getMessage()); + text.append("\r\n"); + + Throwable t = record.getThrown(); + if (t != null) { + StringWriter writer = new StringWriter(); + t.printStackTrace(new PrintWriter(writer)); + text.append(writer.toString()); + } + + return text.toString(); + } +}