diff --git a/.gitignore b/.gitignore index a190bab..a2b3511 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ target target/classes/com/funkylogclient/Message.class pom.xml - +logs846 \ No newline at end of file diff --git a/src/main/java/com/funkylogclient/FunkyLogSorter.java b/src/main/java/com/funkylogclient/FunkyLogSorter.java index 1e8b427..f7f696a 100644 --- a/src/main/java/com/funkylogclient/FunkyLogSorter.java +++ b/src/main/java/com/funkylogclient/FunkyLogSorter.java @@ -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; @@ -26,6 +26,11 @@ public class FunkyLogSorter { public static LinkedList messages = new LinkedList<>(); public static LinkedList 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(); @@ -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); @@ -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); } @@ -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); @@ -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) { @@ -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); @@ -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(); } diff --git a/src/main/java/com/funkylogclient/FunkyLogs.java b/src/main/java/com/funkylogclient/FunkyLogs.java index 82d749d..2e00ea9 100644 --- a/src/main/java/com/funkylogclient/FunkyLogs.java +++ b/src/main/java/com/funkylogclient/FunkyLogs.java @@ -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(); @@ -72,7 +73,8 @@ 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); @@ -80,14 +82,20 @@ public void start(Stage primaryStage) { 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); @@ -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); } @@ -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") diff --git a/src/main/java/com/funkylogclient/LogFileProcesser.java b/src/main/java/com/funkylogclient/LogFileProcesser.java new file mode 100644 index 0000000..7923eea --- /dev/null +++ b/src/main/java/com/funkylogclient/LogFileProcesser.java @@ -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 messages = new LinkedList(); + while (input.hasNextLine()) { + Message log = new Message(input.nextLine()); + if (log.getValid()) { + messages.add(log); + } + } + SavedFunkyLogs.displaySavedLogs(messages, primaryStage, file.getName()); + } + +} diff --git a/src/main/java/com/funkylogclient/Message.java b/src/main/java/com/funkylogclient/Message.java index 24b23b0..7212638 100644 --- a/src/main/java/com/funkylogclient/Message.java +++ b/src/main/java/com/funkylogclient/Message.java @@ -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 { @@ -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; } } @@ -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; } @@ -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; } diff --git a/src/main/java/com/funkylogclient/SavedFunkyLogs.java b/src/main/java/com/funkylogclient/SavedFunkyLogs.java new file mode 100644 index 0000000..670cc13 --- /dev/null +++ b/src/main/java/com/funkylogclient/SavedFunkyLogs.java @@ -0,0 +1,97 @@ +package com.funkylogclient; + +import java.util.LinkedList; + +import javafx.application.Platform; +import javafx.geometry.*; +import javafx.scene.Scene; +import javafx.scene.control.Button; +import javafx.scene.control.Label; +import javafx.scene.control.ScrollPane; +import javafx.scene.layout.BorderPane; +import javafx.scene.layout.StackPane; +import javafx.scene.layout.VBox; +import javafx.scene.paint.Color; +import javafx.stage.Stage; +import javafx.stage.StageStyle; + +public class SavedFunkyLogs { + + private static VBox messageZone; + + private static Stage popupStage; + + public static void displaySavedLogs(LinkedList messages, Stage primaryStage, String fileName) { + popupStage = new Stage(); + popupStage.setTitle("Log File Reader"); + + BorderPane root = new BorderPane(); + root.getStyleClass().add("root"); + + VBox center = new VBox(); + center.setStyle(Styles.CENTER); + center.setPadding(new Insets(10, 10, 10, 10)); + + messageZone = new VBox(); + messageZone.setPrefSize(100000, 100000); + messageZone.setPadding(new Insets(5, 20, 5, 20)); + messageZone.setSpacing(2.0); + messageZone.setStyle("-fx-background-color: rgb(50, 50, 50);"); + + ScrollPane mScrollPane = new ScrollPane(messageZone); + mScrollPane.setFitToWidth(true); + mScrollPane.setFitToHeight(true); + + mScrollPane.setStyle("-fx-background-color: rgb(50, 50, 50);"); + + center.getChildren().add(mScrollPane); + + root.setCenter(center); + + Button closeButton = new Button("Close"); + closeButton.setOnAction(event -> popupStage.close()); + closeButton.setStyle("-fx-background-color: red; -fx-text-fill: white;"); + + Label fileNameLabel = new Label(fileName); + fileNameLabel.setStyle("-fx-text-fill: white;"); + + StackPane topPane = new StackPane(); + StackPane.setAlignment(closeButton, Pos.TOP_CENTER); + StackPane.setAlignment(fileNameLabel, Pos.TOP_LEFT); + topPane.setPadding(new Insets(10)); + topPane.getChildren().addAll(closeButton, fileNameLabel); + + root.setTop(topPane); + + Scene scene = new Scene(root, Color.TRANSPARENT); + popupStage.initStyle(StageStyle.TRANSPARENT); + popupStage.setScene(scene); + popupStage.requestFocus(); + popupStage.show(); + popupStage.toFront(); + + setStageSize(popupStage); + + root.setStyle("-fx-background-radius: 10; -fx-background-color:rgb(50, 50, 50);"); + + displayMessages(messages); + + primaryStage.setOnCloseRequest(event -> popupStage.close()); + } + + private static void displayMessages(LinkedList messages) { + Platform.runLater(() -> { + for (Message msg : messages) { + messageZone.getChildren().add(msg.getComponent()); + } + }); + } + + private static void setStageSize(Stage stage) { + stage.setX(200); + stage.setY(150); + + stage.setWidth(700); + stage.setHeight(500); + } +} diff --git a/src/main/java/com/funkylogclient/SidebarOtherSettings.java b/src/main/java/com/funkylogclient/SidebarOtherSettings.java index 84168c0..96fde22 100644 --- a/src/main/java/com/funkylogclient/SidebarOtherSettings.java +++ b/src/main/java/com/funkylogclient/SidebarOtherSettings.java @@ -1,7 +1,6 @@ package com.funkylogclient; import javafx.beans.value.ChangeListener; -import javafx.beans.value.ObservableValue; import javafx.geometry.Insets; import javafx.scene.control.Button; import javafx.scene.control.CheckBox; @@ -29,39 +28,54 @@ public static VBox getSidebarOtherSettings(ChangeListener autoScrollCha otherSettingsBox.getChildren().add(midSpacing); Button clearLogsButton = new Button("Clear logs"); - clearLogsButton.setOnAction((ev) -> {FunkyLogSorter.clear();}); + clearLogsButton.setOnAction((ev) -> { + FunkyLogSorter.clear(); + FunkyLogSorter.makeNewLogFile(); + }); otherSettingsBox.getChildren().add(clearLogsButton); - Region buttonSpacing = new Region(); - buttonSpacing.setMinHeight(20); - otherSettingsBox.getChildren().add(buttonSpacing); - - Button exportButton = new Button("Save logs"); - exportButton.setOnAction((ev) -> {FunkyLogSorter.saveToFile(primaryStage);}); - otherSettingsBox.getChildren().add(exportButton); - - Region testLogButtonspacing = new Region(); - testLogButtonspacing.setMinHeight(20); - otherSettingsBox.getChildren().add(testLogButtonspacing); - - Button testLogButton = new Button("Test Log"); - testLogButton.setOnAction((ev) -> { - FunkyLogSorter.createTestLog(primaryStage); - }); - otherSettingsBox.getChildren().add(testLogButton); + // Region buttonSpacing = new Region(); + // buttonSpacing.setMinHeight(20); + // otherSettingsBox.getChildren().add(buttonSpacing); - Button testWarningButton = new Button("Test Warning"); - testWarningButton.setOnAction((ev) -> { - FunkyLogSorter.createTestWarning(primaryStage); - }); - otherSettingsBox.getChildren().add(testWarningButton); + // Button exportButton = new Button("Save logs"); + // exportButton.setOnAction((ev) -> { + // FunkyLogSorter.saveToFile(primaryStage); + // }); + // otherSettingsBox.getChildren().add(exportButton); + + Region fileSelectionDialogSpacing = new Region(); + fileSelectionDialogSpacing.setMinHeight(20); + otherSettingsBox.getChildren().add(fileSelectionDialogSpacing); - Button testErrorButton = new Button("Test Error"); - testErrorButton.setOnAction((ev) -> { - FunkyLogSorter.createTestError(primaryStage); + Button fileSelectionDialog = new Button("Select File"); + fileSelectionDialog.setOnAction((ev) -> { + LogFileProcesser.selectFile(primaryStage); }); - otherSettingsBox.getChildren().add(testErrorButton); + otherSettingsBox.getChildren().add(fileSelectionDialog); + + // Region testLogButtonspacing = new Region(); + // testLogButtonspacing.setMinHeight(20); + // otherSettingsBox.getChildren().add(testLogButtonspacing); + + // Button testLogButton = new Button("Test Log"); + // testLogButton.setOnAction((ev) -> { + // FunkyLogSorter.createTestLog(primaryStage); + // }); + // otherSettingsBox.getChildren().add(testLogButton); + + // Button testWarningButton = new Button("Test Warning"); + // testWarningButton.setOnAction((ev) -> { + // FunkyLogSorter.createTestWarning(primaryStage); + // }); + // otherSettingsBox.getChildren().add(testWarningButton); + + // Button testErrorButton = new Button("Test Error"); + // testErrorButton.setOnAction((ev) -> { + // FunkyLogSorter.createTestError(primaryStage); + // }); + // otherSettingsBox.getChildren().add(testErrorButton); return otherSettingsBox; } diff --git a/src/main/java/com/funkylogclient/UDPClient.java b/src/main/java/com/funkylogclient/UDPClient.java index fdd8b93..9076a86 100644 --- a/src/main/java/com/funkylogclient/UDPClient.java +++ b/src/main/java/com/funkylogclient/UDPClient.java @@ -4,17 +4,6 @@ import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketTimeoutException; -import java.util.ArrayList; -import java.util.LinkedList; -import java.util.List; -import java.util.Queue; -import java.util.concurrent.Callable; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.Future; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; public class UDPClient { public static String serverIP = "10.8.46.2"; @@ -43,11 +32,12 @@ private static void threadFN() { } catch (Exception exc) { continue; } - + long ctime = System.currentTimeMillis(); if (ctime - lastKeepAlive > 500) { byte[] sendData = "~~~".getBytes(); - DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, serverAddress, UDPClient.port); + DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, serverAddress, + UDPClient.port); socket.send(sendPacket); lastKeepAlive = ctime; @@ -112,4 +102,3 @@ public static void start() { networkingThread.start(); } } - diff --git a/target/classes/com/funkylogclient/FunkyLogSorter.class b/target/classes/com/funkylogclient/FunkyLogSorter.class index db0ea04..2c5aca6 100644 Binary files a/target/classes/com/funkylogclient/FunkyLogSorter.class and b/target/classes/com/funkylogclient/FunkyLogSorter.class differ diff --git a/target/classes/com/funkylogclient/FunkyLogs$1.class b/target/classes/com/funkylogclient/FunkyLogs$1.class index 2bbc076..bebe64b 100644 Binary files a/target/classes/com/funkylogclient/FunkyLogs$1.class and b/target/classes/com/funkylogclient/FunkyLogs$1.class differ diff --git a/target/classes/com/funkylogclient/FunkyLogs.class b/target/classes/com/funkylogclient/FunkyLogs.class index 49d5935..f387bd6 100644 Binary files a/target/classes/com/funkylogclient/FunkyLogs.class and b/target/classes/com/funkylogclient/FunkyLogs.class differ diff --git a/target/classes/com/funkylogclient/Message.class b/target/classes/com/funkylogclient/Message.class index 3d296c1..c4571a9 100644 Binary files a/target/classes/com/funkylogclient/Message.class and b/target/classes/com/funkylogclient/Message.class differ diff --git a/target/classes/com/funkylogclient/SidebarOtherSettings.class b/target/classes/com/funkylogclient/SidebarOtherSettings.class index 7a37528..815d324 100644 Binary files a/target/classes/com/funkylogclient/SidebarOtherSettings.class and b/target/classes/com/funkylogclient/SidebarOtherSettings.class differ diff --git a/target/classes/com/funkylogclient/UDPClient.class b/target/classes/com/funkylogclient/UDPClient.class index f488df5..3126898 100644 Binary files a/target/classes/com/funkylogclient/UDPClient.class and b/target/classes/com/funkylogclient/UDPClient.class differ