Added basic command logging and logging to file.

This commit is contained in:
sk89q 2010-11-05 22:37:16 -07:00
parent 75b023d194
commit 49739bab76
3 changed files with 113 additions and 2 deletions

View File

@ -33,7 +33,7 @@ public class WorldEdit extends Plugin {
/** /**
* Logger. * Logger.
*/ */
private static final Logger logger = Logger.getLogger("Minecraft"); private static final Logger logger = Logger.getLogger("Minecraft.WorldEdit");
/** /**
* WorldEditLibrary instance. * WorldEditLibrary instance.
*/ */

View File

@ -23,6 +23,8 @@ import java.util.Set;
import java.util.HashSet; import java.util.HashSet;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import java.util.logging.Handler;
import java.util.logging.FileHandler;
import java.io.*; import java.io.*;
import com.sk89q.worldedit.*; import com.sk89q.worldedit.*;
import com.sk89q.worldedit.blocks.*; import com.sk89q.worldedit.blocks.*;
@ -39,7 +41,7 @@ public class WorldEditListener extends PluginListener {
/** /**
* Logger. * Logger.
*/ */
private static final Logger logger = Logger.getLogger("Minecraft"); private static final Logger logger = Logger.getLogger("Minecraft.WorldEdit");
/** /**
* Default list of allowed block types. * Default list of allowed block types.
@ -90,6 +92,10 @@ public class WorldEditListener extends PluginListener {
* Max radius for commands that use a radius. * Max radius for commands that use a radius.
*/ */
private int maxRadius = -1; private int maxRadius = -1;
/**
* Indicates whether commands should be logged to the console.
*/
private boolean logComands = false;
/** /**
* Construct an instance of the plugin. * Construct an instance of the plugin.
@ -293,6 +299,11 @@ public class WorldEditListener extends PluginListener {
WorldEditSession session, EditSession editSession, String[] split) WorldEditSession session, EditSession editSession, String[] split)
throws WorldEditException throws WorldEditException
{ {
if (logComands) {
logger.log(Level.INFO, "WorldEdit: " + player.getName() + ": "
+ joinString(split, " "));
}
// Jump to the first free position // Jump to the first free position
if (split[0].equalsIgnoreCase("/unstuck")) { if (split[0].equalsIgnoreCase("/unstuck")) {
checkArgs(split, 0, 0, split[0]); checkArgs(split, 0, 0, split[0]);
@ -1405,6 +1416,24 @@ public class WorldEditListener extends PluginListener {
String type = properties.getString("shell-save-type", "").trim(); String type = properties.getString("shell-save-type", "").trim();
shellSaveType = type.equals("") ? null : type; 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; 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();
}
} }

View File

@ -0,0 +1,64 @@
// $Id$
/*
* WorldEdit
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
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();
}
}