diff --git a/.idea/vcs.xml b/.idea/vcs.xml
new file mode 100644
index 0000000..94a25f7
--- /dev/null
+++ b/.idea/vcs.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/log/18-02-2022 09-56-58.log b/log/18-02-2022 09-56-58.log
new file mode 100644
index 0000000..e69de29
diff --git a/log/latest.log b/log/latest.log
new file mode 100644
index 0000000..3898b11
--- /dev/null
+++ b/log/latest.log
@@ -0,0 +1,34 @@
+[09:58:54 INFO] Got ticket with UUID: eb64d1ad-a701-4890-a629-7fbbbc551778
+[09:58:54 INFO] Got ticket with UUID: f9a11e85-a7e7-422e-9533-6b7b23e4163b
+[09:58:54 WARN] max amount of tickets reached, waiting...
+[09:58:59 INFO] ticket with UUID f9a11e85-a7e7-422e-9533-6b7b23e4163b expired.
+[09:58:59 INFO] ticket with UUID eb64d1ad-a701-4890-a629-7fbbbc551778 expired.
+[09:58:59 INFO] i expired, oof :c
+[09:58:59 INFO] i expired, oof :c
+[09:58:59 INFO] Got ticket with UUID: 3df300a7-3e63-48be-9f96-85297927ad0f
+[09:58:59 INFO] Got ticket with UUID: 9e166410-dcb5-46d5-b294-f3df7c1f93ca
+[09:58:59 WARN] max amount of tickets reached, waiting...
+[09:59:04 INFO] ticket with UUID 3df300a7-3e63-48be-9f96-85297927ad0f expired.
+[09:59:04 INFO] ticket with UUID 9e166410-dcb5-46d5-b294-f3df7c1f93ca expired.
+[09:59:04 INFO] i expired, oof :c
+[09:59:04 INFO] i expired, oof :c
+[09:59:04 INFO] Got ticket with UUID: 13ba05cd-c27f-4e68-b75d-7794aeeeb45a
+[09:59:04 INFO] Got ticket with UUID: 58c4c964-8bb7-4b0f-897a-fa5025d31977
+[09:59:04 WARN] max amount of tickets reached, waiting...
+[09:59:09 INFO] ticket with UUID 58c4c964-8bb7-4b0f-897a-fa5025d31977 expired.
+[09:59:09 INFO] ticket with UUID 13ba05cd-c27f-4e68-b75d-7794aeeeb45a expired.
+[09:59:09 INFO] i expired, oof :c
+[09:59:09 INFO] i expired, oof :c
+[09:59:09 INFO] Got ticket with UUID: f01ad999-9819-4b0f-9f36-87f2bd337935
+[09:59:09 INFO] Got ticket with UUID: 1db2ff99-9f5b-425e-9ee8-62e2072dd8ce
+[09:59:09 WARN] max amount of tickets reached, waiting...
+[09:59:14 INFO] ticket with UUID f01ad999-9819-4b0f-9f36-87f2bd337935 expired.
+[09:59:14 INFO] ticket with UUID 1db2ff99-9f5b-425e-9ee8-62e2072dd8ce expired.
+[09:59:14 INFO] i expired, oof :c
+[09:59:14 INFO] Got ticket with UUID: e0f84463-9df9-4fc5-b9ff-5144207e0e30
+[09:59:14 INFO] i expired, oof :c
+[09:59:14 INFO] Got ticket with UUID: 3ba2bf87-0264-4799-a921-68c08383a245
+[09:59:19 INFO] ticket with UUID e0f84463-9df9-4fc5-b9ff-5144207e0e30 expired.
+[09:59:19 INFO] ticket with UUID 3ba2bf87-0264-4799-a921-68c08383a245 expired.
+[09:59:19 INFO] i expired, oof :c
+[09:59:19 INFO] i expired, oof :c
diff --git a/src/net/horizoncode/tickettest/TicketTest.java b/src/net/horizoncode/tickettest/TicketTest.java
index 7016ec0..15c3d9f 100644
--- a/src/net/horizoncode/tickettest/TicketTest.java
+++ b/src/net/horizoncode/tickettest/TicketTest.java
@@ -1,21 +1,25 @@
package net.horizoncode.tickettest;
import lombok.Getter;
-import net.horizoncode.tickettest.manager.Ticket;
-import net.horizoncode.tickettest.manager.TicketCallback;
+import net.horizoncode.tickettest.logging.Logger;
import net.horizoncode.tickettest.manager.TicketManager;
+import java.io.File;
import java.util.stream.IntStream;
public class TicketTest {
+ @Getter private static TicketTest instance;
@Getter private final TicketManager ticketManager;
+ @Getter private final Logger logger;
public static void main(String[] args) {
new TicketTest();
}
public TicketTest() {
+ instance = this;
+ this.logger = new Logger(new File("log"), true);
this.ticketManager = TicketManager.builder().ticketLifespan(5000).maxTicketsExisting(2).build();
IntStream.range(0, 10)
@@ -24,7 +28,7 @@ public class TicketTest {
getTicketManager()
.addTicket(
() -> {
- System.out.println("i expired oof");
+ getLogger().info("i expired, oof :c");
})
.use());
diff --git a/src/net/horizoncode/tickettest/logging/ConsoleColors.java b/src/net/horizoncode/tickettest/logging/ConsoleColors.java
new file mode 100644
index 0000000..2599ad8
--- /dev/null
+++ b/src/net/horizoncode/tickettest/logging/ConsoleColors.java
@@ -0,0 +1,68 @@
+package net.horizoncode.tickettest.logging;
+
+public class ConsoleColors
+{
+ public static final String RESET = "\u001b[0m";
+ public static final String BLACK = "\u001b[0;30m";
+ public static final String RED = "\u001b[0;31m";
+ public static final String GREEN = "\u001b[0;32m";
+ public static final String YELLOW = "\u001b[0;33m";
+ public static final String BLUE = "\u001b[0;34m";
+ public static final String PURPLE = "\u001b[0;35m";
+ public static final String CYAN = "\u001b[0;36m";
+ public static final String WHITE = "\u001b[0;37m";
+ public static final String BLACK_BOLD = "\u001b[1;30m";
+ public static final String RED_BOLD = "\u001b[1;31m";
+ public static final String GREEN_BOLD = "\u001b[1;32m";
+ public static final String YELLOW_BOLD = "\u001b[1;33m";
+ public static final String BLUE_BOLD = "\u001b[1;34m";
+ public static final String PURPLE_BOLD = "\u001b[1;35m";
+ public static final String CYAN_BOLD = "\u001b[1;36m";
+ public static final String WHITE_BOLD = "\u001b[1;37m";
+ public static final String BLACK_UNDERLINED = "\u001b[4;30m";
+ public static final String RED_UNDERLINED = "\u001b[4;31m";
+ public static final String GREEN_UNDERLINED = "\u001b[4;32m";
+ public static final String YELLOW_UNDERLINED = "\u001b[4;33m";
+ public static final String BLUE_UNDERLINED = "\u001b[4;34m";
+ public static final String PURPLE_UNDERLINED = "\u001b[4;35m";
+ public static final String CYAN_UNDERLINED = "\u001b[4;36m";
+ public static final String WHITE_UNDERLINED = "\u001b[4;37m";
+ public static final String BLACK_BACKGROUND = "\u001b[40m";
+ public static final String RED_BACKGROUND = "\u001b[41m";
+ public static final String GREEN_BACKGROUND = "\u001b[42m";
+ public static final String YELLOW_BACKGROUND = "\u001b[43m";
+ public static final String BLUE_BACKGROUND = "\u001b[44m";
+ public static final String PURPLE_BACKGROUND = "\u001b[45m";
+ public static final String CYAN_BACKGROUND = "\u001b[46m";
+ public static final String WHITE_BACKGROUND = "\u001b[47m";
+ public static final String BLACK_BRIGHT = "\u001b[0;90m";
+ public static final String RED_BRIGHT = "\u001b[0;91m";
+ public static final String GREEN_BRIGHT = "\u001b[0;92m";
+ public static final String YELLOW_BRIGHT = "\u001b[0;93m";
+ public static final String BLUE_BRIGHT = "\u001b[0;94m";
+ public static final String PURPLE_BRIGHT = "\u001b[0;95m";
+ public static final String CYAN_BRIGHT = "\u001b[0;96m";
+ public static final String WHITE_BRIGHT = "\u001b[0;97m";
+ public static final String BLACK_BOLD_BRIGHT = "\u001b[1;90m";
+ public static final String RED_BOLD_BRIGHT = "\u001b[1;91m";
+ public static final String GREEN_BOLD_BRIGHT = "\u001b[1;92m";
+ public static final String YELLOW_BOLD_BRIGHT = "\u001b[1;93m";
+ public static final String BLUE_BOLD_BRIGHT = "\u001b[1;94m";
+ public static final String PURPLE_BOLD_BRIGHT = "\u001b[1;95m";
+ public static final String CYAN_BOLD_BRIGHT = "\u001b[1;96m";
+ public static final String WHITE_BOLD_BRIGHT = "\u001b[1;97m";
+ public static final String BLACK_BACKGROUND_BRIGHT = "\u001b[0;100m";
+ public static final String RED_BACKGROUND_BRIGHT = "\u001b[0;101m";
+ public static final String GREEN_BACKGROUND_BRIGHT = "\u001b[0;102m";
+ public static final String YELLOW_BACKGROUND_BRIGHT = "\u001b[0;103m";
+ public static final String BLUE_BACKGROUND_BRIGHT = "\u001b[0;104m";
+ public static final String PURPLE_BACKGROUND_BRIGHT = "\u001b[0;105m";
+ public static final String CYAN_BACKGROUND_BRIGHT = "\u001b[0;106m";
+ public static final String WHITE_BACKGROUND_BRIGHT = "\u001b[0;107m";
+
+ public static String stripColors(final String msg) {
+ String stripped = msg;
+ stripped = stripped.replace("\u001b[0m", "").replace("\u001b[0;30m", "").replace("\u001b[0;31m", "").replace("\u001b[0;32m", "").replace("\u001b[0;33m", "").replace("\u001b[0;34m", "").replace("\u001b[0;35m", "").replace("\u001b[0;36m", "").replace("\u001b[0;37m", "").replace("\u001b[40m", "").replace("\u001b[41m", "").replace("\u001b[42m", "").replace("\u001b[43m", "").replace("\u001b[44m", "").replace("\u001b[45m", "").replace("\u001b[46m", "").replace("\u001b[47m", "").replace("\u001b[0;100m", "").replace("\u001b[0;101m", "").replace("\u001b[0;102m", "").replace("\u001b[0;103m", "").replace("\u001b[0;104m", "").replace("\u001b[0;105m", "").replace("\u001b[0;106m", "").replace("\u001b[0;107m", "").replace("\u001b[1;30m", "").replace("\u001b[1;31m", "").replace("\u001b[1;32m", "").replace("\u001b[1;33m", "").replace("\u001b[1;34m", "").replace("\u001b[1;35m", "").replace("\u001b[1;36m", "").replace("\u001b[1;37m", "").replace("\u001b[1;90m", "").replace("\u001b[1;91m", "").replace("\u001b[1;92m", "").replace("\u001b[1;93m", "").replace("\u001b[1;94m", "").replace("\u001b[1;95m", "").replace("\u001b[1;96m", "").replace("\u001b[1;97m", "").replace("\u001b[0;90m", "").replace("\u001b[0;91m", "").replace("\u001b[0;92m", "").replace("\u001b[0;93m", "").replace("\u001b[0;94m", "").replace("\u001b[0;95m", "").replace("\u001b[0;96m", "").replace("\u001b[0;97m", "").replace("\u001b[4;30m", "").replace("\u001b[4;31m", "").replace("\u001b[4;32m", "").replace("\u001b[4;33m", "").replace("\u001b[4;34m", "").replace("\u001b[4;35m", "").replace("\u001b[4;36m", "").replace("\u001b[4;37m", "");
+ return stripped;
+ }
+}
\ No newline at end of file
diff --git a/src/net/horizoncode/tickettest/logging/Logger.java b/src/net/horizoncode/tickettest/logging/Logger.java
new file mode 100644
index 0000000..2e17b48
--- /dev/null
+++ b/src/net/horizoncode/tickettest/logging/Logger.java
@@ -0,0 +1,120 @@
+package net.horizoncode.tickettest.logging;
+
+import org.apache.commons.io.FileUtils;
+
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.concurrent.ConcurrentLinkedDeque;
+
+public class Logger {
+ private final SimpleDateFormat dateFormat;
+ private final File logfile;
+ private final ConcurrentLinkedDeque logList;
+ private final boolean debug;
+
+ public Logger(final File dir, boolean debug) {
+ this.debug = debug;
+ SimpleDateFormat dateFormat_logfile = new SimpleDateFormat("dd-MM-yyyy HH-mm-ss");
+ this.dateFormat = new SimpleDateFormat("HH:mm:ss");
+ this.logList = new ConcurrentLinkedDeque<>();
+ if (!dir.exists()) {
+ dir.mkdir();
+ }
+ final File latestdel = new File(dir, "latest.log");
+ if (latestdel.exists() && !latestdel.isDirectory()) {
+ final String date = dateFormat_logfile.format(new Date(latestdel.lastModified()));
+ final File newdatefile = new File(dir, date + ".log");
+ try {
+ FileUtils.copyFile(latestdel, newdatefile);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ this.logfile = new File(dir, "latest.log");
+ if (!this.logfile.exists()) {
+ try {
+ this.logfile.createNewFile();
+ } catch (IOException e2) {
+ e2.printStackTrace();
+ }
+ }
+ }
+
+ public void emptyLine() {
+ System.out.println(" ");
+ this.logToFile(" ");
+ }
+
+ public void logCommand(final String msg) {
+ this.logToFile("> " + msg);
+ }
+
+ public void info(final String msg) {
+ final String date = this.dateFormat.format(new Date());
+ final String[] split = msg.split("\n");
+ for (final String msgeh : split) {
+ final String colout = "\u001b[0m[" + date + " " + "\u001b[0;34m" + "INFO" + "\u001b[0m" + "] " + msgeh;
+ final String out = "[" + date + " INFO] " + ConsoleColors.stripColors(msgeh);
+ System.out.println(colout);
+ this.logToFile(out);
+ }
+ }
+
+ public void debug(final String msg) {
+ final String date = this.dateFormat.format(new Date());
+ final String[] split = msg.split("\n");
+ for (final String msgeh : split) {
+ final String colout = "\u001b[0m[" + date + " " + "\u001b[4;33m" + "DEBUG" + "\u001b[0m" + "] " + msgeh;
+ final String out = "[" + date + " DEBUG] " + ConsoleColors.stripColors(msgeh);
+ if (debug)
+ System.out.println(colout);
+ this.logToFile(out);
+ }
+ }
+
+ public void warning(final String msg) {
+ final String date = this.dateFormat.format(new Date());
+ final String[] split = msg.split("\n");
+ for (final String msgeh : split) {
+ final String colout = "\u001b[0m[" + date + " " + "\u001b[4;33m" + "WARN" + "\u001b[0m" + "] " + msgeh;
+ final String out = "[" + date + " WARN] " + ConsoleColors.stripColors(msgeh);
+ System.out.println(colout);
+ this.logToFile(out);
+ }
+ }
+
+ public void error(final String msg) {
+ final String date = this.dateFormat.format(new Date());
+ final String[] split = msg.split("\n");
+ for (final String msgeh : split) {
+ final String colout = "\u001b[0m[" + date + " " + "\u001b[1;31m" + "ERROR" + "\u001b[0m" + "] " + msgeh;
+ final String out = "[" + date + " ERROR] " + ConsoleColors.stripColors(msgeh);
+ System.out.println(colout);
+ this.logToFile(out);
+ }
+ }
+
+ private void logToFile(final String s) {
+ this.logList.add(s);
+ this.saveFile();
+ }
+
+ private void saveFile() {
+ try {
+ synchronized (this.logfile) {
+ final BufferedWriter bw = new BufferedWriter(new FileWriter(this.logfile));
+ for (final String str : this.logList) {
+ bw.write(str);
+ bw.newLine();
+ }
+ bw.close();
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/net/horizoncode/tickettest/manager/Ticket.java b/src/net/horizoncode/tickettest/manager/Ticket.java
index 2aad54c..f6adfc6 100644
--- a/src/net/horizoncode/tickettest/manager/Ticket.java
+++ b/src/net/horizoncode/tickettest/manager/Ticket.java
@@ -3,6 +3,7 @@ package net.horizoncode.tickettest.manager;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
+import net.horizoncode.tickettest.TicketTest;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
@@ -29,7 +30,7 @@ public class Ticket extends CompletableFuture {
.submit(
() -> {
TimeUnit.MILLISECONDS.sleep(ticketManager.getTicketLifespan());
- System.out.println("ticket with UUID: " + getTicketID() + " expired.");
+ TicketTest.getInstance().getLogger().info("ticket with UUID " + getTicketID() + " expired.");
ticketManager.getTicketSet().remove(this);
getOnExpire().run();
return null;
diff --git a/src/net/horizoncode/tickettest/manager/TicketCallback.java b/src/net/horizoncode/tickettest/manager/TicketCallback.java
deleted file mode 100644
index 9e71596..0000000
--- a/src/net/horizoncode/tickettest/manager/TicketCallback.java
+++ /dev/null
@@ -1,8 +0,0 @@
-package net.horizoncode.tickettest.manager;
-
-public class TicketCallback{
- public Ticket call(Ticket ticket) {
- System.out.println("Ticket " + ticket.getTicketID() + " expired.");
- return ticket;
- }
-}
diff --git a/src/net/horizoncode/tickettest/manager/TicketManager.java b/src/net/horizoncode/tickettest/manager/TicketManager.java
index 09606e9..b45f72c 100644
--- a/src/net/horizoncode/tickettest/manager/TicketManager.java
+++ b/src/net/horizoncode/tickettest/manager/TicketManager.java
@@ -5,6 +5,7 @@ import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import lombok.Builder;
import lombok.Getter;
+import net.horizoncode.tickettest.TicketTest;
import java.util.HashSet;
import java.util.Set;
@@ -29,14 +30,14 @@ public class TicketManager {
service.submit(
() -> {
if (waitForFreeTicket())
- System.out.println("max amount of tickets reached, waiting...");
+ TicketTest.getInstance().getLogger().warning("max amount of tickets reached, waiting...");
while (waitForFreeTicket()) {
TimeUnit.MILLISECONDS.sleep(1);
}
Ticket t =
Ticket.builder().timeTicketCreated(ticketLifespan).onExpire(onExpire).ticketManager(this).build();
- System.out.println("Got ticket with UUID: " + t.getTicketID());
+ TicketTest.getInstance().getLogger().info("Got ticket with UUID: " + t.getTicketID());
return t;
});
try {
diff --git a/target/classes/net/horizoncode/tickettest/TicketTest.class b/target/classes/net/horizoncode/tickettest/TicketTest.class
index b4e1de4..5079aa8 100644
Binary files a/target/classes/net/horizoncode/tickettest/TicketTest.class and b/target/classes/net/horizoncode/tickettest/TicketTest.class differ
diff --git a/target/classes/net/horizoncode/tickettest/logging/ConsoleColors.class b/target/classes/net/horizoncode/tickettest/logging/ConsoleColors.class
new file mode 100644
index 0000000..27067a3
Binary files /dev/null and b/target/classes/net/horizoncode/tickettest/logging/ConsoleColors.class differ
diff --git a/target/classes/net/horizoncode/tickettest/logging/Logger.class b/target/classes/net/horizoncode/tickettest/logging/Logger.class
new file mode 100644
index 0000000..8f34200
Binary files /dev/null and b/target/classes/net/horizoncode/tickettest/logging/Logger.class differ
diff --git a/target/classes/net/horizoncode/tickettest/manager/Ticket$Builder.class b/target/classes/net/horizoncode/tickettest/manager/Ticket$Builder.class
index 159d4b4..9db17c1 100644
Binary files a/target/classes/net/horizoncode/tickettest/manager/Ticket$Builder.class and b/target/classes/net/horizoncode/tickettest/manager/Ticket$Builder.class differ
diff --git a/target/classes/net/horizoncode/tickettest/manager/Ticket$InternalTicketBuilder.class b/target/classes/net/horizoncode/tickettest/manager/Ticket$InternalTicketBuilder.class
index 991f2c3..a5fa954 100644
Binary files a/target/classes/net/horizoncode/tickettest/manager/Ticket$InternalTicketBuilder.class and b/target/classes/net/horizoncode/tickettest/manager/Ticket$InternalTicketBuilder.class differ
diff --git a/target/classes/net/horizoncode/tickettest/manager/Ticket.class b/target/classes/net/horizoncode/tickettest/manager/Ticket.class
index 12f86f1..6d91080 100644
Binary files a/target/classes/net/horizoncode/tickettest/manager/Ticket.class and b/target/classes/net/horizoncode/tickettest/manager/Ticket.class differ
diff --git a/target/classes/net/horizoncode/tickettest/manager/TicketCallback.class b/target/classes/net/horizoncode/tickettest/manager/TicketCallback.class
deleted file mode 100644
index 8be9555..0000000
Binary files a/target/classes/net/horizoncode/tickettest/manager/TicketCallback.class and /dev/null differ
diff --git a/target/classes/net/horizoncode/tickettest/manager/TicketManager$Builder.class b/target/classes/net/horizoncode/tickettest/manager/TicketManager$Builder.class
index 78f4ea8..ecb01e5 100644
Binary files a/target/classes/net/horizoncode/tickettest/manager/TicketManager$Builder.class and b/target/classes/net/horizoncode/tickettest/manager/TicketManager$Builder.class differ
diff --git a/target/classes/net/horizoncode/tickettest/manager/TicketManager$InternalTicketManagerBuilder.class b/target/classes/net/horizoncode/tickettest/manager/TicketManager$InternalTicketManagerBuilder.class
index 308c10c..3406277 100644
Binary files a/target/classes/net/horizoncode/tickettest/manager/TicketManager$InternalTicketManagerBuilder.class and b/target/classes/net/horizoncode/tickettest/manager/TicketManager$InternalTicketManagerBuilder.class differ
diff --git a/target/classes/net/horizoncode/tickettest/manager/TicketManager.class b/target/classes/net/horizoncode/tickettest/manager/TicketManager.class
index 447e286..385f3ad 100644
Binary files a/target/classes/net/horizoncode/tickettest/manager/TicketManager.class and b/target/classes/net/horizoncode/tickettest/manager/TicketManager.class differ