Skip to content

Commit

Permalink
Added debugging verification class and log
Browse files Browse the repository at this point in the history
  • Loading branch information
OldSerpskiStalker committed Sep 17, 2024
1 parent 7301ddb commit 6dfec3d
Show file tree
Hide file tree
Showing 3 changed files with 200 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@
import net.minecraftforge.fml.common.event.FMLServerStartingEvent;
import net.minecraftforge.fml.common.event.FMLServerStoppedEvent;

import org.imesense.dynamicspawncontrol.debug.CheckDebugger;
import org.imesense.dynamicspawncontrol.technical.customlibrary.Log;
import org.imesense.dynamicspawncontrol.technical.proxy.IProxy;

import java.io.File;

/**
* Main class of modification
*/
Expand Down Expand Up @@ -43,12 +47,31 @@ public class DynamicSpawnControl
@Mod.Instance
public static DynamicSpawnControl Instance;

/**
*
*/
private static CheckDebugger checkDebugger;

/**
*
*/
private static File globalDirectory = null;

/**
*
* @return
*/
public static File getGlobalPathToConfigs()
{
return globalDirectory;
}

/**
* Sided proxy settings
*/
@SidedProxy(
clientSide = "org.imesense.dynamicspawncontrol.proxy.ClientProxy",
serverSide = "org.imesense.dynamicspawncontrol.proxy.ServerProxy"
clientSide = "org.imesense.dynamicspawncontrol.technical.proxy.ClientProxy",
serverSide = "org.imesense.dynamicspawncontrol.technical.proxy.ServerProxy"
)
public static IProxy Proxy;

Expand All @@ -68,6 +91,12 @@ public DynamicSpawnControl()
@EventHandler
public synchronized void preInit(FMLPreInitializationEvent event)
{
checkDebugger = new CheckDebugger();
globalDirectory = event.getModConfigurationDirectory();

Log.createLogFile(globalDirectory.getPath() + File.separator + "DynamicsSpawnControl");
Log.writeDataToLogFile(Log.TypeLog[0], "Check debugger -> " + checkDebugger.IsRunDebugger);

Proxy.preInit(event);
}

Expand Down
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"));
}
}
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();
}
}

0 comments on commit 6dfec3d

Please sign in to comment.