Skip to content

Commit

Permalink
Merge pull request #1 from Team846/apBranch
Browse files Browse the repository at this point in the history
Merging 2024 offseason work

New features:
- Automatically saves log files
- Dialog to open files
- Error messages popup
  • Loading branch information
VyaasBaskar authored Dec 22, 2024
2 parents 73d8b71 + d4fc6e0 commit 2c7dd4d
Show file tree
Hide file tree
Showing 14 changed files with 310 additions and 71 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
target
target/classes/com/funkylogclient/Message.class
pom.xml

logs846
87 changes: 75 additions & 12 deletions src/main/java/com/funkylogclient/FunkyLogSorter.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.LinkedHashSet;
import java.util.LinkedList;

import javafx.animation.PauseTransition;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javafx.util.Duration;

public class FunkyLogSorter {
private static int MAX_LEN = 1200;
Expand All @@ -26,6 +26,11 @@ public class FunkyLogSorter {
public static LinkedList<Message> messages = new LinkedList<>();
public static LinkedList<Message> filtered = new LinkedList<>();

public static int num_open_alerts = 0;

public static String log_file_directory = System.getProperty("user.dir") + "/logs846";
public static FileWriter log_file;

public static void clear() {
messages.clear();
filtered.clear();
Expand All @@ -35,7 +40,7 @@ public static void reFilter() {
filtered.clear();

for (Message m : messages) {
if (!checkMessageBySearch(m)) {
if (!checkMessageBySearch(m)) {
continue;
} else if (allowLogs && m.isLog()) {
filtered.add(m);
Expand All @@ -48,7 +53,8 @@ public static void reFilter() {
}

private static boolean checkMessageBySearch(Message msg) {
if (searchTerm.equals("")) return true;
if (searchTerm.equals(""))
return true;

return msg.getSender().contains(searchTerm) || msg.getContent().contains(searchTerm);
}
Expand All @@ -73,7 +79,39 @@ public static void addMessage(Message m) {

messages.add(m);

if (!checkMessageBySearch(m)) {
if (log_file != null) {
try {
log_file.write(m.toString() + "\n");
} catch (IOException exc) {
exc.printStackTrace();
}
}

if (m.isError()) {
if (num_open_alerts < 5) {
Alert alert = new Alert(AlertType.ERROR);
alert.setTitle("FunkyLogs Error Notification");
alert.setHeaderText(m.getSender());
alert.setContentText(m.getContent());

alert.setX(alert.getX() + (num_open_alerts * 70));
alert.setY(alert.getY() + (num_open_alerts * 70));

alert.show();

PauseTransition delay = new PauseTransition(Duration.seconds(5));

delay.setOnFinished(event -> {
alert.close();
FunkyLogSorter.num_open_alerts--;
});

delay.play();

}
}

if (!checkMessageBySearch(m)) {

} else if (allowLogs && m.isLog()) {
filtered.add(m);
Expand Down Expand Up @@ -114,6 +152,31 @@ public static void logAllMessages() {
System.out.println("END\n");
}

public static String makeLogFileName() {
LocalDateTime dateTime = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss");
String dateString = dateTime.format(formatter);
return dateString + ".log846";
}

public static String getLogFileDirectory() {
return log_file_directory;
}

public static void makeNewLogFile() {
try {
File directory = new File(log_file_directory);
if (!directory.exists()) {
directory.mkdir();
}
if (log_file != null)
log_file.close();
log_file = new FileWriter(log_file_directory + "/" + makeLogFileName());
} catch (IOException exc) {
exc.printStackTrace();
}
}

public static String stringifyAllMessages() {
StringBuilder result = new StringBuilder();
for (Message m : messages) {
Expand All @@ -127,14 +190,14 @@ public static void saveToFile(Stage pstage) {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Save Log File");

FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("FunkyLogs File", ".log846");
fileChooser.getExtensionFilters().add(extFilter);

LocalDateTime dateTime = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss");
String dateString = dateTime.format(formatter);

fileChooser.setInitialFileName(dateString + ".txt");

FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("Text documents", ".txt");
fileChooser.getExtensionFilters().add(extFilter);
fileChooser.setInitialFileName(dateString + ".log846");

File file = fileChooser.showSaveDialog(pstage);

Expand All @@ -159,7 +222,7 @@ public static void createTestWarning(Stage pstage) {
}

public static void createTestError(Stage pstage) {
addMessage(new Message("2;TestSender;This is an Error;0.0;0;0.0"));
addMessage(new Message("2;TestSender;This is an Error;0.0;0;0.0"));
logAllMessages();
}

Expand Down
30 changes: 19 additions & 11 deletions src/main/java/com/funkylogclient/FunkyLogs.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class FunkyLogs extends Application {

@Override
public void start(Stage primaryStage) {
FunkyLogSorter.makeNewLogFile();
primaryStage.setTitle("FunkyLogs v1.0.0");

root = new BorderPane();
Expand Down Expand Up @@ -72,22 +73,29 @@ public void start(Stage primaryStage) {
mScrollPane.setFitToWidth(true);
mScrollPane.setFitToHeight(true);
messageZone.heightProperty().addListener((observable, oldValue, newValue) -> {
if (FunkyLogs.auto_scroll) mScrollPane.setVvalue(1.0);
if (FunkyLogs.auto_scroll)
mScrollPane.setVvalue(1.0);
});
mScrollPane.setStyle(Styles.SCROLL_PANE_STYLE);

center.getChildren().add(mScrollPane);

root.setCenter(center);

root.setRight(RightSidebar.getRightSidebar(getClass().getResource("logo.png"),
getClass().getResource("exit.png"), primaryStage,
(observable, prev, value) -> { FunkyLogs.auto_scroll = value; },
(observable, prev, value) -> { FunkyLogs.serverIP = value; },
(observable, prev, value) -> { FunkyLogs.port = Integer.parseInt(value); },
(ev) -> {
UDPClient.setConnectionAddress(FunkyLogs.serverIP, FunkyLogs.port);
}));
root.setRight(RightSidebar.getRightSidebar(getClass().getResource("logo.png"),
getClass().getResource("exit.png"), primaryStage,
(observable, prev, value) -> {
FunkyLogs.auto_scroll = value;
},
(observable, prev, value) -> {
FunkyLogs.serverIP = value;
},
(observable, prev, value) -> {
FunkyLogs.port = Integer.parseInt(value);
},
(ev) -> {
UDPClient.setConnectionAddress(FunkyLogs.serverIP, FunkyLogs.port);
}));

Scene scene = new Scene(root, Color.TRANSPARENT);
primaryStage.initStyle(StageStyle.TRANSPARENT);
Expand All @@ -109,7 +117,7 @@ protected Void call() throws Exception {
}
Thread.sleep(200);
try {
FunkyLogs.updateMessageZone();
FunkyLogs.updateMessageZone(primaryStage);
} catch (Exception exc) {
System.out.println(exc);
}
Expand All @@ -123,7 +131,7 @@ protected Void call() throws Exception {
updateThread.start();
}

private static void updateMessageZone() {
private static void updateMessageZone(Stage stage) {
Platform.runLater(() -> {
FunkyLogs.messageZone.getChildren().clear();
@SuppressWarnings("unchecked")
Expand Down
60 changes: 60 additions & 0 deletions src/main/java/com/funkylogclient/LogFileProcesser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.funkylogclient;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.LinkedList;
import java.util.Scanner;

import javafx.stage.Stage;
import javafx.stage.FileChooser;

public class LogFileProcesser {

private static Scanner input;
private static File file;

public static void selectFile(Stage stage) {
try {
FileChooser fileChooser = new FileChooser();
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("FunkyLogs File", "*.log846");
fileChooser.getExtensionFilters().add(extFilter);
fileChooser.setTitle("Open Log File");

fileChooser.setInitialDirectory(new File(FunkyLogSorter.getLogFileDirectory()));

file = fileChooser.showOpenDialog(stage);
readFile(file, stage);
} catch (Exception e) {
System.out.println(e.getMessage());
}

}

public void setFile(String pathname) {
file = new File(pathname);
}

public File getFile() {
if (file != null)
System.out.println(file.getAbsolutePath());
return file;
}

public static void readFile(File file, Stage primaryStage) {
try {
input = new Scanner(file);
} catch (FileNotFoundException ex) {
System.out.println("File not found");
System.exit(1);
}
LinkedList<Message> messages = new LinkedList<Message>();
while (input.hasNextLine()) {
Message log = new Message(input.nextLine());
if (log.getValid()) {
messages.add(log);
}
}
SavedFunkyLogs.displaySavedLogs(messages, primaryStage, file.getName());
}

}
18 changes: 13 additions & 5 deletions src/main/java/com/funkylogclient/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.layout.Background;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;

public class Message {
Expand All @@ -18,20 +16,25 @@ public class Message {
private double period_timestamp;
private int period;

private boolean isValid;

public Message(String unparsed) {
try {
String[] split = unparsed.split(";");

type = Integer.parseInt(split[0]);
time = Double.parseDouble(split[3]);
sender = split[1];
content = split[2];

isValid = true;
} catch (Exception exc) {
System.out.println("Error in parsing message: " + unparsed);

content = new String();
sender = new String("Unknown");
time = 0.0;

isValid = false;
}
}

Expand All @@ -44,6 +47,10 @@ public Message(int type, String sender, String content, double time, int period,
this.period_timestamp = period_timestamp;
}

public Boolean getValid() {
return isValid;
}

public String getContent() {
return content;
}
Expand Down Expand Up @@ -79,8 +86,9 @@ public boolean isError() {
@Override
public String toString() {
String output = "";
output += type + ";" + sender + ";" + content + ";" + "<" + time + ">" + ";" + period + ";" + "<" + period_timestamp + ">";

output += type + ";" + sender + ";" + content + ";" + time + ";" + period + ";"
+ period_timestamp;

return output;
}

Expand Down
Loading

0 comments on commit 2c7dd4d

Please sign in to comment.