-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added debugging verification class and log
- Loading branch information
1 parent
7301ddb
commit 6dfec3d
Showing
3 changed files
with
200 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
...awncontrol-1.12.2/src/main/java/org/imesense/dynamicspawncontrol/debug/CheckDebugger.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package org.imesense.dynamicspawncontrol.debug; | ||
|
||
public final class CheckDebugger | ||
{ | ||
public boolean IsRunDebugger; | ||
|
||
private static CheckDebugger instance = null; | ||
|
||
public CheckDebugger() | ||
{ | ||
instance = this; | ||
IsRunDebugger = run(); | ||
} | ||
|
||
public static CheckDebugger getInstance() | ||
{ | ||
return instance; | ||
} | ||
|
||
private boolean run() | ||
{ | ||
return (System.getProperty("java.class.path").toLowerCase().contains("idea_rt.jar")); | ||
} | ||
} |
145 changes: 145 additions & 0 deletions
145
...ol-1.12.2/src/main/java/org/imesense/dynamicspawncontrol/technical/customlibrary/Log.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
package org.imesense.dynamicspawncontrol.technical.customlibrary; | ||
|
||
import javax.annotation.Nonnull; | ||
import java.io.*; | ||
import java.text.SimpleDateFormat; | ||
import java.util.ArrayList; | ||
import java.util.Date; | ||
import java.util.List; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
|
||
public final class Log | ||
{ | ||
private static File logFile; | ||
|
||
public static final String[] TypeLog = { "[INFO]: ", "[WARN]: ", "[ERROR]: " }; | ||
|
||
private static final ExecutorService executor = Executors.newSingleThreadExecutor(); | ||
|
||
public static void createLogFile(String path) | ||
{ | ||
try | ||
{ | ||
File logsDirectory = new File(path, "logs"); | ||
|
||
if (!logsDirectory.exists()) | ||
{ | ||
if (logsDirectory.mkdirs()) | ||
{ | ||
System.out.println("The 'logs' folder has been created successfully: " + logsDirectory.getAbsolutePath()); | ||
} | ||
else | ||
{ | ||
System.err.println("The 'logs' folder could not be created."); | ||
return; | ||
} | ||
} | ||
|
||
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss"); | ||
String currentDate = dateFormat.format(new Date()); | ||
|
||
String fileName = logsDirectory + "/log_" + currentDate + ".txt"; | ||
logFile = new File(fileName); | ||
|
||
FileWriter writer = new FileWriter(logFile); | ||
|
||
writer.write("***********************************************"); | ||
writer.write("\n** Log file created: " + currentDate); | ||
writer.write("\n** DynamicSpawnControl. Authors: OldSerpskiStalker, acidicMercury8"); | ||
writer.write("\n***********************************************"); | ||
|
||
writer.close(); | ||
|
||
System.out.println("The file was successfully created: " + logFile.getAbsolutePath()); | ||
} | ||
catch (IOException e) | ||
{ | ||
System.err.println("Error creating the file: " + e.getMessage()); | ||
} | ||
} | ||
|
||
private static void cleanFile(File file, int maxLines) | ||
{ | ||
try | ||
{ | ||
String line; | ||
List<String> lines = new ArrayList<>(); | ||
BufferedReader reader = new BufferedReader(new FileReader(file)); | ||
|
||
while ((line = reader.readLine()) != null) | ||
{ | ||
lines.add(line); | ||
} | ||
|
||
reader.close(); | ||
|
||
if (lines.size() >= maxLines) | ||
{ | ||
final int START_LINE = 5; | ||
|
||
int startIndex = Math.max(0, START_LINE - 1); | ||
int endIndex = Math.min(lines.size(), startIndex + maxLines); | ||
|
||
lines.subList(startIndex, endIndex).clear(); | ||
|
||
BufferedWriter writer = new BufferedWriter(new FileWriter(file)); | ||
|
||
for (int i = 0; i < lines.size(); i++) | ||
{ | ||
writer.write(lines.get(i)); | ||
|
||
if (i < lines.size() - 1) | ||
{ | ||
writer.newLine(); | ||
} | ||
} | ||
|
||
writer.close(); | ||
|
||
System.out.println("The file was successfully updated."); | ||
} | ||
else | ||
{ | ||
System.out.println("No update needed. The file has not reached the maximum number of lines."); | ||
} | ||
} | ||
catch (IOException e) | ||
{ | ||
System.err.println("Error updating the file: " + e.getMessage()); | ||
} | ||
} | ||
|
||
public static void writeDataToLogFile(@Nonnull String typeInfo, String data) | ||
{ | ||
if (logFile != null) | ||
{ | ||
executor.submit(() -> | ||
{ | ||
try | ||
{ | ||
FileWriter writer = new FileWriter(logFile, true); | ||
|
||
writer.write("\n" + typeInfo + data); | ||
writer.close(); | ||
|
||
cleanFile(logFile, 2000/*INFConfigLog._logMaxLines*/); | ||
System.out.println("The data has been successfully written to the log file: " + logFile.getAbsolutePath()); | ||
} | ||
catch (IOException e) | ||
{ | ||
System.err.println("Error writing data to a file: " + e.getMessage()); | ||
} | ||
}); | ||
} | ||
else | ||
{ | ||
System.err.println("The log file has not been created. First, create a log file."); | ||
} | ||
} | ||
|
||
public static void closeExecutor() | ||
{ | ||
executor.shutdown(); | ||
} | ||
} |