diff --git a/src/main/java/com/cleanroommc/groovyscript/api/GroovyLog.java b/src/main/java/com/cleanroommc/groovyscript/api/GroovyLog.java index 7e01f6b4c..249b7431f 100644 --- a/src/main/java/com/cleanroommc/groovyscript/api/GroovyLog.java +++ b/src/main/java/com/cleanroommc/groovyscript/api/GroovyLog.java @@ -75,7 +75,7 @@ static Msg msg(String msg, Object... args) { * * @return the locations of this log file */ - Path getLogFilerPath(); + Path getLogFilePath(); /** * Formats and logs a {@link Msg} object to this log. diff --git a/src/main/java/com/cleanroommc/groovyscript/command/GSCommand.java b/src/main/java/com/cleanroommc/groovyscript/command/GSCommand.java index e0e06ac3e..2fa2ecee2 100644 --- a/src/main/java/com/cleanroommc/groovyscript/command/GSCommand.java +++ b/src/main/java/com/cleanroommc/groovyscript/command/GSCommand.java @@ -7,6 +7,7 @@ import com.cleanroommc.groovyscript.documentation.Documentation; import com.cleanroommc.groovyscript.network.NetworkHandler; import com.cleanroommc.groovyscript.network.SReloadScripts; +import com.cleanroommc.groovyscript.sandbox.GroovyLogImpl; import com.cleanroommc.groovyscript.sandbox.LoadStage; import net.minecraft.command.ICommandSender; import net.minecraft.creativetab.CreativeTabs; @@ -43,6 +44,9 @@ public GSCommand() { addSubcommand(new SimpleCommand("reload", (server, sender, args) -> { if (sender instanceof EntityPlayerMP) { + if (hasArgument(args, "--clean")) { + GroovyLogImpl.LOG.cleanLog(); + } runReload((EntityPlayerMP) sender, server); } })); @@ -65,24 +69,26 @@ public GSCommand() { addSubcommand(new InfoLookingCommand()); addSubcommand(new InfoSelfCommand()); - addSubcommand(new SimpleCommand("wiki", (server, sender, args) -> - sender.sendMessage(new TextComponentString("GroovyScript wiki") - .setStyle(new Style() - .setColor(TextFormatting.GOLD) - .setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new TextComponentString("Click to open wiki in browser"))) - .setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, "https://cleanroommc.com/groovy-script/")))) - , "doc", "docs", "documentation")); + addSubcommand(new SimpleCommand("wiki", (server, sender, args) -> sender.sendMessage( + new TextComponentString("GroovyScript wiki").setStyle(new Style().setColor(TextFormatting.GOLD).setHoverEvent( + new HoverEvent(HoverEvent.Action.SHOW_TEXT, + new TextComponentString("Click to open wiki in browser"))).setClickEvent( + new ClickEvent(ClickEvent.Action.OPEN_URL, "https://cleanroommc.com/groovy-script/")))), "doc", "docs", + "documentation")); addSubcommand(new SimpleCommand("generateWiki", (server, sender, args) -> { Documentation.generateWiki(); - sender.sendMessage(new TextComponentString("Generated a local version of the Groovyscript wiki has been generated to the ") - .appendSibling(getTextForFile("Wiki Folder", Documentation.WIKI.toPath().toString(), new TextComponentString("Click to open the generated GroovyScript wiki folder")))); + sender.sendMessage( + new TextComponentString("Generated a local version of the Groovyscript wiki has been generated to the ").appendSibling( + getTextForFile("Wiki Folder", Documentation.WIKI.toPath().toString(), + new TextComponentString("Click to open the generated GroovyScript wiki folder")))); }, "generateDoc", "generateDocs", "generateDocumentation")); addSubcommand(new SimpleCommand("generateExamples", (server, sender, args) -> { Documentation.generateExamples(); - sender.sendMessage(new TextComponentString("Generated examples for the enabled Groovyscript compat to the ") - .appendSibling(getTextForFile("Examples Folder", Documentation.EXAMPLES.toPath().toString(), new TextComponentString("Click to open the Groovyscript examples folder")))); + sender.sendMessage(new TextComponentString("Generated examples for the enabled Groovyscript compat to the ").appendSibling( + getTextForFile("Examples Folder", Documentation.EXAMPLES.toPath().toString(), + new TextComponentString("Click to open the Groovyscript examples folder")))); })); addSubcommand(new SimpleCommand("creativeTabs", (server, sender, args) -> { @@ -90,8 +96,9 @@ public GSCommand() { for (CreativeTabs tab : CreativeTabs.CREATIVE_TAB_ARRAY) { GroovyLog.get().getWriter().println(" - " + tab.getTabLabel()); } - sender.sendMessage(new TextComponentString("Creative tabs has been logged to the ") - .appendSibling(GSCommand.getTextForFile("Groovy Log", GroovyLog.get().getLogFilerPath().toString(), new TextComponentString("Click to open GroovyScript log")))); + sender.sendMessage(new TextComponentString("Creative tabs has been logged to the ").appendSibling( + GSCommand.getTextForFile("Groovy Log", GroovyLog.get().getLogFilePath().toString(), + new TextComponentString("Click to open GroovyScript log")))); })); addSubcommand(new SimpleCommand("deleteScriptCache", (server, sender, args) -> { @@ -104,10 +111,15 @@ public GSCommand() { addSubcommand(new SimpleCommand("runLS", (server, sender, args) -> { if (GroovyScript.runLanguageServer()) { - sender.sendMessage(new TextComponentString("Starting language server")); + sender.sendMessage(new TextComponentString(TextFormatting.GREEN + "Starting language server")); } else { - sender.sendMessage(new TextComponentString("Language server is already running")); + sender.sendMessage(new TextComponentString(TextFormatting.YELLOW + "Language server is already running")); } + }, "runLanguageServer")); + + addSubcommand(new SimpleCommand("cleanLog", (server, sender, args) -> { + GroovyLogImpl.LOG.cleanLog(); + sender.sendMessage(new TextComponentString(TextFormatting.GREEN + "Cleaned Groovy log")); })); if (ModSupport.MEKANISM.isLoaded()) { @@ -137,8 +149,8 @@ public String getUsage(@NotNull ICommandSender sender) { } public static void postLogFiles(ICommandSender sender) { - sender.sendMessage(getTextForFile("Groovy Log", GroovyLog.get().getLogFilerPath().toString(), new TextComponentString("Click to open GroovyScript log"))); - sender.sendMessage(getTextForFile("Minecraft Log", GroovyLog.get().getLogFilerPath().getParent().toString() + File.separator + "latest.log", new TextComponentString("Click to open Minecraft log"))); + sender.sendMessage(getTextForFile("Groovy Log", GroovyLog.get().getLogFilePath().toString(), new TextComponentString("Click to open GroovyScript log"))); + sender.sendMessage(getTextForFile("Minecraft Log", GroovyLog.get().getLogFilePath().getParent().toString() + File.separator + "latest.log", new TextComponentString("Click to open Minecraft log"))); } public static ITextComponent getTextForFile(String name, String path, ITextComponent hoverText) { @@ -148,4 +160,11 @@ public static ITextComponent getTextForFile(String name, String path, ITextCompo .setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, hoverText))); } + public static boolean hasArgument(String[] args, String arg) { + for (String a : args) { + if (a.equals(arg)) return true; + } + return false; + } + } diff --git a/src/main/java/com/cleanroommc/groovyscript/compat/mods/jei/JeiPlugin.java b/src/main/java/com/cleanroommc/groovyscript/compat/mods/jei/JeiPlugin.java index 66f966a5e..27900602f 100644 --- a/src/main/java/com/cleanroommc/groovyscript/compat/mods/jei/JeiPlugin.java +++ b/src/main/java/com/cleanroommc/groovyscript/compat/mods/jei/JeiPlugin.java @@ -89,7 +89,7 @@ public static SimpleCommand getJeiCategoriesCommand() { GroovyLog.get().getWriter().println(" - " + category.getUid()); } sender.sendMessage(new TextComponentString("JEI Categories has been logged to the ") - .appendSibling(GSCommand.getTextForFile("Groovy Log", GroovyLog.get().getLogFilerPath().toString(), new TextComponentString("Click to open GroovyScript log")))); + .appendSibling(GSCommand.getTextForFile("Groovy Log", GroovyLog.get().getLogFilePath().toString(), new TextComponentString("Click to open GroovyScript log")))); }); } } diff --git a/src/main/java/com/cleanroommc/groovyscript/sandbox/GroovyLogImpl.java b/src/main/java/com/cleanroommc/groovyscript/sandbox/GroovyLogImpl.java index df3484b3e..bec2710e0 100644 --- a/src/main/java/com/cleanroommc/groovyscript/sandbox/GroovyLogImpl.java +++ b/src/main/java/com/cleanroommc/groovyscript/sandbox/GroovyLogImpl.java @@ -36,15 +36,23 @@ public class GroovyLogImpl implements GroovyLog { private static final Logger logger = LogManager.getLogger("GroovyLog"); private final Path logFilePath; - private final PrintWriter printWriter; + private PrintWriter printWriter; private final DateFormat timeFormat = new SimpleDateFormat("[HH:mm:ss]"); private List errors = new ArrayList<>(); private GroovyLogImpl() { File minecraftHome = (File) FMLInjectionData.data()[6]; File logFile = new File(minecraftHome, "logs" + File.separator + getLogFileName()); - logFilePath = logFile.toPath(); - PrintWriter tempWriter; + this.logFilePath = logFile.toPath(); + this.printWriter = setupLog(logFile); + } + + public void cleanLog() { + this.printWriter = setupLog(this.logFilePath.toFile()); + } + + private PrintWriter setupLog(File logFile) { + PrintWriter writer; try { // delete file if it exists if (logFile.exists() && !logFile.isDirectory()) { @@ -53,15 +61,15 @@ private GroovyLogImpl() { // create file Files.createFile(logFilePath); // create writer which automatically flushes on write - tempWriter = new PrintWriter(new BufferedWriter(new OutputStreamWriter(Files.newOutputStream(logFile.toPath()))), true); + writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(Files.newOutputStream(logFile.toPath()))), true); } catch (IOException e) { - e.printStackTrace(); - tempWriter = new PrintWriter(System.out); + GroovyScript.LOGGER.throwing(e); + writer = new PrintWriter(System.out); } - this.printWriter = tempWriter; DateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy"); - writeLogLine("============ GroovyLog ==== " + dateFormat.format(new Date()) + " ============"); - writeLogLine("GroovyScript version: " + GroovyScript.VERSION); + writer.println("============ GroovyLog ==== " + dateFormat.format(new Date()) + " ============"); + writer.println("GroovyScript version: " + GroovyScript.VERSION); + return writer; } private static String getLogFileName() { @@ -86,7 +94,7 @@ public PrintWriter getWriter() { } @Override - public Path getLogFilerPath() { + public Path getLogFilePath() { return logFilePath; } @@ -243,9 +251,8 @@ public void errorMC(String msg, Object... args) { } /** - * Logs an exception to the groovy log AND Minecraft's log. - * It does NOT throw the exception! - * The stacktrace for the groovy log will be stripped for better readability. + * Logs an exception to the groovy log AND Minecraft's log. It does NOT throw the exception! The stacktrace for the groovy log will be + * stripped for better readability. * * @param throwable exception */ @@ -286,10 +293,13 @@ private List prepareStackTrace(StackTraceElement[] stackTrace) { private String formatLine(String level, String msg) { return timeFormat.format(new Date()) + - (FMLCommonHandler.instance().getEffectiveSide().isClient() ? " [CLIENT/" : " [SERVER/") + - level + "]" + - " [" + getSource() + "]: " + - msg; + (FMLCommonHandler.instance().getEffectiveSide().isClient() ? " [CLIENT/" : " [SERVER/") + + level + + "]" + + " [" + + getSource() + + "]: " + + msg; } private String getSource() { @@ -326,8 +336,7 @@ public static class MsgImpl implements GroovyLog.Msg { private final List messages = new ArrayList<>(); private Level level = Level.INFO; private boolean logToMcLog; - @Nullable - private Throwable throwable; + @Nullable private Throwable throwable; private MsgImpl(String msg, Object... data) { this.mainMsg = GroovyLog.format(msg, data); diff --git a/src/main/java/com/cleanroommc/groovyscript/sandbox/GroovyScriptSandbox.java b/src/main/java/com/cleanroommc/groovyscript/sandbox/GroovyScriptSandbox.java index 1e9118700..7c26befb6 100644 --- a/src/main/java/com/cleanroommc/groovyscript/sandbox/GroovyScriptSandbox.java +++ b/src/main/java/com/cleanroommc/groovyscript/sandbox/GroovyScriptSandbox.java @@ -303,7 +303,7 @@ protected Class loadScriptClass(GroovyScriptEngine engine, File file) { Class clazz = super.loadScriptClass(engine, relativeFile); if (comp.clazz == null) { // should not happen - GroovyLog.get().errorMC("Class for {} was loaded, but didn't receive class created callback! Index: {}", relativeFile, this.index); + GroovyLog.get().errorMC("Class for {} was loaded, but didn't receive class created callback!", relativeFile); if (ENABLE_CACHE) comp.clazz = clazz; } } else {