From aa688a0ca4c5b93ae92ffee5f4c1fd5fdf7ddee9 Mon Sep 17 00:00:00 2001 From: JeremyStarTM Date: Sun, 9 Jun 2024 21:10:22 +0200 Subject: [PATCH] Add LogEvent and EngineCrashEvent --- .../base/events/EngineCrashEvent.java | 36 +++++++++++ .../sosengine/base/events/LogEvent.java | 60 +++++++++++++++++++ .../base/events/internal/package-info.java | 24 ++++++++ .../sosengine/base/logging/CrashHandler.java | 4 ++ .../sosengine/base/logging/Logger.java | 4 ++ 5 files changed, 128 insertions(+) create mode 100644 base/src/main/java/de/staropensource/sosengine/base/events/EngineCrashEvent.java create mode 100644 base/src/main/java/de/staropensource/sosengine/base/events/LogEvent.java create mode 100644 base/src/main/java/de/staropensource/sosengine/base/events/internal/package-info.java diff --git a/base/src/main/java/de/staropensource/sosengine/base/events/EngineCrashEvent.java b/base/src/main/java/de/staropensource/sosengine/base/events/EngineCrashEvent.java new file mode 100644 index 00000000..203c8ebf --- /dev/null +++ b/base/src/main/java/de/staropensource/sosengine/base/events/EngineCrashEvent.java @@ -0,0 +1,36 @@ +/* + STAROPENSOURCE ENGINE SOURCE FILE + Copyright (c) 2024 The StarOpenSource Engine Contributors + Licensed under the GNU Affero General Public License v3 + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as + published by the Free Software Foundation, either version 3 of the + License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . + */ + +package de.staropensource.sosengine.base.events; + +import de.staropensource.sosengine.base.classes.Event; + +/** + * Called in the event of an engine crash, just before the JVM exists. + * + * @since 1-alpha0 + */ +@SuppressWarnings({ "unused" }) +public final class EngineCrashEvent extends Event { + /** {@inheritDoc} */ + @Override + public void callEvent() { + invokeAnnotatedMethods(); + } +} diff --git a/base/src/main/java/de/staropensource/sosengine/base/events/LogEvent.java b/base/src/main/java/de/staropensource/sosengine/base/events/LogEvent.java new file mode 100644 index 00000000..5f1a8b9b --- /dev/null +++ b/base/src/main/java/de/staropensource/sosengine/base/events/LogEvent.java @@ -0,0 +1,60 @@ +/* + STAROPENSOURCE ENGINE SOURCE FILE + Copyright (c) 2024 The StarOpenSource Engine Contributors + Licensed under the GNU Affero General Public License v3 + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as + published by the Free Software Foundation, either version 3 of the + License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . + */ + +package de.staropensource.sosengine.base.events; + +import de.staropensource.sosengine.base.classes.Event; +import de.staropensource.sosengine.base.types.LogIssuer; +import de.staropensource.sosengine.base.types.LogLevel; +import org.jetbrains.annotations.NotNull; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +/** + * Called before a new log message is printed. + * + * @since 1-alpha0 + */ +@SuppressWarnings({ "unused" }) +public final class LogEvent extends Event { + /** + * {@inheritDoc} + * @deprecated use the {@code callEvent} method with arguments + * @see LogEvent#callEventNew(LogLevel, LogIssuer, String) + */ + @Deprecated + @Override + public void callEvent() { + invokeAnnotatedMethods(); + } + + /** + * Calls the event and notifies all annotated methods about it. + * + * @since 1-alpha0 + */ + public void callEventNew(@NotNull LogLevel level, @NotNull LogIssuer logIssuer, @NotNull String message) { + for (Method method : getAnnotatedMethods()) { + try { + method.invoke(null, level, logIssuer, message); + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NullPointerException | ExceptionInInitializerError ignored) {} + } + } +} diff --git a/base/src/main/java/de/staropensource/sosengine/base/events/internal/package-info.java b/base/src/main/java/de/staropensource/sosengine/base/events/internal/package-info.java new file mode 100644 index 00000000..92288dd9 --- /dev/null +++ b/base/src/main/java/de/staropensource/sosengine/base/events/internal/package-info.java @@ -0,0 +1,24 @@ +/* + STAROPENSOURCE ENGINE SOURCE FILE + Copyright (c) 2024 The StarOpenSource Engine Contributors + Licensed under the GNU Affero General Public License v3 + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as + published by the Free Software Foundation, either version 3 of the + License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . + */ + +/** + * Events used for engine-internal communication. + * These events are meant to be listened on by the base engine and it's subsystems. + */ +package de.staropensource.sosengine.base.events.internal; diff --git a/base/src/main/java/de/staropensource/sosengine/base/logging/CrashHandler.java b/base/src/main/java/de/staropensource/sosengine/base/logging/CrashHandler.java index 01b52a34..a2de405c 100644 --- a/base/src/main/java/de/staropensource/sosengine/base/logging/CrashHandler.java +++ b/base/src/main/java/de/staropensource/sosengine/base/logging/CrashHandler.java @@ -21,6 +21,7 @@ package de.staropensource.sosengine.base.logging; import de.staropensource.sosengine.base.EngineConfiguration; import de.staropensource.sosengine.base.classes.Placeholder; +import de.staropensource.sosengine.base.events.EngineCrashEvent; import de.staropensource.sosengine.base.logging.placeholders.crashhandler.*; import de.staropensource.sosengine.base.types.LogIssuer; import de.staropensource.sosengine.base.types.LogLevel; @@ -142,6 +143,9 @@ public final class CrashHandler { // Print log message Logger.getLoggerImplementation().print(LogLevel.CRASH, logIssuer, base); + // Send EngineCrash event + EngineCrashEvent.getInstance().callEvent(); + // Shutdown JVM if (EngineConfiguration.getInstance().isLoggerImmediateShutdown()) Runtime.getRuntime().halt(69); diff --git a/base/src/main/java/de/staropensource/sosengine/base/logging/Logger.java b/base/src/main/java/de/staropensource/sosengine/base/logging/Logger.java index 3134de12..6792edf3 100644 --- a/base/src/main/java/de/staropensource/sosengine/base/logging/Logger.java +++ b/base/src/main/java/de/staropensource/sosengine/base/logging/Logger.java @@ -22,6 +22,7 @@ package de.staropensource.sosengine.base.logging; import de.staropensource.sosengine.base.EngineConfiguration; import de.staropensource.sosengine.base.classes.LoggerImpl; import de.staropensource.sosengine.base.classes.Placeholder; +import de.staropensource.sosengine.base.events.LogEvent; import de.staropensource.sosengine.base.logging.placeholders.logger.*; import de.staropensource.sosengine.base.types.LogIssuer; import de.staropensource.sosengine.base.types.LogLevel; @@ -113,6 +114,9 @@ public final class Logger { // Execute LoggerImpl#postPlaceholder base = loggerImplementation.postPlaceholder(level, logIssuer, base); + // Call event + ((LogEvent) LogEvent.getInstance()).callEventNew(level, logIssuer, message); + // Print log message loggerImplementation.print(level, logIssuer, base); }