From 5d167b38848c3f69bbcfd952272ac39e48a9af93 Mon Sep 17 00:00:00 2001 From: Clyhtsuriva Date: Sat, 26 Jun 2021 21:54:26 +0200 Subject: LogHandler now working fine. --- log/spoteezer.log | 3 ++ src/main/java/musichub/main/Main.java | 12 ++++- src/main/java/musichub/util/LogHandler.java | 56 +++++++++++++++++++++ src/main/java/musichub/util/LogWriter.java | 27 ---------- .../classes/musichub/business/PathValidation.class | Bin 865 -> 0 bytes target/classes/musichub/main/Main.class | Bin 10064 -> 10287 bytes .../musichub/business/PathValidationTest.class | Bin 1190 -> 0 bytes 7 files changed, 70 insertions(+), 28 deletions(-) create mode 100644 src/main/java/musichub/util/LogHandler.java delete mode 100644 src/main/java/musichub/util/LogWriter.java delete mode 100644 target/classes/musichub/business/PathValidation.class delete mode 100644 target/test-classes/musichub/business/PathValidationTest.class diff --git a/log/spoteezer.log b/log/spoteezer.log index e69de29..091f5e0 100644 --- a/log/spoteezer.log +++ b/log/spoteezer.log @@ -0,0 +1,3 @@ +[yyyy-MM-dd HH:mm:ss.SSS] TYPE: Message +[2021-06-26 21:37:09.507] WARNING: The music file was not found with the path you've provided. +[2021-06-26 21:45:11.453] WARNING: The music file was not found with the path you've provided. \ No newline at end of file diff --git a/src/main/java/musichub/main/Main.java b/src/main/java/musichub/main/Main.java index e06d056..e7ca721 100644 --- a/src/main/java/musichub/main/Main.java +++ b/src/main/java/musichub/main/Main.java @@ -1,6 +1,7 @@ package musichub.main; import musichub.business.*; +import musichub.util.LogHandler; import javax.sound.sampled.LineUnavailableException; import javax.sound.sampled.UnsupportedAudioFileException; @@ -93,7 +94,9 @@ public class Main { System.out.println("Song content: "); String content = scan.nextLine(); if (!isPathValid(content)) { - System.out.println("The music file was not found with the path you've provided.\nType h for available commands"); + String logMsg = "The music file was not found with the path you've provided."; + LogHandler.write(logMsg, "WARNING"); //write a line in the log file + System.out.println(logMsg + "\nType h for available commands"); choice = scan.nextLine(); break; } @@ -240,6 +243,12 @@ public class Main { printAvailableCommands(); choice = scan.nextLine(); break; + case 'o': + //consult the app logs + LogHandler.read(); + System.out.println("Type h for available commands"); + choice = scan.nextLine(); + break; default: break; @@ -260,6 +269,7 @@ public class Main { System.out.println("p: create a new playlist from existing songs and audio books"); System.out.println("-: delete an existing playlist"); System.out.println("s: save elements, albums, playlists"); + System.out.println("o: consult the app logs"); System.out.println("q: quit program"); } } \ No newline at end of file diff --git a/src/main/java/musichub/util/LogHandler.java b/src/main/java/musichub/util/LogHandler.java new file mode 100644 index 0000000..f6522f5 --- /dev/null +++ b/src/main/java/musichub/util/LogHandler.java @@ -0,0 +1,56 @@ +package musichub.util; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardOpenOption; +import java.sql.Timestamp; + +/** + * Class offering a log writing method. + * + * @author Aimeric ADJUTOR + * @version 1.0 + */ + +public final class LogHandler { + + /** + * Method that just throws an AssertionError if the class is called + * + * @throws AssertionError you can't instantiate this class + * @author Aimeric ADJUTOR + */ + + public LogHandler() { + throw new AssertionError("You just can't instantiate this class."); + } + + public static void write(String msg, String type) throws IOException { + + Timestamp timestamp = new Timestamp(System.currentTimeMillis()); + + //Create the log using the given message + String logMsg = "\n[" + timestamp + "] " + type + ": " + msg; + + // Define the file name of the file + Path fileName = Path.of("log/spoteezer.log"); + + // Write into the file + Files.writeString(fileName, logMsg, StandardOpenOption.APPEND); + + + } + + public static void read() throws IOException { + Path fileName = Path.of("log/spoteezer.log"); + + // Read the content of the file + String file_content = Files.readString(fileName); + + // Print the content inside the file + System.out.println("\n" + file_content + "\n"); + + } + +} \ No newline at end of file diff --git a/src/main/java/musichub/util/LogWriter.java b/src/main/java/musichub/util/LogWriter.java deleted file mode 100644 index 46d602e..0000000 --- a/src/main/java/musichub/util/LogWriter.java +++ /dev/null @@ -1,27 +0,0 @@ -package musichub.util; - -/** - * Class offering a log writing method. - * - * @author Aimeric ADJUTOR - * @version 1.0 - */ - -public final class LogWriter { - - /** - * Method that just throws an AssertionError if the class is called - * - * @throws AssertionError you can't instantiate this class - * @author Aimeric ADJUTOR - */ - - public LogWriter() { - throw new AssertionError("You just can't instantiate this class."); - } - - public static void write(String msg) { - - } - -} \ No newline at end of file diff --git a/target/classes/musichub/business/PathValidation.class b/target/classes/musichub/business/PathValidation.class deleted file mode 100644 index 5e3ac15..0000000 Binary files a/target/classes/musichub/business/PathValidation.class and /dev/null differ diff --git a/target/classes/musichub/main/Main.class b/target/classes/musichub/main/Main.class index f3d2052..632e576 100644 Binary files a/target/classes/musichub/main/Main.class and b/target/classes/musichub/main/Main.class differ diff --git a/target/test-classes/musichub/business/PathValidationTest.class b/target/test-classes/musichub/business/PathValidationTest.class deleted file mode 100644 index 24d063b..0000000 Binary files a/target/test-classes/musichub/business/PathValidationTest.class and /dev/null differ -- cgit v1.2.3