Add LogEvent and EngineCrashEvent

This commit is contained in:
JeremyStar™ 2024-06-09 21:10:22 +02:00
parent b4cf8f81b1
commit aa688a0ca4
Signed by: JeremyStarTM
GPG key ID: E366BAEF67E4704D
5 changed files with 128 additions and 0 deletions

View file

@ -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 <https://www.gnu.org/licenses/>.
*/
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();
}
}

View file

@ -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 <https://www.gnu.org/licenses/>.
*/
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) {}
}
}
}

View file

@ -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 <https://www.gnu.org/licenses/>.
*/
/**
* 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;

View file

@ -21,6 +21,7 @@ package de.staropensource.sosengine.base.logging;
import de.staropensource.sosengine.base.EngineConfiguration; import de.staropensource.sosengine.base.EngineConfiguration;
import de.staropensource.sosengine.base.classes.Placeholder; 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.logging.placeholders.crashhandler.*;
import de.staropensource.sosengine.base.types.LogIssuer; import de.staropensource.sosengine.base.types.LogIssuer;
import de.staropensource.sosengine.base.types.LogLevel; import de.staropensource.sosengine.base.types.LogLevel;
@ -142,6 +143,9 @@ public final class CrashHandler {
// Print log message // Print log message
Logger.getLoggerImplementation().print(LogLevel.CRASH, logIssuer, base); Logger.getLoggerImplementation().print(LogLevel.CRASH, logIssuer, base);
// Send EngineCrash event
EngineCrashEvent.getInstance().callEvent();
// Shutdown JVM // Shutdown JVM
if (EngineConfiguration.getInstance().isLoggerImmediateShutdown()) if (EngineConfiguration.getInstance().isLoggerImmediateShutdown())
Runtime.getRuntime().halt(69); Runtime.getRuntime().halt(69);

View file

@ -22,6 +22,7 @@ package de.staropensource.sosengine.base.logging;
import de.staropensource.sosengine.base.EngineConfiguration; import de.staropensource.sosengine.base.EngineConfiguration;
import de.staropensource.sosengine.base.classes.LoggerImpl; import de.staropensource.sosengine.base.classes.LoggerImpl;
import de.staropensource.sosengine.base.classes.Placeholder; 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.logging.placeholders.logger.*;
import de.staropensource.sosengine.base.types.LogIssuer; import de.staropensource.sosengine.base.types.LogIssuer;
import de.staropensource.sosengine.base.types.LogLevel; import de.staropensource.sosengine.base.types.LogLevel;
@ -113,6 +114,9 @@ public final class Logger {
// Execute LoggerImpl#postPlaceholder // Execute LoggerImpl#postPlaceholder
base = loggerImplementation.postPlaceholder(level, logIssuer, base); base = loggerImplementation.postPlaceholder(level, logIssuer, base);
// Call event
((LogEvent) LogEvent.getInstance()).callEventNew(level, logIssuer, message);
// Print log message // Print log message
loggerImplementation.print(level, logIssuer, base); loggerImplementation.print(level, logIssuer, base);
} }