From 85b219282e29cec14d9704099e077e334da57d5a Mon Sep 17 00:00:00 2001 From: Rigby Date: Thu, 31 Mar 2011 16:07:03 +0100 Subject: [PATCH] DebugLog + Messaging (Forgot these :)) --- src/com/onarandombox/utils/DebugLog.java | 70 +++++++++++++++++++++++ src/com/onarandombox/utils/Messaging.java | 25 ++++++++ 2 files changed, 95 insertions(+) create mode 100644 src/com/onarandombox/utils/DebugLog.java create mode 100644 src/com/onarandombox/utils/Messaging.java diff --git a/src/com/onarandombox/utils/DebugLog.java b/src/com/onarandombox/utils/DebugLog.java new file mode 100644 index 00000000..82cca6a3 --- /dev/null +++ b/src/com/onarandombox/utils/DebugLog.java @@ -0,0 +1,70 @@ +package com.onarandombox.utils; + +import java.io.IOException; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.text.SimpleDateFormat; +import java.util.logging.FileHandler; +import java.util.logging.Formatter; +import java.util.logging.Handler; +import java.util.logging.Level; +import java.util.logging.LogRecord; +import java.util.logging.Logger; + +public class DebugLog { + + private FileHandler fh; + private Logger log; + + public DebugLog(String logger, String file) { + log = Logger.getLogger(logger); + + try { + fh = new FileHandler(file, true); + log.setUseParentHandlers(false); + for (Handler handler : log.getHandlers()) { + log.removeHandler(handler); + } + log.addHandler(fh); + log.setLevel(Level.ALL); + fh.setFormatter(new LogFormatter()); + } catch (SecurityException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public void log(Level level, String msg) { + log.log(level, msg); + } + + private class LogFormatter extends Formatter { + private final SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + + @Override + public String format(LogRecord record) { + StringBuilder builder = new StringBuilder(); + Throwable ex = record.getThrown(); + + builder.append(date.format(record.getMillis())); + builder.append(" ["); + builder.append(record.getLevel().getLocalizedName().toUpperCase()); + builder.append("] "); + builder.append(record.getMessage()); + builder.append('\n'); + + if (ex != null) { + StringWriter writer = new StringWriter(); + ex.printStackTrace(new PrintWriter(writer)); + builder.append(writer); + } + + return builder.toString(); + } + } + + public void close() { + fh.close(); + } +} diff --git a/src/com/onarandombox/utils/Messaging.java b/src/com/onarandombox/utils/Messaging.java new file mode 100644 index 00000000..37772839 --- /dev/null +++ b/src/com/onarandombox/utils/Messaging.java @@ -0,0 +1,25 @@ +package com.onarandombox.utils; + +import org.bukkit.command.CommandSender; + +import com.onarandombox.MultiverseCore.MultiverseCore; + +public class Messaging { + + public void send(CommandSender player, String msg, String... params) { + player.sendMessage(parameterizeMessage(msg, params)); + } + + public void broadcast(MultiverseCore plugin, String msg, String... params) { + plugin.getServer().broadcastMessage(parameterizeMessage(msg, params)); + } + + private String parameterizeMessage(String msg, String... params) { + msg = "§cHeroes: " + msg; + for (int i = 0; i < params.length; i++) { + msg = msg.replace("$" + (i + 1), "§f" + params[i] + "§c"); + } + return msg; + } + +}