-
Notifications
You must be signed in to change notification settings - Fork 0
Fix reseting errors and logging #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
101e677
0d92619
98be609
1d1d31b
9fc8aae
4c283c7
0274f50
becc58e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -5,26 +5,69 @@ | |||||
| import com.mojang.serialization.DataResult; | ||||||
| import com.mojang.serialization.JsonOps; | ||||||
| import com.mojang.serialization.codecs.RecordCodecBuilder; | ||||||
| import com.nyxz.fabric.locationwebsocket.handler.ERRORS; | ||||||
| import net.minecraft.util.StrictJsonParser; | ||||||
|
|
||||||
| import java.io.*; | ||||||
| import java.nio.file.Files; | ||||||
| import java.nio.file.Path; | ||||||
| import java.nio.file.StandardOpenOption; | ||||||
|
|
||||||
| import static net.fabricmc.fabric.impl.resource.loader.ModResourcePackUtil.GSON; | ||||||
|
|
||||||
| public class Config { | ||||||
| public static int WEBSOCKET_PORT = 8080; | ||||||
| public static String WEBSOCKET_URL = "127.0.0.1"; | ||||||
| public static int webSocketPort = 8080; | ||||||
| public static String webSocketUrl = "127.0.0.1"; | ||||||
|
|
||||||
| public static boolean enableMod = true; | ||||||
|
|
||||||
|
|
||||||
| public static File errorFilePath; | ||||||
| public static Writer errorFile; | ||||||
| /** | ||||||
| * Lock object for synchronizing access to errorFile. | ||||||
| * All code that writes to errorFile must synchronize on this lock. | ||||||
| */ | ||||||
| private static final Object errorFileLock = new Object(); | ||||||
|
|
||||||
| /** | ||||||
| * Thread-safe helper for writing to the error file. | ||||||
| * Usage: Config.writeErrorLine("message"); | ||||||
| */ | ||||||
| public static void writeErrorLine(String line) throws IOException { | ||||||
| if (errorFile != null) { | ||||||
| synchronized (errorFileLock) { | ||||||
| errorFile.write(line); | ||||||
| errorFile.write(System.lineSeparator()); | ||||||
| errorFile.flush(); | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
Pepijn-DB marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| * Constructor for the Config file | ||||||
| * @param WEBSOCKET_PORT The port for the WebSocket server | ||||||
| * @param WEBSOCKET_URL The URL for the WebSocket server | ||||||
| * @param webSocketPort The port for the WebSocket server | ||||||
| * @param webSocketUrl The URL for the WebSocket server | ||||||
| */ | ||||||
| public Config(int WEBSOCKET_PORT, String WEBSOCKET_URL) { | ||||||
| Config.WEBSOCKET_PORT = WEBSOCKET_PORT; | ||||||
| Config.WEBSOCKET_URL = WEBSOCKET_URL; | ||||||
| public Config(int webSocketPort, String webSocketUrl, boolean enableMod) { | ||||||
| Config.enableMod = enableMod; | ||||||
| Config.webSocketPort = webSocketPort; | ||||||
| Config.webSocketUrl = webSocketUrl; | ||||||
| } | ||||||
|
|
||||||
| public Config(int webSocketPort, String webSocketUrl, boolean enableMod, File errorFile) throws IOException { | ||||||
| Config.enableMod = enableMod; | ||||||
| Config.webSocketPort = webSocketPort; | ||||||
| Config.webSocketUrl = webSocketUrl; | ||||||
| Config.errorFile = new FileWriter(errorFile); | ||||||
| Config.errorFilePath = errorFile; | ||||||
|
|
||||||
| try{ | ||||||
| Files.newByteChannel(errorFilePath.toPath(), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING).close(); | ||||||
| } | ||||||
|
Comment on lines
+65
to
+67
|
||||||
| catch (IOException e){ | ||||||
| LocationWebSocket.LOGGER.error("Failed to create error log file", e); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| public static final Codec<Config> CODEC = RecordCodecBuilder.create(instance -> instance.group( | ||||||
|
|
@@ -33,19 +76,31 @@ public Config(int WEBSOCKET_PORT, String WEBSOCKET_URL) { | |||||
| .forGetter(Config::getWebsocketPort), | ||||||
| Codec.STRING | ||||||
| .fieldOf("websocketURL") | ||||||
| .forGetter(Config::getWebsocketUrl) | ||||||
| .forGetter(Config::getWebsocketUrl), | ||||||
| Codec.BOOL | ||||||
| .fieldOf("enableMod") | ||||||
| .forGetter(Config::getEnabled) | ||||||
| ).apply(instance, Config::new)); | ||||||
|
|
||||||
|
|
||||||
| public int getWebsocketPort() { | ||||||
| return WEBSOCKET_PORT; | ||||||
| return webSocketPort; | ||||||
| } | ||||||
|
|
||||||
| public String getWebsocketUrl() { | ||||||
| return WEBSOCKET_URL; | ||||||
| return webSocketUrl; | ||||||
| } | ||||||
|
|
||||||
| public boolean getEnabled() { | ||||||
| return enableMod; | ||||||
| } | ||||||
|
|
||||||
| public Writer getErrorFile() { | ||||||
|
||||||
| public Writer getErrorFile() { | |
| public Writer getErrorWriter() { |
Copilot
AI
Dec 6, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logging methods logErrors, logInfo, and logDebug have significant code duplication. Consider extracting the common logic into a private helper method like log(String level, String message) to improve maintainability.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The field name
errorFilePathsuggests it stores a path, but it's declared asFile. Consider renaming toerrorFilefor consistency with how it's used, or change the type toPathif you want to emphasize it's a path.