Skip to content

Commit

Permalink
Modified the Logger class to write to a file regardless of whether th…
Browse files Browse the repository at this point in the history
…e debug flag is enabled. (#72)
  • Loading branch information
dmccoystephenson authored Apr 6, 2022
1 parent b879a41 commit aa31449
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/main/java/dansapps/interakt/misc/CONFIG.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
* @since January 15th, 2022
*/
public class CONFIG {
public static boolean DEBUG_FLAG = true;
public static final int TIME_SLOT_LENGTH_IN_SECONDS = 60;
public static final int GRID_SIZE = 2;
public static boolean DEBUG_FLAG = false;
public static final int TIME_SLOT_LENGTH_IN_SECONDS = 16;
public static final int GRID_SIZE = 10;
public static final int MAX_CHANCE_TO_MOVE = 25;
public static final int MAX_CHANCE_TO_BEFRIEND = 50;
public static final int MAX_CHANCE_TO_ATTACK = 50;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/dansapps/interakt/objects/Actor.java
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ public String toString() {
"Num friends: " + friends.size() + "\n";
if (CONFIG.SHOW_LINEAGE_INFO) {
if (parentIDs.size() > 0) {
toReturn += "Parents: " + getParentNamesSeparatedByCommas();
toReturn += "Parents: " + getParentNamesSeparatedByCommas() + "\n";
}
if (childIDs.size() > 0) {
toReturn += "Children: " + getChildrenNamesSeparatedByCommas();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/
public class LocalStorageService {
private static LocalStorageService instance;
private final static String FILE_PATH = "/Interakt/";
public final static String FILE_PATH = "/Interakt/";
private final static String ACTORS_FILE_NAME = "actors.json";
private final static String WORLDS_FILE_NAME = "worlds.json";
private final static String REGIONS_FILE_NAME = "regions.json";
Expand Down
35 changes: 33 additions & 2 deletions src/main/java/dansapps/interakt/utils/Logger.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,31 @@
import dansapps.interakt.Interakt;
import dansapps.interakt.objects.Event;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.time.format.DateTimeFormatter;

import static dansapps.interakt.services.LocalStorageService.FILE_PATH;

/**
* @author Daniel McCoy Stephenson
* @since January 7th, 2022
*/
public class Logger {
private static Logger instance;
private boolean localDebugFlag = false;
private boolean logEventsToConsoleFlag = false;
private static String PATH = FILE_PATH + "log.txt";
private File file = new File(PATH);

private Logger() {

try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}

public static Logger getInstance() {
Expand All @@ -33,12 +46,14 @@ public static Logger getInstance() {
* @param message The message to log to the console.
*/
public void logInfo(String message) {
writeToFile(message);
if (isLocalDebugFlagEnabled() || Interakt.getInstance().isDebugEnabled()) {
System.out.println("[INFO] " + message);
}
}

public void logError(String errorMessage) {
writeToFile(errorMessage);
if (Interakt.getInstance().isDebugEnabled()) {
System.out.println("[ERROR] " + errorMessage);
}
Expand All @@ -47,7 +62,11 @@ public void logError(String errorMessage) {
public void logEvent(Event event) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedDateTime = event.getTimestamp().format(formatter);
System.out.println("[" + formattedDateTime + "] " + event.getMessage());
String datemessage = "[" + formattedDateTime + "] " + event.getMessage();
writeToFile(datemessage);
if (logEventsToConsoleFlag) {
System.out.println(datemessage);
}
}

public static void setInstance(Logger instance) {
Expand All @@ -61,4 +80,16 @@ public boolean isLocalDebugFlagEnabled() {
public void setLocalDebugFlagEnabled(boolean localDebugFlag) {
this.localDebugFlag = localDebugFlag;
}

private void writeToFile(String toWrite) {
try {
FileWriter fileWriter = new FileWriter(file, true);
BufferedWriter out = new BufferedWriter(fileWriter);
out.write(toWrite + "\n");
out.close();
fileWriter.close();
} catch(IOException e) {
e.printStackTrace();
}
}
}

0 comments on commit aa31449

Please sign in to comment.