Skip to content

Commit

Permalink
add clean log command
Browse files Browse the repository at this point in the history
  • Loading branch information
brachy84 committed Oct 6, 2024
1 parent 0874204 commit 9e6f64c
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
53 changes: 36 additions & 17 deletions src/main/java/com/cleanroommc/groovyscript/command/GSCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}));
Expand All @@ -65,33 +69,36 @@ 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) -> {
GroovyLog.get().info("All creative tabs:");
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) -> {
Expand All @@ -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()) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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"))));
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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()) {
Expand All @@ -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() {
Expand All @@ -86,7 +94,7 @@ public PrintWriter getWriter() {
}

@Override
public Path getLogFilerPath() {
public Path getLogFilePath() {
return logFilePath;
}

Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -286,10 +293,13 @@ private List<String> 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() {
Expand Down Expand Up @@ -326,8 +336,7 @@ public static class MsgImpl implements GroovyLog.Msg {
private final List<String> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 9e6f64c

Please sign in to comment.