Compare commits

..

No commits in common. "develop" and "v1-alpha7" have entirely different histories.

194 changed files with 8625 additions and 7539 deletions

View file

@ -8,7 +8,7 @@ jobs:
build: build:
runs-on: docker runs-on: docker
container: container:
image: git.staropensource.de/infrastructure/actions-docker:java image: git.staropensource.de/staropensource/actions-docker:java
steps: steps:
- name: Checkout - name: Checkout
uses: https://code.forgejo.org/actions/checkout@v4 uses: https://code.forgejo.org/actions/checkout@v4
@ -58,7 +58,7 @@ jobs:
generate-javadoc: generate-javadoc:
runs-on: docker runs-on: docker
container: container:
image: git.staropensource.de/infrastructure/actions-docker:java image: git.staropensource.de/staropensource/actions-docker:java
steps: steps:
- name: Checkout - name: Checkout
uses: https://code.forgejo.org/actions/checkout@v4 uses: https://code.forgejo.org/actions/checkout@v4
@ -99,7 +99,7 @@ jobs:
test: test:
runs-on: docker runs-on: docker
container: container:
image: git.staropensource.de/infrastructure/actions-docker:java image: git.staropensource.de/staropensource/actions-docker:java
steps: steps:
- name: Checkout - name: Checkout
uses: https://code.forgejo.org/actions/checkout@v4 uses: https://code.forgejo.org/actions/checkout@v4

View file

@ -7,7 +7,7 @@ jobs:
generate-javadoc: generate-javadoc:
runs-on: docker runs-on: docker
container: container:
image: git.staropensource.de/infrastructure/actions-docker:java image: git.staropensource.de/staropensource/actions-docker:java
steps: steps:
- name: Checkout - name: Checkout
uses: https://code.forgejo.org/actions/checkout@v4 uses: https://code.forgejo.org/actions/checkout@v4
@ -34,7 +34,7 @@ jobs:
test: test:
runs-on: docker runs-on: docker
container: container:
image: git.staropensource.de/infrastructure/actions-docker:java image: git.staropensource.de/staropensource/actions-docker:java
steps: steps:
- name: Checkout - name: Checkout
uses: https://code.forgejo.org/actions/checkout@v4 uses: https://code.forgejo.org/actions/checkout@v4

3
.gitignore vendored
View file

@ -42,6 +42,3 @@ bin/
### Mac OS ### ### Mac OS ###
.DS_Store .DS_Store
### Java ###
hs_err_pid*.log

View file

@ -72,8 +72,6 @@ To change them, simply append `-P<property>` or `-P<property>=<value>`, like thi
#### Parallelism #### Parallelism
Use the `jobs` property to control how many jobs will get executed simultaneously. Use the `jobs` property to control how many jobs will get executed simultaneously.
On Linux, specify `-Pjobs=$(nproc)`. Defaults to `8`. On Linux, specify `-Pjobs=$(nproc)`. Defaults to `8`.
#### Rendering
You can use the `renderingPlatform` property to control which rendering platform to initialize for.
#### JVM Home #### JVM Home
You can use the `graalHome` property to specify the `$JAVA_HOME` of your local GraalVM installation. You can use the `graalHome` property to specify the `$JAVA_HOME` of your local GraalVM installation.
Only used in the `nativeImage` task. Useful if you aren't using GraalVM as your primary JDK. Only used in the `nativeImage` task. Useful if you aren't using GraalVM as your primary JDK.

View file

@ -26,6 +26,7 @@ import de.staropensource.engine.base.type.logging.LogLevel;
import org.fusesource.jansi.Ansi; import org.fusesource.jansi.Ansi;
import org.fusesource.jansi.AnsiConsole; import org.fusesource.jansi.AnsiConsole;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/** /**
* Prints colored log output using the Jansi library. * Prints colored log output using the Jansi library.
@ -42,16 +43,28 @@ public class AnsiLoggingAdapter implements LoggingAdapter {
*/ */
public AnsiLoggingAdapter() {} public AnsiLoggingAdapter() {}
/** {@inheritDoc} */
@Override
public @NotNull String prePlaceholder(@NotNull LogLevel level, @NotNull Class<?> issuerClass, @NotNull String issuerOrigin, @Nullable String issuerMetadata, @NotNull String message, @NotNull String format) {
return format; // No modifications necessary
}
/** {@inheritDoc} */
@Override
public @NotNull String postPlaceholder(@NotNull LogLevel level, @NotNull Class<?> issuerClass, @NotNull String issuerOrigin, @Nullable String issuerMetadata, @NotNull String message, @NotNull String format) {
return format; // No modifications necessary
}
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
@SuppressWarnings({ "resource" }) // Using try-with-resources will cause issues here @SuppressWarnings({ "resource" }) // Using try-with-resources will cause issues here
public void print(@NotNull LogLevel level, @NotNull StackTraceElement issuer, @NotNull String message, @NotNull String format) { public void print(@NotNull LogLevel level, @NotNull Class<?> issuerClass, @NotNull String issuerOrigin, @Nullable String issuerMetadata, @NotNull String message, @NotNull String format) {
// Convert to Ansi // Convert to Ansi
Ansi output = new AnsiShortcodeParser(format, true).getAnsi(); Ansi output = new AnsiShortcodeConverter(format, true).getAnsi();
// Print message // Print message
if (level == LogLevel.ERROR || level == LogLevel.CRASH) if (level == LogLevel.ERROR || level == LogLevel.CRASH)
if (EngineConfiguration.getInstance().isLogForceStandardOutput()) if (EngineConfiguration.getInstance().isLoggerForceStandardOutput())
AnsiConsole.out().println(output); AnsiConsole.out().println(output);
else else
AnsiConsole.err().println(output); AnsiConsole.err().println(output);

View file

@ -28,13 +28,12 @@ import java.util.HashSet;
import java.util.Set; import java.util.Set;
/** /**
* Implementation of the {@link ShortcodeParser} class * Converts shortcodes such as {@code <bold>} or {@code <blink>} into a usable {@link Ansi} escape sequence.
* with ANSI support using the Jansi library.
* *
* @see ShortcodeParser * @see ShortcodeParser
* @since v1-alpha8 * @since v1-alpha2
*/ */
public final class AnsiShortcodeParser extends ShortcodeParser { public final class AnsiShortcodeConverter extends ShortcodeParser {
/** /**
* Creates and initializes an instance of this class. * Creates and initializes an instance of this class.
* *
@ -43,7 +42,7 @@ public final class AnsiShortcodeParser extends ShortcodeParser {
* @throws ParserException when parsing failed * @throws ParserException when parsing failed
* @since v1-alpha2 * @since v1-alpha2
*/ */
public AnsiShortcodeParser(@NotNull String string, boolean ignoreInvalidEscapes) throws ParserException { public AnsiShortcodeConverter(@NotNull String string, boolean ignoreInvalidEscapes) throws ParserException {
super(string, ignoreInvalidEscapes); super(string, ignoreInvalidEscapes);
} }

View file

@ -21,7 +21,6 @@ package de.staropensource.engine.ansi;
import de.staropensource.engine.base.annotation.EngineSubsystem; import de.staropensource.engine.base.annotation.EngineSubsystem;
import de.staropensource.engine.base.implementable.SubsystemClass; import de.staropensource.engine.base.implementable.SubsystemClass;
import de.staropensource.engine.base.implementation.logging.PlainLoggingAdapter;
import de.staropensource.engine.base.utility.information.EngineInformation; import de.staropensource.engine.base.utility.information.EngineInformation;
import de.staropensource.engine.base.implementation.versioning.StarOpenSourceVersioningSystem; import de.staropensource.engine.base.implementation.versioning.StarOpenSourceVersioningSystem;
import de.staropensource.engine.base.logging.Logger; import de.staropensource.engine.base.logging.Logger;
@ -60,7 +59,7 @@ public final class AnsiSubsystem extends SubsystemClass {
if (instance == null) if (instance == null)
instance = this; instance = this;
else else
Logger.crash("Only one instance of this class is allowed, use getInstance() instead of creating a new instance"); logger.crash("Only one instance of this class is allowed, use getInstance() instead of creating a new instance");
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
@ -72,8 +71,7 @@ public final class AnsiSubsystem extends SubsystemClass {
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public void initializeSubsystem() { public void initializeSubsystem() {
if (Logger.getLoggingAdapter().getClass().getName().equals(PlainLoggingAdapter.class.getName())) Logger.setLoggingAdapter(new AnsiLoggingAdapter());
Logger.setLoggingAdapter(new AnsiLoggingAdapter());
} }
/** {@inheritDoc} */ /** {@inheritDoc} */

View file

@ -27,17 +27,14 @@ import de.staropensource.engine.base.implementable.ShutdownHandler;
import de.staropensource.engine.base.implementable.SubsystemClass; import de.staropensource.engine.base.implementable.SubsystemClass;
import de.staropensource.engine.base.implementable.helper.EventHelper; import de.staropensource.engine.base.implementable.helper.EventHelper;
import de.staropensource.engine.base.implementation.versioning.StarOpenSourceVersioningSystem; import de.staropensource.engine.base.implementation.versioning.StarOpenSourceVersioningSystem;
import de.staropensource.engine.base.event.InternalEngineShutdownEvent; import de.staropensource.engine.base.internal.event.InternalEngineShutdownEvent;
import de.staropensource.engine.base.internal.type.DependencySubsystemVector; import de.staropensource.engine.base.internal.type.DependencySubsystemVector;
import de.staropensource.engine.base.logging.PrintStreamService;
import de.staropensource.engine.base.logging.*; import de.staropensource.engine.base.logging.*;
import de.staropensource.engine.base.logging.backend.async.LoggingQueue;
import de.staropensource.engine.base.logging.backend.async.LoggingThread;
import de.staropensource.engine.base.type.DependencyVector; import de.staropensource.engine.base.type.DependencyVector;
import de.staropensource.engine.base.type.EngineState; import de.staropensource.engine.base.type.EngineState;
import de.staropensource.engine.base.type.immutable.ImmutableLinkedList;
import de.staropensource.engine.base.utility.DependencyResolver; import de.staropensource.engine.base.utility.DependencyResolver;
import de.staropensource.engine.base.utility.FileAccess; import de.staropensource.engine.base.utility.Miscellaneous;
import de.staropensource.engine.base.utility.misc.Miscellaneous;
import de.staropensource.engine.base.utility.PlaceholderEngine; import de.staropensource.engine.base.utility.PlaceholderEngine;
import de.staropensource.engine.base.utility.information.EngineInformation; import de.staropensource.engine.base.utility.information.EngineInformation;
import de.staropensource.engine.base.utility.information.JvmInformation; import de.staropensource.engine.base.utility.information.JvmInformation;
@ -87,6 +84,14 @@ public final class Engine extends SubsystemClass {
@Getter @Getter
private static final ThreadGroup threadGroup = new ThreadGroup("sos!engine"); private static final ThreadGroup threadGroup = new ThreadGroup("sos!engine");
/**
* Contains the {@link LoggerInstance} for this instance.
*
* @see LoggerInstance
* @since v1-alpha0
*/
private static final LoggerInstance logger = new LoggerInstance.Builder().setClazz(Engine.class).setOrigin("ENGINE").setMetadata(EngineInformation.getVersioningCodename()).build();
/** /**
* Contains the engine state. * Contains the engine state.
* *
@ -98,7 +103,7 @@ public final class Engine extends SubsystemClass {
* @since v1-alpha2 * @since v1-alpha2
*/ */
@Getter @Getter
private @NotNull EngineState state; private @NotNull EngineState state = EngineState.UNKNOWN;
/** /**
* Contains a list of all registered subsystems. * Contains a list of all registered subsystems.
@ -113,7 +118,7 @@ public final class Engine extends SubsystemClass {
* @since v1-alpha1 * @since v1-alpha1
*/ */
@Getter @Getter
private @NotNull List<@NotNull DependencySubsystemVector> subsystems = Collections.emptyList(); private @NotNull ImmutableLinkedList<@NotNull DependencySubsystemVector> subsystems = new ImmutableLinkedList<>();
/** /**
* Contains the engine's shutdown handler. * Contains the engine's shutdown handler.
@ -164,116 +169,57 @@ public final class Engine extends SubsystemClass {
} }
// Print warning about shutdown // Print warning about shutdown
Logger.warn("Trying to shut down engine using shutdown hook.\nThis approach to shutting down the engine and JVM is NOT RECOMMENDED, please use Engine#shutdown() instead."); logger.warn("Trying to shut down engine using shutdown hook.\nThis approach to shutting down the engine and JVM is NOT RECOMMENDED, please use Engine#shutdown() instead.");
// Shutdown // Shutdown
Engine.getInstance().shutdown(); Engine.getInstance().shutdown();
// Print last message // Print last message
Logger.warn("Engine successfully shut down using shutdown hook. PLEASE USE Engine#shutdown() INSTEAD OF System#exit() or Runtime#exit()!"); InitLogger.warn(getClass(), "ENGINE", EngineInformation.getVersioningCodename(), "Engine successfully shut down using shutdown hook. PLEASE USE Engine#shutdown() INSTEAD OF System#exit() or Runtime#exit()!");
}); });
/** /**
* Initializes the StarOpenSource Engine. * Initializes the StarOpenSource Engine.
* *
* @throws RuntimeException for all exceptions thrown by this constructor * @throws IllegalStateException when running in an incompatible environment
* @since v1-alpha8 * @since v1-alpha6
*/ */
private Engine() throws RuntimeException { private Engine() throws IllegalStateException {
try { long initTime = Miscellaneous.measureExecutionTime(() -> {
instance = this; instance = this;
state = EngineState.EARLY_STARTUP; state = EngineState.EARLY_STARTUP;
// For measuring the initialization time
long initTimeEarly = System.currentTimeMillis();
long initTimeLate = initTimeEarly;
// Check for incompatible JVM implementations
checkJvmIncompatibilities();
// Display that the engine is initializing
Logger.verb("Initializing engine");
// Start the logging thread
Logger.diag("Starting logging infrastructure");
LoggingThread.startThread(false);
PrintStreamService.initializeStreams();
// Initialize EngineInternals
Logger.diag("Initializing EngineInternals class");
new EngineInternals();
// Load engine configuration
Logger.diag("Loading engine configuration");
new EngineConfiguration(); new EngineConfiguration();
EngineConfiguration.getInstance().loadConfiguration(); EngineConfiguration.getInstance().loadConfiguration();
// Load engine build information logger.info("Initializing engine");
Logger.diag("Loading engine build information"); initializeClasses(); // Initialize classes
EngineInformation.update(); if (checkEnvironment()) // Check environment
throw new IllegalStateException("Running in an incompatible environment");
ensureEnvironment(); // Prepare the environment and ensure safety
populateCrashContent(); // Populate crash content
cacheEvents(); // Cache event listeners
startThreads(); // Start threads
// Check for reflective classpath scanning compatibility logger.verb("Completing early initialization stage");
checkReflectiveClasspathScanningCompatibility();
// Check for Java version incompatibilities
checkJavaVersion();
// Initialize PlaceholderEngine
Logger.diag("Initializing PlaceholderEngine");
PlaceholderEngine.initialize();
// Initialize static FileAccess instances
Logger.diag("Initializing static FileAccess instances");
FileAccess.initializeInstances();
// Install the safety shutdown hook
Logger.diag("Installing safety shutdown hook");
EngineInternals.getInstance().installSafetyShutdownHook(true);
// Cache events
Logger.diag("Caching event listeners");
cacheEvents();
// Complete early initialization stage
Logger.verb("Completing early initialization stage");
state = EngineState.STARTUP; state = EngineState.STARTUP;
initTimeEarly = System.currentTimeMillis() - initTimeEarly;
// Perform automatic subsystem initialization // Perform automatic subsystem initialization
if (EngineConfiguration.getInstance().isInitialPerformSubsystemInitialization()) { if (EngineConfiguration.getInstance().isInitialPerformSubsystemInitialization()) {
// Collect all subsystems collectSubsystems(); // Collect subsystems
Logger.diag("Collecting subsystems");
collectSubsystems();
// Initialize subsystems // Initialize subsystems
try { try {
initializeSubsystems(); initializeSubsystems();
} catch (Exception exception) { } catch (Exception exception) {
Logger.error("Subsystem dependency resolution failed"); logger.error("Subsystem dependency resolution failed");
} }
} }
});
// Complete late initialization stage logger.verb("Completing late initialization stage");
Logger.verb("Completing late initialization stage"); state = EngineState.RUNNING;
state = EngineState.RUNNING; logger.info("Initialized sos!engine %engine_version% (commit %engine_git_commit_id_long%-%engine_git_branch%, dirty %engine_git_dirty%) in " + initTime + "ms\nThe StarOpenSource Engine is licensed under the GNU AGPL v3. Copyright (c) 2024 The StarOpenSource Engine Authors.");
initTimeLate = System.currentTimeMillis() - initTimeLate;
// Print welcome message
Logger.info(
"""
Welcome to the StarOpenSource Engine "%engine_version_codename%" %engine_version%!
Running commit %engine_git_commit_id_long% (dirty %engine_git_dirty%).
Initialization took %init_time_total%ms (early %init_time_early%ms, late %init_time_late%ms).
Copyright (c) 2024 The StarOpenSource Engine Authors
Licensed under the GNU Affero General Public License v3"""
.replace("%init_time_total%", String.valueOf(initTimeEarly + initTimeLate))
.replace("%init_time_early%", String.valueOf(initTimeEarly))
.replace("%init_time_late%", String.valueOf(initTimeLate))
);
} catch (Exception exception) {
throw new RuntimeException(exception);
}
} }
/** /**
@ -284,72 +230,147 @@ public final class Engine extends SubsystemClass {
* @throws RuntimeException on engine initialization failure * @throws RuntimeException on engine initialization failure
* @since v1-alpha6 * @since v1-alpha6
*/ */
public static void initialize() throws RuntimeException { public static void initialize() throws IllegalStateException, RuntimeException {
try { try {
if (instance == null) if (instance == null)
new Engine(); new Engine();
} catch (RuntimeException exception) { } catch (IllegalStateException exception) {
Logger.error("Engine initialization failed\n" + Miscellaneous.stacktraceAsStringRecursive(exception.getCause(), true, true)); throw exception;
Logger.flush(); } catch (Exception exception) {
logger.error("Engine initialization failed");
throw new RuntimeException("Engine initialization failed", exception.getCause()); logger.error(Miscellaneous.getStackTraceHeader(exception));
for (String line : Miscellaneous.getStackTraceAsString(exception, true).split("\n"))
logger.error(line);
throw new RuntimeException("Engine initialization failed", exception);
} }
} }
/** /**
* Checks if the running JVM implementation is not supported by the engine. * Initializes all classes.
* *
* @since v1-alpha8 * @since v1-alpha0
*/ */
private void checkJvmIncompatibilities() { private void initializeClasses() {
if (System.getProperties().getProperty("sosengine.base.allowUnsupportedJVMInitialization", "false").equals("true")) { logger.verb("Initializing engine classes");
Logger.warn("Skipping JVM implementation incompatibilities check"); new EngineInternals();
return; PlaceholderEngine.initialize();
}
// Substrate VM (GraalVM Community) EngineInformation.update();
PrintStreamService.initializeStreams();
}
/**
* Checks if the environment is compatible with the engine build.
*
* @since v1-alpha4
*/
private boolean checkEnvironment() {
logger.diag("Checking environment");
// Warn about potential Java incompatibilities
if (JvmInformation.getJavaVersion() > EngineInformation.getJavaSource())
logger.warn("The StarOpenSource Engine is running on an untested Java version.\nThings may not work as expected or features which can improve performance, stability, compatibility or ease of use may be missing.\nIf you encounter issues, try running a JVM implementing Java " + EngineInformation.getJavaSource());
// Shutdown if running in an unsupported JVM
if (JvmInformation.getImplementationName().equals("Substrate VM") && JvmInformation.getImplementationVendor().equals("GraalVM Community")) { if (JvmInformation.getImplementationName().equals("Substrate VM") && JvmInformation.getImplementationVendor().equals("GraalVM Community")) {
Logger.error("##############################################################################################"); logger.error("##############################################################################################");
Logger.error("## Running in Substrate VM, which is the name of the JVM used by GraalVM native-image. ##"); logger.error("## Running in Substrate VM, which is the name of the JVM used by GraalVM native-image. ##");
Logger.error("## The StarOpenSource Engine does not support native-image as using reflection in a certain ##"); logger.error("## The StarOpenSource Engine does not support native-image as using reflection in a certain ##");
Logger.error("## way seems to cause the Substrate JVM to crash. Workarounds have failed. ##"); logger.error("## way seems to cause the Substrate JVM to crash. Workarounds have failed. ##");
Logger.error("## This has already been noted in issue #3, which you can view here: ##"); logger.error("## This has already been noted in issue #3, which you can view here: ##");
Logger.error("## https://git.staropensource.de/StarOpenSource/Engine/issues/3 ##"); logger.error("## https://git.staropensource.de/StarOpenSource/Engine/issues/3 ##");
Logger.error("## ##"); logger.error("## ##");
Logger.error("## While this is sad, we unfortunately can't do anything against it unless we introduce ##"); logger.error("## While this is sad, we unfortunately can't do anything against it unless we introduce ##");
Logger.error("## annoying and stupid changes into the engine, which we don't want to do. ##"); logger.error("## annoying and stupid changes into the engine, which we don't want to do. ##");
Logger.error("## ##"); logger.error("## ##");
Logger.error("## We're truly sorry for this inconvenience. The sos!engine will now terminate. ##"); logger.error("## We're truly sorry for this inconvenience. The sos!engine will now terminate. ##");
Logger.error("##############################################################################################"); logger.error("##############################################################################################");
Runtime.getRuntime().exit(255); Runtime.getRuntime().exit(255);
} }
}
/**
* Checks if reflective classpath scanning is supported.
*
* @since v1-alpha8
*/
private void checkReflectiveClasspathScanningCompatibility() {
// Check if reflective classpath scanning is supported // Check if reflective classpath scanning is supported
if (System.getProperties().getProperty("sosengine.base.considerEnvironmentUnfriendlyToClasspathScanning", "false").equals("true")) { if (checkClasspathScanningSupport()) {
Logger.warn("Running in an classpath scanning-unfriendly environment, disabling classpath scanning support."); logger.warn("Running in an classpath scanning-unfriendly environment, disabling.");
Logger.warn("If shit doesn't work and is expected to be discovered by annotations, you'll need to"); logger.warn("If shit doesn't work and is expected to be discovered by annotations, you'll need to");
Logger.warn("either register it first or have to update some engine configuration setting."); logger.warn("either register it first or have to place classes in some package.");
Logger.warn("Please consult sos!engine's documentation for more information about this issue."); logger.warn("Please consult sos!engine's documentation for more information about this issue.");
EngineInternals.getInstance().overrideReflectiveClasspathScanning(false); EngineInternals.getInstance().overrideReflectiveClasspathScanning(false);
} }
return false;
} }
/** /**
* Checks and warns if the Java version of the * Returns whether scanning the classpath is supported.
* running JVM is higher than the engine supports.
* *
* @since v1-alpha8 * @return test results
* @since v1-alpha5
*/ */
private void checkJavaVersion() { private boolean checkClasspathScanningSupport() {
if (JvmInformation.getJavaVersion() > EngineInformation.getJavaSource()) // This may be expanded in the future
Logger.warn("The StarOpenSource Engine is running on an untested Java version.\nThings may not work as expected or features which can improve performance, stability, compatibility or ease of use may be missing.\nIf you encounter issues, try running a JVM implementing Java " + EngineInformation.getJavaSource()); return EngineConfiguration.getInstance().isInitialForceDisableClasspathScanning();
}
/**
* Ensures the execution safety of the environment.
*
* @since v1-alpha4
*/
private void ensureEnvironment() {
EngineInternals.getInstance().installSafetyShutdownHook(true);
}
/**
* This method populates {@link CrashHandler#crashContent} with content.
*
* @see CrashHandler#getCrashContent()
* @since v1-alpha0
*/
@SuppressWarnings({ "ExtractMethodRecommender" })
private void populateCrashContent() {
logger.diag("Populating crash content");
// Issuer
Map<@NotNull String, @NotNull String> crashContentIssuer = new LinkedHashMap<>();
crashContentIssuer.put("Code part", "%issuer_origin%");
crashContentIssuer.put("Classpath", "%issuer_path%");
crashContentIssuer.put("Additional information", "%issuer_metadata%");
crashContentIssuer.put("Message", "%crash_message%");
// Engine -> Dependencies
LinkedList<@NotNull String> crashContentEngineDependencies = new LinkedList<>();
crashContentEngineDependencies.add("Subsystem 'base': Reflections: %engine_dependency_reflections%");
crashContentEngineDependencies.add("Subsystem 'ansi': Jansi: %engine_dependency_jansi%");
crashContentEngineDependencies.add("Subsystem 'slf4j-compat': SLF4J: %engine_dependency_slf4j%");
crashContentEngineDependencies.add("Subsystems 'glfw', 'opengl' & 'vulkan': LWJGL: %engine_dependency_lwjgl%");
// Engine -> *
Map<@NotNull String, @NotNull Object> crashContentEngine = new LinkedHashMap<>();
crashContentEngine.put("Version", "%engine_version%");
crashContentEngine.put("Dependencies", crashContentEngineDependencies);
// JVM -> Implementation
Map<@NotNull String, @NotNull String> crashContentJvmImplementation = new LinkedHashMap<>();
crashContentJvmImplementation.put("Name", "%jvm_implementation_name%");
crashContentJvmImplementation.put("Version", "%jvm_implementation_version%");
crashContentJvmImplementation.put("Vendor", "%jvm_implementation_vendor%");
// JVM -> *
Map<@NotNull String, @NotNull Object> crashContentJvm = new LinkedHashMap<>();
crashContentJvm.put("Java Version", "%jvm_java%");
crashContentJvm.put("Implementation", crashContentJvmImplementation);
crashContentJvm.put("Arguments", JvmInformation.getArguments());
// Operating system
Map<@NotNull String, @NotNull String> crashContentOS = new LinkedHashMap<>();
crashContentOS.put("Time", "%time_hour%:%time_minute%:%time_second% (%time_zone%, UNIX Epoch: %time_epoch%)");
crashContentOS.put("Date", "%date_day%.%date_month%.%date_year%");
// Add to crash handler
CrashHandler.getCrashContent().put("Issuer", crashContentIssuer);
CrashHandler.getCrashContent().put("Engine", crashContentEngine);
CrashHandler.getCrashContent().put("Java Virtual Machine", crashContentJvm);
CrashHandler.getCrashContent().put("Operating system", crashContentOS);
CrashHandler.getCrashContent().put("Stacktrace", "\n%stacktrace%");
CrashHandler.getCrashContent().put("Stacktrace for all threads", "\n%stacktrace_all%");
} }
/** /**
@ -358,11 +379,28 @@ public final class Engine extends SubsystemClass {
* @since v1-alpha0 * @since v1-alpha0
*/ */
private void cacheEvents() { private void cacheEvents() {
logger.diag("Caching events");
// Internal events
EventHelper.cacheEvent(InternalEngineShutdownEvent.class);
// General events
EventHelper.cacheEvent(EngineCrashEvent.class); EventHelper.cacheEvent(EngineCrashEvent.class);
EventHelper.cacheEvent(EngineShutdownEvent.class); EventHelper.cacheEvent(EngineShutdownEvent.class);
EventHelper.cacheEvent(EngineSoftCrashEvent.class); EventHelper.cacheEvent(EngineSoftCrashEvent.class);
EventHelper.cacheEvent(InternalEngineShutdownEvent.class);
EventHelper.cacheEvent(LogEvent.class); EventHelper.cacheEvent(LogEvent.class);
EventHelper.cacheEvent(ThrowableCatchEvent.class);
}
/**
* Starts engine threads.
*
* @since v1-alpha1
*/
public void startThreads() {
logger.diag("Starting threads");
LoggingThread.startThread();
} }
/** /**
@ -386,18 +424,18 @@ public final class Engine extends SubsystemClass {
if (initializedClassRaw instanceof SubsystemClass) if (initializedClassRaw instanceof SubsystemClass)
initializedClass = (SubsystemClass) initializedClassRaw; initializedClass = (SubsystemClass) initializedClassRaw;
else else
Logger.crash("Failed to initialize subsystem " + clazz.getName() + ": Does not implement " + SubsystemClass.class.getName()); logger.crash("Failed to initialize subsystem " + clazz.getName() + ": Does not implement " + SubsystemClass.class.getName());
//noinspection DataFlowIssue // the crash call will prevent a NullPointerException //noinspection DataFlowIssue // the crash call will prevent a NullPointerException
subsystemsMutable.add(new DependencySubsystemVector(initializedClass.getDependencyVector(), initializedClass)); subsystemsMutable.add(new DependencySubsystemVector(initializedClass.getDependencyVector(), initializedClass));
} catch (Exception exception) { } catch (Exception exception) {
if (exception.getClass() == IllegalStateException.class && exception.getMessage().startsWith("The version string is invalid: ")) if (exception.getClass() == IllegalStateException.class && exception.getMessage().startsWith("The version string is invalid: "))
Logger.crash("Failed to initialize subsystem " + clazz.getName() + ": Invalid version string: " + exception.getMessage().replace("The version string is invalid: ", "")); logger.crash("Failed to initialize subsystem " + clazz.getName() + ": Invalid version string: " + exception.getMessage().replace("The version string is invalid: ", ""));
Logger.crash("Failed to initialize subsystem " + clazz.getName() + ": Method invocation error", exception); logger.crash("Failed to initialize subsystem " + clazz.getName() + ": Method invocation error", exception);
} }
// Update 'subsystems' // Update 'subsystems'
subsystems = Collections.unmodifiableList(subsystemsMutable); subsystems = new ImmutableLinkedList<>(subsystemsMutable);
} }
/** /**
@ -423,10 +461,10 @@ public final class Engine extends SubsystemClass {
} else } else
for (String path : EngineConfiguration.getInstance().getInitialIncludeSubsystemClasses()) for (String path : EngineConfiguration.getInstance().getInitialIncludeSubsystemClasses())
try { try {
Logger.diag("Resolving class " + path); logger.diag("Resolving class " + path);
classes.add(Class.forName(path)); classes.add(Class.forName(path));
} catch (ClassNotFoundException exception) { } catch (ClassNotFoundException exception) {
Logger.error("Failed loading subsystem class " + path + ": Class not found"); logger.error("Failed loading subsystem class " + path + ": Class not found");
} }
return classes; return classes;
@ -447,7 +485,7 @@ public final class Engine extends SubsystemClass {
resolver.addVectors(subsystems); resolver.addVectors(subsystems);
// Resolve dependencies and get order // Resolve dependencies and get order
Logger.diag("Resolving subsystem dependencies"); logger.verb("Resolving subsystem dependencies");
try { try {
for (DependencyVector vector : resolver.resolve().getOrder()) // smol workaround for (DependencyVector vector : resolver.resolve().getOrder()) // smol workaround
order.add((DependencySubsystemVector) vector); order.add((DependencySubsystemVector) vector);
@ -462,29 +500,29 @@ public final class Engine extends SubsystemClass {
.append("- ") .append("- ")
.append(error); .append(error);
Logger.crash("Found unresolved dependencies:" + list, throwable); logger.crash("Found unresolved dependencies:" + list, throwable);
return; return;
} }
Logger.crash("An error occurred trying to resolve subsystem dependencies: " + throwable.getClass().getName() + (throwable.getMessage() == null ? "" : ": " + throwable.getMessage())); logger.crash("An error occurred trying to resolve subsystem dependencies: " + throwable.getClass().getName() + (throwable.getMessage() == null ? "" : ": " + throwable.getMessage()));
throw throwable; throw throwable;
} }
// Initialize subsystems // Initialize subsystems
Logger.diag("Initializing engine subsystems"); logger.verb("Initializing engine subsystems");
long initTime; long initTime;
for (DependencySubsystemVector vector : subsystems) { for (DependencySubsystemVector vector : subsystems) {
Logger.diag("Initializing subsystem '" + vector.getSubsystemClass().getName() + "' (" + vector.getSubsystemClass().getClass().getName() + ")"); logger.diag("Initializing subsystem " + vector.getSubsystemClass().getName() + " (" + vector.getSubsystemClass().getClass().getName() + ")");
try { try {
initTime = Miscellaneous.measureExecutionTime(() -> vector.getSubsystemClass().initializeSubsystem()); initTime = Miscellaneous.measureExecutionTime(() -> vector.getSubsystemClass().initializeSubsystem());
} catch (Throwable throwable) { } catch (Throwable throwable) {
Logger.crash("An error occurred trying to initialize subsystem " + vector.getSubsystemClass().getName() + " (" + vector.getSubsystemClass().getClass().getName() + "): " + throwable.getClass().getName() + (throwable.getMessage() == null ? "" : ": " + throwable.getMessage())); logger.crash("An error occurred trying to initialize subsystem " + vector.getSubsystemClass().getName() + " (" + vector.getSubsystemClass().getClass().getName() + "): " + throwable.getClass().getName() + (throwable.getMessage() == null ? "" : ": " + throwable.getMessage()));
throw throwable; throw throwable;
} }
Logger.diag("Initialized subsystem '" + vector.getSubsystemClass().getName() + "' (" + vector.getSubsystemClass().getClass().getName() + ") in " + initTime + "ms"); logger.diag("Initialized subsystem " + vector.getSubsystemClass().getName() + " (" + vector.getSubsystemClass().getClass().getName() + ") in " + initTime + "ms");
} }
// Update 'subsystems' // Update 'subsystems'
subsystems = Collections.unmodifiableList(order); subsystems = new ImmutableLinkedList<>(order);
} }
/** /**
@ -497,7 +535,7 @@ public final class Engine extends SubsystemClass {
if (state == EngineState.UNKNOWN || state == EngineState.SHUTDOWN) if (state == EngineState.UNKNOWN || state == EngineState.SHUTDOWN)
return; return;
Logger.info("Shutting engine down"); logger.info("Shutting engine down");
if (state != EngineState.CRASHED) if (state != EngineState.CRASHED)
state = EngineState.SHUTDOWN; state = EngineState.SHUTDOWN;
@ -508,7 +546,7 @@ public final class Engine extends SubsystemClass {
EngineConfiguration.getInstance().loadConfiguration(properties); EngineConfiguration.getInstance().loadConfiguration(properties);
// Flush log messages // Flush log messages
LoggingQueue.flush(); Logger.flushLogMessages();
// Disable safety shutdown hook // Disable safety shutdown hook
try { try {
@ -516,17 +554,14 @@ public final class Engine extends SubsystemClass {
} catch (Exception ignored) {} } catch (Exception ignored) {}
// Send events // Send events
Logger.verb("Notifying classes about shutdown"); logger.verb("Notifying classes about shutdown");
new EngineShutdownEvent().callEvent(); new EngineShutdownEvent().callEvent();
Logger.verb("Notifying subsystems about shutdown"); logger.verb("Notifying subsystems about shutdown");
new InternalEngineShutdownEvent().callEvent(); new InternalEngineShutdownEvent().callEvent();
// Delete scheduled files
FileAccess.deleteScheduled();
// Invoke shutdown handler // Invoke shutdown handler
Logger.verb("Invoking shutdown handler (code " + exitCode + ")"); logger.verb("Invoking shutdown handler (code " + exitCode + ")");
shutdownHandler.shutdown((short) exitCode); shutdownHandler.shutdown((short) exitCode);
} }
@ -546,11 +581,7 @@ public final class Engine extends SubsystemClass {
return "base"; return "base";
} }
/** /** {@inheritDoc} */
* This method does nothing.
*
* @since v1-alpha1
*/
@Override @Override
public void initializeSubsystem() {} public void initializeSubsystem() {}
@ -603,12 +634,12 @@ public final class Engine extends SubsystemClass {
Runtime.getRuntime().addShutdownHook(thread); Runtime.getRuntime().addShutdownHook(thread);
Runtime.getRuntime().removeShutdownHook(thread); Runtime.getRuntime().removeShutdownHook(thread);
} catch (IllegalStateException exception) { } catch (IllegalStateException exception) {
Logger.warn("Terminating JVM: Already shutting down, skipping"); logger.warn("Terminating JVM: Already shutting down, skipping");
return; return;
} }
Logger.warn("Terminating JVM"); logger.warn("Terminating JVM");
Runtime.getRuntime().exit(exitCode); System.exit(exitCode);
} }
} }
} }

View file

@ -19,13 +19,11 @@
package de.staropensource.engine.base; package de.staropensource.engine.base;
import de.staropensource.engine.base.annotation.EngineSubsystem;
import de.staropensource.engine.base.event.LogEvent;
import de.staropensource.engine.base.implementable.Configuration; import de.staropensource.engine.base.implementable.Configuration;
import de.staropensource.engine.base.implementable.ShortcodeParser; import de.staropensource.engine.base.implementable.ShortcodeParser;
import de.staropensource.engine.base.implementable.SubsystemClass; import de.staropensource.engine.base.logging.CrashHandler;
import de.staropensource.engine.base.logging.Logger; import de.staropensource.engine.base.logging.Logger;
import de.staropensource.engine.base.logging.backend.async.LoggingThread; import de.staropensource.engine.base.logging.LoggingThread;
import de.staropensource.engine.base.type.EngineState; import de.staropensource.engine.base.type.EngineState;
import de.staropensource.engine.base.type.logging.LogLevel; import de.staropensource.engine.base.type.logging.LogLevel;
import de.staropensource.engine.base.type.vector.Vec2f; import de.staropensource.engine.base.type.vector.Vec2f;
@ -35,7 +33,10 @@ import lombok.Getter;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.*; import java.util.Arrays;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;
/** /**
* Provides the base engine configuration. * Provides the base engine configuration.
@ -69,11 +70,11 @@ public final class EngineConfiguration extends Configuration {
private static EngineConfiguration instance; private static EngineConfiguration instance;
/** /**
* Contains the configuration prefix. * Contains prefix properties must begin with.
* *
* @since v1-alpha0 * @since v1-alpha0
* -- GETTER -- * -- GETTER --
* Returns the configuration prefix. * Returns prefix properties must begin with.
* *
* @return property group * @return property group
* @since v1-alpha0 * @since v1-alpha0
@ -82,331 +83,261 @@ public final class EngineConfiguration extends Configuration {
/** /**
* Contains if debugging options should be allowed. * If enabled, allows for unintentional behaviour
* All debugging options will be forcefully set to * and excess logging. Unless you want to debug or work
* {@code false} if this option is set to {@code false}. * on a sensitive part of the engine, don't enable this!
* *
* @since v1-alpha0 * @since v1-alpha0
* -- GETTER -- * -- GETTER --
* Returns if debugging options should be allowed. * Gets the value for {@link #debug}.
* All debugging options will be forcefully set to
* {@code false} if this option is set to {@code false}.
* *
* @return debugging enabled? * @return variable value
* @see #debug
* @since v1-alpha0 * @since v1-alpha0
*/ */
private boolean debug; private boolean debug;
/** /**
* Contains whether or not to log * If enabled, all called events will be logged.
* events being emitted.
* <p>
* This will cause all events to
* be logged, with the exception
* of the {@link LogEvent}.
* *
* @since v1-alpha0 * @since v1-alpha0
* -- GETTER -- * -- GETTER --
* Returns whether or not to log * Gets the value for {@link #debugEvents}.
* events being emitted.
* <p>
* This will cause all events to
* be logged, with the exception
* of the {@link LogEvent}.
* *
* @return detailed event logging enabled? * @return variable value
* @see #debugEvents
* @since v1-alpha0 * @since v1-alpha0
*/ */
private boolean debugEvents; private boolean debugEvents;
/** /**
* Contains whether or not to automatically discover * If enabled, will try to automatically initialize every
* and initialize any class extending {@link SubsystemClass} * subsystem found though reflection.
* whilst being annotated with {@link EngineSubsystem}.
* <p> * <p>
* This mechanism may fail in certain situations, where * This however may fail in certain situation, where manual
* manual subsystem initialization may be desired. Make * subsystem initialization may be required. For this reason,
* sure to disable this setting before engine startup * this can be turned off before the engine initializes. Please
* and then initialize all subsystems manually. * note though that dependency resolution between subsystems
* won't be performed, be careful when initializing subsystems manually.
* *
* @see Engine * @see Engine
* @since v1-alpha5 * @since v1-alpha2
* -- GETTER -- * -- GETTER --
* Returns whether or not to automatically discover * Gets the value for {@link #initialPerformSubsystemInitialization}.
* and initialize any class extending {@link SubsystemClass}
* whilst being annotated with {@link EngineSubsystem}.
* <p>
* This mechanism may fail in certain situations, where
* manual subsystem initialization may be desired. Make
* sure to disable this setting before engine startup
* and then initialize all subsystems manually.
* *
* @return automatically discover and initialize subsystems? * @return variable value
* @see #initialPerformSubsystemInitialization
* @since v1-alpha5 * @since v1-alpha5
*/ */
private boolean initialPerformSubsystemInitialization; private boolean initialPerformSubsystemInitialization;
/** /**
* Contains a set of class names to try to load * If enabled, will force-disable reflective classpath scanning.
* and initialize as subsystems. Will only take effect *
* if {@link #initialPerformSubsystemInitialization} is * @see EngineInternals#getReflectiveClasspathScanning()
* turned off. * @see EngineInternals#overrideReflectiveClasspathScanning(boolean)
* @since v1-alpha5
* -- GETTER --
* Gets the value for {@link #initialForceDisableClasspathScanning}.
*
* @return variable value
* @see #initialForceDisableClasspathScanning
* @since v1-alpha5
*/
private boolean initialForceDisableClasspathScanning;
/**
* Will try to load the specified classes as subsystems,
* if reflective classpath scanning is disabled.
* *
* @since v1-alpha5 * @since v1-alpha5
* -- GETTER -- * -- GETTER --
* Returns a set of class names to try to load * Gets the value for {@link #initialIncludeSubsystemClasses}.
* and initialize as subsystems. Will only take effect
* if {@link #getInitialIncludeSubsystemClasses()} is
* turned off.
* *
* @return set of class names to try and initialize as subsystems * @return variable value
* @see #initialIncludeSubsystemClasses
* @since v1-alpha5 * @since v1-alpha5
*/ */
private Set<@NotNull String> initialIncludeSubsystemClasses; private Set<@NotNull String> initialIncludeSubsystemClasses;
/** /**
* Contains whether or not to complain about invalid * If enabled, will cause {@link ShortcodeParser} to print
* shortcodes. * invalid shortcodes as {@link LogLevel#SILENT_WARNING}s.
* <p>
* Requires the active log level to be set at least
* to {@link LogLevel#SILENT_WARNING} to have effect.
* *
* @see #logLevel * @see ShortcodeParser
* @see #loggerLevel
* @since v1-alpha0 * @since v1-alpha0
* -- GETTER -- * -- GETTER --
* Returns whether or not to complain about invalid * Gets the value for {@link #errorShortcodeParser}.
* shortcodes.
* <p>
* Requires the active log level to be set at least
* to {@link LogLevel#SILENT_WARNING} to have effect.
* *
* @return complain about invalid shortcodes? * @return variable value
* @see #getLogLevel() * @see #errorShortcodeParser
* @since v1-alpha0 * @since v1-alpha0
*/ */
private boolean errorShortcodeParser; private boolean errorShortcodeParser;
/** /**
* Contains if to log asynchronously. * If enabled, will makes the {@link Logger} work asynchronous,
* <p> * in a separate platform thread. Don't disable unless you want
* If enabled, will cause a logging thread * your application to run <b>extremely</b> slowly.
* to spawn. All log messages will be queued
* and printed after a set delay
* ({@link #logPollingSpeed}).
* Highly recommended to keep enabled, or
* the performance of your application will
* very likely suffer.
* *
* @see #logPollingSpeed * @see #loggerPollingSpeed
* @see Thread
* @since v1-alpha0 * @since v1-alpha0
* -- GETTER -- * -- GETTER --
* Returns if to log asynchronously. * Gets the value for {@link #optimizeLogging}.
* <p>
* If enabled, will cause a logging thread
* to spawn. All log messages will be queued
* and printed after a set delay
* ({@link #getLogPollingSpeed()}).
* Highly recommended to keep enabled, or
* the performance of your application will
* very likely suffer.
* *
* @return log asynchronously? * @return variable value
* @see #getLogPollingSpeed() * @see #optimizeLogging
* @since v1-alpha0 * @since v1-alpha0
*/ */
private boolean optimizeLogging; private boolean optimizeLogging;
/** /**
* Contains whether or not to emit events * If enabled, will make all events asynchronous,
* asynchronously. * in separate virtual threads. Don't disable unless you
* <p> * want your application to run slower.
* This will cause a
* <a href="https://openjdk.org/jeps/444">VirtualThread</a>
* to spawn every time an event is emitted.
* *
* @see VirtualThread
* @since v1-alpha0 * @since v1-alpha0
* -- GETTER -- * -- GETTER --
* Contains whether or not to emit events * Gets the value for {@link #optimizeEvents}.
* asynchronously.
* <p>
* This will cause a
* <a href="https://openjdk.org/jeps/444">VirtualThread</a>
* to spawn every time an event is emitted.
* *
* @return emit events asynchronously? * @return variable value
* @see #optimizeEvents
* @since v1-alpha0 * @since v1-alpha0
*/ */
private boolean optimizeEvents; private boolean optimizeEvents;
/** /**
* Contains the minimum allowed log level. * Contains which logger levels are allowed
* <p> * by setting the minimum logger level.
* The priority list is as follows (from high to low priority):
* <ul>
* <li>{@link LogLevel#CRASH}</li>
* <li>{@link LogLevel#ERROR}</li>
* <li>{@link LogLevel#WARNING}</li>
* <li>{@link LogLevel#INFORMATIONAL}</li>
* <li>{@link LogLevel#SILENT_WARNING}</li>
* <li>{@link LogLevel#VERBOSE}</li>
* <li>{@link LogLevel#DIAGNOSTIC}</li>
* </ul>
* *
* @see Logger
* @since v1-alpha0 * @since v1-alpha0
* -- GETTER -- * -- GETTER --
* Returns the minimum allowed log level. * Gets the value for {@link #loggerLevel}.
* <p>
* The priority list is as follows (from high to low priority):
* <ul>
* <li>{@link LogLevel#CRASH}</li>
* <li>{@link LogLevel#ERROR}</li>
* <li>{@link LogLevel#WARNING}</li>
* <li>{@link LogLevel#INFORMATIONAL}</li>
* <li>{@link LogLevel#SILENT_WARNING}</li>
* <li>{@link LogLevel#VERBOSE}</li>
* <li>{@link LogLevel#DIAGNOSTIC}</li>
* </ul>
* *
* @return minimum allowed log level * @return variable value
* @see #loggerLevel
* @since v1-alpha0 * @since v1-alpha0
*/ */
private LogLevel logLevel; private LogLevel loggerLevel;
/** /**
* Contains a comma-separated list of optional * Contains the logging template used for creating log messages.
* features to add to the final log output.
* <p>
* Available features (in order of appearance):
* <ul>
* <li><code>formatting</code></li>
* <li><code>runtime</code></li>
* <li><code>date</code></li>
* <li><code>time</code></li>
* <li><code>shortIssuerClass</code></li>
* <li><code>moduleName</code></li>
* <li><code>moduleVersion</code> (requires <code>moduleName</code>)</li>
* <li><code>methodName</code></li>
* <li><code>lineNumber</code></li>
* </ul>
* *
* @since v1-alpha8 * @see Logger
* @since v1-alpha0
* -- GETTER -- * -- GETTER --
* Returns a comma-separated list of optional * Gets the value for {@link #loggerTemplate}
* features to add to the final log output.
* <p>
* Available features (in order of appearance):
* <ul>
* <li><code>formatting</code></li>
* <li><code>runtime</code></li>
* <li><code>date</code></li>
* <li><code>time</code></li>
* <li><code>shortIssuerClass</code></li>
* <li><code>moduleName</code></li>
* <li><code>moduleVersion</code> (requires <code>moduleName</code>)</li>
* <li><code>methodName</code></li>
* <li><code>lineNumber</code></li>
* <li><code>level</code></li>
* <li><code>origin</code></li>
* </ul>
* *
* @return optional features to enable * @return variable value
* @since v1-alpha8 * @see #loggerTemplate
* @since v1-alpha0
*/ */
private Set<@NotNull String> logFeatures; private String loggerTemplate;
/** /**
* Contains how fast the logging thread will * Contains how fast the logging thread will
* poll for queued messages in milliseconds. * poll for queued messages. This also causes
* This also causes messages to be buffered. * messages to be buffered.
* <p> * <p>
* Only applies if {@code optimizeLogging} is turned on. * Only applies if {@code optimizeLogging} is turned on.
* Values below {@code 1} will poll for queued messages * Values below {@code 1} will poll for queued
* as fast as it can. This however has pretty much no * messages as fast as it can. This however has pretty much
* benefit. Leave it at {@code 5}, it works quite well. * no benefit. Leave it at {@code 5}, it works quite well.
* *
* @see #optimizeLogging * @see #optimizeLogging
* @since v1-alpha4 * @since v1-alpha4
* -- GETTER -- * -- GETTER --
* Contains how fast the logging thread will * Gets the value for {@link #loggerPollingSpeed}.
* poll for queued messages, in milliseconds.
* This also causes messages to be buffered.
* <p>
* Only applies if {@code optimizeLogging} is turned on.
* Values below {@code 1} will poll for queued messages
* as fast as it can. This however has pretty much no
* benefit. Leave it at {@code 5}, it works quite well.
* *
* @return logging thread polling speed in milliseconds * @return variable value
* @see #isOptimizeLogging() * @see #loggerPollingSpeed
* @since v1-alpha4 * @since v1-alpha4
*/ */
private int logPollingSpeed; private int loggerPollingSpeed;
/** /**
* Contains whether or not to forcefully write * If enabled, will force the {@link Logger} and {@link CrashHandler} to use
* to the standard output instead of the * <a href="https://www.man7.org/linux/man-pages/man3/stderr.3.html">the standard output</a>
* standard error stream. * instead of <a href="https://www.man7.org/linux/man-pages/man3/stderr.3.html">the standard error</a>
* <p> * for logging {@code ERROR} and {@code CRASH}.
* This only applies to the {@link LogLevel#ERROR} and
* {@link LogLevel#CRASH} log levels, as these use
* the standard error stream by default.
* *
* @see <a href="https://man7.org/linux/man-pages/man3/stderr.3.html">man page about standard streams</a>
* @since v1-alpha0 * @since v1-alpha0
* -- GETTER -- * -- GETTER --
* Contains whether or not to forcefully write * Gets the value for {@link #loggerForceStandardOutput}.
* to the standard output instead of the
* standard error stream.
* <p>
* This only applies to the {@link LogLevel#ERROR} and
* {@link LogLevel#CRASH} log levels, as these use
* the standard error stream by default.
* *
* @return force use stdout? * @return variable value
* @see <a href="https://man7.org/linux/man-pages/man3/stderr.3.html">man page about standard streams</a> * @see #loggerForceStandardOutput
* @since v1-alpha0 * @since v1-alpha0
*/ */
private boolean logForceStandardOutput; private boolean loggerForceStandardOutput;
/**
* If enabled, will enable support for printing
* log messages on multiple lines. By enabling this
* configuration setting, logger throughput will be
* decreased slightly when encountering a log message
* with newlines found in it. This performance hit is
* negligible though and should not affect application
* performance, especially with logger multi-threading
* turned on (see {@link #optimizeLogging}).
*
* @since v1-alpha4
* -- GETTER --
* Gets the value for {@link #loggerEnableNewlineSupport}.
*
* @return variable value
* @see #loggerEnableNewlineSupport
* @since v1-alpha4
*/
private boolean loggerEnableNewlineSupport;
/**
* If enabled, the JVM will be shutdown immediately
* after printing a fatal crash report. This will
* prevent shutdown hooks from executing.
* Note: This will also prevent Jansi and potentially other libraries
* from removing temporary native libraries at shutdown.
*
* @see CrashHandler
* @since v1-alpha0
* -- GETTER --
* Gets the value for {@link #loggerImmediateShutdown}.
*
* @return variable value
* @see #loggerImmediateShutdown
* @since v1-alpha0
*/
private boolean loggerImmediateShutdown;
/** /**
* Contains if to truncate the full path * Will truncate the path of types when using
* of a class when invoking using their * their {@code toString} method.
* {@link #toString()} method.
* <p> * <p>
* Here's an example: Lets say that you have a * Here's an example: Lets say that you
* {@link Vec2f} instance and want to convert * have a {@link Vec2f} and to convert it
* it to a String. You can do that by using * to a String, which you can do with
* {@link Vec2f}'s {@link Vec2f#toString()} * {@link Vec2f#toString()}. With this flag
* method. With this flag disabled it will * disabled it would return
* return * {@code de.staropensource.engine.base.types.vectors.}{@link Vec2i}{@code (x=64 y=64)},
* {@code de.staropensource.engine.base.types.vectors.}{@link Vec2i}{@code (x=64 y=64)}. * with it however it would just return
* With this flag enabled however the method will return
* {@link Vec2i}{@code (x=64 y=64)}, * {@link Vec2i}{@code (x=64 y=64)},
* which is much smaller. * which is much smaller.
* *
* @since v1-alpha2 * @since v1-alpha2
* -- GETTER -- * -- GETTER --
* Returns if to truncate the full path * Gets the value for {@link #hideFullTypePath}.
* of a class when invoking using their
* {@link #toString()} method.
* <p>
* Here's an example: Lets say that you have a
* {@link Vec2f} instance and want to convert
* it to a String. You can do that by using
* {@link Vec2f}'s {@link Vec2f#toString()}
* method. With this flag disabled it will
* return
* {@code de.staropensource.engine.base.types.vectors.}{@link Vec2i}{@code (x=64 y=64)}.
* With this flag enabled however the method will return
* {@link Vec2i}{@code (x=64 y=64)},
* which is much smaller.
* *
* @return truncate class paths? * @return variable value
* @see #hideFullTypePath
* @since v1-alpha2 * @since v1-alpha2
*/ */
private boolean hideFullTypePath; private boolean hideFullTypePath;
@ -418,7 +349,7 @@ public final class EngineConfiguration extends Configuration {
* @since v1-alpha6 * @since v1-alpha6
*/ */
EngineConfiguration() { EngineConfiguration() {
super(); super("ENGINE");
instance = this; instance = this;
@ -435,6 +366,7 @@ public final class EngineConfiguration extends Configuration {
case "debugEvents" -> debugEvents = parser.getBoolean(group + property); case "debugEvents" -> debugEvents = parser.getBoolean(group + property);
case "initialPerformSubsystemInitialization" -> initialPerformSubsystemInitialization = parser.getBoolean(group + property); case "initialPerformSubsystemInitialization" -> initialPerformSubsystemInitialization = parser.getBoolean(group + property);
case "initialForceDisableClasspathScanning" -> initialForceDisableClasspathScanning = parser.getBoolean(group + property);
case "initialIncludeSubsystemClasses" -> { case "initialIncludeSubsystemClasses" -> {
initialIncludeSubsystemClasses = new HashSet<>(); initialIncludeSubsystemClasses = new HashSet<>();
initialIncludeSubsystemClasses.addAll(Arrays.stream(parser.getString(group + property).split(",")).toList()); initialIncludeSubsystemClasses.addAll(Arrays.stream(parser.getString(group + property).split(",")).toList());
@ -446,22 +378,23 @@ public final class EngineConfiguration extends Configuration {
optimizeLogging = parser.getBoolean(group + property); optimizeLogging = parser.getBoolean(group + property);
// Start logging thread automatically // Start logging thread automatically
if (optimizeLogging && Engine.getInstance().getState() == EngineState.RUNNING) { if (optimizeLogging && Engine.getInstance().getState() == EngineState.RUNNING)
LoggingThread.startThread(false); LoggingThread.startThread();
}
} }
case "optimizeEvents" -> optimizeEvents = parser.getBoolean(group + property); case "optimizeEvents" -> optimizeEvents = parser.getBoolean(group + property);
case "logLevel" -> { case "loggerLevel" -> {
try { try {
logLevel = LogLevel.valueOf(parser.getString(group + property).toUpperCase()); loggerLevel = LogLevel.valueOf(parser.getString(group + property).toUpperCase());
} catch (IllegalArgumentException ignored) { } catch (IllegalArgumentException ignored) {
Logger.error("The log level '" + parser.getString(group + property) + "' is not valid"); System.out.println("Logger level " + parser.getString(group + property) + " is not valid");
} }
} }
case "logFeatures" -> logFeatures = Set.copyOf(Arrays.stream(parser.getString(group + property).split(",")).toList()); case "loggerTemplate" -> loggerTemplate = parser.getString(group + property);
case "logPollingSpeed" -> logPollingSpeed = parser.getInteger(group + property, true); case "loggerPollingSpeed" -> loggerPollingSpeed = parser.getInteger(group + property, true);
case "logForceStandardOutput" -> logForceStandardOutput = parser.getBoolean(group + property); case "loggerForceStandardOutput" -> loggerForceStandardOutput = parser.getBoolean(group + property);
case "loggerEnableNewlineSupport" -> loggerEnableNewlineSupport = parser.getBoolean(group + property);
case "loggerImmediateShutdown" -> loggerImmediateShutdown = parser.getBoolean(group + property);
case "hideFullTypePath" -> hideFullTypePath = parser.getBoolean(group + property); case "hideFullTypePath" -> hideFullTypePath = parser.getBoolean(group + property);
} }
@ -484,6 +417,7 @@ public final class EngineConfiguration extends Configuration {
debugEvents = false; debugEvents = false;
initialPerformSubsystemInitialization = true; initialPerformSubsystemInitialization = true;
initialForceDisableClasspathScanning = false;
initialIncludeSubsystemClasses = new HashSet<>(); initialIncludeSubsystemClasses = new HashSet<>();
errorShortcodeParser = true; errorShortcodeParser = true;
@ -491,10 +425,12 @@ public final class EngineConfiguration extends Configuration {
optimizeLogging = true; optimizeLogging = true;
optimizeEvents = true; optimizeEvents = true;
logLevel = LogLevel.INFORMATIONAL; loggerLevel = LogLevel.INFORMATIONAL;
logFeatures = Set.of("formatting", "time", "methodName", "lineNumber", "level", "origin"); loggerTemplate = "%log_color_primary%[%time_hour%:%time_minute%:%time_second%] [%log_level% %log_path%%log_metadata%] %log_message_prefix%%log_color_primary%%log_color_secondary%%log_message%<reset>";
logPollingSpeed = 5; loggerPollingSpeed = 5;
logForceStandardOutput = false; loggerForceStandardOutput = false;
loggerEnableNewlineSupport = true;
loggerImmediateShutdown = false;
hideFullTypePath = false; hideFullTypePath = false;
} }
@ -507,6 +443,7 @@ public final class EngineConfiguration extends Configuration {
case "debugEvents" -> debugEvents; case "debugEvents" -> debugEvents;
case "initialPerformSubsystemInitialization" -> initialPerformSubsystemInitialization; case "initialPerformSubsystemInitialization" -> initialPerformSubsystemInitialization;
case "initialForceDisableClasspathScanning" -> initialForceDisableClasspathScanning;
case "initialIncludeSubsystemClasses" -> initialIncludeSubsystemClasses; case "initialIncludeSubsystemClasses" -> initialIncludeSubsystemClasses;
case "errorShortcodeParser" -> errorShortcodeParser; case "errorShortcodeParser" -> errorShortcodeParser;
@ -514,10 +451,12 @@ public final class EngineConfiguration extends Configuration {
case "optimizeLogging" -> optimizeLogging; case "optimizeLogging" -> optimizeLogging;
case "optimizeEvents" -> optimizeEvents; case "optimizeEvents" -> optimizeEvents;
case "logLevel" -> logLevel; case "loggerLevel" -> loggerLevel;
case "logFeatures" -> logFeatures; case "loggerTemplate" -> loggerTemplate;
case "logPollingSpeed" -> logPollingSpeed; case "loggerPollingSpeed" -> loggerPollingSpeed;
case "logForceStandardOutput" -> logForceStandardOutput; case "loggerForceStandardOutput" -> loggerForceStandardOutput;
case "loggerEnableNewlineSupport" -> loggerEnableNewlineSupport;
case "loggerImmediateShutdown" -> loggerImmediateShutdown;
case "hideFullTypePath" -> hideFullTypePath; case "hideFullTypePath" -> hideFullTypePath;
default -> null; default -> null;

View file

@ -20,11 +20,11 @@
package de.staropensource.engine.base; package de.staropensource.engine.base;
import de.staropensource.engine.base.exception.IllegalAccessException; import de.staropensource.engine.base.exception.IllegalAccessException;
import de.staropensource.engine.base.implementable.Event;
import de.staropensource.engine.base.implementable.EventListenerCode; import de.staropensource.engine.base.implementable.EventListenerCode;
import de.staropensource.engine.base.implementable.ShutdownHandler; import de.staropensource.engine.base.implementable.ShutdownHandler;
import de.staropensource.engine.base.implementable.helper.EventHelper; import de.staropensource.engine.base.implementable.helper.EventHelper;
import de.staropensource.engine.base.logging.Logger; import de.staropensource.engine.base.logging.LoggerInstance;
import de.staropensource.engine.base.type.EventPriority;
import de.staropensource.engine.base.type.InternalAccessArea; import de.staropensource.engine.base.type.InternalAccessArea;
import lombok.Getter; import lombok.Getter;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -54,6 +54,14 @@ public final class EngineInternals {
@Getter @Getter
private static EngineInternals instance; private static EngineInternals instance;
/**
* Contains the {@link LoggerInstance} for this instance.
*
* @see LoggerInstance
* @since v1-alpha4
*/
private static final LoggerInstance logger = new LoggerInstance.Builder().setClazz(EngineInternals.class).setOrigin("ENGINE").build();
/** /**
* Contains all disabled internal access areas. * Contains all disabled internal access areas.
* *
@ -77,7 +85,7 @@ public final class EngineInternals {
* use case and application. * use case and application.
* *
* @see EventHelper#registerEvent(Class, EventListenerCode) * @see EventHelper#registerEvent(Class, EventListenerCode)
* @see EventHelper#registerEvent(Class, EventListenerCode, Event.Priority) * @see EventHelper#registerEvent(Class, EventListenerCode, EventPriority)
* @since v1-alpha5 * @since v1-alpha5
*/ */
private boolean reflectiveClasspathScanning = true; private boolean reflectiveClasspathScanning = true;
@ -92,7 +100,7 @@ public final class EngineInternals {
if (instance == null && Engine.getInstance() != null) if (instance == null && Engine.getInstance() != null)
instance = this; instance = this;
else else
Logger.crash("Only one instance of this class is allowed, use getInstance() instead of creating a new instance"); logger.crash("Only one instance of this class is allowed, use getInstance() instead of creating a new instance");
} }
/** /**
@ -191,7 +199,7 @@ public final class EngineInternals {
* @return reflective classpath scanning flag state * @return reflective classpath scanning flag state
* @throws IllegalAccessException when restricted ({@link InternalAccessArea#REFLECTIVE_CLASSPATH_SCANNING_GET}) * @throws IllegalAccessException when restricted ({@link InternalAccessArea#REFLECTIVE_CLASSPATH_SCANNING_GET})
* @see EventHelper#registerEvent(Class, EventListenerCode) * @see EventHelper#registerEvent(Class, EventListenerCode)
* @see EventHelper#registerEvent(Class, EventListenerCode, Event.Priority) * @see EventHelper#registerEvent(Class, EventListenerCode, EventPriority)
* @since v1-alpha5 * @since v1-alpha5
*/ */
public boolean getReflectiveClasspathScanning() throws IllegalAccessException { public boolean getReflectiveClasspathScanning() throws IllegalAccessException {
@ -216,7 +224,7 @@ public final class EngineInternals {
* @param reflectiveClasspathScanning new reflective classpath scanning * @param reflectiveClasspathScanning new reflective classpath scanning
* @throws IllegalAccessException when restricted ({@link InternalAccessArea#REFLECTIVE_CLASSPATH_SCANNING_OVERRIDE}) * @throws IllegalAccessException when restricted ({@link InternalAccessArea#REFLECTIVE_CLASSPATH_SCANNING_OVERRIDE})
* @see EventHelper#registerEvent(Class, EventListenerCode) * @see EventHelper#registerEvent(Class, EventListenerCode)
* @see EventHelper#registerEvent(Class, EventListenerCode, Event.Priority) * @see EventHelper#registerEvent(Class, EventListenerCode, EventPriority)
* @since v1-alpha5 * @since v1-alpha5
*/ */
public void overrideReflectiveClasspathScanning(boolean reflectiveClasspathScanning) throws IllegalAccessException { public void overrideReflectiveClasspathScanning(boolean reflectiveClasspathScanning) throws IllegalAccessException {

View file

@ -20,6 +20,7 @@
package de.staropensource.engine.base.annotation; package de.staropensource.engine.base.annotation;
import de.staropensource.engine.base.implementable.Event; import de.staropensource.engine.base.implementable.Event;
import de.staropensource.engine.base.type.EventPriority;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.lang.annotation.*; import java.lang.annotation.*;
@ -47,8 +48,8 @@ public @interface EventListener {
* Specifies the priority of the event. * Specifies the priority of the event.
* *
* @return event priority * @return event priority
* @see Event.Priority * @see EventPriority
* @since v1-alpha0 * @since v1-alpha0
*/ */
@NotNull Event.Priority priority() default Event.Priority.DEFAULT; @NotNull EventPriority priority() default EventPriority.DEFAULT;
} }

View file

@ -0,0 +1,61 @@
/*
* STAROPENSOURCE ENGINE SOURCE FILE
* Copyright (c) 2024 The StarOpenSource Engine Authors
* 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.engine.base.event;
import de.staropensource.engine.base.implementable.Event;
import de.staropensource.engine.base.implementable.helper.EventHelper;
import de.staropensource.engine.base.utility.Miscellaneous;
import org.jetbrains.annotations.NotNull;
/**
* Called when an exception is caught by {@link Miscellaneous#executeSafely(Runnable, String)}.
*
* @see Miscellaneous#executeSafely(Runnable, String)
* @since v1-alpha0
*/
public final class ThrowableCatchEvent implements Event {
/**
* Creates and initializes an instance of this event.
*
* @since v1-alpha0
*/
public ThrowableCatchEvent() {}
/**
* {@inheritDoc}
*
* @deprecated use the {@code callEvent} method with arguments
* @see ThrowableCatchEvent#callEvent(Throwable, String)
*/
@Deprecated
@Override
public void callEvent() {}
/**
* Emits the event and calls all event listeners.
*
* @param throwable caught throwable
* @param identifier an identifier given to the runnable
* @since v1-alpha0
*/
public void callEvent(@NotNull Throwable throwable, @NotNull String identifier) {
EventHelper.invokeAnnotatedMethods(getClass(), throwable, identifier);
}
}

View file

@ -20,7 +20,7 @@
package de.staropensource.engine.base.exception.reflection; package de.staropensource.engine.base.exception.reflection;
import de.staropensource.engine.base.type.reflection.ClassType; import de.staropensource.engine.base.type.reflection.ClassType;
import de.staropensource.engine.base.utility.misc.ListFormatter; import de.staropensource.engine.base.utility.ListFormatter;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
/** /**

View file

@ -19,6 +19,7 @@
package de.staropensource.engine.base.implementable; package de.staropensource.engine.base.implementable;
import de.staropensource.engine.base.logging.LoggerInstance;
import de.staropensource.engine.base.utility.PropertiesReader; import de.staropensource.engine.base.utility.PropertiesReader;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@ -36,11 +37,23 @@ import java.util.Properties;
*/ */
public abstract class Configuration { public abstract class Configuration {
/** /**
* Creates and initializes an instance of this abstract class. * Contains the {@link LoggerInstance} for this instance.
* *
* @see LoggerInstance
* @since v1-alpha1
*/
protected final @NotNull LoggerInstance logger;
/**
* Initializes this abstract class.
*
* @param origin see {@link LoggerInstance.Builder#setOrigin(String)}
* @since v1-alpha2 * @since v1-alpha2
*/ */
protected Configuration() { protected Configuration(@NotNull String origin) {
// Set logger instance
logger = new LoggerInstance.Builder().setClazz(getClass()).setOrigin(origin).build();
// Load default configuration // Load default configuration
loadDefaultConfiguration(); loadDefaultConfiguration();
} }

View file

@ -19,7 +19,6 @@
package de.staropensource.engine.base.implementable; package de.staropensource.engine.base.implementable;
import de.staropensource.engine.base.annotation.EventListener;
import de.staropensource.engine.base.implementable.helper.EventHelper; import de.staropensource.engine.base.implementable.helper.EventHelper;
/** /**
@ -35,76 +34,4 @@ public interface Event {
* @since v1-alpha0 * @since v1-alpha0
*/ */
void callEvent(); void callEvent();
/**
* Specifies in which order {@link EventListener}s shall be called.
*
* @since v1-alpha9
*/
@SuppressWarnings({ "unused" })
enum Priority {
/**
* {@link EventListener}s with this
* priority are called before all others.
* <p>
* This priority is exclusive to the
* engine and subsystems and should
* not be used by applications.
*
* @since v1-alpha9
*/
EXCLUSIVELY_IMPORTANT,
/**
* {@link EventListener}s with
* this priority are called 2nd.
*
* @since v1-alpha9
*/
VERY_IMPORTANT,
/**
* {@link EventListener}s with
* this priority are called 3rd.
*
* @since v1-alpha9
*/
IMPORTANT,
/**
* {@link EventListener}s with
* this priority are called 4th.
*
* @since v1-alpha9
*/
DEFAULT,
/**
* {@link EventListener}s with
* this priority are called 5th.
*
* @since v1-alpha9
*/
UNIMPORTANT,
/**
* {@link EventListener}s with
* this priority are called 6th.
*
* @since v1-alpha9
*/
VERY_UNIMPORTANT,
/**
* {@link EventListener}s with this
* priority are called after all others.
* <p>
* This priority is exclusive to the
* engine and subsystems and should
* not be used by applications.
*
* @since v1-alpha9
*/
EXCLUSIVELY_UNIMPORTANT
}
} }

View file

@ -20,6 +20,7 @@
package de.staropensource.engine.base.implementable; package de.staropensource.engine.base.implementable;
import de.staropensource.engine.base.implementable.helper.EventHelper; import de.staropensource.engine.base.implementable.helper.EventHelper;
import de.staropensource.engine.base.type.EventPriority;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
/** /**
@ -38,10 +39,10 @@ public abstract class EventListenerCode {
* *
* @since v1-alpha5 * @since v1-alpha5
*/ */
public @NotNull Event.Priority priority = Event.Priority.DEFAULT; public @NotNull EventPriority priority = EventPriority.DEFAULT;
/** /**
* Creates and initializes an instance of this abstract class. * Initializes this abstract class.
* *
* @since v1-alpha5 * @since v1-alpha5
*/ */

View file

@ -22,6 +22,7 @@ package de.staropensource.engine.base.implementable;
import de.staropensource.engine.base.logging.Logger; import de.staropensource.engine.base.logging.Logger;
import de.staropensource.engine.base.type.logging.LogLevel; import de.staropensource.engine.base.type.logging.LogLevel;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/** /**
* Interface for implementing custom logger implementations. * Interface for implementing custom logger implementations.
@ -31,13 +32,43 @@ import org.jetbrains.annotations.NotNull;
*/ */
public interface LoggingAdapter { public interface LoggingAdapter {
/** /**
* Prints a log message. * Invoked before anything is done with the log message.
* *
* @param level level of the log call * @param level level
* @param issuer {@link StackTraceElement} of the issuer * @param issuerClass issuer class
* @param message raw message * @param issuerOrigin issuer origin
* @param format processed log call output (print this!) * @param issuerMetadata issuer metadata
* @param message raw message
* @param format unmodified log format
* @return new log message, return {@code null} for no change
* @since v1-alpha2 * @since v1-alpha2
*/ */
void print(@NotNull LogLevel level, @NotNull StackTraceElement issuer, @NotNull String message, @NotNull String format); @Nullable String prePlaceholder(@NotNull LogLevel level, @NotNull Class<?> issuerClass, @NotNull String issuerOrigin, @Nullable String issuerMetadata, @NotNull String message, @NotNull String format);
/**
* Invoked after placeholders have been processed and replaced.
*
* @param level level
* @param issuerClass issuer class
* @param issuerOrigin issuer origin
* @param issuerMetadata issuer metadata
* @param message raw message
* @param format unmodified log format
* @return new log format
* @since v1-alpha2
*/
@NotNull String postPlaceholder(@NotNull LogLevel level, @NotNull Class<?> issuerClass, @NotNull String issuerOrigin, @Nullable String issuerMetadata, @NotNull String message, @NotNull String format);
/**
* Prints the log message.
*
* @param level level
* @param issuerClass issuer class
* @param issuerOrigin issuer origin
* @param issuerMetadata issuer metadata
* @param message raw message
* @param format unmodified log format
* @since v1-alpha2
*/
void print(@NotNull LogLevel level, @NotNull Class<?> issuerClass, @NotNull String issuerOrigin, @Nullable String issuerMetadata, @NotNull String message, @NotNull String format);
} }

View file

@ -21,7 +21,7 @@ package de.staropensource.engine.base.implementable;
import de.staropensource.engine.base.EngineConfiguration; import de.staropensource.engine.base.EngineConfiguration;
import de.staropensource.engine.base.exception.ParserException; import de.staropensource.engine.base.exception.ParserException;
import de.staropensource.engine.base.logging.Logger; import de.staropensource.engine.base.logging.LoggerInstance;
import lombok.Getter; import lombok.Getter;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -29,7 +29,7 @@ import java.util.LinkedList;
import java.util.Locale; import java.util.Locale;
/** /**
* Base class for implementing a shortcode parser. * Base class for implementing a shortcode converter.
* <p> * <p>
* This class parses a string and converts it into a list of * This class parses a string and converts it into a list of
* components, which can then be in turn be converted to something * components, which can then be in turn be converted to something
@ -48,9 +48,16 @@ import java.util.Locale;
* *
* @since v1-alpha1 * @since v1-alpha1
*/ */
@Getter
@SuppressWarnings({ "JavadocDeclaration" }) @SuppressWarnings({ "JavadocDeclaration" })
public abstract class ShortcodeParser { public abstract class ShortcodeParser {
/**
* Contains the {@link LoggerInstance} for this instance.
*
* @see LoggerInstance
* @since v1-alpha1
*/
protected final @NotNull LoggerInstance logger;
/** /**
* Contains a list of components the parsed text is made out of. * Contains a list of components the parsed text is made out of.
* *
@ -61,10 +68,11 @@ public abstract class ShortcodeParser {
* @return component list * @return component list
* @since v1-alpha1 * @since v1-alpha1
*/ */
@Getter
protected final @NotNull LinkedList<String> components; protected final @NotNull LinkedList<String> components;
/** /**
* Creates and initializes an instance of this abstract class. * Initializes this abstract class.
* *
* @param string string to parse * @param string string to parse
* @param ignoreInvalidEscapes if {@code true}, will ignore and treat invalid escapes as text * @param ignoreInvalidEscapes if {@code true}, will ignore and treat invalid escapes as text
@ -72,6 +80,7 @@ public abstract class ShortcodeParser {
* @since v1-alpha2 * @since v1-alpha2
*/ */
protected ShortcodeParser(@NotNull String string, boolean ignoreInvalidEscapes) throws ParserException { protected ShortcodeParser(@NotNull String string, boolean ignoreInvalidEscapes) throws ParserException {
logger = new LoggerInstance.Builder().setClazz(getClass()).setOrigin("ENGINE").build();
components = parse(string, ignoreInvalidEscapes); components = parse(string, ignoreInvalidEscapes);
} }
@ -125,7 +134,7 @@ public abstract class ShortcodeParser {
else { else {
// Complain about invalid shortcode // Complain about invalid shortcode
if (EngineConfiguration.getInstance() != null && EngineConfiguration.getInstance().isErrorShortcodeParser()) if (EngineConfiguration.getInstance() != null && EngineConfiguration.getInstance().isErrorShortcodeParser())
Logger.sarn("Invalid shortcode: " + part); logger.sarn("Invalid shortcode: " + part);
// Convert tag regular text // Convert tag regular text
components.add("TEXT:" + "<" + part + ">"); components.add("TEXT:" + "<" + part + ">");
@ -137,7 +146,7 @@ public abstract class ShortcodeParser {
else { else {
// Complain about invalid shortcode // Complain about invalid shortcode
if (EngineConfiguration.getInstance() != null && EngineConfiguration.getInstance().isErrorShortcodeParser()) if (EngineConfiguration.getInstance() != null && EngineConfiguration.getInstance().isErrorShortcodeParser())
Logger.sarn("Invalid shortcode: " + part); logger.sarn("Invalid shortcode: " + part);
// Convert tag regular text // Convert tag regular text
components.add("TEXT:" + "<" + part + ">"); components.add("TEXT:" + "<" + part + ">");
@ -165,7 +174,7 @@ public abstract class ShortcodeParser {
else { else {
// Complain about invalid shortcode // Complain about invalid shortcode
if (EngineConfiguration.getInstance() != null && EngineConfiguration.getInstance().isErrorShortcodeParser()) if (EngineConfiguration.getInstance() != null && EngineConfiguration.getInstance().isErrorShortcodeParser())
Logger.sarn("Invalid shortcode: " + part); logger.sarn("Invalid shortcode: " + part);
// Convert tag regular text // Convert tag regular text
components.add("TEXT:" + "<" + part + ">"); components.add("TEXT:" + "<" + part + ">");

View file

@ -22,7 +22,8 @@ package de.staropensource.engine.base.implementable;
import de.staropensource.engine.base.Engine; import de.staropensource.engine.base.Engine;
import de.staropensource.engine.base.annotation.EngineSubsystem; import de.staropensource.engine.base.annotation.EngineSubsystem;
import de.staropensource.engine.base.annotation.EventListener; import de.staropensource.engine.base.annotation.EventListener;
import de.staropensource.engine.base.event.InternalEngineShutdownEvent; import de.staropensource.engine.base.internal.event.InternalEngineShutdownEvent;
import de.staropensource.engine.base.logging.LoggerInstance;
import de.staropensource.engine.base.type.DependencyVector; import de.staropensource.engine.base.type.DependencyVector;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -34,7 +35,15 @@ import org.jetbrains.annotations.NotNull;
*/ */
public abstract class SubsystemClass { public abstract class SubsystemClass {
/** /**
* Creates and initializes an instance of this abstract class. * Contains the {@link LoggerInstance} for this instance.
*
* @see LoggerInstance
* @since v1-alpha0
*/
public final LoggerInstance logger = new LoggerInstance.Builder().setClazz(getClass()).setOrigin("ENGINE").build();
/**
* Initializes this abstract class.
* *
* @since v1-alpha2 * @since v1-alpha2
*/ */

View file

@ -30,9 +30,9 @@ import de.staropensource.engine.base.exception.reflection.StaticInitializerExcep
import de.staropensource.engine.base.implementable.Event; import de.staropensource.engine.base.implementable.Event;
import de.staropensource.engine.base.implementable.EventListenerCode; import de.staropensource.engine.base.implementable.EventListenerCode;
import de.staropensource.engine.base.internal.implementation.EventListenerMethod; import de.staropensource.engine.base.internal.implementation.EventListenerMethod;
import de.staropensource.engine.base.logging.LoggerInstance;
import de.staropensource.engine.base.logging.Logger; import de.staropensource.engine.base.type.EventPriority;
import de.staropensource.engine.base.utility.misc.ListFormatter; import de.staropensource.engine.base.utility.ListFormatter;
import lombok.Getter; import lombok.Getter;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@ -52,6 +52,14 @@ import java.util.*;
*/ */
@Getter @Getter
public final class EventHelper { public final class EventHelper {
/**
* Contains the {@link LoggerInstance} for this instance.
*
* @see LoggerInstance
* @since v1-alpha1
*/
private static final @NotNull LoggerInstance logger = new LoggerInstance.Builder().setClazz(EventHelper.class).setOrigin("ENGINE").build();
/** /**
* Holds all cached events. * Holds all cached events.
* *
@ -77,7 +85,7 @@ public final class EventHelper {
* @see EngineInternals#getReflectiveClasspathScanning() * @see EngineInternals#getReflectiveClasspathScanning()
* @since v1-alpha5 * @since v1-alpha5
*/ */
public static synchronized void registerEvent(@NotNull Class<? extends Event> event, @NotNull EventListenerCode eventListener, @NotNull Event.Priority priority) { public static synchronized void registerEvent(@NotNull Class<? extends Event> event, @NotNull EventListenerCode eventListener, @NotNull EventPriority priority) {
if (EngineInternals.getInstance().getReflectiveClasspathScanning()) if (EngineInternals.getInstance().getReflectiveClasspathScanning())
return; return;
@ -97,7 +105,7 @@ public final class EventHelper {
cachedEventListeners.put(event, list); cachedEventListeners.put(event, list);
} }
Logger.diag("Registered event listener " + eventListener + " for event " + event + " with priority " + priority.name()); logger.diag("Registered event listener " + eventListener + " for event " + event + " with priority " + priority.name());
} }
/** /**
@ -111,7 +119,7 @@ public final class EventHelper {
* @since v1-alpha5 * @since v1-alpha5
*/ */
public static void registerEvent(@NotNull Class<? extends Event> event, @NotNull EventListenerCode eventListener) { public static void registerEvent(@NotNull Class<? extends Event> event, @NotNull EventListenerCode eventListener) {
registerEvent(event, eventListener, Event.Priority.DEFAULT); registerEvent(event, eventListener, EventPriority.DEFAULT);
} }
/** /**
@ -169,26 +177,26 @@ public final class EventHelper {
public static void invokeAnnotatedMethods(@NotNull Class<? extends Event> event, Object... arguments) { public static void invokeAnnotatedMethods(@NotNull Class<? extends Event> event, Object... arguments) {
if (event != LogEvent.class && EngineConfiguration.getInstance().isDebugEvents()) if (event != LogEvent.class && EngineConfiguration.getInstance().isDebugEvents())
if (arguments.length == 0) if (arguments.length == 0)
Logger.diag("Event " + event.getName() + " was emitted"); logger.diag("Event " + event.getName() + " was emitted");
else else
Logger.diag("Event " + event.getName() + " was emitted, passing arguments " + ListFormatter.formatArray(arguments)); logger.diag("Event " + event.getName() + " was emitted, passing arguments " + ListFormatter.formatArray(arguments));
Runnable eventCode = () -> { Runnable eventCode = () -> {
for (EventListenerCode eventListener : getAnnotatedMethods(event)) { for (EventListenerCode eventListener : getAnnotatedMethods(event)) {
try { try {
eventListener.run(arguments); eventListener.run(arguments);
} catch (NoAccessException exception) { } catch (NoAccessException exception) {
Logger.warn("Event listener " + eventListener + " could not be called as the method could not be accessed"); logger.warn("Event listener " + eventListener + " could not be called as the method could not be accessed");
} catch (InvalidMethodSignatureException exception) { } catch (InvalidMethodSignatureException exception) {
Logger.warn("Event listener " + eventListener + " has an invalid method signature"); logger.warn("Event listener " + eventListener + " has an invalid method signature");
} catch (InvocationTargetException exception) { } catch (InvocationTargetException exception) {
Logger.crash("Event listener " + eventListener + " threw a throwable", exception.getTargetException(), true); logger.crash("Event listener " + eventListener + " threw a throwable", exception.getTargetException(), true);
} catch (InstanceMethodFromStaticContextException exception) { } catch (InstanceMethodFromStaticContextException exception) {
Logger.warn("Event listener " + eventListener + " is not static. Event listener methods must be static for them to be called."); logger.warn("Event listener " + eventListener + " is not static. Event listener methods must be static for them to be called.");
} catch (StaticInitializerException exception) { } catch (StaticInitializerException exception) {
Logger.crash("Event listener " + eventListener + " could not be called as the static initializer failed", exception.getThrowable(), true); logger.crash("Event listener " + eventListener + " could not be called as the static initializer failed", exception.getThrowable(), true);
} catch (Exception exception) { } catch (Exception exception) {
Logger.crash("Event listener " + eventListener + " could not be called as an error occurred during reflection", exception, true); logger.crash("Event listener " + eventListener + " could not be called as an error occurred during reflection", exception, true);
} }
} }
}; };

View file

@ -23,8 +23,9 @@ import de.staropensource.engine.base.EngineConfiguration;
import de.staropensource.engine.base.implementable.LoggingAdapter; import de.staropensource.engine.base.implementable.LoggingAdapter;
import de.staropensource.engine.base.logging.Logger; import de.staropensource.engine.base.logging.Logger;
import de.staropensource.engine.base.type.logging.LogLevel; import de.staropensource.engine.base.type.logging.LogLevel;
import de.staropensource.engine.base.implementation.shortcode.EmptyShortcodeParser; import de.staropensource.engine.base.implementation.shortcode.EmptyShortcodeConverter;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/** /**
* Prints log messages to the console, without any fancy colors or formatting. * Prints log messages to the console, without any fancy colors or formatting.
@ -43,10 +44,22 @@ public class PlainLoggingAdapter implements LoggingAdapter {
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public void print(@NotNull LogLevel level, @NotNull StackTraceElement issuer, @NotNull String message, @NotNull String format) { public @Nullable String prePlaceholder(@NotNull LogLevel level, @NotNull Class<?> issuerClass, @NotNull String issuerOrigin, @Nullable String issuerMetadata, @NotNull String message, @NotNull String format) {
format = new EmptyShortcodeParser(format, true).getClean(); return null; // No modifications necessary
}
/** {@inheritDoc} */
@Override
public @NotNull String postPlaceholder(@NotNull LogLevel level, @NotNull Class<?> issuerClass, @NotNull String issuerOrigin, @Nullable String issuerMetadata, @NotNull String message, @NotNull String format) {
return format; // No modifications necessary
}
/** {@inheritDoc} */
@Override
public void print(@NotNull LogLevel level, @NotNull Class<?> issuerClass, @NotNull String issuerOrigin, @Nullable String issuerMetadata, @NotNull String message, @NotNull String format) {
format = new EmptyShortcodeConverter(format, true).getClean();
if (level == LogLevel.ERROR || level == LogLevel.CRASH) if (level == LogLevel.ERROR || level == LogLevel.CRASH)
if (EngineConfiguration.getInstance() != null && EngineConfiguration.getInstance().isLogForceStandardOutput()) if (EngineConfiguration.getInstance() != null && EngineConfiguration.getInstance().isLoggerForceStandardOutput())
System.out.println(format); System.out.println(format);
else else
System.err.println(format); System.err.println(format);

View file

@ -40,5 +40,17 @@ public class QuietLoggingAdapter implements LoggingAdapter {
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public void print(@NotNull LogLevel level, @NotNull StackTraceElement issuer, @NotNull String message, @NotNull String format) {} public @Nullable String prePlaceholder(@NotNull LogLevel level, @NotNull Class<?> issuerClass, @NotNull String issuerOrigin, @Nullable String issuerMetadata, @NotNull String message, @NotNull String format) {
return null;
}
/** {@inheritDoc} */
@Override
public @NotNull String postPlaceholder(@NotNull LogLevel level, @NotNull Class<?> issuerClass, @NotNull String issuerOrigin, @Nullable String issuerMetadata, @NotNull String message, @NotNull String format) {
return format;
}
/** {@inheritDoc} */
@Override
public void print(@NotNull LogLevel level, @NotNull Class<?> issuerClass, @NotNull String issuerOrigin, @Nullable String issuerMetadata, @NotNull String message, @NotNull String format) {}
} }

View file

@ -44,9 +44,21 @@ public class RawLoggingAdapter implements LoggingAdapter {
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public void print(@NotNull LogLevel level, @NotNull StackTraceElement issuer, @NotNull String message, @NotNull String format) { public @Nullable String prePlaceholder(@NotNull LogLevel level, @NotNull Class<?> issuerClass, @NotNull String issuerOrigin, @Nullable String issuerMetadata, @NotNull String message, @NotNull String format) {
return null; // No modifications necessary
}
/** {@inheritDoc} */
@Override
public @NotNull String postPlaceholder(@NotNull LogLevel level, @NotNull Class<?> issuerClass, @NotNull String issuerOrigin, @Nullable String issuerMetadata, @NotNull String message, @NotNull String format) {
return format; // No modifications necessary
}
/** {@inheritDoc} */
@Override
public void print(@NotNull LogLevel level, @NotNull Class<?> issuerClass, @NotNull String issuerOrigin, @Nullable String issuerMetadata, @NotNull String message, @NotNull String format) {
if (level == LogLevel.ERROR || level == LogLevel.CRASH) if (level == LogLevel.ERROR || level == LogLevel.CRASH)
if (EngineConfiguration.getInstance().isLogForceStandardOutput()) if (EngineConfiguration.getInstance().isLoggerForceStandardOutput())
System.out.println(format); System.out.println(format);
else else
System.err.println(format); System.err.println(format);

View file

@ -27,18 +27,18 @@ import org.jetbrains.annotations.NotNull;
* Cleans the string of any tags. * Cleans the string of any tags.
* *
* @see ShortcodeParser * @see ShortcodeParser
* @since v1-alpha8 * @since v1-alpha1
*/ */
public final class EmptyShortcodeParser extends ShortcodeParser { public final class EmptyShortcodeConverter extends ShortcodeParser {
/** /**
* Creates and initializes an instance of this class. * Creates and initializes an instance of this class.
* *
* @param string string to parse * @param string string to parse
* @param ignoreInvalidEscapes if {@code true}, will ignore and treat invalid escapes as text * @param ignoreInvalidEscapes if {@code true}, will ignore and treat invalid escapes as text
* @throws ParserException on error * @throws ParserException on error
* @since v1-alpha8 * @since v1-alpha1
*/ */
public EmptyShortcodeParser(@NotNull String string, boolean ignoreInvalidEscapes) throws ParserException { public EmptyShortcodeConverter(@NotNull String string, boolean ignoreInvalidEscapes) throws ParserException {
super(string, ignoreInvalidEscapes); super(string, ignoreInvalidEscapes);
} }
@ -46,7 +46,7 @@ public final class EmptyShortcodeParser extends ShortcodeParser {
* Returns the parsed string without any tags. * Returns the parsed string without any tags.
* *
* @return cleaned input string * @return cleaned input string
* @since v1-alpha8 * @since v1-alpha1
*/ */
public @NotNull String getClean() { public @NotNull String getClean() {
StringBuilder output = new StringBuilder(); StringBuilder output = new StringBuilder();

View file

@ -22,7 +22,7 @@ package de.staropensource.engine.base.implementation.versioning;
import de.staropensource.engine.base.implementable.VersioningSystem; import de.staropensource.engine.base.implementable.VersioningSystem;
import de.staropensource.engine.base.exception.versioning.IncompatibleVersioningSystemException; import de.staropensource.engine.base.exception.versioning.IncompatibleVersioningSystemException;
import de.staropensource.engine.base.exception.versioning.InvalidVersionStringException; import de.staropensource.engine.base.exception.versioning.InvalidVersionStringException;
import de.staropensource.engine.base.utility.misc.StringUtil; import de.staropensource.engine.base.utility.Miscellaneous;
import lombok.Getter; import lombok.Getter;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Range; import org.jetbrains.annotations.Range;
@ -98,7 +98,7 @@ public final class FourNumberVersioningSystem implements VersioningSystem {
*/ */
public FourNumberVersioningSystem(@NotNull String versionString) throws InvalidVersionStringException { public FourNumberVersioningSystem(@NotNull String versionString) throws InvalidVersionStringException {
String[] separatorList = new String[]{ ".", "-" }; String[] separatorList = new String[]{ ".", "-" };
String separator = StringUtil.getSeparatorRequired(versionString, separatorList, 3); String separator = Miscellaneous.getSeparator(versionString, separatorList, 3);
// Escape separator or throw error if invalid // Escape separator or throw error if invalid
switch (separator) { switch (separator) {

View file

@ -22,7 +22,7 @@ package de.staropensource.engine.base.implementation.versioning;
import de.staropensource.engine.base.implementable.VersioningSystem; import de.staropensource.engine.base.implementable.VersioningSystem;
import de.staropensource.engine.base.exception.versioning.IncompatibleVersioningSystemException; import de.staropensource.engine.base.exception.versioning.IncompatibleVersioningSystemException;
import de.staropensource.engine.base.exception.versioning.InvalidVersionStringException; import de.staropensource.engine.base.exception.versioning.InvalidVersionStringException;
import de.staropensource.engine.base.utility.misc.StringUtil; import de.staropensource.engine.base.utility.Miscellaneous;
import lombok.Getter; import lombok.Getter;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@ -117,7 +117,7 @@ public final class SemanticVersioningSystem implements VersioningSystem {
*/ */
public SemanticVersioningSystem(@NotNull String versionString) throws InvalidVersionStringException { public SemanticVersioningSystem(@NotNull String versionString) throws InvalidVersionStringException {
String[] separatorList = new String[]{ "." }; String[] separatorList = new String[]{ "." };
String separator = StringUtil.getSeparatorRequired(versionString, separatorList, 2); String separator = Miscellaneous.getSeparator(versionString, separatorList, 2);
// Escape separator or throw error if invalid // Escape separator or throw error if invalid
switch (separator) { switch (separator) {

View file

@ -22,7 +22,7 @@ package de.staropensource.engine.base.implementation.versioning;
import de.staropensource.engine.base.implementable.VersioningSystem; import de.staropensource.engine.base.implementable.VersioningSystem;
import de.staropensource.engine.base.exception.versioning.IncompatibleVersioningSystemException; import de.staropensource.engine.base.exception.versioning.IncompatibleVersioningSystemException;
import de.staropensource.engine.base.exception.versioning.InvalidVersionStringException; import de.staropensource.engine.base.exception.versioning.InvalidVersionStringException;
import de.staropensource.engine.base.utility.misc.StringUtil; import de.staropensource.engine.base.utility.Miscellaneous;
import lombok.Getter; import lombok.Getter;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Range; import org.jetbrains.annotations.Range;
@ -86,7 +86,7 @@ public final class ThreeNumberVersioningSystem implements VersioningSystem {
*/ */
public ThreeNumberVersioningSystem(@NotNull String versionString) throws InvalidVersionStringException { public ThreeNumberVersioningSystem(@NotNull String versionString) throws InvalidVersionStringException {
String[] separatorList = new String[]{ ".", "-" }; String[] separatorList = new String[]{ ".", "-" };
String separator = StringUtil.getSeparatorRequired(versionString, separatorList, 2); String separator = Miscellaneous.getSeparator(versionString, separatorList, 2);
// Escape separator or throw error if invalid // Escape separator or throw error if invalid
switch (separator) { switch (separator) {

View file

@ -22,7 +22,7 @@ package de.staropensource.engine.base.implementation.versioning;
import de.staropensource.engine.base.implementable.VersioningSystem; import de.staropensource.engine.base.implementable.VersioningSystem;
import de.staropensource.engine.base.exception.versioning.IncompatibleVersioningSystemException; import de.staropensource.engine.base.exception.versioning.IncompatibleVersioningSystemException;
import de.staropensource.engine.base.exception.versioning.InvalidVersionStringException; import de.staropensource.engine.base.exception.versioning.InvalidVersionStringException;
import de.staropensource.engine.base.utility.misc.StringUtil; import de.staropensource.engine.base.utility.Miscellaneous;
import lombok.Getter; import lombok.Getter;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Range; import org.jetbrains.annotations.Range;
@ -74,7 +74,7 @@ public final class TwoNumberVersioningSystem implements VersioningSystem {
*/ */
public TwoNumberVersioningSystem(@NotNull String versionString) throws InvalidVersionStringException { public TwoNumberVersioningSystem(@NotNull String versionString) throws InvalidVersionStringException {
String[] separatorList = new String[]{ ".", "-" }; String[] separatorList = new String[]{ ".", "-" };
String separator = StringUtil.getSeparatorRequired(versionString, separatorList, 1); String separator = Miscellaneous.getSeparator(versionString, separatorList, 1);
// Escape separator or throw error if invalid // Escape separator or throw error if invalid
switch (separator) { switch (separator) {

View file

@ -17,7 +17,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
package de.staropensource.engine.base.event; package de.staropensource.engine.base.internal.event;
import de.staropensource.engine.base.implementable.Event; import de.staropensource.engine.base.implementable.Event;
import de.staropensource.engine.base.implementable.helper.EventHelper; import de.staropensource.engine.base.implementable.helper.EventHelper;

View file

@ -0,0 +1,24 @@
/*
* STAROPENSOURCE ENGINE SOURCE FILE
* Copyright (c) 2024 The StarOpenSource Engine Authors
* 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.engine.base.internal.event;

View file

@ -20,7 +20,7 @@
package de.staropensource.engine.base.internal.implementation.placeholder; package de.staropensource.engine.base.internal.implementation.placeholder;
import de.staropensource.engine.base.implementable.Placeholder; import de.staropensource.engine.base.implementable.Placeholder;
import de.staropensource.engine.base.utility.misc.NumberUtil; import de.staropensource.engine.base.utility.Math;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.Calendar; import java.util.Calendar;
@ -43,6 +43,6 @@ public final class DateDay implements Placeholder {
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public @NotNull String replace(@NotNull String text) { public @NotNull String replace(@NotNull String text) {
return text.replace("%date_day%", NumberUtil.padNumbers(Calendar.getInstance().get(Calendar.DAY_OF_MONTH), 2)); return text.replace("%date_day%", Math.padNumbers(Calendar.getInstance().get(Calendar.DAY_OF_MONTH), 2));
} }
} }

View file

@ -20,7 +20,7 @@
package de.staropensource.engine.base.internal.implementation.placeholder; package de.staropensource.engine.base.internal.implementation.placeholder;
import de.staropensource.engine.base.implementable.Placeholder; import de.staropensource.engine.base.implementable.Placeholder;
import de.staropensource.engine.base.utility.misc.NumberUtil; import de.staropensource.engine.base.utility.Math;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.Calendar; import java.util.Calendar;
@ -43,6 +43,6 @@ public final class DateMonth implements Placeholder {
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public @NotNull String replace(@NotNull String text) { public @NotNull String replace(@NotNull String text) {
return text.replace("%date_month%", NumberUtil.padNumbers(Calendar.getInstance().get(Calendar.MONTH), 2)); return text.replace("%date_month%", Math.padNumbers(Calendar.getInstance().get(Calendar.MONTH), 2));
} }
} }

View file

@ -20,7 +20,7 @@
package de.staropensource.engine.base.internal.implementation.placeholder; package de.staropensource.engine.base.internal.implementation.placeholder;
import de.staropensource.engine.base.implementable.Placeholder; import de.staropensource.engine.base.implementable.Placeholder;
import de.staropensource.engine.base.utility.misc.NumberUtil; import de.staropensource.engine.base.utility.Math;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.Calendar; import java.util.Calendar;
@ -43,6 +43,6 @@ public final class DateYear implements Placeholder {
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public @NotNull String replace(@NotNull String text) { public @NotNull String replace(@NotNull String text) {
return text.replace("%date_year%", NumberUtil.padNumbers(Calendar.getInstance().get(Calendar.YEAR), 4)); return text.replace("%date_year%", Math.padNumbers(Calendar.getInstance().get(Calendar.YEAR), 4));
} }
} }

View file

@ -20,7 +20,7 @@
package de.staropensource.engine.base.internal.implementation.placeholder; package de.staropensource.engine.base.internal.implementation.placeholder;
import de.staropensource.engine.base.implementable.Placeholder; import de.staropensource.engine.base.implementable.Placeholder;
import de.staropensource.engine.base.utility.misc.NumberUtil; import de.staropensource.engine.base.utility.Math;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
/** /**
@ -42,6 +42,6 @@ public final class TimeEpoch implements Placeholder {
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public @NotNull String replace(@NotNull String text) { public @NotNull String replace(@NotNull String text) {
return text.replace("%time_epoch%", NumberUtil.padNumbers(System.currentTimeMillis(), String.valueOf(Long.MAX_VALUE).length())); return text.replace("%time_epoch%", Math.padNumbers(System.currentTimeMillis(), String.valueOf(Long.MAX_VALUE).length()));
} }
} }

View file

@ -20,7 +20,7 @@
package de.staropensource.engine.base.internal.implementation.placeholder; package de.staropensource.engine.base.internal.implementation.placeholder;
import de.staropensource.engine.base.implementable.Placeholder; import de.staropensource.engine.base.implementable.Placeholder;
import de.staropensource.engine.base.utility.misc.NumberUtil; import de.staropensource.engine.base.utility.Math;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.Calendar; import java.util.Calendar;
@ -43,6 +43,6 @@ public final class TimeHour implements Placeholder {
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public @NotNull String replace(@NotNull String text) { public @NotNull String replace(@NotNull String text) {
return text.replace("%time_hour%", NumberUtil.padNumbers(Calendar.getInstance().get(Calendar.HOUR_OF_DAY), 2)); return text.replace("%time_hour%", Math.padNumbers(Calendar.getInstance().get(Calendar.HOUR_OF_DAY), 2));
} }
} }

View file

@ -20,7 +20,7 @@
package de.staropensource.engine.base.internal.implementation.placeholder; package de.staropensource.engine.base.internal.implementation.placeholder;
import de.staropensource.engine.base.implementable.Placeholder; import de.staropensource.engine.base.implementable.Placeholder;
import de.staropensource.engine.base.utility.misc.NumberUtil; import de.staropensource.engine.base.utility.Math;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.Calendar; import java.util.Calendar;
@ -43,6 +43,6 @@ public final class TimeMinute implements Placeholder {
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public @NotNull String replace(@NotNull String text) { public @NotNull String replace(@NotNull String text) {
return text.replace("%time_minute%", NumberUtil.padNumbers(Calendar.getInstance().get(Calendar.MINUTE), 2)); return text.replace("%time_minute%", Math.padNumbers(Calendar.getInstance().get(Calendar.MINUTE), 2));
} }
} }

View file

@ -20,7 +20,7 @@
package de.staropensource.engine.base.internal.implementation.placeholder; package de.staropensource.engine.base.internal.implementation.placeholder;
import de.staropensource.engine.base.implementable.Placeholder; import de.staropensource.engine.base.implementable.Placeholder;
import de.staropensource.engine.base.utility.misc.NumberUtil; import de.staropensource.engine.base.utility.Math;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.Calendar; import java.util.Calendar;
@ -43,6 +43,6 @@ public final class TimeSecond implements Placeholder {
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public @NotNull String replace(@NotNull String text) { public @NotNull String replace(@NotNull String text) {
return text.replace("%time_second%", NumberUtil.padNumbers(Calendar.getInstance().get(Calendar.SECOND), 2)); return text.replace("%time_second%", Math.padNumbers(Calendar.getInstance().get(Calendar.SECOND), 2));
} }
} }

View file

@ -17,30 +17,40 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
package de.staropensource.engine.base.internal.implementation.placeholder; package de.staropensource.engine.base.internal.implementation.placeholder.crashhandler;
import de.staropensource.engine.base.implementable.Placeholder; import de.staropensource.engine.base.implementable.Placeholder;
import de.staropensource.engine.base.utility.information.EngineInformation;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
/** /**
* Implements the {@code engine_version_codename} placeholder. * Implements the {@code crash_message} placeholder.
* *
* @see Placeholder * @see Placeholder
* @since v1-alpha8 * @since v1-alpha0
*/ */
@SuppressWarnings({ "unused" }) @SuppressWarnings({ "unused" })
public final class EngineVersionCodename implements Placeholder { public final class CrashMessage implements Placeholder {
/**
* Contains the message to use.
*
* @since v1-alpha0
*/
@NotNull
private final String message;
/** /**
* Creates and initializes an instance of this event. * Creates and initializes an instance of this event.
* *
* @since v1-alpha8 * @param message message to use
* @since v1-alpha0
*/ */
public EngineVersionCodename() {} public CrashMessage(@NotNull String message) {
this.message = message;
}
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public @NotNull String replace(@NotNull String text) { public @NotNull String replace(@NotNull String text) {
return text.replace("%engine_version_codename%", EngineInformation.getVersioningCodename()); return text.replace("%crash_message%", message);
} }
} }

View file

@ -0,0 +1,55 @@
/*
* STAROPENSOURCE ENGINE SOURCE FILE
* Copyright (c) 2024 The StarOpenSource Engine Authors
* 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.engine.base.internal.implementation.placeholder.crashhandler;
import de.staropensource.engine.base.implementable.Placeholder;
import org.jetbrains.annotations.NotNull;
/**
* Implements the {@code issuer_class} placeholder.
*
* @see Placeholder
* @since v1-alpha0
*/
@SuppressWarnings({ "unused" })
public final class IssuerClass implements Placeholder {
/**
* Contains the issuer class to use.
*
* @since v1-alpha2
*/
private final @NotNull Class<?> issuerClass;
/**
* Creates and initializes an instance of this event.
*
* @param issuerClass issuer class to use
* @since v1-alpha0
*/
public IssuerClass(@NotNull Class<?> issuerClass) {
this.issuerClass = issuerClass;
}
/** {@inheritDoc} */
@Override
public @NotNull String replace(@NotNull String text) {
return text.replace("%issuer_class%", issuerClass.getName().replace(issuerClass.getPackageName() + ".", ""));
}
}

View file

@ -0,0 +1,61 @@
/*
* STAROPENSOURCE ENGINE SOURCE FILE
* Copyright (c) 2024 The StarOpenSource Engine Authors
* 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.engine.base.internal.implementation.placeholder.crashhandler;
import de.staropensource.engine.base.implementable.Placeholder;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* Implements the {@code issuer_metadata} placeholder.
*
* @see Placeholder
* @since v1-alpha0
*/
@SuppressWarnings({ "unused" })
public final class IssuerMetadata implements Placeholder {
/**
* Contains the issuer metadata to use.
*
* @since v1-alpha0
*/
private final @Nullable String issuerMetadata;
/**
* Creates and initializes an instance of this event.
*
* @param issuerMetadata issuer metadata to use
* @since v1-alpha0
*/
public IssuerMetadata(@Nullable String issuerMetadata) {
this.issuerMetadata = issuerMetadata;
}
/** {@inheritDoc} */
@Override
public @NotNull String replace(@NotNull String text) {
String replacement = "\\<none>";
if (issuerMetadata != null)
replacement = issuerMetadata;
return text.replace("%issuer_metadata%", replacement);
}
}

View file

@ -0,0 +1,57 @@
/*
* STAROPENSOURCE ENGINE SOURCE FILE
* Copyright (c) 2024 The StarOpenSource Engine Authors
* 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.engine.base.internal.implementation.placeholder.crashhandler;
import de.staropensource.engine.base.implementable.Placeholder;
import org.jetbrains.annotations.NotNull;
import java.util.Locale;
/**
* Implements the {@code issuer_origin} placeholder.
*
* @see Placeholder
* @since v1-alpha0
*/
@SuppressWarnings({ "unused" })
public final class IssuerOrigin implements Placeholder {
/**
* Contains the issuer origin to use.
*
* @since v1-alpha2
*/
private final @NotNull String issuerOrigin;
/**
* Creates and initializes an instance of this event.
*
* @param issuerOrigin issuer origin to use
* @since v1-alpha0
*/
public IssuerOrigin(@NotNull String issuerOrigin) {
this.issuerOrigin = issuerOrigin;
}
/** {@inheritDoc} */
@Override
public @NotNull String replace(@NotNull String text) {
return text.replace("%issuer_origin%", issuerOrigin.toUpperCase(Locale.ROOT));
}
}

View file

@ -0,0 +1,55 @@
/*
* STAROPENSOURCE ENGINE SOURCE FILE
* Copyright (c) 2024 The StarOpenSource Engine Authors
* 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.engine.base.internal.implementation.placeholder.crashhandler;
import de.staropensource.engine.base.implementable.Placeholder;
import org.jetbrains.annotations.NotNull;
/**
* Implements the {@code issuer_package} placeholder.
*
* @see Placeholder
* @since v1-alpha0
*/
@SuppressWarnings({ "unused" })
public final class IssuerPackage implements Placeholder {
/**
* Contains the issuer class to use.
*
* @since v1-alpha2
*/
private final @NotNull Class<?> issuerClass;
/**
* Creates and initializes an instance of this event.
*
* @param issuerClass issuer class to use
* @since v1-alpha0
*/
public IssuerPackage(@NotNull Class<?> issuerClass) {
this.issuerClass = issuerClass;
}
/** {@inheritDoc} */
@Override
public @NotNull String replace(@NotNull String text) {
return text.replace("%issuer_package%", issuerClass.getPackageName());
}
}

View file

@ -0,0 +1,55 @@
/*
* STAROPENSOURCE ENGINE SOURCE FILE
* Copyright (c) 2024 The StarOpenSource Engine Authors
* 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.engine.base.internal.implementation.placeholder.crashhandler;
import de.staropensource.engine.base.implementable.Placeholder;
import org.jetbrains.annotations.NotNull;
/**
* Implements the {@code issuer_path} placeholder.
*
* @see Placeholder
* @since v1-alpha0
*/
@SuppressWarnings({ "unused" })
public final class IssuerPath implements Placeholder {
/**
* Contains the issuer class to use.
*
* @since v1-alpha2
*/
private final @NotNull Class<?> issuerClass;
/**
* Creates and initializes an instance of this event.
*
* @param issuerClass issuer class to use
* @since v1-alpha0
*/
public IssuerPath(@NotNull Class<?> issuerClass) {
this.issuerClass = issuerClass;
}
/** {@inheritDoc} */
@Override
public @NotNull String replace(@NotNull String text) {
return text.replace("%issuer_path%", issuerClass.getName());
}
}

View file

@ -0,0 +1,101 @@
/*
* STAROPENSOURCE ENGINE SOURCE FILE
* Copyright (c) 2024 The StarOpenSource Engine Authors
* 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.engine.base.internal.implementation.placeholder.crashhandler;
import de.staropensource.engine.base.implementable.Placeholder;
import de.staropensource.engine.base.utility.Miscellaneous;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.lang.reflect.InvocationTargetException;
/**
* Implements the {@code stacktrace} placeholder.
*
* @see Placeholder
* @since v1-alpha0
*/
@SuppressWarnings({ "unused" })
public final class Stacktrace implements Placeholder {
/**
* Contains the {@link Throwable} to use.
*
* @since v1-alpha0
*/
private final @Nullable Throwable throwable;
/**
* Creates and initializes an instance of this event.
*
* @param throwable {@link Throwable} to use
* @since v1-alpha0
*/
public Stacktrace(@Nullable Throwable throwable) {
this.throwable = throwable;
}
/** {@inheritDoc} */
@Override
public @NotNull String replace(@NotNull String text) {
return text.replace("%stacktrace%", throwable == null ? "No stack trace is available." : getFullStackTrace(throwable));
}
/**
* Returns the full stack trace.
*
* @param throwable throwable get the full stack trace of
* @return full stack trace
* @since v1-alpha2
*/
@NotNull
private static String getFullStackTrace(@NotNull Throwable throwable) {
return getFullStackTrace(throwable, new StringBuilder());
}
/**
* Returns the full stack trace.
*
* @param throwable throwable to operate on
* @param stacktrace stacktrace to append and return
* @return full stack trace
* @since v1-alpha2
*/
@NotNull
private static String getFullStackTrace(@NotNull Throwable throwable, @NotNull StringBuilder stacktrace) {
// Add newline
if (!stacktrace.isEmpty())
stacktrace.append("\n");
// Append stack trace
stacktrace
.append(Miscellaneous.getStackTraceHeader(throwable))
.append("\n")
.append(Miscellaneous.getStackTraceAsString(throwable, true));
// Handle throwables which contain other throwables
if (throwable instanceof InvocationTargetException invocationTargetException)
getFullStackTrace(invocationTargetException.getTargetException(), stacktrace);
// Return stack trace
return stacktrace
.toString()
.replace("<", "\\<");
}
}

View file

@ -0,0 +1,71 @@
/*
* STAROPENSOURCE ENGINE SOURCE FILE
* Copyright (c) 2024 The StarOpenSource Engine Authors
* 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.engine.base.internal.implementation.placeholder.crashhandler;
import de.staropensource.engine.base.implementable.Placeholder;
import de.staropensource.engine.base.utility.Miscellaneous;
import org.jetbrains.annotations.NotNull;
import java.util.Map;
/**
* Implements the {@code stacktrace_all} placeholder.
*
* @see Placeholder
* @since v1-alpha0
*/
@SuppressWarnings({ "unused" })
public final class StacktraceAll implements Placeholder {
/**
* Creates and initializes an instance of this event.
*
* @since v1-alpha2
*/
public StacktraceAll() {}
/** {@inheritDoc} */
@Override
public @NotNull String replace(@NotNull String text) {
StringBuilder output = new StringBuilder();
Map<Thread, StackTraceElement[]> stacktraces = Thread.getAllStackTraces();
for (Thread thread : stacktraces.keySet()) {
if (!output.isEmpty())
output.append("\n\n");
output
.append(thread.getName())
.append(" (id=")
.append(thread.threadId())
.append(" priority=")
.append(thread.getPriority())
.append(" group=")
.append(thread.getThreadGroup() == null ? "<unknown>" : thread.getThreadGroup().getName())
.append(" state=")
.append(thread.getState().name())
.append(" daemon=")
.append(thread.isDaemon())
.append(")")
.append("\n")
.append(Miscellaneous.getStackTraceAsString(stacktraces.get(thread), false));
}
return text.replace("%stacktrace_all%", output.toString().replace("<", "\\<"));
}
}

View file

@ -0,0 +1,26 @@
/*
* STAROPENSOURCE ENGINE SOURCE FILE
* Copyright (c) 2024 The StarOpenSource Engine Authors
* 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/>.
*/
/**
* Placeholders used in the {@link de.staropensource.engine.base.logging.CrashHandler}.
*
* @see de.staropensource.engine.base.logging.CrashHandler
* @since v1-alpha0
*/
package de.staropensource.engine.base.internal.implementation.placeholder.crashhandler;

View file

@ -0,0 +1,55 @@
/*
* STAROPENSOURCE ENGINE SOURCE FILE
* Copyright (c) 2024 The StarOpenSource Engine Authors
* 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.engine.base.internal.implementation.placeholder.logger;
import de.staropensource.engine.base.implementable.Placeholder;
import org.jetbrains.annotations.NotNull;
/**
* Implements the {@code log_class} placeholder.
*
* @see Placeholder
* @since v1-alpha0
*/
@SuppressWarnings({ "unused" })
public final class LogClass implements Placeholder {
/**
* Contains the issuer class to use.
*
* @since v1-alpha0
*/
private final @NotNull Class<?> issuerClass;
/**
* Creates and initializes an instance of this event.
*
* @param issuerClass issuer class to use
* @since v1-alpha0
*/
public LogClass(@NotNull Class<?> issuerClass) {
this.issuerClass = issuerClass;
}
/** {@inheritDoc} */
@Override
public @NotNull String replace(@NotNull String text) {
return text.replace("%log_class%", issuerClass.getName().replace(issuerClass.getPackageName() + ".", ""));
}
}

View file

@ -0,0 +1,68 @@
/*
* STAROPENSOURCE ENGINE SOURCE FILE
* Copyright (c) 2024 The StarOpenSource Engine Authors
* 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.engine.base.internal.implementation.placeholder.logger;
import de.staropensource.engine.base.implementable.Placeholder;
import de.staropensource.engine.base.type.logging.LogLevel;
import org.jetbrains.annotations.NotNull;
/**
* Implements the {@code log_color_primary} placeholder.
*
* @see Placeholder
* @since v1-alpha0
*/
@SuppressWarnings({ "unused" })
public final class LogColorPrimary implements Placeholder {
/**
* Contains the {@link LogLevel} to use.
*
* @since v1-alpha0
*/
@NotNull
private final LogLevel level;
/**
* Creates and initializes an instance of this event.
*
* @param level {@link LogLevel} to use
* @since v1-alpha0
*/
public LogColorPrimary(@NotNull LogLevel level) {
this.level = level;
}
/** {@inheritDoc} */
@Override
public @NotNull String replace(@NotNull String text) {
String color;
switch (level) {
case DIAGNOSTIC, VERBOSE -> color = "<fg:blue>";
case SILENT_WARNING, WARNING -> color = "<fg:yellow>";
case INFORMATIONAL -> color = "<fg:white>";
case ERROR -> color = "<fg:red>";
case CRASH -> color = "You should not be seeing this";
default -> color = "";
}
return text.replace("%log_color_primary%", color);
}
}

View file

@ -0,0 +1,64 @@
/*
* STAROPENSOURCE ENGINE SOURCE FILE
* Copyright (c) 2024 The StarOpenSource Engine Authors
* 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.engine.base.internal.implementation.placeholder.logger;
import de.staropensource.engine.base.implementable.Placeholder;
import de.staropensource.engine.base.type.logging.LogLevel;
import org.jetbrains.annotations.NotNull;
/**
* Implements the {@code log_color_secondary} placeholder.
*
* @see Placeholder
* @since v1-alpha0
*/
@SuppressWarnings({ "unused" })
public final class LogColorSecondary implements Placeholder {
/**
* Contains the {@link LogLevel} to use.
*
* @since v1-alpha0
*/
@NotNull
private final LogLevel level;
/**
* Creates and initializes an instance of this event.
*
* @param level {@link LogLevel} to use
* @since v1-alpha0
*/
public LogColorSecondary(@NotNull LogLevel level) {
this.level = level;
}
/** {@inheritDoc} */
@Override
public @NotNull String replace(@NotNull String text) {
String color;
switch (level) {
case DIAGNOSTIC, SILENT_WARNING -> color = "<italic>";
default -> color = "";
}
return text.replace("%log_color_secondary%", color);
}
}

View file

@ -0,0 +1,66 @@
/*
* STAROPENSOURCE ENGINE SOURCE FILE
* Copyright (c) 2024 The StarOpenSource Engine Authors
* 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.engine.base.internal.implementation.placeholder.logger;
import de.staropensource.engine.base.implementable.Placeholder;
import de.staropensource.engine.base.type.logging.LogLevel;
import org.jetbrains.annotations.NotNull;
/**
* Implements the {@code log_level} placeholder.
*
* @see Placeholder
* @since v1-alpha0
*/
@SuppressWarnings({ "unused" })
public final class LogLevelEvent implements Placeholder {
/**
* Contains the {@link LogLevel} to use.
*
* @since v1-alpha0
*/
@NotNull
private final LogLevel level;
/**
* Creates and initializes an instance of this event.
*
* @param level {@link LogLevel} to use
* @since v1-alpha0
*/
public LogLevelEvent(@NotNull LogLevel level) {
this.level = level;
}
/** {@inheritDoc} */
@Override
public @NotNull String replace(@NotNull String text) {
String levelText;
switch (level) {
case CRASH -> levelText = "CRSH";
case ERROR -> levelText = "ERR!";
case SILENT_WARNING -> levelText = "SARN";
default -> levelText = level.name().substring(0, 4);
}
return text.replace("%log_level%", levelText);
}
}

View file

@ -0,0 +1,61 @@
/*
* STAROPENSOURCE ENGINE SOURCE FILE
* Copyright (c) 2024 The StarOpenSource Engine Authors
* 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.engine.base.internal.implementation.placeholder.logger;
import de.staropensource.engine.base.implementable.Placeholder;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* Implements the {@code log_metadata} placeholder.
*
* @see Placeholder
* @since v1-alpha0
*/
@SuppressWarnings({ "unused" })
public final class LogMetadata implements Placeholder {
/**
* Contains the issuer metadata to use
*
* @since v1-alpha0
*/
private final @Nullable String issuerMetadata;
/**
* Creates and initializes an instance of this event.
*
* @param issuerMetadata issuer metadata to use
* @since v1-alpha0
*/
public LogMetadata(@Nullable String issuerMetadata) {
this.issuerMetadata = issuerMetadata;
}
/** {@inheritDoc} */
@Override
public @NotNull String replace(@NotNull String text) {
String replacement = "";
if (issuerMetadata != null && !issuerMetadata.isEmpty())
replacement = "/" + issuerMetadata;
return text.replace("%log_metadata%", replacement);
}
}

View file

@ -0,0 +1,57 @@
/*
* STAROPENSOURCE ENGINE SOURCE FILE
* Copyright (c) 2024 The StarOpenSource Engine Authors
* 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.engine.base.internal.implementation.placeholder.logger;
import de.staropensource.engine.base.implementable.Placeholder;
import org.jetbrains.annotations.NotNull;
import java.util.Locale;
/**
* Implements the {@code log_origin} placeholder.
*
* @see Placeholder
* @since v1-alpha0
*/
@SuppressWarnings({ "unused" })
public final class LogOrigin implements Placeholder {
/**
* Contains the issuer origin to use.
*
* @since v1-alpha0
*/
private final @NotNull String issuerOrigin;
/**
* Creates and initializes an instance of this event.
*
* @param issuerOrigin issuer origin to use
* @since v1-alpha0
*/
public LogOrigin(@NotNull String issuerOrigin) {
this.issuerOrigin = issuerOrigin;
}
/** {@inheritDoc} */
@Override
public @NotNull String replace(@NotNull String text) {
return text.replace("%log_origin%", issuerOrigin.toUpperCase(Locale.ROOT));
}
}

View file

@ -0,0 +1,55 @@
/*
* STAROPENSOURCE ENGINE SOURCE FILE
* Copyright (c) 2024 The StarOpenSource Engine Authors
* 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.engine.base.internal.implementation.placeholder.logger;
import de.staropensource.engine.base.implementable.Placeholder;
import org.jetbrains.annotations.NotNull;
/**
* Implements the {@code log_package} placeholder.
*
* @see Placeholder
* @since v1-alpha0
*/
@SuppressWarnings({ "unused" })
public final class LogPackage implements Placeholder {
/**
* Contains the issuer class to use.
*
* @since v1-alpha0
*/
private final @NotNull Class<?> issuerClass;
/**
* Creates and initializes an instance of this event.
*
* @param issuerClass issuer class to use
* @since v1-alpha0
*/
public LogPackage(@NotNull Class<?> issuerClass) {
this.issuerClass = issuerClass;
}
/** {@inheritDoc} */
@Override
public @NotNull String replace(@NotNull String text) {
return text.replace("%log_package%", issuerClass.getPackageName());
}
}

View file

@ -0,0 +1,56 @@
/*
* STAROPENSOURCE ENGINE SOURCE FILE
* Copyright (c) 2024 The StarOpenSource Engine Authors
* 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.engine.base.internal.implementation.placeholder.logger;
import de.staropensource.engine.base.implementable.Placeholder;
import org.jetbrains.annotations.NotNull;
/**
* Implements the {@code log_path} placeholder.
*
* @see Placeholder
* @since v1-alpha0
*/
@SuppressWarnings({ "unused" })
public final class LogPath implements Placeholder {
/**
* Contains the issuer class to use.
*
* @since v1-alpha0
*/
@NotNull
private final Class<?> issuerClass;
/**
* Creates and initializes an instance of this event.
*
* @param issuerClass issuer class to use
* @since v1-alpha0
*/
public LogPath(@NotNull Class<?> issuerClass) {
this.issuerClass = issuerClass;
}
/** {@inheritDoc} */
@Override
public @NotNull String replace(@NotNull String text) {
return text.replace("%log_path%", issuerClass.getName());
}
}

View file

@ -0,0 +1,26 @@
/*
* STAROPENSOURCE ENGINE SOURCE FILE
* Copyright (c) 2024 The StarOpenSource Engine Authors
* 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/>.
*/
/**
* Placeholders used in the {@link de.staropensource.engine.base.logging.Logger}.
*
* @see de.staropensource.engine.base.logging.Logger
* @since v1-alpha0
*/
package de.staropensource.engine.base.internal.implementation.placeholder.logger;

View file

@ -19,25 +19,33 @@
package de.staropensource.engine.base.internal.type; package de.staropensource.engine.base.internal.type;
import de.staropensource.engine.base.logging.backend.async.LoggingThread; import de.staropensource.engine.base.logging.LoggingThread;
import de.staropensource.engine.base.type.logging.LogLevel; import de.staropensource.engine.base.type.logging.LogLevel;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/** /**
* Represents a queued log call. * Represents a queued log message.
* *
* @param level level of the log call * @param level Level of the log call.
* @param issuer {@link StackTraceElement} of the issuer * @param issuerClass Class of the issuer.
* @param message message * @param issuerOrigin Origin of the issuer.
* @see LoggingThread * @param issuerMetadata Metadata about the issuer.
* @since v1-alpha8 * @param message Message of the log call.
* @see LoggingThread#startThread()
* @since v1-alpha1
*/ */
@SuppressWarnings({ "unused" }) @SuppressWarnings({ "unused" })
public record QueuedLogCall(@NotNull LogLevel level, @NotNull StackTraceElement issuer, @NotNull String message) { public record QueuedLogMessage(@NotNull LogLevel level, @NotNull Class<?> issuerClass, @NotNull String issuerOrigin, @Nullable String issuerMetadata, @NotNull String message) {
/** /**
* Creates and initializes an instance of this class. * Creates and initializes an instance of this class.
* *
* @since v1-alpha8 * @param level level
* @param issuerClass class of the issuer
* @param issuerOrigin origin of the issuer
* @param issuerMetadata metadata about the issuer
* @param message message
* @since v1-alpha1
*/ */
public QueuedLogCall {} public QueuedLogMessage {}
} }

View file

@ -0,0 +1,280 @@
/*
* STAROPENSOURCE ENGINE SOURCE FILE
* Copyright (c) 2024 The StarOpenSource Engine Authors
* 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.engine.base.logging;
import de.staropensource.engine.base.Engine;
import de.staropensource.engine.base.EngineConfiguration;
import de.staropensource.engine.base.implementable.Placeholder;
import de.staropensource.engine.base.event.EngineCrashEvent;
import de.staropensource.engine.base.event.EngineSoftCrashEvent;
import de.staropensource.engine.base.internal.implementation.placeholder.crashhandler.*;
import de.staropensource.engine.base.type.EngineState;
import de.staropensource.engine.base.type.logging.LogLevel;
import de.staropensource.engine.base.utility.PlaceholderEngine;
import lombok.Getter;
import lombok.Setter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
/**
* Handles engine and application crashes.
*
* @see Logger
* @see LoggerInstance
* @since v1-alpha0
*/
@SuppressWarnings({ "JavadocDeclaration" })
public final class CrashHandler {
/**
* Contains the template used to print a crash report.
*
* @since v1-alpha0
* -- GETTER --
* Gets the template used to print a crash report.
*
* @return crash report template
* @since v1-alpha0
* -- SETTER --
* Sets the template used to print a crash report.
*
* @param crashTemplate new crash template
* @since v1-alpha0
*/
@Getter
@Setter
private static @NotNull String crashTemplate = """
<fg:red><bold>
%handled%
------------------------
sos!engine crash
------------------------
Dear user: The application or game you've tried to use seems to have made an oopsie. Please report this to the developer so they can fix it! Thank you \\<3
Dear developer: FIX YOUR GODDAMN SHIT! Please check if your code or 3rd party subsystems are causing problems.
If not, please report it at https://git.staropensource.de/StarOpenSource/Engine/issues. Thank you \\<3
%content%
Dear user: The application or game you've tried to use seems to have made an oopsie. Please report this to the developer so they can fix it! Thank you \\<3
Dear developer: FIX YOUR GODDAMN SHIT! Please check if your code or 3rd party subsystems are causing problems.
If not, please report it at https://git.staropensource.de/StarOpenSource/Engine/issues. Thank you \\<3
------------------------
sos!engine crash
------------------------
%handled%<reset>
""";
/**
* Contains nested {@link LinkedHashMap}s that contain the content printed at the time of a crash.
*
* @since v1-alpha0
* -- GETTER --
* Returns the nested {@link LinkedHashMap}s that contain the content printed at the time of a crash.
*
* @return crash content
* @since v1-alpha0
*/
@Getter
private static final @NotNull LinkedHashMap<@NotNull Object, @NotNull Object> crashContent = new LinkedHashMap<>();
/**
* Creates and initializes an instance of this class.
*
* @since v1-alpha6
*/
private CrashHandler() {}
/**
* Handles a crash.
*
* @param issuerClass class of the issuer
* @param issuerOrigin origin of the issuer
* @param issuerMetadata metadata about the issuer
* @param message crash error detail
* @param throwable simply to provide stacktrace and further insight into the crash, can be set to {@code null}
* @param throwableHandled declares the throwable has handled, not causing the engine to shutdown
* @since v1-alpha0
*/
public static synchronized void handleCrash(@NotNull Class<?> issuerClass, @NotNull String issuerOrigin, @Nullable String issuerMetadata, @NotNull String message, @Nullable Throwable throwable, boolean throwableHandled) {
Engine.getInstance().setState(EngineState.CRASHED); // Update engine state
// Prevent throwable handled warning if set to true but no throwable has been supplied
if (throwable == null)
throwableHandled = false;
// Escape message
message = message
.replace("\n", "\n ")
.replace("\\", "\\\\")
.replace("<", "\\<");
// Replace %content% and %handled%
String base = crashTemplate
.replace("%content%", processCrashContent())
.replace("%handled%", throwableHandled ? "!!! This throwable is declared as handled and has been passed down the execution chain !!!" : "");
// Invoke LoggingAdapter#prePlaceholder
String temp = Logger.getLoggingAdapter().prePlaceholder(LogLevel.CRASH, issuerClass, issuerOrigin, issuerMetadata, message, base);
if (temp != null)
base = temp;
// Create list of temporary placeholders
List<@NotNull Placeholder> temporaryPlaceholders = new ArrayList<>();
temporaryPlaceholders.add(new CrashMessage(message)); // log_message is out of order to allow for placeholder usage
// issuer_*
temporaryPlaceholders.add(new IssuerClass(issuerClass));
temporaryPlaceholders.add(new IssuerOrigin(issuerOrigin));
temporaryPlaceholders.add(new IssuerMetadata(issuerMetadata));
temporaryPlaceholders.add(new IssuerPackage(issuerClass));
temporaryPlaceholders.add(new IssuerPath(issuerClass));
// stacktrace*
temporaryPlaceholders.add(new Stacktrace(throwable));
temporaryPlaceholders.add(new StacktraceAll());
// Replace placeholders
base = PlaceholderEngine.getInstance().process(base, temporaryPlaceholders);
// Invoke LoggingAdapter#postPlaceholder
base = Logger.getLoggingAdapter().postPlaceholder(LogLevel.CRASH, issuerClass, issuerOrigin, issuerMetadata, message, base);
// Print log message by invoking LoggingAdapter#print
Logger.flushLogMessages();
Logger.getLoggingAdapter().print(LogLevel.CRASH, issuerClass, issuerOrigin, issuerMetadata, message, base);
// Emit event
if (throwableHandled)
new EngineSoftCrashEvent().callEvent();
else
new EngineCrashEvent().callEvent();
// Shutdown engine/JVM
if (!throwableHandled)
if (EngineConfiguration.getInstance().isLoggerImmediateShutdown()) {
System.out.println("Halting JVM");
Runtime.getRuntime().halt(69);
} else
Engine.getInstance().shutdown(69);
}
/**
* Internal method for generating the crash message content. Do not call.
*
* @param map {@link LinkedHashMap} to process
* @param indentationSize indentation level
* @return crash content string
* @see #processCrashContent()
* @since v1-alpha0
*/
/*
* Note: This entire method causes a compilation warning as we are using "unchecked or unsecure operations".
* We can safely ignore this as this method
* 1. checks data types before using them,
* 2. only works on Strings, Lists and LinkedHashMaps and not on Files or something which could cause damage, and
* 3. we can trust our own engine and possibly subsystems not doing shit in here.
* As a subsystem developer you'll likely want useful crash information.
*
* But hey, if someone breaks this method (which may be possible idk didn't test it) then congrats!
*/
private static @NotNull String processCrashContent(@NotNull LinkedHashMap<Object, Object> map, int indentationSize) {
StringBuilder content = new StringBuilder();
for (Object key : map.keySet()) {
// Ensure key is of type String
if (!(key instanceof String))
continue;
// Ensure value is of type String, List or LinkedHashMap
if (!(map.get(key) instanceof String
|| map.get(key) instanceof List<?>
|| map.get(key) instanceof LinkedHashMap<?, ?>))
// Invalid content value, skip
continue;
// Add newline
if (!content.isEmpty())
content.append("\n");
// Indent key
if (indentationSize == 0)
content.append("\n");
else
content
.append(" ".repeat(indentationSize))
.append("-> ");
if (map.get(key) == null)
// Append key name, there's no content
// Format: %key%
content.append(key);
else if (map.get(key) instanceof String)
// Append key and it's value
// Format: %key%: %value%
content
.append(key)
.append(": ")
.append(map.get(key));
else if (map.get(key) instanceof List<?>) {
// Append key and list the list's contents
content.append(key);
for (Object item : (List<?>) map.get(key)) {
if (item instanceof String)
item = ((String) item)
.replace("\\", "\\\\")
.replace("\n", "\\n")
.replace("\"", "\\\"");
content
.append("\n")
.append(" ".repeat(indentationSize))
.append(" -> ")
.append(item);
}
} else
// So this one processes a map recursively
// Format:
// -> %parent_key%
// -> %child_key0%: %child_value0%
// -> %child_key1%
// -> %nested_key0%: %nested_value0%
// -> %nested_key1%: %nested_value1%
// -> %child_key2%: %child_value1%
//noinspection unchecked
content.append(key).append("\n").append(processCrashContent((LinkedHashMap<Object, Object>) map.get(key), indentationSize + 1));
}
return content.toString();
}
/**
* Generates the content for a crash report. Processes {@code crashContent} and spits out a String.
*
* @return crash content string
* @since v1-alpha0
*/
public static String processCrashContent() {
return processCrashContent(crashContent, 0);
}
}

View file

@ -0,0 +1,201 @@
/*
* STAROPENSOURCE ENGINE SOURCE FILE
* Copyright (c) 2024 The StarOpenSource Engine Authors
* 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.engine.base.logging;
import de.staropensource.engine.base.EngineConfiguration;
import de.staropensource.engine.base.internal.implementation.placeholder.logger.LogLevelEvent;
import de.staropensource.engine.base.internal.implementation.placeholder.logger.LogPath;
import de.staropensource.engine.base.type.logging.LogLevel;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* Provides logging capabilities during engine startup.
*
* @since v1-alpha4
*/
public final class InitLogger {
/**
* Contains the logging template used during startup.
*
* @since v1-alpha4
*/
private static final @NotNull String template = "%log_level% %log_path% %log_message%";
/**
* Creates and initializes an instance of this class.
*
* @since v1-alpha6
*/
private InitLogger() {}
/**
* {@link Logger#log(LogLevel, Class, String, String, String)} and {@link Logger#processLogMessage(LogLevel, Class, String, String, String)} combined into one method.
*
* @param level level
* @param issuerClass class of the issuer
* @param issuerOrigin origin of the issuer
* @param issuerMetadata metadata about the issuer
* @param message message
* @since v1-alpha4
*/
private static synchronized void log(@NotNull LogLevel level, @NotNull Class<?> issuerClass, @NotNull String issuerOrigin, @Nullable String issuerMetadata, @NotNull String message) {
// Dismiss if level is not allowed
if ((EngineConfiguration.getInstance() != null && level.compareTo(EngineConfiguration.getInstance().getLoggerLevel()) < 0) || level.compareTo(LogLevel.INFORMATIONAL) < 0)
return;
// Invoke LoggingAdapter#prePlaceholder
String base = Logger.getLoggingAdapter().prePlaceholder(level, issuerClass, issuerOrigin, issuerMetadata, message, template);
if (base == null)
base = template;
// Replace placeholders
// This is done manually to avoid depending on PlaceholderEngine
base = new LogLevelEvent(level).replace(base);
base = new LogPath(issuerClass).replace(base);
base = base.replace("%log_message%", message.replace("\n", ""));
// Invoke LoggingAdapter#postPlaceholder
base = Logger.getLoggingAdapter().postPlaceholder(level, issuerClass, issuerOrigin, issuerMetadata, message, base);
// Print log message by invoking LoggingAdapter#print
Logger.getLoggingAdapter().print(level, issuerClass, issuerOrigin, issuerMetadata, message, base);
}
/**
* Prints a diagnostic message.
*
* @param issuerClass class of the issuer
* @param issuerOrigin origin of the issuer
* @param issuerMetadata metadata about the issuer
* @param message message
* @since v1-alpha4
*/
public static void diag(@NotNull Class<?> issuerClass, @NotNull String issuerOrigin, @Nullable String issuerMetadata, @NotNull String message) {
log(LogLevel.DIAGNOSTIC, issuerClass, issuerOrigin, issuerMetadata, message);
}
/**
* Prints a verbose message.
*
* @param issuerClass class of the issuer
* @param issuerOrigin origin of the issuer
* @param issuerMetadata metadata about the issuer
* @param message message
* @since v1-alpha4
*/
public static void verb(@NotNull Class<?> issuerClass, @NotNull String issuerOrigin, @Nullable String issuerMetadata, @NotNull String message) {
log(LogLevel.VERBOSE, issuerClass, issuerOrigin, issuerMetadata, message);
}
/**
* Prints a silent warning message.
*
* @param issuerClass class of the issuer
* @param issuerOrigin origin of the issuer
* @param issuerMetadata metadata about the issuer
* @param message message
* @since v1-alpha4
*/
public static void sarn(@NotNull Class<?> issuerClass, @NotNull String issuerOrigin, @Nullable String issuerMetadata, @NotNull String message) {
log(LogLevel.SILENT_WARNING, issuerClass, issuerOrigin, issuerMetadata, message);
}
/**
* Prints an informational message.
*
* @param issuerClass class of the issuer
* @param issuerOrigin origin of the issuer
* @param issuerMetadata metadata about the issuer
* @param message message
* @since v1-alpha4
*/
public static void info(@NotNull Class<?> issuerClass, @NotNull String issuerOrigin, @Nullable String issuerMetadata, @NotNull String message) {
log(LogLevel.INFORMATIONAL, issuerClass, issuerOrigin, issuerMetadata, message);
}
/**
* Prints a warning message.
*
* @param issuerClass class of the issuer
* @param issuerOrigin origin of the issuer
* @param issuerMetadata metadata about the issuer
* @param message message
* @since v1-alpha4
*/
public static void warn(@NotNull Class<?> issuerClass, @NotNull String issuerOrigin, @Nullable String issuerMetadata, @NotNull String message) {
log(LogLevel.WARNING, issuerClass, issuerOrigin, issuerMetadata, message);
}
/**
* Prints an error message.
*
* @param issuerClass class of the issuer
* @param issuerOrigin origin of the issuer
* @param issuerMetadata metadata about the issuer
* @param message message
* @since v1-alpha4
*/
public static void error(@NotNull Class<?> issuerClass, @NotNull String issuerOrigin, @Nullable String issuerMetadata, @NotNull String message) {
log(LogLevel.ERROR, issuerClass, issuerOrigin, issuerMetadata, message);
}
/**
* Crashes the entire engine.
*
* @param issuerClass class of the issuer
* @param issuerOrigin origin of the issuer
* @param issuerMetadata metadata about the issuer
* @param message message
* @param throwable the throwable that caused this crash
* @param handled declares the throwable has handled, not causing the engine to shutdown
* @since v1-alpha4
*/
public static void crash(@NotNull Class<?> issuerClass, @NotNull String issuerOrigin, @Nullable String issuerMetadata, @NotNull String message, @NotNull Throwable throwable, boolean handled) {
Logger.crash(issuerClass, issuerOrigin, issuerMetadata, message, throwable, handled);
}
/**
* Crashes the entire engine.
*
* @param issuerClass class of the issuer
* @param issuerOrigin origin of the issuer
* @param issuerMetadata metadata about the issuer
* @param message message
* @param throwable the throwable that caused this crash
* @since v1-alpha4
*/
public static void crash(@NotNull Class<?> issuerClass, @NotNull String issuerOrigin, @Nullable String issuerMetadata, @NotNull String message, @NotNull Throwable throwable) {
Logger.crash(issuerClass, issuerOrigin, issuerMetadata, message, throwable);
}
/**
* Crashes the entire engine.
*
* @param issuerClass class of the issuer
* @param issuerOrigin origin of the issuer
* @param issuerMetadata metadata about the issuer
* @param message message
* @since v1-alpha4
*/
public static void crash(@NotNull Class<?> issuerClass, @NotNull String issuerOrigin, @Nullable String issuerMetadata, @NotNull String message) {
Logger.crash(issuerClass, issuerOrigin, issuerMetadata, message);
}
}

View file

@ -19,26 +19,46 @@
package de.staropensource.engine.base.logging; package de.staropensource.engine.base.logging;
import de.staropensource.engine.base.Engine;
import de.staropensource.engine.base.EngineConfiguration;
import de.staropensource.engine.base.event.LogEvent;
import de.staropensource.engine.base.implementable.LoggingAdapter; import de.staropensource.engine.base.implementable.LoggingAdapter;
import de.staropensource.engine.base.implementable.helper.EventHelper;
import de.staropensource.engine.base.implementation.logging.PlainLoggingAdapter; import de.staropensource.engine.base.implementation.logging.PlainLoggingAdapter;
import de.staropensource.engine.base.logging.backend.CrashHandler; import de.staropensource.engine.base.implementation.shortcode.EmptyShortcodeConverter;
import de.staropensource.engine.base.logging.backend.Filterer; import de.staropensource.engine.base.internal.implementation.placeholder.logger.*;
import de.staropensource.engine.base.logging.backend.Processor; import de.staropensource.engine.base.internal.type.QueuedLogMessage;
import de.staropensource.engine.base.logging.backend.async.LoggingQueue;
import de.staropensource.engine.base.type.logging.LogLevel; import de.staropensource.engine.base.type.logging.LogLevel;
import de.staropensource.engine.base.type.logging.LogRule;
import de.staropensource.engine.base.type.logging.LogRuleType;
import de.staropensource.engine.base.utility.PlaceholderEngine;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import org.intellij.lang.annotations.RegExp;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
/** /**
* The frontend class for sos!engine's logging system. * Provides the engine's logging infrastructure, except for
* crash handling, which is handled by {@link CrashHandler}.
* *
* @since v1-alpha8 * @see LoggerInstance
* @see CrashHandler
* @since v1-alpha0
*/ */
@SuppressWarnings({ "JavadocDeclaration" }) @SuppressWarnings({ "JavadocDeclaration" })
public final class Logger { public final class Logger {
/**
* Contains a list of {@link QueuedLogMessage}s.
*
* @since v1-alpha1
*/
private static final LinkedList<QueuedLogMessage> queuedMessages = new LinkedList<>();
/** /**
* Refers to the active {@link LoggingAdapter} that is used to process and print log messages. * Refers to the active {@link LoggingAdapter} that is used to process and print log messages.
* *
@ -62,393 +82,286 @@ public final class Logger {
private static @NotNull LoggingAdapter loggingAdapter = new PlainLoggingAdapter(); private static @NotNull LoggingAdapter loggingAdapter = new PlainLoggingAdapter();
/** /**
* Contains if messages shall be * Contains all active {@link LogRule}s.
* sanitized during processing.
* <p>
* Enabling this flag will prevent
* tag usage and with that formatting.
* <p>
* Recommended to leave enabled. Only
* disable if you are developing a
* command line application.
* *
* @since v1-alpha0 * @since v1-alpha1
* -- GETTER -- * -- GETTER --
* Returns if messages shall be * Returns all active {@link LogRule}s.
* sanitized during processing.
* <p>
* Enabling this flag will prevent
* tag usage and with that formatting.
* <p>
* Recommended to leave enabled. Only
* disable if you are developing a
* command line application.
* *
* @return sanitize messages? * @return active rules
* @since v1-alpha0 * @since v1-alpha1
* -- SETTER --
* Sets if messages shall be
* sanitized during processing.
* <p>
* Enabling this flag will prevent
* tag usage and with that formatting.
* <p>
* Recommended to leave enabled. Only
* disable if you are developing a
* command line application.
*
* @param sanitizeMessages sanitize messages?
* @since v1-alpha0
*/ */
@Getter @Getter
@Setter private static final List<LogRule> activeRules = new ArrayList<>();
private static boolean sanitizeMessages = true;
/** /**
* Creates and initializes an instance of this class * Creates and initializes an instance of this class.
* *
* @since v1-alpha8 * @since v1-alpha6
*/ */
private Logger() {} private Logger() {}
// -----> Internal management methods
// These methods forward calls to internal methods so
// these can be accessed without exporting a whole package.
/** /**
* Handles incoming log calls and either * Prints all queued log messages.
* processes them directly or queues them in.
* <p>
* **This is an internal method. Use with care.**
* *
* @param level level of the log call * @since v1-alpha1
* @param issuer {@link StackTraceElement} of the issuer
* @param message message
* @since v1-alpha8
*/ */
public static void handle(@NotNull LogLevel level, @NotNull StackTraceElement issuer, @NotNull String message) { public static synchronized void flushLogMessages() {
Processor.handle(level, issuer, message); // Only execute code if queued messages list is not empty
if (!queuedMessages.isEmpty()) {
// Clone queued messages and clear original to avoid issues
//noinspection unchecked
LinkedList<QueuedLogMessage> queuedMessagesCloned = (LinkedList<QueuedLogMessage>) queuedMessages.clone();
queuedMessages.clear();
// Invoke processLogMessage method for every queued message
for (QueuedLogMessage queuedLogMessage : queuedMessagesCloned) {
processLogMessage(queuedLogMessage.level(), queuedLogMessage.issuerClass(), queuedLogMessage.issuerOrigin(), queuedLogMessage.issuerMetadata(), queuedLogMessage.message());
}
}
} }
/** /**
* Flushes the logging queue. * Processes a log message.
* <p>
* **This is an internal method. Use with care.**
* *
* @since v1-alpha8 * @param level level
* @param issuerClass class of the issuer
* @param issuerOrigin origin of the issuer
* @param issuerMetadata metadata about the issuer
* @param message message
* @since v1-alpha1
*/ */
public static void flush() { private static void processLogMessage(@NotNull LogLevel level, @NotNull Class<?> issuerClass, @NotNull String issuerOrigin, @Nullable String issuerMetadata, @NotNull String message) {
LoggingQueue.flush(); // Evaluate active rules
for (LogRule rule : activeRules) {
if (rule.evaluate(level, issuerClass, issuerOrigin, issuerMetadata, message)) {
if (rule.getType() == LogRuleType.WHITELIST) break; // Continue processing
if (rule.getType() == LogRuleType.BLACKLIST) return; // Cancel processing
}
}
// Invoke LoggingAdapter#prePlaceholder
String format = loggingAdapter.prePlaceholder(level, issuerClass, issuerOrigin, issuerMetadata, message, EngineConfiguration.getInstance().getLoggerTemplate());
if (format == null)
format = EngineConfiguration.getInstance().getLoggerTemplate();
// Replace placeholders using PlaceholderEngine
format = PlaceholderEngine.getInstance().process(format);
// Replace logger placeholders (no colors)
format = new LogClass(issuerClass).replace(format);
format = new LogLevelEvent(level).replace(format);
format = new LogMetadata(issuerMetadata).replace(format);
format = new LogOrigin(issuerOrigin).replace(format);
format = new LogPackage(issuerClass).replace(format);
format = new LogPath(issuerClass).replace(format);
// Handle newlines
if (EngineConfiguration.getInstance().isLoggerEnableNewlineSupport() && message.contains("\n"))
try (Scanner scanner = new Scanner(message)) {
int indexPrefix = format.indexOf("%log_message_prefix%");
int indexMessage = format.indexOf("%log_message%");
StringBuilder formatNew = new StringBuilder();
String prefix;
String suffix;
String formatShadow;
if (indexMessage == -1)
crash(Logger.class, "ENGINE", null, "The log format must contain %log_message%");
prefix = indexPrefix == -1 ? "" : format.substring(indexPrefix + 20, indexMessage);
suffix = format.substring(indexMessage + 13);
formatShadow = " "
.repeat(new EmptyShortcodeConverter(
format.substring(0, indexPrefix == -1 ? indexMessage : indexPrefix)
.replace("%log_color_primary%", "")
.replace("%log_color_secondary%", ""), false
).getClean().length());
while (scanner.hasNextLine()) {
if (formatNew.isEmpty())
formatNew.append(format, 0, indexPrefix == -1 ? indexMessage : indexPrefix);
else
formatNew
.append("\n")
.append(formatShadow);
formatNew
.append(prefix)
.append(scanner.nextLine())
.append(suffix);
}
format = formatNew.toString();
}
else
// No newline found, use performance-efficient replacing
format = format
.replace("%log_message_prefix%", "")
.replace("%log_message%", message.replace("\n", "\\n"));
// Replace placeholders involving colors
format = new LogColorPrimary(level).replace(format);
format = new LogColorSecondary(level).replace(format);
// Replace placeholders using PlaceholderEngine again
format = PlaceholderEngine.getInstance().process(format);
// Invoke LoggingAdapter#postPlaceholder
format = loggingAdapter.postPlaceholder(level, issuerClass, issuerOrigin, issuerMetadata, message, format);
// Call event
if (!(issuerClass.getName().equals("de.staropensource.engine.slf4j_compat.CompatibilityLogger")
|| issuerClass.equals(EventHelper.class)))
new LogEvent().callEvent(level, issuerClass, issuerOrigin, issuerMetadata, message);
// Print log message by invoking LoggingAdapter#print
loggingAdapter.print(level, issuerClass, issuerOrigin, issuerMetadata, message, format);
} }
/** /**
* Disallows one or multiple classes. * Handler for all log messages.
* *
* @param regex regex * @param level level
* @since v1-alpha8 * @param issuerClass class of the issuer
* @param issuerOrigin origin of the issuer
* @param issuerMetadata metadata about the issuer
* @param message message
* @since v1-alpha0
*/ */
public static void disallowClass(@RegExp @NotNull String regex) { private static synchronized void log(@NotNull LogLevel level, @NotNull Class<?> issuerClass, @NotNull String issuerOrigin, @Nullable String issuerMetadata, @NotNull String message) {
Filterer.disallowClass(regex); // Check if engine has initialized
if (Engine.getInstance() == null) return;
// Dismiss if level is not allowed
if (level.compareTo(EngineConfiguration.getInstance().getLoggerLevel()) < 0)
return;
if (EngineConfiguration.getInstance().isOptimizeLogging())
// Optimizations enabled, add to message queue
queuedMessages.add(new QueuedLogMessage(level, issuerClass, issuerOrigin, issuerMetadata, message));
else
// Optimizations disabled, print right away
processLogMessage(level, issuerClass, issuerOrigin, issuerMetadata, message);
} }
/**
* Disallows one or multiple modules.
*
* @param regex regex
* @since v1-alpha8
*/
public static void disallowModule(@RegExp @NotNull String regex) {
Filterer.disallowModule(regex);
}
/**
* Disallows one or multiple messages.
*
* @param regex regex
* @since v1-alpha8
*/
public static void disallowMessage(@RegExp @NotNull String regex) {
Filterer.disallowMessage(regex);
}
// -----> Redirection methods
/**
* Redirects regular log messages.
*
* @param level level of the log call
* @param message message
* @since v1-alpha8
*/
public static void redirectCall(@NotNull LogLevel level, @NotNull String message) {
Processor.handle(level, Thread.currentThread().getStackTrace()[3], message);
}
/**
* Redirects crash calls.
*
* @param message message
* @param throwable {@link Throwable} which caused the error
* @param fatal if to terminate the engine
* @since v1-alpha8
*/
public static void redirectCall(@NotNull String message, @Nullable Throwable throwable, boolean fatal) {
CrashHandler.handleCrash(Thread.currentThread().getStackTrace()[3], message, throwable, fatal);
}
// -----> Frontend methods
/**
* Logs a diagnostic message.
*
* @param message message to log
* @since v1-alpha8
*/
public static void diag(@NotNull String message) {
redirectCall(LogLevel.DIAGNOSTIC, message);
}
/**
* Logs a verbose message.
*
* @param message message to log
* @since v1-alpha8
*/
public static void verb(@NotNull String message) {
redirectCall(LogLevel.VERBOSE, message);
}
/**
* Logs a silent warning.
*
* @param message message to log
* @since v1-alpha8
*/
public static void sarn(@NotNull String message) {
redirectCall(LogLevel.SILENT_WARNING, message);
}
/**
* Logs an informational message.
*
* @param message message to log
* @since v1-alpha8
*/
public static void info(@NotNull String message) {
redirectCall(LogLevel.INFORMATIONAL, message);
}
/**
* Logs a warning.
*
* @param message message to log
* @since v1-alpha8
*/
public static void warn(@NotNull String message) {
redirectCall(LogLevel.WARNING, message);
}
/**
* Logs an error.
*
* @param message message to log
* @since v1-alpha8
*/
public static void error(@NotNull String message) {
redirectCall(LogLevel.ERROR, message);
}
/**
* Prints a crash report and optionally crashes the engine.
*
* @param message message to log
* @param throwable {@link Throwable} which caused the crash
* @param fatal terminates the engine if {@code true}
* @since v1-alpha8
*/
public static void crash(@NotNull String message, @NotNull Throwable throwable, boolean fatal) {
redirectCall(message, throwable, fatal);
}
/**
* Prints a crash report and optionally crashes the engine.
*
* @param message message to log
* @param throwable {@link Throwable} which caused the crash
* @since v1-alpha8
*/
public static void crash(@NotNull String message, @NotNull Throwable throwable) {
redirectCall(message, throwable, true);
}
/**
* Prints a crash report and optionally crashes the engine.
*
* @param message message to log
* @param fatal terminates the engine if {@code true}
* @since v1-alpha8
*/
public static void crash(@NotNull String message, boolean fatal) {
redirectCall(message, null, fatal);
}
/**
* Prints a crash report and optionally crashes the engine.
*
* @param message message to log
* @since v1-alpha8
*/
public static void crash(@NotNull String message) {
redirectCall(message, null, true);
}
// -----> Legacy frontend methods
// This improves compatibility with old code
// still using the old logger frontend.
/** /**
* Prints a diagnostic message. * Prints a diagnostic message.
* *
* @param ignoredIssuerClass class of the issuer * @param issuerClass class of the issuer
* @param ignoredIssuerOrigin origin of the issuer * @param issuerOrigin origin of the issuer
* @param ignoredIssuerMetadata metadata about the issuer * @param issuerMetadata metadata about the issuer
* @param message message * @param message message
* @deprecated The old logging system has been deprecated and replaced by a new one
* @see #diag(String)
* @since v1-alpha0 * @since v1-alpha0
*/ */
@Deprecated(forRemoval = true) public static void diag(@NotNull Class<?> issuerClass, @NotNull String issuerOrigin, @Nullable String issuerMetadata, @NotNull String message) {
public static void diag(@NotNull Class<?> ignoredIssuerClass, @NotNull String ignoredIssuerOrigin, @Nullable String ignoredIssuerMetadata, @NotNull String message) { log(LogLevel.DIAGNOSTIC, issuerClass, issuerOrigin, issuerMetadata, message);
redirectCall(LogLevel.DIAGNOSTIC, message);
} }
/** /**
* Prints a verbose message. * Prints a verbose message.
* *
* @param ignoredIssuerClass class of the issuer * @param issuerClass class of the issuer
* @param ignoredIssuerOrigin origin of the issuer * @param issuerOrigin origin of the issuer
* @param ignoredIssuerMetadata metadata about the issuer * @param issuerMetadata metadata about the issuer
* @param message message * @param message message
* @deprecated The old logging system has been deprecated and replaced by a new one
* @see #verb(String)
* @since v1-alpha0 * @since v1-alpha0
*/ */
@Deprecated(forRemoval = true) public static void verb(@NotNull Class<?> issuerClass, @NotNull String issuerOrigin, @Nullable String issuerMetadata, @NotNull String message) {
public static void verb(@NotNull Class<?> ignoredIssuerClass, @NotNull String ignoredIssuerOrigin, @Nullable String ignoredIssuerMetadata, @NotNull String message) { log(LogLevel.VERBOSE, issuerClass, issuerOrigin, issuerMetadata, message);
redirectCall(LogLevel.VERBOSE, message);
} }
/** /**
* Prints a silent warning message. * Prints a silent warning message.
* *
* @param ignoredIssuerClass class of the issuer * @param issuerClass class of the issuer
* @param ignoredIssuerOrigin origin of the issuer * @param issuerOrigin origin of the issuer
* @param ignoredIssuerMetadata metadata about the issuer * @param issuerMetadata metadata about the issuer
* @param message message * @param message message
* @deprecated The old logging system has been deprecated and replaced by a new one
* @see #sarn(String)
* @since v1-alpha0 * @since v1-alpha0
*/ */
@Deprecated(forRemoval = true) public static void sarn(@NotNull Class<?> issuerClass, @NotNull String issuerOrigin, @Nullable String issuerMetadata, @NotNull String message) {
public static void sarn(@NotNull Class<?> ignoredIssuerClass, @NotNull String ignoredIssuerOrigin, @Nullable String ignoredIssuerMetadata, @NotNull String message) { log(LogLevel.SILENT_WARNING, issuerClass, issuerOrigin, issuerMetadata, message);
redirectCall(LogLevel.SILENT_WARNING, message);
} }
/** /**
* Prints an informational message. * Prints an informational message.
* *
* @param ignoredIssuerClass class of the issuer * @param issuerClass class of the issuer
* @param ignoredIssuerOrigin origin of the issuer * @param issuerOrigin origin of the issuer
* @param ignoredIssuerMetadata metadata about the issuer * @param issuerMetadata metadata about the issuer
* @param message message * @param message message
* @deprecated The old logging system has been deprecated and replaced by a new one
* @see #info(String)
* @since v1-alpha0 * @since v1-alpha0
*/ */
@Deprecated(forRemoval = true) public static void info(@NotNull Class<?> issuerClass, @NotNull String issuerOrigin, @Nullable String issuerMetadata, @NotNull String message) {
public static void info(@NotNull Class<?> ignoredIssuerClass, @NotNull String ignoredIssuerOrigin, @Nullable String ignoredIssuerMetadata, @NotNull String message) { log(LogLevel.INFORMATIONAL, issuerClass, issuerOrigin, issuerMetadata, message);
redirectCall(LogLevel.INFORMATIONAL, message);
} }
/** /**
* Prints a warning message. * Prints a warning message.
* *
* @param ignoredIssuerClass class of the issuer * @param issuerClass class of the issuer
* @param ignoredIssuerOrigin origin of the issuer * @param issuerOrigin origin of the issuer
* @param ignoredIssuerMetadata metadata about the issuer * @param issuerMetadata metadata about the issuer
* @param message message * @param message message
* @deprecated The old logging system has been deprecated and replaced by a new one
* @see #warn(String)
* @since v1-alpha0 * @since v1-alpha0
*/ */
@Deprecated(forRemoval = true) public static void warn(@NotNull Class<?> issuerClass, @NotNull String issuerOrigin, @Nullable String issuerMetadata, @NotNull String message) {
public static void warn(@NotNull Class<?> ignoredIssuerClass, @NotNull String ignoredIssuerOrigin, @Nullable String ignoredIssuerMetadata, @NotNull String message) { log(LogLevel.WARNING, issuerClass, issuerOrigin, issuerMetadata, message);
redirectCall(LogLevel.WARNING, message);
} }
/** /**
* Prints an error message. * Prints an error message.
* *
* @param ignoredIssuerClass class of the issuer * @param issuerClass class of the issuer
* @param ignoredIssuerOrigin origin of the issuer * @param issuerOrigin origin of the issuer
* @param ignoredIssuerMetadata metadata about the issuer * @param issuerMetadata metadata about the issuer
* @param message message * @param message message
* @deprecated The old logging system has been deprecated and replaced by a new one
* @see #error(String)
* @since v1-alpha0 * @since v1-alpha0
*/ */
@Deprecated(forRemoval = true) public static void error(@NotNull Class<?> issuerClass, @NotNull String issuerOrigin, @Nullable String issuerMetadata, @NotNull String message) {
public static void error(@NotNull Class<?> ignoredIssuerClass, @NotNull String ignoredIssuerOrigin, @Nullable String ignoredIssuerMetadata, @NotNull String message) { log(LogLevel.ERROR, issuerClass, issuerOrigin, issuerMetadata, message);
redirectCall(LogLevel.ERROR, message);
} }
/** /**
* Crashes the entire engine. * Crashes the entire engine.
* *
* @param ignoredIssuerClass class of the issuer * @param issuerClass class of the issuer
* @param ignoredIssuerOrigin origin of the issuer * @param issuerOrigin origin of the issuer
* @param ignoredIssuerMetadata metadata about the issuer * @param issuerMetadata metadata about the issuer
* @param message message * @param message message
* @param throwable the throwable that caused this crash * @param throwable the throwable that caused this crash
* @param handled declares the throwable has handled, not causing the engine to shutdown * @param handled declares the throwable has handled, not causing the engine to shutdown
* @deprecated The old logging system has been deprecated and replaced by a new one
* @see #crash(String, Throwable, boolean)
* @since v1-alpha0 * @since v1-alpha0
*/ */
@Deprecated(forRemoval = true) public static void crash(@NotNull Class<?> issuerClass, @NotNull String issuerOrigin, @Nullable String issuerMetadata, @NotNull String message, @NotNull Throwable throwable, boolean handled) {
public static void crash(@NotNull Class<?> ignoredIssuerClass, @NotNull String ignoredIssuerOrigin, @Nullable String ignoredIssuerMetadata, @NotNull String message, @NotNull Throwable throwable, boolean handled) { CrashHandler.handleCrash(issuerClass, issuerOrigin, issuerMetadata, message, throwable, handled);
redirectCall(message, throwable, !handled);
} }
/** /**
* Crashes the entire engine. * Crashes the entire engine.
* *
* @param ignoredIssuerClass class of the issuer * @param issuerClass class of the issuer
* @param ignoredIssuerOrigin origin of the issuer * @param issuerOrigin origin of the issuer
* @param ignoredIssuerMetadata metadata about the issuer * @param issuerMetadata metadata about the issuer
* @param message message * @param message message
* @param throwable the throwable that caused this crash * @param throwable the throwable that caused this crash
* @deprecated The old logging system has been deprecated and replaced by a new one
* @see #crash(String, Throwable)
* @since v1-alpha0 * @since v1-alpha0
*/ */
@Deprecated(forRemoval = true) public static void crash(@NotNull Class<?> issuerClass, @NotNull String issuerOrigin, @Nullable String issuerMetadata, @NotNull String message, @NotNull Throwable throwable) {
public static void crash(@NotNull Class<?> ignoredIssuerClass, @NotNull String ignoredIssuerOrigin, @Nullable String ignoredIssuerMetadata, @NotNull String message, @NotNull Throwable throwable) { CrashHandler.handleCrash(issuerClass, issuerOrigin, issuerMetadata, message, throwable, false);
redirectCall(message, throwable, true);
} }
/** /**
* Crashes the entire engine. * Crashes the entire engine.
* *
* @param ignoredIssuerClass class of the issuer * @param issuerClass class of the issuer
* @param ignoredIssuerOrigin origin of the issuer * @param issuerOrigin origin of the issuer
* @param ignoredIssuerMetadata metadata about the issuer * @param issuerMetadata metadata about the issuer
* @param message message * @param message message
* @deprecated The old logging system has been deprecated and replaced by a new one
* @see #crash(String)
* @since v1-alpha0 * @since v1-alpha0
*/ */
@Deprecated(forRemoval = true) public static void crash(@NotNull Class<?> issuerClass, @NotNull String issuerOrigin, @Nullable String issuerMetadata, @NotNull String message) {
public static void crash(@NotNull Class<?> ignoredIssuerClass, @NotNull String ignoredIssuerOrigin, @Nullable String ignoredIssuerMetadata, @NotNull String message) { CrashHandler.handleCrash(issuerClass, issuerOrigin, issuerMetadata, message, null, false);
redirectCall(message, null, true);
} }
} }

View file

@ -19,29 +19,69 @@
package de.staropensource.engine.base.logging; package de.staropensource.engine.base.logging;
import de.staropensource.engine.base.Engine;
import de.staropensource.engine.base.type.EngineState;
import lombok.Getter; import lombok.Getter;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
/** /**
* Legacy frontend. * Removes the need to add {@code getClass} to all calls to {@link Logger}.
* <p>
* This improves compatibility with old code
* still using the old logger frontend.
* *
* @deprecated The old logging system has been deprecated and replaced by a new one * @since v1-alpha0
* @see Logger
* @since v1-alpha8
*/ */
@Getter @Getter
@Deprecated(forRemoval = true) @SuppressWarnings({ "JavadocDeclaration" })
public final class LoggerInstance { public final class LoggerInstance {
/**
* Contains the {@link Class} of the issuer.
*
* @since v1-alpha2
* -- GETTER --
* Returns the {@link Class} of the issuer.
*
* @return class of the issuer
* @since v1-alpha2
*/
private final @NotNull Class<?> clazz;
/**
* Contains the metadata about the issuer.
*
* @since v1-alpha2
* -- GETTER --
* Returns the metadata about the issuer.
*
* @return metadata about the issuer
* @since v1-alpha2
*/
private final @Nullable String metadata;
/**
* Contains the origin of the issuer.
*
* @since v1-alpha2
* -- GETTER --
* Returns the origin of the issuer.
*
* @return origin of the issuer
* @since v1-alpha2
*/
private final @NotNull String origin;
/** /**
* Creates and initializes an instance of this class. * Creates and initializes an instance of this class.
* *
* @since v1-alpha8 * @param clazz class of the issuer
* @param origin origin of the issuer
* @param metadata metadata about the issuer
* @since v1-alpha0
*/ */
private LoggerInstance() {} private LoggerInstance(@NotNull Class<?> clazz, @NotNull String origin, @Nullable String metadata) {
this.clazz = clazz;
this.origin = origin;
this.metadata = metadata;
}
/** /**
* Prints a diagnostic message. * Prints a diagnostic message.
@ -50,7 +90,10 @@ public final class LoggerInstance {
* @since v1-alpha0 * @since v1-alpha0
*/ */
public void diag(@NotNull String message) { public void diag(@NotNull String message) {
Logger.diag(message); if (Engine.getInstance().getState() == EngineState.EARLY_STARTUP)
InitLogger.diag(clazz, origin, metadata, message);
else
Logger.diag(clazz, origin, metadata, message);
} }
/** /**
@ -60,7 +103,10 @@ public final class LoggerInstance {
* @since v1-alpha0 * @since v1-alpha0
*/ */
public void verb(@NotNull String message) { public void verb(@NotNull String message) {
Logger.verb(message); if (Engine.getInstance().getState() == EngineState.EARLY_STARTUP)
InitLogger.verb(clazz, origin, metadata, message);
else
Logger.verb(clazz, origin, metadata, message);
} }
/** /**
@ -70,7 +116,10 @@ public final class LoggerInstance {
* @since v1-alpha0 * @since v1-alpha0
*/ */
public void sarn(@NotNull String message) { public void sarn(@NotNull String message) {
Logger.sarn(message); if (Engine.getInstance().getState() == EngineState.EARLY_STARTUP)
InitLogger.sarn(clazz, origin, metadata, message);
else
Logger.sarn(clazz, origin, metadata, message);
} }
/** /**
@ -80,7 +129,10 @@ public final class LoggerInstance {
* @since v1-alpha0 * @since v1-alpha0
*/ */
public void info(@NotNull String message) { public void info(@NotNull String message) {
Logger.info(message); if (Engine.getInstance().getState() == EngineState.EARLY_STARTUP)
InitLogger.info(clazz, origin, metadata, message);
else
Logger.info(clazz, origin, metadata, message);
} }
/** /**
@ -90,7 +142,10 @@ public final class LoggerInstance {
* @since v1-alpha0 * @since v1-alpha0
*/ */
public void warn(@NotNull String message) { public void warn(@NotNull String message) {
Logger.warn(message); if (Engine.getInstance().getState() == EngineState.EARLY_STARTUP)
InitLogger.warn(clazz, origin, metadata, message);
else
Logger.warn(clazz, origin, metadata, message);
} }
/** /**
@ -100,7 +155,10 @@ public final class LoggerInstance {
* @since v1-alpha0 * @since v1-alpha0
*/ */
public void error(@NotNull String message) { public void error(@NotNull String message) {
Logger.error(message); if (Engine.getInstance().getState() == EngineState.EARLY_STARTUP)
InitLogger.error(clazz, origin, metadata, message);
else
Logger.error(clazz, origin, metadata, message);
} }
/** /**
@ -109,10 +167,11 @@ public final class LoggerInstance {
* @param message message * @param message message
* @param throwable throwable that caused the crash * @param throwable throwable that caused the crash
* @param handled declares the throwable has handled * @param handled declares the throwable has handled
* @see CrashHandler
* @since v1-alpha0 * @since v1-alpha0
*/ */
public void crash(@NotNull String message, @NotNull Throwable throwable, boolean handled) { public void crash(@NotNull String message, @NotNull Throwable throwable, boolean handled) {
Logger.crash(message, throwable, !handled); Logger.crash(clazz, origin, metadata, message, throwable, handled);
} }
/** /**
@ -120,50 +179,52 @@ public final class LoggerInstance {
* *
* @param message message * @param message message
* @param throwable throwable that caused the crash * @param throwable throwable that caused the crash
* @see CrashHandler
* @since v1-alpha0 * @since v1-alpha0
*/ */
public void crash(@NotNull String message, @NotNull Throwable throwable) { public void crash(@NotNull String message, @NotNull Throwable throwable) {
Logger.crash(message, throwable); Logger.crash(clazz, origin, metadata, message, throwable);
} }
/** /**
* Crashes the entire engine. * Crashes the entire engine.
* *
* @param message message * @param message message
* @see CrashHandler
* @since v1-alpha0 * @since v1-alpha0
*/ */
public void crash(@NotNull String message) { public void crash(@NotNull String message) {
Logger.crash(message); Logger.crash(clazz, origin, metadata, message);
} }
/** /**
* Provides an API for building {@link LoggerInstance}s more easily. * Provides an API for building {@link LoggerInstance}s more easily.
* *
* @deprecated The old logging system has been deprecated and replaced by a new one * @since v1-alpha2
* @see Logger
* @since v1-alpha8
*/ */
@Deprecated(forRemoval = true)
@SuppressWarnings({ "unused" }) @SuppressWarnings({ "unused" })
public static final class Builder { public static final class Builder {
/** /**
* Contains the class of the issuer. * Contains the class of the issuer.
* *
* @since v1-alpha8 * @see LoggerInstance#clazz
* @since v1-alpha2
*/ */
private @Nullable Class<?> clazz = null; private @Nullable Class<?> clazz = null;
/** /**
* Contains the origin of the issuer. * Contains the origin of the issuer.
* *
* @since v1-alpha8 * @see LoggerInstance#origin
* @since v1-alpha2
*/ */
private @Nullable String origin = null; private @Nullable String origin = null;
/** /**
* Contains metadata about the issuer. * Contains metadata about the issuer.
* *
* @since v1-alpha8 * @see LoggerInstance#metadata
* @since v1-alpha2
*/ */
private @Nullable String metadata = null; private @Nullable String metadata = null;
@ -192,13 +253,14 @@ public final class LoggerInstance {
if (metadata == null || metadata.isBlank()) if (metadata == null || metadata.isBlank())
metadata = null; metadata = null;
return new LoggerInstance(); return new LoggerInstance(clazz, origin, metadata);
} }
/** /**
* Returns the class of the issuer. * Returns the class of the issuer.
* *
* @return class of the issuer * @return class of the issuer
* @see LoggerInstance#clazz
* @since v1-alpha2 * @since v1-alpha2
*/ */
public @Nullable Class<?> getClazz() { public @Nullable Class<?> getClazz() {
@ -209,6 +271,7 @@ public final class LoggerInstance {
* Returns the origin of the issuer. * Returns the origin of the issuer.
* *
* @return origin of the issuer * @return origin of the issuer
* @see LoggerInstance#origin
* @since v1-alpha2 * @since v1-alpha2
*/ */
public @Nullable String getOrigin() { public @Nullable String getOrigin() {
@ -219,6 +282,7 @@ public final class LoggerInstance {
* Returns metadata about the issuer. * Returns metadata about the issuer.
* *
* @return metadata about the issuer * @return metadata about the issuer
* @see LoggerInstance#metadata
* @since v1-alpha2 * @since v1-alpha2
*/ */
public @Nullable String getMetadata() { public @Nullable String getMetadata() {
@ -230,6 +294,7 @@ public final class LoggerInstance {
* *
* @param clazz new class of the issuer * @param clazz new class of the issuer
* @return builder instance * @return builder instance
* @see LoggerInstance#clazz
* @since v1-alpha2 * @since v1-alpha2
*/ */
public @NotNull Builder setClazz(@Nullable Class<?> clazz) { public @NotNull Builder setClazz(@Nullable Class<?> clazz) {
@ -242,6 +307,7 @@ public final class LoggerInstance {
* *
* @param origin new origin of the issuer * @param origin new origin of the issuer
* @return builder instance * @return builder instance
* @see LoggerInstance#origin
* @since v1-alpha2 * @since v1-alpha2
*/ */
public @NotNull Builder setOrigin(@Nullable String origin) { public @NotNull Builder setOrigin(@Nullable String origin) {
@ -254,6 +320,7 @@ public final class LoggerInstance {
* *
* @param metadata new metadata about the issuer * @param metadata new metadata about the issuer
* @return builder instance * @return builder instance
* @see LoggerInstance#metadata
* @since v1-alpha2 * @since v1-alpha2
*/ */
public @NotNull Builder setMetadata(@Nullable String metadata) { public @NotNull Builder setMetadata(@Nullable String metadata) {

View file

@ -0,0 +1,144 @@
/*
* STAROPENSOURCE ENGINE SOURCE FILE
* Copyright (c) 2024 The StarOpenSource Engine Authors
* 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.engine.base.logging;
import de.staropensource.engine.base.Engine;
import de.staropensource.engine.base.EngineConfiguration;
import de.staropensource.engine.base.type.EngineState;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
/**
* Controls the logging thread of the engine.
*
* @since v1-alpha4
*/
@Slf4j
public class LoggingThread {
/**
* Contains the {@link LoggerInstance} for this instance.
*
* @see LoggerInstance
* @since v1-alpha4
*/
protected static final @NotNull LoggerInstance logger = new LoggerInstance.Builder().setClazz(LoggingThread.class).setOrigin("ENGINE").build();
/**
* Contains the logic of the logging thread.
*
* @since v1-alpha4
*/
private static final @NotNull Runnable loggingThreadLogic = () -> {
while (!Thread.currentThread().isInterrupted()) {
// Stop thread when engine is shutting down
if (Engine.getInstance().getState() == EngineState.SHUTDOWN
|| Engine.getInstance().getState() == EngineState.CRASHED
|| !EngineConfiguration.getInstance().isOptimizeLogging())
return;
// Process all log messages
Logger.flushLogMessages();
// Sleep for whatever has been configured
if (EngineConfiguration.getInstance().getLoggerPollingSpeed() > 0) {
long sleepDuration = System.currentTimeMillis() + EngineConfiguration.getInstance().getLoggerPollingSpeed();
while (System.currentTimeMillis() < sleepDuration)
Thread.onSpinWait();
}
}
};
/**
* Contains a reference to the logging thread.
*
* @since v1-alpha4
*/
private static Thread loggingThread;
/**
* Creates and initializes an instance of this class
*
* @since v1-alpha4
*/
private LoggingThread() {}
/**
* Reconstructs the {@link #loggingThread} thread.
*
* @throws IllegalStateException if the logging thread's state is not {@link Thread.State#TERMINATED}
* @since v1-alpha4
*/
private static void reconstructThread() throws IllegalStateException {
if (loggingThread != null && loggingThread.getState() != Thread.State.TERMINATED)
throw new IllegalStateException("The logging thread needs to be terminated before reconstruction");
loggingThread = Thread
.ofPlatform()
.daemon()
.name("Logging thread")
.group(Engine.getThreadGroup())
.stackSize(10)
.unstarted(loggingThreadLogic);
}
/**
* Starts the logging thread.
*
* @since v1-alpha4
* @throws IllegalStateException if thread reconstruction fails because the thread isn't {@link Thread.State#TERMINATED}, see {@link #reconstructThread()}
* @see #loggingThread
*/
public static void startThread() {
if (loggingThread == null)
reconstructThread();
if (loggingThread.isAlive()) {
// Executing the thread restart logic prevents blocking the current thread
// while still ensuring that the logging thread is properly restarted
Thread
.ofVirtual()
.name("Logging thread restart thread")
.start(() -> {
logger.diag("Restarting the logging thread");
loggingThread.interrupt();
// Make sure that the logging thread is dead before reconstructing and starting it
while (loggingThread.isAlive())
Thread.onSpinWait();
reconstructThread();
loggingThread.start();
});
} else
loggingThread.start();
}
/**
* Returns the {@link Thread.State} of the logging thread.
*
* @return logging thread state
* @since v1-alpha4
* @see Thread.State
*/
public static @NotNull Thread.State getState() {
return loggingThread.getState();
}
}

View file

@ -19,8 +19,6 @@
package de.staropensource.engine.base.logging; package de.staropensource.engine.base.logging;
import de.staropensource.engine.base.Engine;
import de.staropensource.engine.base.type.EngineState;
import de.staropensource.engine.base.type.logging.LogLevel; import de.staropensource.engine.base.type.logging.LogLevel;
import lombok.Getter; import lombok.Getter;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -36,18 +34,26 @@ import java.nio.charset.StandardCharsets;
* *
* @since v1-alpha4 * @since v1-alpha4
*/ */
@SuppressWarnings({ "unused", "JavadocDeclaration" }) @SuppressWarnings({ "unused" })
public final class PrintStreamService { public final class PrintStreamService {
/**
* Contains the {@link LoggerInstance} for this instance.
*
* @see LoggerInstance
* @since v1-alpha4
*/
private static final @NotNull LoggerInstance logger = new LoggerInstance.Builder().setClazz(PrintStreamService.class).setOrigin("ENGINE").build();
/** /**
* Contains the diagnostic stream. * Contains the diagnostic stream.
* Anything sent will be redirected to * Anything sent will be redirected to
* {@link Logger#info(String)} * {@link Logger#info(Class, String, String, String)}
* *
* @since v1-alpha4 * @since v1-alpha4
* -- GETTER -- * -- GETTER --
* Returns the diagnostic stream. * Returns the diagnostic stream.
* Anything sent will be redirected to * Anything sent will be redirected to
* {@link Logger#info(String)} * {@link Logger#info(Class, String, String, String)}
* *
* @return diagnostic stream * @return diagnostic stream
* @since v1-alpha4 * @since v1-alpha4
@ -58,13 +64,13 @@ public final class PrintStreamService {
/** /**
* Contains the verbose stream. * Contains the verbose stream.
* Anything sent will be redirected to * Anything sent will be redirected to
* {@link Logger#error(String)} * {@link Logger#error(Class, String, String, String)}
* *
* @since v1-alpha4 * @since v1-alpha4
* -- GETTER -- * -- GETTER --
* Returns the verbose stream. * Returns the verbose stream.
* Anything sent will be redirected to * Anything sent will be redirected to
* {@link Logger#error(String)} * {@link Logger#error(Class, String, String, String)}
* *
* @return verbose stream * @return verbose stream
* @since v1-alpha4 * @since v1-alpha4
@ -75,13 +81,13 @@ public final class PrintStreamService {
/** /**
* Contains the silent warning stream. * Contains the silent warning stream.
* Anything sent will be redirected to * Anything sent will be redirected to
* {@link Logger#error(String)} * {@link Logger#error(Class, String, String, String)}
* *
* @since v1-alpha4 * @since v1-alpha4
* -- GETTER -- * -- GETTER --
* Returns the silent warning stream. * Returns the silent warning stream.
* Anything sent will be redirected to * Anything sent will be redirected to
* {@link Logger#error(String)} * {@link Logger#error(Class, String, String, String)}
* *
* @return silent warning stream * @return silent warning stream
* @since v1-alpha4 * @since v1-alpha4
@ -92,13 +98,13 @@ public final class PrintStreamService {
/** /**
* Contains the informational stream. * Contains the informational stream.
* Anything sent will be redirected to * Anything sent will be redirected to
* {@link Logger#info(String)} * {@link Logger#info(Class, String, String, String)}
* *
* @since v1-alpha4 * @since v1-alpha4
* -- GETTER -- * -- GETTER --
* Returns the informational stream. * Returns the informational stream.
* Anything sent will be redirected to * Anything sent will be redirected to
* {@link Logger#info(String)} * {@link Logger#info(Class, String, String, String)}
* *
* @return informational stream * @return informational stream
* @since v1-alpha4 * @since v1-alpha4
@ -109,13 +115,13 @@ public final class PrintStreamService {
/** /**
* Contains the warning stream. * Contains the warning stream.
* Anything sent will be redirected to * Anything sent will be redirected to
* {@link Logger#error(String)} * {@link Logger#error(Class, String, String, String)}
* *
* @since v1-alpha4 * @since v1-alpha4
* -- GETTER -- * -- GETTER --
* Returns the warning stream. * Returns the warning stream.
* Anything sent will be redirected to * Anything sent will be redirected to
* {@link Logger#error(String)} * {@link Logger#error(Class, String, String, String)}
* *
* @return warning stream * @return warning stream
* @since v1-alpha4 * @since v1-alpha4
@ -126,13 +132,13 @@ public final class PrintStreamService {
/** /**
* Contains the error stream. * Contains the error stream.
* Anything sent will be redirected to * Anything sent will be redirected to
* {@link Logger#info(String)} * {@link Logger#info(Class, String, String, String)}
* *
* @since v1-alpha4 * @since v1-alpha4
* -- GETTER -- * -- GETTER --
* Returns the error stream. * Returns the error stream.
* Anything sent will be redirected to * Anything sent will be redirected to
* {@link Logger#info(String)} * {@link Logger#info(Class, String, String, String)}
* *
* @return error stream * @return error stream
* @since v1-alpha4 * @since v1-alpha4
@ -143,13 +149,13 @@ public final class PrintStreamService {
/** /**
* Contains the crash stream. * Contains the crash stream.
* Anything sent will be redirected to * Anything sent will be redirected to
* {@link Logger#error(String)} * {@link Logger#error(Class, String, String, String)}
* *
* @since v1-alpha4 * @since v1-alpha4
* -- GETTER -- * -- GETTER --
* Returns the crash stream. * Returns the crash stream.
* Anything sent will be redirected to * Anything sent will be redirected to
* {@link Logger#error(String)} * {@link Logger#error(Class, String, String, String)}
* *
* @return crash stream * @return crash stream
* @since v1-alpha4 * @since v1-alpha4
@ -166,14 +172,18 @@ public final class PrintStreamService {
/** /**
* Initializes all {@link PrintStream}s offered by this class. * Initializes all {@link PrintStream}s offered by this class.
* <p>
* Only works during early engine startup.
* *
* @since v1-alpha8 * @since v1-alpha4
*/ */
public static void initializeStreams() { public static void initializeStreams() {
if (Engine.getInstance() == null || Engine.getInstance().getState() != EngineState.EARLY_STARTUP) // Close all existing streams
return; if (diag != null) diag.close();
if (verb != null) verb.close();
if (sarn != null) sarn.close();
if (info != null) info.close();
if (warn != null) warn.close();
if (error != null) error.close();
if (crash != null) crash.close();
// Create streams // Create streams
diag = LogStream.createPrintStream(LogLevel.DIAGNOSTIC); diag = LogStream.createPrintStream(LogLevel.DIAGNOSTIC);
@ -267,13 +277,13 @@ public final class PrintStreamService {
// Check for newline // Check for newline
if (sequence.indexOf("\n") != -1) { if (sequence.indexOf("\n") != -1) {
switch (level) { switch (level) {
case DIAGNOSTIC -> Logger.diag(sequence.toString()); case DIAGNOSTIC -> logger.diag(sequence.toString());
case VERBOSE -> Logger.verb(sequence.toString()); case VERBOSE -> logger.verb(sequence.toString());
case SILENT_WARNING -> Logger.sarn(sequence.toString()); case SILENT_WARNING -> logger.sarn(sequence.toString());
case INFORMATIONAL -> Logger.info(sequence.toString()); case INFORMATIONAL -> logger.info(sequence.toString());
case WARNING -> Logger.warn(sequence.toString()); case WARNING -> logger.warn(sequence.toString());
case ERROR -> Logger.error(sequence.toString()); case ERROR -> logger.error(sequence.toString());
case CRASH -> Logger.crash(sequence.toString()); case CRASH -> logger.crash(sequence.toString());
} }
sequence = new StringBuilder(); sequence = new StringBuilder();

View file

@ -1,338 +0,0 @@
/*
* STAROPENSOURCE ENGINE SOURCE FILE
* Copyright (c) 2024 The StarOpenSource Engine Authors
* 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.engine.base.logging.backend;
import de.staropensource.engine.base.Engine;
import de.staropensource.engine.base.EngineConfiguration;
import de.staropensource.engine.base.EngineInternals;
import de.staropensource.engine.base.logging.Logger;
import de.staropensource.engine.base.type.logging.LogLevel;
import de.staropensource.engine.base.utility.misc.NumberUtil;
import de.staropensource.engine.base.utility.misc.Miscellaneous;
import de.staropensource.engine.base.utility.information.EngineInformation;
import de.staropensource.engine.base.utility.information.JvmInformation;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.*;
import static de.staropensource.engine.base.logging.backend.Processor.isFeatureEnabled;
/**
* Handles crashes.
*
* @since v1-alpha8
*/
public final class CrashHandler {
/**
* Contains all random witty comments.
*
* @since v1-alpha8
*/
public static final @NotNull String[] WITTYCOMMENTS = new String[]{
"Who fucked up here?",
"What is it now?",
":neofox_woozy:",
"Oh no!",
"my engine brokey brokey",
"weird",
"Is this a feature?",
"$ git blame",
};
/**
* Creates and initializes an instance of this class.
*
* @since v1-alpha8
*/
private CrashHandler() {}
/**
* Handles crash reports.
*
* @param issuer {@link StackTraceElement} of the issuer
* @param message message
* @param throwable {@link Throwable} which caused the error
* @param fatal if to terminate the engine
* @since v1-alpha8
*/
public static void handleCrash(@NotNull StackTraceElement issuer, @NotNull String message, @Nullable Throwable throwable, boolean fatal) {
StringBuilder output = new StringBuilder();
String outputFinal;
// Header
output
.append("==== sos!engine crash report ====")
.append("\nDear user: The application or game you just used seems to have run into a problem.")
.append("\n Please be so kind and report this crash report to the developers so they can fix the issue.")
.append("\nDear developer: FIX YOUR SHIT. If the engine is at fault here, please report the crash")
.append("\n to StarOpenSource here: https://git.staropensource.de/StarOpenSource/Engine/issues")
.append("\n\n// ")
.append(WITTYCOMMENTS[new Random().nextInt(WITTYCOMMENTS.length)])
.append("\n\n");
// Information about the crash
output
.append("---- Crash Information ----\n")
.append("Issuer: ")
.append(issuer.getClassName())
.append("@")
.append(issuer.getModuleName())
.append("#")
.append(issuer.getMethodName())
.append("~")
.append(issuer.getLineNumber())
.append("\nFatal: ")
.append(fatal);
if (throwable == null)
output
.append("\nCaused by:")
.append("\nNo throwable has been passed.");
else {
output
.append("\n")
.append(Miscellaneous.stacktraceAsStringRecursive(throwable, true, true))
.append("\n");
}
output.append("\nMessage: \n")
.append(message)
.append("\n\n");
// Environment information
output
.append("---- Environment ----")
.append("\nTime and date: ")
.append(NumberUtil.padNumbers(Calendar.getInstance().get(Calendar.DAY_OF_MONTH), 2))
.append(".")
.append(NumberUtil.padNumbers(Calendar.getInstance().get(Calendar.MONTH), 2))
.append(".")
.append(NumberUtil.padNumbers(Calendar.getInstance().get(Calendar.YEAR), 4))
.append(" ")
.append(NumberUtil.padNumbers(Calendar.getInstance().get(Calendar.HOUR_OF_DAY), 2))
.append(":")
.append(NumberUtil.padNumbers(Calendar.getInstance().get(Calendar.MINUTE), 2))
.append(":")
.append(NumberUtil.padNumbers(Calendar.getInstance().get(Calendar.SECOND), 2))
.append(" [")
.append(TimeZone.getDefault().getDisplayName(false, TimeZone.SHORT, Locale.US))
.append("]")
.append("\nUNIX Epoch: ")
.append(NumberUtil.padNumbers(System.currentTimeMillis(), String.valueOf(Long.MAX_VALUE).length()))
.append("\nOperating system: ")
.append(System.getProperty("os.name"))
.append("\nCPU architecture: ")
.append(System.getProperty("os.arch"))
.append("\nMemory: ")
.append(JvmInformation.getMemoryUsed() / 1024)
.append(" MiB")
.append("/")
.append(JvmInformation.getMemoryLimit() / 1024)
.append(" MiB")
.append(" (stack ")
.append(JvmInformation.getMemoryStack().getUsed() / 1024)
.append(" MiB")
.append("/")
.append(JvmInformation.getMemoryStack().getMax() == -1 ? "" : JvmInformation.getMemoryStack().getMax() / 1024)
.append(" MiB")
.append(", heap ")
.append(JvmInformation.getMemoryHeap().getUsed() / 1024)
.append(" MiB")
.append("/")
.append(JvmInformation.getMemoryHeap().getMax() == -1 ? "" : JvmInformation.getMemoryStack().getMax() / 1024)
.append(" MiB)")
.append("\nJVM: ")
.append(JvmInformation.getImplementationName())
.append(" ")
.append(JvmInformation.getImplementationVersion())
.append(" @ ")
.append(JvmInformation.getJavaVersion())
.append(" by ")
.append(JvmInformation.getImplementationVendor())
.append("\nJVM arguments: ");
for (String argument : JvmInformation.getArguments())
output
.append("\n- '")
.append(argument)
.append("'");
output.append("\n\n");
// Engine
output.append("---- sos!engine ----\n");
if (EngineInformation.getVersioningString() == null)
output.append("EngineInformation is not yet initialized");
else
output
.append("Version: ")
.append(EngineInformation.getVersioningString())
.append("\nCommit: ")
.append(EngineInformation.getGitCommitIdentifierLong())
.append("\nDirty: ")
.append(EngineInformation.isGitDirty());
output.append("\n\n");
// Engine configuration
output.append("---- sos!engine configuration ----\n");
if (EngineConfiguration.getInstance() == null)
output.append("EngineConfiguration is not yet initialized");
else
output
.append("EngineConfiguration#debug='")
.append(EngineConfiguration.getInstance().isDebug())
.append("'\n")
.append("EngineConfiguration#debugEvents='")
.append(EngineConfiguration.getInstance().isDebugEvents())
.append("'\n")
.append("EngineConfiguration#initialPerformSubsystemInitialization='")
.append(EngineConfiguration.getInstance().isInitialPerformSubsystemInitialization())
.append("'\n")
.append("EngineConfiguration#initialIncludeSubsystemClasses='")
.append(EngineConfiguration.getInstance().getInitialIncludeSubsystemClasses())
.append("'\n")
.append("EngineConfiguration#errorShortcodeParser='")
.append(EngineConfiguration.getInstance().isErrorShortcodeParser())
.append("'\n")
.append("EngineConfiguration#optimizeLogging='")
.append(EngineConfiguration.getInstance().isOptimizeLogging())
.append("'\n")
.append("EngineConfiguration#optimizeEvents='")
.append(EngineConfiguration.getInstance().isOptimizeEvents())
.append("'\n")
.append("EngineConfiguration#logLevel='")
.append(EngineConfiguration.getInstance().getLogLevel().name())
.append("'\n")
.append("EngineConfiguration#logSettings='")
.append(EngineConfiguration.getInstance().getLogFeatures())
.append("'\n")
.append("EngineConfiguration#logPollingSpeed='")
.append(EngineConfiguration.getInstance().getLogPollingSpeed())
.append("'\n")
.append("EngineConfiguration#logForceStandardOutput='")
.append(EngineConfiguration.getInstance().isLogForceStandardOutput())
.append("'\n")
.append("EngineConfiguration#hideFullTypePath='")
.append(EngineConfiguration.getInstance().isHideFullTypePath());
output.append("'\n\n");
// System properties
output.append("---- System properties ----");
for (String property : System.getProperties().stringPropertyNames().stream().sorted().toList())
output
.append("\n")
.append(property)
.append("='")
.append(System.getProperties().getProperty(property).replace("\n", "\\n"))
.append("'");
output.append("\n\n");
// Stacktraces of all threads
output.append("---- Stacktraces of all threads ----");
{
Map<Thread, StackTraceElement[]> stacktraces = Thread.getAllStackTraces();
for (Thread thread : stacktraces.keySet())
output
.append("\n")
.append(thread.getName())
.append(" (id=")
.append(thread.threadId())
.append(" priority=")
.append(thread.getPriority())
.append(" group=")
.append(thread.getThreadGroup() == null ? "<unknown>" : thread.getThreadGroup().getName())
.append(" state=")
.append(thread.getState().name())
.append(" daemon=")
.append(thread.isDaemon())
.append("):")
.append("\n")
.append(Miscellaneous.stacktraceAsString(stacktraces.get(thread), false))
.append("\n");
}
output.append("\n");
// Footer
output
.append("Dear user: The application or game you just used seems to have run into a problem.\n")
.append(" Please be so kind and report this crash report to the developers so they can fix the issue.\n")
.append("Dear developer: FIX YOUR SHIT. If the engine is at fault here, please report the crash\n")
.append(" to StarOpenSource here: https://git.staropensource.de/StarOpenSource/Engine/issues\n")
.append("==== sos!engine crash report ====");
// Formatting
if (isFeatureEnabled("formatting")) {
outputFinal =
"<fg:red><bold>"
+ output
.toString()
.replace("<", "\\<")
+ "<reset>";
} else
outputFinal = output.toString();
// Print
Logger.getLoggingAdapter().print(LogLevel.CRASH, issuer, message, outputFinal);
// Terminate engine
// We do a test on the engine state here
// to prevent bugs and multiple engine shutdowns.
if (fatal)
switch (Engine.getInstance().getState()) {
case UNKNOWN, EARLY_STARTUP, STARTUP -> {
if (EngineInternals.getInstance() == null)
Runtime.getRuntime().exit(69);
else
EngineInternals.getInstance().getShutdownHandler().shutdown((short) 69);
}
case RUNNING -> Engine.getInstance().shutdown(69);
case SHUTDOWN, CRASHED -> {}
}
}
}

View file

@ -1,91 +0,0 @@
/*
* STAROPENSOURCE ENGINE SOURCE FILE
* Copyright (c) 2024 The StarOpenSource Engine Authors
* 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.engine.base.logging.backend;
import org.intellij.lang.annotations.RegExp;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.List;
/**
* Responsible for filtering log messages out.
*
* @since v1-alpha8
*/
public final class Filterer {
/**
* Contains a list of all disallowed classes.
*
* @since v1-alpha8
*/
static @NotNull List<@NotNull String> disallowedClasses = new ArrayList<>();
/**
* Contains a list of all disallowed modules.
*
* @since v1-alpha8
*/
static @NotNull List<@NotNull String> disallowedModules = new ArrayList<>();
/**
* Contains a list of all disallowed messages.
*
* @since v1-alpha8
*/
static @NotNull List<@NotNull String> disallowedMessages = new ArrayList<>();
/**
* Creates and initializes an instance of this class.
*
* @since v1-alpha8
*/
private Filterer() {}
/**
* Disallows one or multiple classes.
*
* @param regex regex
* @since v1-alpha8
*/
public static void disallowClass(@RegExp @NotNull String regex) {
disallowedClasses.add(regex);
}
/**
* Disallows one or multiple modules.
*
* @param regex regex
* @since v1-alpha8
*/
public static void disallowModule(@RegExp @NotNull String regex) {
disallowedModules.add(regex);
}
/**
* Disallows one or multiple messages.
*
* @param regex regex
* @since v1-alpha8
*/
public static void disallowMessage(@RegExp @NotNull String regex) {
disallowedMessages.add(regex);
}
}

View file

@ -1,388 +0,0 @@
/*
* STAROPENSOURCE ENGINE SOURCE FILE
* Copyright (c) 2024 The StarOpenSource Engine Authors
* 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.engine.base.logging.backend;
import de.staropensource.engine.base.EngineConfiguration;
import de.staropensource.engine.base.implementation.shortcode.EmptyShortcodeParser;
import de.staropensource.engine.base.logging.Logger;
import de.staropensource.engine.base.logging.backend.async.LoggingQueue;
import de.staropensource.engine.base.type.logging.LogLevel;
import de.staropensource.engine.base.utility.misc.NumberUtil;
import de.staropensource.engine.base.utility.PlaceholderEngine;
import de.staropensource.engine.base.utility.information.JvmInformation;
import org.jetbrains.annotations.NotNull;
import java.util.Calendar;
/**
* Processes log messages.
*
* @see #handle(LogLevel, StackTraceElement, String)
* @since v1-alpha8
*/
public final class Processor {
/**
* Creates and initializes an instance of this class
*
* @since v1-alpha8
*/
private Processor() {}
/**
* Checks whether the specified feature is enabled.
*
* @param feature feature to check
* @return enabled?
* @since v1-alpha8
*/
public static boolean isFeatureEnabled(@NotNull String feature) {
return EngineConfiguration.getInstance() != null && EngineConfiguration.getInstance().getLogFeatures().contains(feature);
}
/**
* Handles incoming log calls and either
* processes them directly or queues them in.
*
* @param level level of the log call
* @param issuer {@link StackTraceElement} of the issuer
* @param message message
* @since v1-alpha8
*/
public static void handle(@NotNull LogLevel level, @NotNull StackTraceElement issuer, @NotNull String message) {
if (EngineConfiguration.getInstance() != null && EngineConfiguration.getInstance().isOptimizeLogging())
LoggingQueue.add(level, issuer, message);
else
process(level, issuer, message);
}
/**
* Processes a log call.
*
* @param level level of the log call
* @param issuer {@link StackTraceElement} of the issuer
* @param message message
* @since v1-alpha8
*/
public static void process(@NotNull LogLevel level, @NotNull StackTraceElement issuer, @NotNull String message) {
StringBuilder output = new StringBuilder();
// Filter out
if (EngineConfiguration.getInstance() == null) {
LogLevel maxLevel = LogLevel.INFORMATIONAL;
try {
maxLevel = LogLevel.valueOf(System.getProperties().getProperty("sosengine.base.logLevel", "informational").toUpperCase());
} catch (IllegalArgumentException ignored) {
Logger.error("The log level '" + System.getProperties().getProperty("sosengine.base.logLevel", "informational") + "' is not valid");
}
if (level.compareTo(maxLevel) < 0)
return;
} else if (level.compareTo(EngineConfiguration.getInstance().getLogLevel()) < 0)
return;
for (String classNameDisallowed : Filterer.disallowedClasses)
if (issuer.getClassName().matches(classNameDisallowed))
return;
for (String moduleNameDisallowed : Filterer.disallowedModules)
if (issuer.getModuleName().matches(moduleNameDisallowed))
return;
for (String messageDisallowed : Filterer.disallowedModules)
if (message.matches(messageDisallowed))
return;
format(output, level);
runtime(output);
if (isFeatureEnabled("date") || isFeatureEnabled("time")) {
output.append("[");
date(output);
if (isFeatureEnabled("date") && isFeatureEnabled("time"))
output.append(" ");
time(output);
output.append("] ");
}
if (isFeatureEnabled("level") || isFeatureEnabled("origin")) {
output.append("[");
level(output, level);
if (isFeatureEnabled("level") && isFeatureEnabled("origin"))
output.append(" ");
if (isFeatureEnabled("origin")) {
issuerClass(output, issuer);
issuerModule(output, level, issuer);
methodName(output, level, issuer);
lineNumber(output, level, issuer);
}
output.append("] ");
}
message(output, message);
format(output, "reset");
// Print
Logger.getLoggingAdapter().print(level, issuer, message, output.toString());
}
// -----> Formatting
/**
* Adds the {@code formatting} feature.
* <p>
* This method will reset and then color the following
* substring in the log level-specific color.
*
* @param builder {@link StringBuilder} instance to append to
* @param level level of the log call
* @see #format(StringBuilder, String)
* @since v1-alpha8
*/
private static void format(@NotNull StringBuilder builder, @NotNull LogLevel level) {
format(builder, "reset");
format(builder, switch (level) {
case DIAGNOSTIC, VERBOSE -> "fg:blue";
case SILENT_WARNING, WARNING -> "fg:yellow";
case INFORMATIONAL -> "fg:white";
case ERROR -> "fg:red";
case CRASH -> "you should not see this";
});
}
/**
* Adds the {@code formatting} feature.
*
* @param builder {@link StringBuilder} instance to append to
* @param component formatting component
* @see #format(StringBuilder, LogLevel)
* @since v1-alpha8
*/
private static void format(@NotNull StringBuilder builder, @NotNull String component) {
if (isFeatureEnabled("formatting"))
builder
.append("<")
.append(component)
.append(">");
}
/**
* Adds the {@code formatting} feature.
*
* @param string string to sanitize
* @return sanitized string
* @see #format(StringBuilder, LogLevel)
* @since v1-alpha8
*/
private static @NotNull String sanitizeFormat(@NotNull String string) {
if (isFeatureEnabled("formatting"))
return string.replace("<", "\\<");
else
return string;
}
// -----> Features and components
/**
* Adds the {@code runtime} feature.
*
* @param builder {@link StringBuilder} instance to append to
* @since v1-alpha8
*/
private static void runtime(@NotNull StringBuilder builder) {
if (isFeatureEnabled("runtime"))
builder
.append("[")
.append(JvmInformation.getUptime())
.append("ms")
.append("] ");
}
/**
* Adds the {@code time} feature.
*
* @param builder {@link StringBuilder} instance to append to
* @since v1-alpha8
*/
private static void time(@NotNull StringBuilder builder) {
if (isFeatureEnabled("time"))
builder
.append(NumberUtil.padNumbers(Calendar.getInstance().get(Calendar.HOUR_OF_DAY), 2))
.append(":")
.append(NumberUtil.padNumbers(Calendar.getInstance().get(Calendar.MINUTE), 2))
.append(":")
.append(NumberUtil.padNumbers(Calendar.getInstance().get(Calendar.SECOND), 2));
}
/**
* Adds the {@code date} feature.
*
* @param builder {@link StringBuilder} instance to append to
* @since v1-alpha8
*/
private static void date(@NotNull StringBuilder builder) {
if (isFeatureEnabled("date"))
builder
.append(NumberUtil.padNumbers(Calendar.getInstance().get(Calendar.DAY_OF_MONTH), 2))
.append(".")
.append(NumberUtil.padNumbers(Calendar.getInstance().get(Calendar.MONTH), 2))
.append(".")
.append(NumberUtil.padNumbers(Calendar.getInstance().get(Calendar.YEAR), 4));
}
/**
* Adds the {@code level} component.
*
* @param builder {@link StringBuilder} instance to append to
* @param level level of the log call
* @since v1-alpha8
*/
private static void level(@NotNull StringBuilder builder, @NotNull LogLevel level) {
if (isFeatureEnabled("level")) {
format(builder, "bold");
builder.append(switch (level) {
case DIAGNOSTIC -> "DIAG";
case VERBOSE -> "VERB";
case SILENT_WARNING -> "SARN";
case INFORMATIONAL -> "INFO";
case WARNING -> "WARN";
case ERROR -> "ERR!";
case CRASH -> "CRSH";
});
format(builder, level);
}
}
/**
* Adds the {@code issuer class} component.
*
* @param builder {@link StringBuilder} instance to append to
* @param issuer {@link StackTraceElement} of the issuer
* @since v1-alpha8
*/
private static void issuerClass(@NotNull StringBuilder builder, @NotNull StackTraceElement issuer) {
format(builder, "bold");
if (isFeatureEnabled("shortIssuerClass")) {
String[] classNameSplit = issuer.getClassName().split("\\.");
builder.append(classNameSplit[classNameSplit.length - 1]);
} else
builder.append(issuer.getClassName());
}
/**
* Adds the {@code moduleName} and {@code moduleVersion} features.
*
* @param builder {@link StringBuilder} instance to append to
* @param level level of the log call
* @param issuer {@link StackTraceElement} of the issuer
* @since v1-alpha8
*/
private static void issuerModule(@NotNull StringBuilder builder, @NotNull LogLevel level, @NotNull StackTraceElement issuer) {
if (isFeatureEnabled("moduleName") && issuer.getModuleName() != null) {
format(builder, "bold");
builder
.append("@")
.append(issuer.getModuleName());
if (isFeatureEnabled("moduleVersion") && issuer.getModuleVersion() != null)
builder
.append("v")
.append(issuer.getModuleVersion());
format(builder, level);
}
}
/**
* Adds the {@code methodName} feature.
*
* @param builder {@link StringBuilder} instance to append to
* @param level level of the log call
* @param issuer {@link StackTraceElement} of the issuer
* @since v1-alpha8
*/
private static void methodName(@NotNull StringBuilder builder, @NotNull LogLevel level, @NotNull StackTraceElement issuer) {
if (isFeatureEnabled("methodName")) {
builder.append("#");
format(builder, "bold");
builder.append(sanitizeFormat(issuer.getMethodName()));
format(builder, level);
}
}
/**
* Adds the {@code lineNumber} feature.
*
* @param builder {@link StringBuilder} instance to append to
* @param level level of the log call
* @param issuer {@link StackTraceElement} of the issuer
* @since v1-alpha8
*/
private static void lineNumber(@NotNull StringBuilder builder, @NotNull LogLevel level, @NotNull StackTraceElement issuer) {
if (isFeatureEnabled("lineNumber")) {
builder.append("~");
format(builder, "bold");
builder.append(issuer.getLineNumber());
format(builder, level);
}
}
/**
* Adds the {@code message} component.
*
* @param builder {@link StringBuilder} instance to append to
* @param message message
* @since v1-alpha8
*/
private static void message(@NotNull StringBuilder builder, @NotNull String message) {
String finalizedMessage = handlePlaceholders(
message.replace(
"\n",
"\n" + " ".repeat(
new EmptyShortcodeParser(builder.toString(), true)
.getClean()
.length()
)
)
);
if (Logger.isSanitizeMessages())
finalizedMessage = sanitizeFormat(finalizedMessage);
builder.append(finalizedMessage);
}
// -----> Utility methods
/**
* Uses the {@link PlaceholderEngine} to replace
* all placeholders within a specified string and
* returns it's result. The original string will
* be returned if the {@link PlaceholderEngine}
* is not yet initialized.
*
* @param string string to use
* @return updated string
* @since v1-alpha8
*/
private static @NotNull String handlePlaceholders(@NotNull String string) {
if (PlaceholderEngine.getInstance() == null)
return string;
else
return PlaceholderEngine.getInstance().process(string);
}
}

View file

@ -1,76 +0,0 @@
/*
* STAROPENSOURCE ENGINE SOURCE FILE
* Copyright (c) 2024 The StarOpenSource Engine Authors
* 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.engine.base.logging.backend.async;
import de.staropensource.engine.base.internal.type.QueuedLogCall;
import de.staropensource.engine.base.logging.backend.Processor;
import de.staropensource.engine.base.type.logging.LogLevel;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* Handles everything related to the logging queue.
*
* @since v1-alpha8
*/
public final class LoggingQueue {
/**
* Contains the logging queue.
*
* @since v1-alpha8
*/
private static final @NotNull List<@NotNull QueuedLogCall> queue = Collections.synchronizedList(new ArrayList<>());
/**
* Creates and initializes an instance of this class
*
* @since v1-alpha8
*/
private LoggingQueue() {}
/**
* Adds a new entry to the logging queue.
*
* @param level level of the log call
* @param issuer {@link StackTraceElement} of the issuer
* @param message message
* @since v1-alpha8
*/
public static void add(@NotNull LogLevel level, @NotNull StackTraceElement issuer, @NotNull String message) {
queue.add(new QueuedLogCall(level, issuer, message));
}
/**
* Flushes the logging queue.
*
* @since v1-alpha8
*/
public static void flush() {
// Get copy of and clear the queue
List<@NotNull QueuedLogCall> queue = new ArrayList<>(LoggingQueue.queue);
LoggingQueue.queue.clear();
for (QueuedLogCall queuedCall : queue)
Processor.process(queuedCall.level(), queuedCall.issuer(), queuedCall.message());
}
}

View file

@ -1,142 +0,0 @@
/*
* STAROPENSOURCE ENGINE SOURCE FILE
* Copyright (c) 2024 The StarOpenSource Engine Authors
* 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.engine.base.logging.backend.async;
import de.staropensource.engine.base.Engine;
import de.staropensource.engine.base.EngineConfiguration;
import de.staropensource.engine.base.logging.Logger;
import de.staropensource.engine.base.type.EngineState;
import org.jetbrains.annotations.NotNull;
/**
* Handles the logging thread.
*
* @since v1-alpha8
*/
public final class LoggingThread {
/**
* Contains the logging thread.
*
* @since v1-alpha8
*/
private static Thread thread;
/**
* Contains the code of the logging thread.
*
* @since v1-alpha8
*/
private static final @NotNull Runnable threadCode = () -> {
int pollingSpeed;
while (!(
Thread.currentThread().isInterrupted()
|| !(EngineConfiguration.getInstance() == null || EngineConfiguration.getInstance().isOptimizeLogging())
|| Engine.getInstance().getState() == EngineState.SHUTDOWN
|| Engine.getInstance().getState() == EngineState.CRASHED
)) {
if (EngineConfiguration.getInstance() == null)
pollingSpeed = 5;
else
pollingSpeed = EngineConfiguration.getInstance().getLogPollingSpeed();
// Flush all log messages
LoggingQueue.flush();
// Sleep for whatever has been configured
if (pollingSpeed > 0) {
long sleepDuration = System.currentTimeMillis() + pollingSpeed;
while (System.currentTimeMillis() < sleepDuration)
Thread.onSpinWait();
}
}
};
static {
constructThread();
}
/**
* Creates and initializes an instance of this class
*
* @since v1-alpha8
*/
private LoggingThread() {}
/**
* Constructs the logging thread.
*
* @see #thread
* @since v1-alpha8
*/
private static void constructThread() {
thread = Thread
.ofPlatform()
.daemon()
.name("Logging thread")
.group(Engine.getThreadGroup())
.priority(Thread.MAX_PRIORITY)
.stackSize(10)
.unstarted(threadCode);
}
/**
* (Re-)Starts the logging thread.
*
* @param allowRestart if the logging thread should be restarted if it's stopped
* @since v1-alpha8
*/
public static void startThread(boolean allowRestart) {
if (allowRestart && thread.isAlive()) {
// Executing the restart logic in another thread prevents
// this thread from being fully blocked while still ensuring
// that the logging thread is properly restarted
Thread
.ofVirtual()
.name("Logging thread restart thread")
.start(() -> {
Logger.diag("Restarting the logging thread");
// Interrupt thread
// This let's our thread code know that it should terminate
thread.interrupt();
// Make sure that the logging thread is dead before reconstructing and starting it
while (thread.isAlive())
Thread.onSpinWait();
constructThread();
thread.start();
});
} else
thread.start();
}
/**
* Returns the {@link Thread.State} of the logging thread.
*
* @return logging thread state
* @see Thread.State
* @since v1-alpha8
*/
public static @NotNull Thread.State getState() {
return thread.getState();
}
}

View file

@ -18,9 +18,11 @@
*/ */
/** /**
* The engine's logging infrastructure. * The engine's custom logging infrastructure.
* *
* @see de.staropensource.engine.base.logging.Logger * @see de.staropensource.engine.base.logging.Logger
* @see de.staropensource.engine.base.logging.LoggerInstance
* @see de.staropensource.engine.base.logging.CrashHandler
* @since v1-alpha0 * @since v1-alpha0
*/ */
package de.staropensource.engine.base.logging; package de.staropensource.engine.base.logging;

View file

@ -19,7 +19,7 @@
package de.staropensource.engine.base.reflection; package de.staropensource.engine.base.reflection;
import de.staropensource.engine.base.logging.Logger; import de.staropensource.engine.base.logging.LoggerInstance;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.io.File; import java.io.File;
@ -39,6 +39,14 @@ import java.util.Map;
* @since v1-alpha2 * @since v1-alpha2
*/ */
public final class ClasspathAccess { public final class ClasspathAccess {
/**
* Contains the {@link LoggerInstance} for this instance.
*
* @see LoggerInstance
* @since v1-alpha2
*/
private static final @NotNull LoggerInstance logger = new LoggerInstance.Builder().setClazz(ClasspathAccess.class).setOrigin("ENGINE").build();
/** /**
* Creates and initializes an instance of this class. * Creates and initializes an instance of this class.
* *
@ -61,7 +69,7 @@ public final class ClasspathAccess {
try { try {
urls.add(new File(path).toURI().toURL()); urls.add(new File(path).toURI().toURL());
} catch (Exception exception) { } catch (Exception exception) {
Logger.crash("Failed converting classpath to URL", exception); logger.crash("Failed converting classpath to URL", exception);
} }
return fixURLs(urls); return fixURLs(urls);

View file

@ -20,7 +20,7 @@
package de.staropensource.engine.base.type; package de.staropensource.engine.base.type;
import de.staropensource.engine.base.EngineConfiguration; import de.staropensource.engine.base.EngineConfiguration;
import de.staropensource.engine.base.utility.misc.NumberUtil; import de.staropensource.engine.base.utility.Math;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import lombok.SneakyThrows; import lombok.SneakyThrows;
@ -42,7 +42,7 @@ import java.util.Locale;
@Getter @Getter
@Setter @Setter
@SuppressWarnings({ "JavadocDeclaration" }) @SuppressWarnings({ "JavadocDeclaration" })
public class Color { public final class Color {
/** /**
* Contains the red color value. * Contains the red color value.
* *
@ -118,10 +118,10 @@ public class Color {
* @since v1-alpha6 * @since v1-alpha6
*/ */
private Color(int red, int green, int blue, int alpha) { private Color(int red, int green, int blue, int alpha) {
this.red = NumberUtil.limitNumber(0, 255, red); this.red = Math.boundNumber(0, 255, red);
this.green = NumberUtil.limitNumber(0, 255, green); this.green = Math.boundNumber(0, 255, green);
this.blue = NumberUtil.limitNumber(0, 255, blue); this.blue = Math.boundNumber(0, 255, blue);
this.alpha = NumberUtil.limitNumber(0, 255, alpha); this.alpha = Math.boundNumber(0, 255, alpha);
} }
/** /**

View file

@ -19,6 +19,9 @@
package de.staropensource.engine.base.type; package de.staropensource.engine.base.type;
import de.staropensource.engine.base.logging.InitLogger;
import de.staropensource.engine.base.logging.Logger;
/** /**
* Determines in which state the engine is currently in. * Determines in which state the engine is currently in.
* *
@ -26,52 +29,58 @@ package de.staropensource.engine.base.type;
*/ */
public enum EngineState { public enum EngineState {
/** /**
* Indicates that the state of the engine is * The state of the engine is currently unknown.
* currently unknown. The engine is most likely * The engine likely has not been initialized yet.
* not initialized yet.
* *
* @since v1-alpha2 * @since v1-alpha2
*/ */
UNKNOWN, UNKNOWN,
/** /**
* Indicates that the engine is largely * The engine is in early startup.
* uninitialized and unsafe to use. * The engine is largely uninitialized and
* the logging infrastructure is not yet ready
* to be used.
* <p>
* This state is used to indicate that the
* {@link InitLogger} shall be used instead
* of {@link Logger}.
* *
* @since v1-alpha8 * @since v1-alpha4
*/ */
EARLY_STARTUP, EARLY_STARTUP,
/** /**
* Indicates that the core engine has fully * The engine is starting up.
* initialized, except for all subsystems. * Some parts of the engine may not be initialized
* yet and may be unsafe to access. The logging
* infrastructure is ready to be used though and is
* safe to access.
* *
* @since v1-alpha8 * @since v1-alpha2
*/ */
STARTUP, STARTUP,
/** /**
* Indicates that the engine is running * The engine is in normal operation mode.
* and operating normally.
* *
* @since v1-alpha2 * @since v1-alpha2
*/ */
RUNNING, RUNNING,
/** /**
* Indicates that the engine or the * The engine has crashed, either by itself or the application.
* application has crashed.
* *
* @since v1-alpha2 * @since v1-alpha2
*/ */
CRASHED, CRASHED,
/** /**
* Indicates that the engine is shutting * The engine is shutting down.
* down. The engine is unusable in this * Some parts of the engine may have already
* state and should not be used. * shut down and may be unsafe to access.
* *
* @since v1-alpha8 * @since v1-alpha2
*/ */
SHUTDOWN, SHUTDOWN,
} }

View file

@ -0,0 +1,81 @@
/*
* STAROPENSOURCE ENGINE SOURCE FILE
* Copyright (c) 2024 The StarOpenSource Engine Authors
* 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.engine.base.type;
import de.staropensource.engine.base.annotation.EventListener;
/**
* Allows an {@link EventListener} method to specify when it should be invoked.
*
* @since v1-alpha0
*/
@SuppressWarnings({ "unused" })
public enum EventPriority {
/**
* {@link EventListener}s with this priority are processed first, even before {@link #VERY_IMPORTANT}.
* This event is exclusive to subsystems and should not be used by applications.
*
* @since v1-alpha0
*/
EXCLUSIVELY_IMPORTANT,
/**
* {@link EventListener}s with this priority are invoked first.
*
* @since v1-alpha0
*/
VERY_IMPORTANT,
/**
* {@link EventListener}s with this priority are invoked second.
*
* @since v1-alpha0
*/
IMPORTANT,
/**
* The default event priority, {@link EventListener}s with this priority are invoked third.
*
* @since v1-alpha0
*/
DEFAULT,
/**
* {@link EventListener}s with this priority are invoked fourth.
*
* @since v1-alpha0
*/
UNIMPORTANT,
/**
* {@link EventListener}s with this priority are invoked last.
*
* @since v1-alpha0
*/
VERY_UNIMPORTANT,
/**
* {@link EventListener}s with this priority are invoked last, even after {@link #VERY_UNIMPORTANT}.
* This event is exclusive to subsystems and should not be used by applications.
*
* @since v1-alpha0
*/
EXCLUSIVELY_UNIMPORTANT
}

View file

@ -29,32 +29,32 @@ import org.jetbrains.annotations.NotNull;
*/ */
public enum Tristate { public enum Tristate {
/** /**
* Defines an unset state. * An unset tristate.
* *
* @since v1-alpha1 * @since v1-alpha1
*/ */
UNSET, UNSET,
/** /**
* Defines a true state. * A true tristate.
* *
* @since v1-alpha1 * @since v1-alpha1
*/ */
TRUE, TRUE,
/** /**
* Defines a false state. * A false tristate.
* *
* @since v1-alpha1 * @since v1-alpha1
*/ */
FALSE; FALSE;
/** /**
* Converts the specified {@link Boolean} into a {@link Tristate}. * Converts the {@link Boolean} into a {@link Tristate}.
* *
* @param bool boolean to convert * @param bool boolean to convert
* @return {@link Tristate} representation of the specified boolean * @return tristated boolean
* @since v1-alpha9 * @since v1-alpha5
*/ */
public static @NotNull Tristate toTristate(boolean bool) { public static @NotNull Tristate toTristate(boolean bool) {
if (bool) return Tristate.TRUE; if (bool) return Tristate.TRUE;
@ -62,28 +62,12 @@ public enum Tristate {
} }
/** /**
* Converts the specified {@link Integer} into a {@link Tristate}. * Converts the {@link Tristate} into a {@link Boolean}.
*
* @param integer integer to convert
* @return {@link Tristate} representation of the specified integer
* @since v1-alpha9
*/
public static @NotNull Tristate toTristate(int integer) {
return switch (integer) {
case 0 -> Tristate.FALSE;
case 1 -> Tristate.TRUE;
case 2 -> Tristate.UNSET;
default -> throw new IndexOutOfBoundsException("Supplied integer '" + integer + "' is not in range 0-2");
};
}
/**
* Converts this {@link Tristate} into a {@link Boolean}.
* Make sure to check for {@link #UNSET} first. * Make sure to check for {@link #UNSET} first.
* *
* @return trimmed boolean representation of this {@link Tristate} * @return booleanized {@link Tristate}
* @throws TristateConversionException on encountering {@link #UNSET} * @throws TristateConversionException when encountering {@link #UNSET}
* @since v1-alpha9 * @since v1-alpha2
*/ */
public boolean toBoolean() { public boolean toBoolean() {
return switch (this) { return switch (this) {
@ -92,19 +76,4 @@ public enum Tristate {
case FALSE -> false; case FALSE -> false;
}; };
} }
/**
* Converts this {@link Tristate} into an {@link Integer}.
*
* @return integer representation of this {@link Tristate}
* @throws TristateConversionException when encountering {@link #UNSET}
* @since v1-alpha9
*/
public int toInteger() {
return switch (this) {
case FALSE -> 0;
case TRUE -> 1;
case UNSET -> 2;
};
}
} }

View file

@ -22,8 +22,7 @@ package de.staropensource.engine.base.type;
import de.staropensource.engine.base.implementation.versioning.StarOpenSourceVersioningSystem; import de.staropensource.engine.base.implementation.versioning.StarOpenSourceVersioningSystem;
/** /**
* Provides all available version types as specified * Provides all available version types specified in the StarOpenSource Versioning System v2.
* by the StarOpenSource Versioning System v2.
* *
* @see StarOpenSourceVersioningSystem * @see StarOpenSourceVersioningSystem
* @since v1-alpha0 * @since v1-alpha0

View file

@ -0,0 +1,222 @@
/*
* STAROPENSOURCE ENGINE SOURCE FILE
* Copyright (c) 2024 The StarOpenSource Engine Authors
* 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.engine.base.type.immutable;
import org.jetbrains.annotations.NotNull;
import java.util.*;
import java.util.function.UnaryOperator;
/**
* An unmodifiable {@link ArrayList}.
*
* @param <E> contained type
* @since v1-alpha1
*/
@SuppressWarnings({ "unused" })
public class ImmutableArrayList<E> extends ArrayList<E> {
/**
* Creates and initializes an instance of this class.
*
* @since v1-alpha1
*/
public ImmutableArrayList() {}
/**
* Creates and initializes an instance of this class.
* Converts a {@link LinkedList} into an {@link ImmutableArrayList}.
*
* @param list {@link List} to convert
* @since v1-alpha1
*/
public ImmutableArrayList(@NotNull List<E> list) {
super.addAll(list);
}
/**
* Converts the {@link ImmutableArrayList} into a regular {@link ArrayList}.
*
* @return mutable {@link ArrayList}
* @since v1-alpha2
*/
public @NotNull ArrayList<E> toMutable() {
return new ArrayList<>(this);
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException always, array list is immutable
*/
@Override
public boolean add(E e) {
throw new UnsupportedOperationException("This method cannot be executed on an ImmutableArrayList");
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException always, array list is immutable
*/
@Override
public boolean remove(Object o) {
throw new UnsupportedOperationException("This method cannot be executed on an ImmutableArrayList");
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException always, array list is immutable
*/
@Override
public boolean addAll(@NotNull Collection<? extends E> c) {
throw new UnsupportedOperationException("This method cannot be executed on an ImmutableArrayList");
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException always, array list is immutable
*/
@Override
public boolean addAll(int index, @NotNull Collection<? extends E> c) {
throw new UnsupportedOperationException("This method cannot be executed on an ImmutableArrayList");
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException always, array list is immutable
*/
@Override
public boolean removeAll(@NotNull Collection<?> c) {
throw new UnsupportedOperationException("This method cannot be executed on an ImmutableArrayList");
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException always, array list is immutable
*/
@Override
public boolean retainAll(@NotNull Collection<?> c) {
throw new UnsupportedOperationException("This method cannot be executed on an ImmutableArrayList");
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException always, array list is immutable
*/
@Override
public void replaceAll(UnaryOperator<E> operator) {
throw new UnsupportedOperationException("This method cannot be executed on an ImmutableArrayList");
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException always, array list is immutable
*/
@Override
public void sort(Comparator<? super E> c) {
throw new UnsupportedOperationException("This method cannot be executed on an ImmutableArrayList");
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException always, array list is immutable
*/
@Override
public void clear() {
throw new UnsupportedOperationException("This method cannot be executed on an ImmutableArrayList");
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException always, array list is immutable
*/
@Override
public E set(int index, E element) throws UnsupportedOperationException {
throw new UnsupportedOperationException("This method cannot be executed on an ImmutableArrayList");
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException always, array list is immutable
*/
@Override
public void add(int index, E element) {
throw new UnsupportedOperationException("This method cannot be executed on an ImmutableArrayList");
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException always, array list is immutable
*/
@Override
public E remove(int index) {
throw new UnsupportedOperationException("This method cannot be executed on an ImmutableArrayList");
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException always, array list is immutable
*/
@Override
public void addFirst(E e) {
throw new UnsupportedOperationException("This method cannot be executed on an ImmutableArrayList");
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException always, array list is immutable
*/
@Override
public void addLast(E e) {
throw new UnsupportedOperationException("This method cannot be executed on an ImmutableArrayList");
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException always, array list is immutable
*/
@Override
public E removeFirst() {
throw new UnsupportedOperationException("This method cannot be executed on an ImmutableArrayList");
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException always, array list is immutable
*/
@Override
public E removeLast() {
throw new UnsupportedOperationException("This method cannot be executed on an ImmutableArrayList");
}
}

View file

@ -0,0 +1,167 @@
/*
* STAROPENSOURCE ENGINE SOURCE FILE
* Copyright (c) 2024 The StarOpenSource Engine Authors
* 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.engine.base.type.immutable;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiFunction;
/**
* An unmodifiable {@link HashMap}.
*
* @param <K> contained key type
* @param <V> contained value type
* @since v1-alpha1
*/
@SuppressWarnings({ "unused" })
public class ImmutableHashMap<K, V> extends HashMap<K, V> {
/**
* Creates and initializes an instance of this class.
*
* @since v1-alpha1
*/
public ImmutableHashMap() {}
/**
* Creates and initializes an instance of this class.
* Converts a {@link Map} into an {@link ImmutableHashMap}.
*
* @param map {@link Map} to convert
* @since v1-alpha1
*/
public ImmutableHashMap(@NotNull Map<K, V> map) {
for (K key : map.keySet())
super.put(key, map.get(key));
}
/**
* Converts the {@link ImmutableHashMap} into a regular {@link HashMap}.
*
* @return mutable {@link HashMap}
* @since v1-alpha2
*/
public @NotNull HashMap<K, V> toMutable() {
return new HashMap<>(this);
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException always, hash map is immutable
*/
@Override
public @Nullable V put(K key, V value) {
throw new UnsupportedOperationException("This method cannot be executed on an ImmutableHashMap");
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException always, hash map is immutable
*/
@Override
public @Nullable V remove(Object key) {
throw new UnsupportedOperationException("This method cannot be executed on an ImmutableHashMap");
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException always, hash map is immutable
*/
@Override
public void putAll(@NotNull Map<? extends K, ? extends V> m) {
throw new UnsupportedOperationException("This method cannot be executed on an ImmutableHashMap");
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException always, hash map is immutable
*/
@Override
public void clear() {
throw new UnsupportedOperationException("This method cannot be executed on an ImmutableHashMap");
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException always, hash map is immutable
*/
@Override
public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
throw new UnsupportedOperationException("This method cannot be executed on an ImmutableHashMap");
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException always, hash map is immutable
*/
@Override
public @Nullable V putIfAbsent(K key, V value) {
throw new UnsupportedOperationException("This method cannot be executed on an ImmutableHashMap");
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException always, hash map is immutable
*/
@Override
public boolean remove(Object key, Object value) {
throw new UnsupportedOperationException("This method cannot be executed on an ImmutableHashMap");
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException always, hash map is immutable
*/
@Override
public boolean replace(K key, V oldValue, V newValue) {
throw new UnsupportedOperationException("This method cannot be executed on an ImmutableHashMap");
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException always, hash map is immutable
*/
@Override
public @Nullable V replace(K key, V value) {
throw new UnsupportedOperationException("This method cannot be executed on an ImmutableHashMap");
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException always, hash map is immutable
*/
@Override
public @Nullable V merge(K key, @NotNull V value, @NotNull BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
throw new UnsupportedOperationException("This method cannot be executed on an ImmutableHashMap");
}
}

View file

@ -0,0 +1,134 @@
/*
* STAROPENSOURCE ENGINE SOURCE FILE
* Copyright (c) 2024 The StarOpenSource Engine Authors
* 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.engine.base.type.immutable;
import org.jetbrains.annotations.NotNull;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Predicate;
/**
* An unmodifiable {@link HashSet}.
*
* @param <E> contained type
* @since v1-alpha2
*/
@SuppressWarnings({ "unused" })
public class ImmutableHashSet<E> extends HashSet<E> {
/**
* Creates and initializes an instance of this class.
*
* @since v1-alpha2
*/
public ImmutableHashSet() {}
/**
* Creates and initializes an instance of this class.
* Converts a {@link Set} into an {@link ImmutableHashSet}.
*
* @param set {@link Set} to convert
* @since v1-alpha2
*/
public ImmutableHashSet(@NotNull Set<E> set) {
super.addAll(set);
}
/**
* Converts the {@link ImmutableHashSet} into a regular {@link HashSet}.
*
* @return mutable {@link HashSet}
* @since v1-alpha2
*/
public @NotNull HashSet<E> toMutable() {
return new HashSet<>(this);
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException always, array list is immutable
*/
@Override
public boolean add(Object o) {
throw new UnsupportedOperationException("This method cannot be executed on an ImmutableHashSet");
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException always, array list is immutable
*/
@Override
public boolean remove(Object o) {
throw new UnsupportedOperationException("This method cannot be executed on an ImmutableHashSet");
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException always, array list is immutable
*/
@Override
public boolean addAll(@NotNull Collection collection) {
throw new UnsupportedOperationException("This method cannot be executed on an ImmutableHashSet");
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException always, array list is immutable
*/
@Override
public boolean retainAll(@NotNull Collection collection) {
throw new UnsupportedOperationException("This method cannot be executed on an ImmutableHashSet");
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException always, array list is immutable
*/
@Override
public boolean removeIf(Predicate filter) {
throw new UnsupportedOperationException("This method cannot be executed on an ImmutableHashSet");
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException always, array list is immutable
*/
@Override
public void clear() {
throw new UnsupportedOperationException("This method cannot be executed on an ImmutableHashSet");
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException always, array list is immutable
*/
@Override
public boolean removeAll(@NotNull Collection collection) {
throw new UnsupportedOperationException("This method cannot be executed on an ImmutableHashSet");
}
}

View file

@ -0,0 +1,167 @@
/*
* STAROPENSOURCE ENGINE SOURCE FILE
* Copyright (c) 2024 The StarOpenSource Engine Authors
* 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.engine.base.type.immutable;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.BiFunction;
/**
* An unmodifiable {@link Map}.
*
* @param <K> contained key type
* @param <V> contained value type
* @since v1-alpha1
*/
@SuppressWarnings({ "unused" })
public class ImmutableLinkedHashMap<K, V> extends LinkedHashMap<K, V> {
/**
* Creates and initializes an instance of this class.
*
* @since v1-alpha1
*/
public ImmutableLinkedHashMap() {}
/**
* Creates and initializes an instance of this class.
* Converts a {@link Map} into an {@link ImmutableLinkedHashMap}.
*
* @param map {@link Map} to convert
* @since v1-alpha1
*/
public ImmutableLinkedHashMap(@NotNull Map<K, V> map) {
for (K key : map.keySet())
super.put(key, map.get(key));
}
/**
* Converts the {@link ImmutableLinkedHashMap} into a regular {@link LinkedHashMap}.
*
* @return mutable {@link LinkedHashMap}
* @since v1-alpha2
*/
public @NotNull LinkedHashMap<K, V> toMutable() {
return new LinkedHashMap<>(this);
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException always, linked hash map is immutable
*/
@Override
public @Nullable V put(K key, V value) {
throw new UnsupportedOperationException("This method cannot be executed on an ImmutableLinkedHashMap");
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException always, linked hash map is immutable
*/
@Override
public @Nullable V remove(Object key) {
throw new UnsupportedOperationException("This method cannot be executed on an ImmutableLinkedHashMap");
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException always, linked hash map is immutable
*/
@Override
public void putAll(@NotNull Map<? extends K, ? extends V> m) {
throw new UnsupportedOperationException("This method cannot be executed on an ImmutableLinkedHashMap");
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException always, linked hash map is immutable
*/
@Override
public void clear() {
throw new UnsupportedOperationException("This method cannot be executed on an ImmutableLinkedHashMap");
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException always, linked hash map is immutable
*/
@Override
public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
throw new UnsupportedOperationException("This method cannot be executed on an ImmutableLinkedHashMap");
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException always, linked hash map is immutable
*/
@Override
public @Nullable V putIfAbsent(K key, V value) {
throw new UnsupportedOperationException("This method cannot be executed on an ImmutableLinkedHashMap");
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException always, linked hash map is immutable
*/
@Override
public boolean remove(Object key, Object value) {
throw new UnsupportedOperationException("This method cannot be executed on an ImmutableLinkedHashMap");
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException always, linked hash map is immutable
*/
@Override
public boolean replace(K key, V oldValue, V newValue) {
throw new UnsupportedOperationException("This method cannot be executed on an ImmutableLinkedHashMap");
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException always, linked hash map is immutable
*/
@Override
public @Nullable V replace(K key, V value) {
throw new UnsupportedOperationException("This method cannot be executed on an ImmutableLinkedHashMap");
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException always, linked hash map is immutable
*/
@Override
public @Nullable V merge(K key, @NotNull V value, @NotNull BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
throw new UnsupportedOperationException("This method cannot be executed on an ImmutableLinkedHashMap");
}
}

View file

@ -0,0 +1,222 @@
/*
* STAROPENSOURCE ENGINE SOURCE FILE
* Copyright (c) 2024 The StarOpenSource Engine Authors
* 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.engine.base.type.immutable;
import org.jetbrains.annotations.NotNull;
import java.util.*;
import java.util.function.UnaryOperator;
/**
* An unmodifiable {@link LinkedList}.
*
* @param <E> contained type
* @since v1-alpha1
*/
@SuppressWarnings({ "unused" })
public class ImmutableLinkedList<E> extends LinkedList<E> {
/**
* Creates and initializes an instance of this class.
*
* @since v1-alpha1
*/
public ImmutableLinkedList() {}
/**
* Creates and initializes an instance of this class.
* Converts a {@link LinkedList} into an {@link ImmutableLinkedList}.
*
* @param list {@link List} to convert
* @since v1-alpha1
*/
public ImmutableLinkedList(@NotNull List<E> list) {
super.addAll(size(), list);
}
/**
* Converts the {@link ImmutableLinkedList} into a regular {@link LinkedList}.
*
* @return mutable {@link LinkedList}
* @since v1-alpha2
*/
public @NotNull LinkedList<E> toMutable() {
return new LinkedList<>(this);
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException always, linked list is immutable
*/
@Override
public boolean add(E e) {
throw new UnsupportedOperationException("This method cannot be executed on an ImmutableLinkedList");
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException always, linked list is immutable
*/
@Override
public boolean remove(Object o) {
throw new UnsupportedOperationException("This method cannot be executed on an ImmutableLinkedList");
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException always, linked list is immutable
*/
@Override
public boolean addAll(@NotNull Collection<? extends E> c) {
throw new UnsupportedOperationException("This method cannot be executed on an ImmutableLinkedList");
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException always, linked list is immutable
*/
@Override
public boolean addAll(int index, @NotNull Collection<? extends E> c) {
throw new UnsupportedOperationException("This method cannot be executed on an ImmutableLinkedList");
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException always, linked list is immutable
*/
@Override
public boolean removeAll(@NotNull Collection<?> c) {
throw new UnsupportedOperationException("This method cannot be executed on an ImmutableLinkedList");
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException always, linked list is immutable
*/
@Override
public boolean retainAll(@NotNull Collection<?> c) {
throw new UnsupportedOperationException("This method cannot be executed on an ImmutableLinkedList");
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException always, linked list is immutable
*/
@Override
public void replaceAll(UnaryOperator<E> operator) {
throw new UnsupportedOperationException("This method cannot be executed on an ImmutableLinkedList");
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException always, linked list is immutable
*/
@Override
public void sort(Comparator<? super E> c) {
throw new UnsupportedOperationException("This method cannot be executed on an ImmutableLinkedList");
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException always, linked list is immutable
*/
@Override
public void clear() {
throw new UnsupportedOperationException("This method cannot be executed on an ImmutableLinkedList");
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException always, linked list is immutable
*/
@Override
public E set(int index, E element) throws UnsupportedOperationException {
throw new UnsupportedOperationException("This method cannot be executed on an ImmutableLinkedList");
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException always, linked list is immutable
*/
@Override
public void add(int index, E element) {
throw new UnsupportedOperationException("This method cannot be executed on an ImmutableLinkedList");
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException always, linked list is immutable
*/
@Override
public E remove(int index) {
throw new UnsupportedOperationException("This method cannot be executed on an ImmutableLinkedList");
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException always, linked list is immutable
*/
@Override
public void addFirst(E e) {
throw new UnsupportedOperationException("This method cannot be executed on an ImmutableLinkedList");
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException always, linked list is immutable
*/
@Override
public void addLast(E e) {
throw new UnsupportedOperationException("This method cannot be executed on an ImmutableLinkedList");
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException always, linked list is immutable
*/
@Override
public E removeFirst() {
throw new UnsupportedOperationException("This method cannot be executed on an ImmutableLinkedList");
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException always, linked list is immutable
*/
@Override
public E removeLast() {
throw new UnsupportedOperationException("This method cannot be executed on an ImmutableLinkedList");
}
}

View file

@ -0,0 +1,25 @@
/*
* STAROPENSOURCE ENGINE SOURCE FILE
* Copyright (c) 2024 The StarOpenSource Engine Authors
* 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/>.
*/
/**
* Immutable variations of regular Java data types.
*
* @since v1-alpha1
*/
package de.staropensource.engine.base.type.immutable;

View file

@ -0,0 +1,68 @@
/*
* STAROPENSOURCE ENGINE SOURCE FILE
* Copyright (c) 2024 The StarOpenSource Engine Authors
* 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.engine.base.type.logging;
import lombok.Getter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* Defines a rule used during logging to allow or prevent certain log calls.
*
* @since v1-alpha1
*/
@Getter
@SuppressWarnings({ "JavadocDeclaration" })
public abstract class LogRule {
/**
* Contains if the log rule should disallow or permit matching log calls.
*
* @see LogRuleType
* @since v1-alpha1
* -- GETTER --
* Returns if the log rule should disallow or permit matching log calls.
*
* @return rule type
* @see LogRuleType
* @since v1-alpha1
*/
private final @NotNull LogRuleType type;
/**
* Initializes this abstract class.
*
* @param type rule type
*/
public LogRule(@NotNull LogRuleType type) {
this.type = type;
}
/**
* Evaluates the rule and returns if the rule matches.
*
* @param level level
* @param issuerClass class of the issuer
* @param issuerOrigin origin of the issuer
* @param issuerMetadata metadata about the issuer
* @param message raw message
* @return if the rule matches
*/
public abstract boolean evaluate(@NotNull LogLevel level, @NotNull Class<?> issuerClass, @NotNull String issuerOrigin, @Nullable String issuerMetadata, @NotNull String message);
}

View file

@ -17,13 +17,27 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
//package de.staropensource.engine.base.utility.information; package de.staropensource.engine.base.type.logging;
/** /**
* Provides information about the environment * Used for determining if a {@link LogRule} should allow (whitelist) or prevent (blacklist) log call processing.
* ie. the operating system, paths, etc..
* *
* @since v1-alpha8 * @since v1-alpha1
*/ */
//public final class EnvironmentInformation { public enum LogRuleType {
//} /**
* Indicates that log calls matching this rule will always be allowed, even if blacklisted.
*
* @see #BLACKLIST
* @since v1-alpha1
*/
WHITELIST,
/**
* Indicates that log calls matching this will always be disallowed except when whitelisted.
*
* @see #WHITELIST
* @since v1-alpha1
*/
BLACKLIST
}

View file

@ -22,7 +22,7 @@ package de.staropensource.engine.base.utility;
import de.staropensource.engine.base.exception.dependency.DependencyCycleException; import de.staropensource.engine.base.exception.dependency.DependencyCycleException;
import de.staropensource.engine.base.exception.dependency.UnmetDependenciesException; import de.staropensource.engine.base.exception.dependency.UnmetDependenciesException;
import de.staropensource.engine.base.implementable.VersioningSystem; import de.staropensource.engine.base.implementable.VersioningSystem;
import de.staropensource.engine.base.logging.Logger; import de.staropensource.engine.base.logging.LoggerInstance;
import de.staropensource.engine.base.type.DependencyVector; import de.staropensource.engine.base.type.DependencyVector;
import lombok.Getter; import lombok.Getter;
import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.ApiStatus;
@ -39,6 +39,14 @@ import java.util.*;
*/ */
@SuppressWarnings({ "unused", "UnusedReturnValue", "JavadocDeclaration" }) @SuppressWarnings({ "unused", "UnusedReturnValue", "JavadocDeclaration" })
public final class DependencyResolver { public final class DependencyResolver {
/**
* Contains the {@link LoggerInstance} for this instance.
*
* @see LoggerInstance
* @since v1-alpha4
*/
private final @NotNull LoggerInstance logger = new LoggerInstance.Builder().setClazz(getClass()).setOrigin("ENGINE").setMetadata(String.valueOf(hashCode())).build();
/** /**
* List of {@link DependencyVector}s to resolve. * List of {@link DependencyVector}s to resolve.
* *
@ -267,7 +275,7 @@ public final class DependencyResolver {
try { try {
versioningSystemResolved = vectorDependency.getVersioningSystem().getDeclaredConstructor(String.class).newInstance(vectorDependency.getVersion()); versioningSystemResolved = vectorDependency.getVersioningSystem().getDeclaredConstructor(String.class).newInstance(vectorDependency.getVersion());
} catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException exception) { } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException exception) {
Logger.crash("Unable to check version of dependency \"" + dependency + "\": Unable to initialize versioning system " + vectorDependency.getVersioningSystem().getName(), exception); logger.crash("Unable to check version of dependency \"" + dependency + "\": Unable to initialize versioning system " + vectorDependency.getVersioningSystem().getName(), exception);
break; break;
} }
@ -279,7 +287,7 @@ public final class DependencyResolver {
try { try {
versioningSystemEquals = vectorDependency.getVersioningSystem().getDeclaredConstructor(String.class).newInstance(versionEqual.toString()); versioningSystemEquals = vectorDependency.getVersioningSystem().getDeclaredConstructor(String.class).newInstance(versionEqual.toString());
} catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException exception) { } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException exception) {
Logger.crash("Unable to check version of dependency \"" + dependency + "\": Unable to initialize versioning system " + vectorDependency.getVersioningSystem().getName(), exception); logger.crash("Unable to check version of dependency \"" + dependency + "\": Unable to initialize versioning system " + vectorDependency.getVersioningSystem().getName(), exception);
break; break;
} }
@ -295,7 +303,7 @@ public final class DependencyResolver {
try { try {
versioningSystemSmaller = vectorDependency.getVersioningSystem().getDeclaredConstructor(String.class).newInstance(versionSmaller.toString()); versioningSystemSmaller = vectorDependency.getVersioningSystem().getDeclaredConstructor(String.class).newInstance(versionSmaller.toString());
} catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException exception) { } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException exception) {
Logger.crash("Unable to check version of dependency \"" + dependency + "\": Unable to initialize versioning system " + vectorDependency.getVersioningSystem().getName(), exception); logger.crash("Unable to check version of dependency \"" + dependency + "\": Unable to initialize versioning system " + vectorDependency.getVersioningSystem().getName(), exception);
break; break;
} }
if (!versionBigger.isEmpty()) if (!versionBigger.isEmpty())
@ -303,7 +311,7 @@ public final class DependencyResolver {
try { try {
versioningSystemBigger = vectorDependency.getVersioningSystem().getDeclaredConstructor(String.class).newInstance(versionBigger.toString()); versioningSystemBigger = vectorDependency.getVersioningSystem().getDeclaredConstructor(String.class).newInstance(versionBigger.toString());
} catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException exception) { } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException exception) {
Logger.crash("Unable to check version of dependency \"" + dependency + "\": Unable to initialize versioning system " + vectorDependency.getVersioningSystem().getName(), exception); logger.crash("Unable to check version of dependency \"" + dependency + "\": Unable to initialize versioning system " + vectorDependency.getVersioningSystem().getName(), exception);
break; break;
} }

View file

@ -17,49 +17,43 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
package de.staropensource.engine.base.utility.misc; package de.staropensource.engine.base.utility;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.Collection; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
/** /**
* Converts arrays, arrays and maps to look like * Converts various data types to {@link String}s.
* nicely formatted lists (as {@link String}s).
* *
* @since v1-alpha9 * @since v1-alpha4
*/ */
public final class ListFormatter { public final class ListFormatter {
/** /**
* Creates and initializes an instance of this class. * Creates and initializes an instance of this class.
* *
* @since v1-alpha9 * @since v1-alpha6
*/ */
private ListFormatter() {} private ListFormatter() {}
/** /**
* Converts the specified array to a {@link String}. * Converts an array to a {@link String}.
* <p>
* For {@code formatArray(array, ", ", " & ")} the
* something like following string will be returned:
* {@code element 1, element 2, element 3 & element 4}
* *
* @param array array to convert * @param array array to convert
* @param itemSeparator string used to separate two items
* @param finalSeparator string used to separate the final two items
* @return formatted string * @return formatted string
* @since v1-alpha9 * @since v1-alpha4
*/ */
public static @NotNull String formatArray(@NotNull Object @NotNull [] array, @NotNull String itemSeparator, @NotNull String finalSeparator) { public static @NotNull String formatArray(@NotNull Object @NotNull [] array) {
StringBuilder output = new StringBuilder(); StringBuilder output = new StringBuilder();
for (int index = 0; index < array.length; index++) { for (int index = 0; index < array.length; index++) {
if (!output.isEmpty()) if (!output.isEmpty())
if (index == array.length - 1) if (index == array.length - 1)
output.append(finalSeparator); output.append(" & ");
else else
output.append(itemSeparator); output.append(", ");
output.append(array[index]); output.append(array[index]);
} }
@ -68,50 +62,25 @@ public final class ListFormatter {
} }
/** /**
* Converts the specified array to a {@link String}. * Converts a {@link Set} to a {@link String}.
* <p>
* This will produce something like this:
* {@code element 1, element 2, element 3 & element 4}
* *
* @param array array to convert * @param set set to convert
* @return formatted string * @return formatted string
* @since v1-alpha9 * @since v1-alpha4
*/ */
public static @NotNull String formatArray(@NotNull Object @NotNull [] array) { public static @NotNull String formatSet(@NotNull Set<?> set) {
return formatArray(array, ", ", " & "); return formatArray(set.toArray());
} }
/** /**
* Converts a {@link List} to a {@link String}.
/**
* Converts the specified array to a {@link String}.
* <p>
* For {@code formatArray(array, ", ", " & ")} the
* something like following string will be returned:
* {@code element 1, element 2, element 3 & element 4}
* *
* @param collection collection to convert * @param list list to convert
* @param itemSeparator string used to separate two items
* @param finalSeparator string used to separate the final two items
* @return formatted string * @return formatted string
* @since v1-alpha9 * @since v1-alpha4
*/ */
public static @NotNull String formatCollection(@NotNull Collection<?> collection, @NotNull String itemSeparator, @NotNull String finalSeparator) { public static @NotNull String formatList(@NotNull List<?> list) {
return formatArray(collection.toArray(), itemSeparator, finalSeparator); return formatArray(list.toArray());
}
/**
* Converts the specified {@link Collection} to a {@link String}.
* <p>
* This will produce something like this:
* {@code element 1, element 2, element 3 & element 4}
*
* @param collection collection to convert
* @return formatted string
* @since v1-alpha9
*/
public static @NotNull String formatCollection(@NotNull Collection<?> collection) {
return formatArray(collection.toArray());
} }
/** /**
@ -119,7 +88,7 @@ public final class ListFormatter {
* *
* @param map map to convert * @param map map to convert
* @return formatted string * @return formatted string
* @since v1-alpha9 * @since v1-alpha4
*/ */
public static @NotNull String formatMap(@NotNull Map<?, ?> map) { public static @NotNull String formatMap(@NotNull Map<?, ?> map) {
StringBuilder output = new StringBuilder(); StringBuilder output = new StringBuilder();

View file

@ -17,7 +17,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
package de.staropensource.engine.base.utility.misc; package de.staropensource.engine.base.utility;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -27,36 +27,31 @@ import java.util.LinkedList;
/** /**
* Utility class for math operations. * Utility class for math operations.
* *
* @since v1-alpha9 * @since v1-alpha2
*/ */
// All mean and double methods have been sourced // All mean and double methods have been sourced
// from https://stackoverflow.com/a/4191729, tysm! // from https://stackoverflow.com/a/4191729, tysm!
@SuppressWarnings({ "unused" }) @SuppressWarnings({ "unused" })
public final class NumberUtil { public final class Math {
/** /**
* Creates and initializes an instance of this class * Creates and initializes an instance of this class
* *
* @since v1-alpha9 * @since v1-alpha6
*/ */
private NumberUtil() {} private Math() {}
/** /**
* Adds padding zeros to a number. * Adds padding zeros to a number.
* <p>
* Example:
* {@code padNumbers(505L, 5) = "00505"}
* *
* @param number number * @param number number
* @param length length * @param length length
* @return the padded number * @return the padded number
* @since v1-alpha9 * @since v1-alpha2
*/ */
public static @NotNull String padNumbers(long number, int length) { public static @NotNull String padNumbers(long number, int length) {
return String.format("%0" + length + "d", number); return String.format("%0" + length + "d", number);
} }
// -----> Number limiting
/** /**
* Ensures the integer is inside the * Ensures the integer is inside the
* specified bounds. If not, the * specified bounds. If not, the
@ -66,10 +61,10 @@ public final class NumberUtil {
* @param max maximum value * @param max maximum value
* @param value value * @param value value
* @return number in threshold * @return number in threshold
* @throws IndexOutOfBoundsException if the minimum value is bigger than the maximum value * @throws IndexOutOfBoundsException when the minimum value is bigger than the maximum value
* @since v1-alpha9 * @since v1-alpha6
*/ */
public static int limitNumber(int min, int max, int value) throws IndexOutOfBoundsException { public static int boundNumber(int min, int max, int value) throws IndexOutOfBoundsException {
if (min > max) if (min > max)
throw new IndexOutOfBoundsException(); throw new IndexOutOfBoundsException();
@ -90,10 +85,10 @@ public final class NumberUtil {
* @param max maximum value * @param max maximum value
* @param value value * @param value value
* @return number in threshold * @return number in threshold
* @throws IndexOutOfBoundsException if the minimum value is bigger than the maximum value * @throws IndexOutOfBoundsException when the minimum value is bigger than the maximum value
* @since v1-alpha9 * @since v1-alpha6
*/ */
public static float limitNumber(float min, float max, float value) throws IndexOutOfBoundsException { public static float boundNumber(float min, float max, float value) throws IndexOutOfBoundsException {
if (min > max) if (min > max)
throw new IndexOutOfBoundsException(); throw new IndexOutOfBoundsException();
@ -114,10 +109,10 @@ public final class NumberUtil {
* @param max maximum value * @param max maximum value
* @param value value * @param value value
* @return number in threshold * @return number in threshold
* @throws IndexOutOfBoundsException if the minimum value is bigger than the maximum value * @throws IndexOutOfBoundsException when the minimum value is bigger than the maximum value
* @since v1-alpha9 * @since v1-alpha6
*/ */
public static double limitNumber(double min, double max, double value) throws IndexOutOfBoundsException { public static double boundNumber(double min, double max, double value) throws IndexOutOfBoundsException {
if (min > max) if (min > max)
throw new IndexOutOfBoundsException(); throw new IndexOutOfBoundsException();
@ -129,16 +124,14 @@ public final class NumberUtil {
return value; return value;
} }
// -----> Means and medians
/** /**
* Returns the mean of a collection of numbers. * Returns the mean of a collection of numbers.
* *
* @param collection collection of {@code double}s * @param collection collection of {@code double}s
* @return mean * @return mean
* @since v1-alpha9 * @since v1-alpha2
*/ */
public static double calculateMeanDouble(@NotNull Collection<@NotNull Double> collection) { public static double getMeanDouble(@NotNull Collection<@NotNull Double> collection) {
double sum = 0; double sum = 0;
for (double number : collection) for (double number : collection)
@ -152,9 +145,9 @@ public final class NumberUtil {
* *
* @param collection collection of {@code float}s * @param collection collection of {@code float}s
* @return mean * @return mean
* @since v1-alpha9 * @since v1-alpha2
*/ */
public static double calculateMeanFloat(@NotNull Collection<@NotNull Float> collection) { public static double getMeanFloat(@NotNull Collection<@NotNull Float> collection) {
double sum = 0; double sum = 0;
for (float number : collection) for (float number : collection)
@ -168,9 +161,9 @@ public final class NumberUtil {
* *
* @param collection collection of {@code long}s * @param collection collection of {@code long}s
* @return mean * @return mean
* @since v1-alpha9 * @since v1-alpha2
*/ */
public static double calculateMeanLong(@NotNull Collection<@NotNull Long> collection) { public static double getMeanLong(@NotNull Collection<@NotNull Long> collection) {
double sum = 0; double sum = 0;
for (long number : collection) for (long number : collection)
@ -184,9 +177,9 @@ public final class NumberUtil {
* *
* @param collection collection of {@code int}s * @param collection collection of {@code int}s
* @return mean * @return mean
* @since v1-alpha9 * @since v1-alpha2
*/ */
public static double calculateMeanInt(@NotNull Collection<@NotNull Integer> collection) { public static double getMeanInt(@NotNull Collection<@NotNull Integer> collection) {
double sum = 0; double sum = 0;
for (int number : collection) for (int number : collection)
@ -200,9 +193,9 @@ public final class NumberUtil {
* *
* @param list linked list of {@code double}s * @param list linked list of {@code double}s
* @return median * @return median
* @since v1-alpha9 * @since v1-alpha2
*/ */
public static double calculateMedianDouble(@NotNull LinkedList<@NotNull Double> list) { public static double getMedianDouble(@NotNull LinkedList<@NotNull Double> list) {
int middle = list.size() / 2; int middle = list.size() / 2;
if (list.size() % 2 == 1) if (list.size() % 2 == 1)
return list.get(middle); return list.get(middle);
@ -215,9 +208,9 @@ public final class NumberUtil {
* *
* @param list linked list of {@code float}s * @param list linked list of {@code float}s
* @return median * @return median
* @since v1-alpha9 * @since v1-alpha2
*/ */
public static double calculateMedianFloat(@NotNull LinkedList<@NotNull Float> list) { public static double getMedianFloat(@NotNull LinkedList<@NotNull Float> list) {
int middle = list.size() / 2; int middle = list.size() / 2;
if (list.size() % 2 == 1) if (list.size() % 2 == 1)
return list.get(middle); return list.get(middle);
@ -230,9 +223,9 @@ public final class NumberUtil {
* *
* @param list linked list of {@code long}s * @param list linked list of {@code long}s
* @return median * @return median
* @since v1-alpha9 * @since v1-alpha2
*/ */
public static double calculateMedianLong(@NotNull LinkedList<@NotNull Long> list) { public static double getMedianLong(@NotNull LinkedList<@NotNull Long> list) {
int middle = list.size() / 2; int middle = list.size() / 2;
if (list.size() % 2 == 1) if (list.size() % 2 == 1)
return list.get(middle); return list.get(middle);
@ -245,9 +238,9 @@ public final class NumberUtil {
* *
* @param list linked list of {@code int}s * @param list linked list of {@code int}s
* @return median * @return median
* @since v1-alpha9 * @since v1-alpha2
*/ */
public static double calculateMedianInt(@NotNull LinkedList<@NotNull Integer> list) { public static double getMedianInt(@NotNull LinkedList<@NotNull Integer> list) {
int middle = list.size() / 2; int middle = list.size() / 2;
if (list.size() % 2 == 1) if (list.size() % 2 == 1)
return list.get(middle); return list.get(middle);

View file

@ -0,0 +1,254 @@
/*
* STAROPENSOURCE ENGINE SOURCE FILE
* Copyright (c) 2024 The StarOpenSource Engine Authors
* 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.engine.base.utility;
import de.staropensource.engine.base.event.ThrowableCatchEvent;
import de.staropensource.engine.base.exception.TristateConversionException;
import de.staropensource.engine.base.type.Tristate;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Range;
import java.lang.ref.WeakReference;
import java.util.*;
import java.util.stream.Collectors;
/**
* Contains smaller functions that don't fit into other utility classes.
*
* @since v1-alpha0
*/
@SuppressWarnings({ "unused" })
public final class Miscellaneous {
/**
* Creates and initializes an instance of this class.
*
* @since v1-alpha6
*/
private Miscellaneous() {}
/**
* Converts a boolean into an integer.
*
* @param bool boolean to convert
* @return converted integer
* @since v1-alpha2
*/
@Range(from = 0, to = 1)
public static int getIntegerizedBoolean(boolean bool) {
return bool ? 1 : 0;
}
/**
* Converts an integer into a {@link Tristate}.
*
* @param integer integer to convert
* @return expected boolean result, except if neither {@code 0} or {@code 1}, in which case {@link Tristate#UNSET} is returned
* @since v1-alpha2
*/
public static Tristate getTristatedInteger(@Range(from = 0, to = 1) int integer) {
return switch (integer) {
case 0 -> Tristate.TRUE;
case 1 -> Tristate.FALSE;
default -> Tristate.UNSET;
};
}
/**
* Converts an integer into a {@link Tristate} and then into a boolean.
*
* @param integer integer to convert
* @return booleanized integer
* @throws TristateConversionException when encountering {@link Tristate#UNSET}.
* @since v1-alpha2
*/
public static boolean getBooleanizedInteger(@Range(from = 0, to = 1) int integer) throws TristateConversionException {
return getTristatedInteger(integer).toBoolean();
}
/**
* Searches for a value in a {@link Map}.
*
* @param map map to use
* @param value value to search for
* @return all keys matching the specified value
* @since v1-alpha0
*/
public static Set<?> getMapValues(@NotNull Map<?, ?> map, @Nullable Object value) {
return map
.entrySet().stream()
.filter(entry -> Objects.equals(entry.getValue(), value))
.map(Map.Entry::getKey)
.collect(Collectors.toSet());
}
/**
* Counts the occurrences of a substring inside of a string.
*
* @param string string to search
* @param substring substring to search for
* @return occurrences
*/
public static long countOccurrences(@NotNull String string, @NotNull String substring) {
return (string.length() - string.replace(substring, "").length()) / substring.length();
}
/**
* Returns the correct separator to use when splitting a string.
*
* @param string string to split
* @param separators separators to check
* @param requiredOccurrences exact amount of occurrences for a separator to be deemed valid
* @return separator to use or {@code null}
* @since v1-alpha1
*/
public static @Nullable String getSeparator(@NotNull String string, @NotNull String @NotNull [] separators, int requiredOccurrences) {
if (string.isBlank() || separators.length == 0 || requiredOccurrences == 0)
return null;
for (String separator : separators)
if (countOccurrences(string, separator) == requiredOccurrences)
return separator;
return null;
}
/**
* Returns the correct separator to use when splitting a string.
*
* @param string string to split
* @param separators separators to check
* @param minimumOccurrences minimum amount of occurrences for a separator to be deemed valid
* @return separator to use or {@code null}
* @since v1-alpha1
*/
public static @Nullable String getSeparator(@NotNull String string, List<String> separators, Integer minimumOccurrences) {
for (String separator : separators)
if (countOccurrences(string, separator) >= minimumOccurrences)
return separator;
return null;
}
/**
* Measures the execution time of a {@link Runnable}.
*
* @param runnable {@link Runnable} to execute
* @return execution time in milliseconds
* @see Runnable
* @since v1-alpha0
*/
public static long measureExecutionTime(@NotNull Runnable runnable) {
long initTime = System.currentTimeMillis();
runnable.run();
return System.currentTimeMillis() - initTime;
}
/**
* Executes a {@link Runnable} and emits {@link ThrowableCatchEvent} if a throwable is caught.
*
* @param runnable {@link Runnable} to execute
* @param identifier some identifier to distinguish {@link Runnable}s
* @since v1-alpha1
*/
public static void executeSafely(@NotNull Runnable runnable, @NotNull String identifier) {
try {
runnable.run();
} catch (Throwable throwable) {
new ThrowableCatchEvent().callEvent(throwable, identifier);
}
}
/**
* Forcefully invokes the garbage collector
* and blocks execution until finished.
* If you want to run it in parallel to your
* program, consider running it in a {@code VirtualThread}.
* <p>
* This method does not guarantee full garbage collection,
* as the JVM only hints the garbage collector to do it's job.
* This is because all garbage collectors are non-deterministic.
*
* @since v1-alpha0
*/
@SuppressWarnings("UnusedAssignment")
public static void invokeGarbageCollector() {
Object object = new Object();
WeakReference<Object> weakReference = new WeakReference<>(object);
object = null;
while(weakReference.get() != null) System.gc();
}
/**
* Ensures that the code is running on the main thread.
*
* @return {@code true} if running on the main thread, {@code false} otherwise
* @since v1-alpha2
*/
public static boolean onMainThread() {
return Thread.currentThread().threadId() == 1;
}
/**
* Returns the {@code Caused by} message usually found when the JVM prints a throwable.
*
* @param throwable {@link Throwable} to use
* @return stack trace header
* @since v1-alpha4
*/
public static @NotNull String getStackTraceHeader(@NotNull Throwable throwable) {
return "Caused by: " + throwable.getClass().getName() + (throwable.getMessage() == null ? "" : ": " + throwable.getMessage());
}
/**
* Converts an array of {@link StackTraceElement}s into a regular string.
*
* @param stacktrace array of {@link StackTraceElement}s to convert
* @param indent if all lines should be indented with a single {@code \t} character, like in regular stack traces
* @return converted stacktrace string
* @since v1-alpha4
*/
public static @NotNull String getStackTraceAsString(@NotNull StackTraceElement @NotNull [] stacktrace, boolean indent) {
StringBuilder output = new StringBuilder();
for (StackTraceElement element : stacktrace) {
if (!output.isEmpty())
output.append("\n");
if (indent)
output.append("\t");
output.append("at ").append(element);
}
return output.toString();
}
/**
* Converts the stacktrace of a throwable into a regular string.
*
* @param throwable throwable to derive the stacktrace from
* @param indent if all lines should be indented with a single {@code \t} character, like in regular stack traces
* @return converted stacktrace string
* @since v1-alpha4
*/
public static @NotNull String getStackTraceAsString(@NotNull Throwable throwable, boolean indent) {
return getStackTraceAsString(throwable.getStackTrace(), indent);
}
}

View file

@ -22,6 +22,7 @@ package de.staropensource.engine.base.utility;
import de.staropensource.engine.base.Engine; import de.staropensource.engine.base.Engine;
import de.staropensource.engine.base.implementable.Placeholder; import de.staropensource.engine.base.implementable.Placeholder;
import de.staropensource.engine.base.internal.implementation.placeholder.*; import de.staropensource.engine.base.internal.implementation.placeholder.*;
import de.staropensource.engine.base.logging.LoggerInstance;
import lombok.Getter; import lombok.Getter;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -49,6 +50,14 @@ public final class PlaceholderEngine {
@Getter @Getter
private static PlaceholderEngine instance; private static PlaceholderEngine instance;
/**
* Contains the {@link LoggerInstance} for this instance.
*
* @see LoggerInstance
* @since v1-alpha1
*/
private final @NotNull LoggerInstance logger = new LoggerInstance.Builder().setClazz(getClass()).setOrigin("ENGINE").build();
/** /**
* Contains all global placeholders. * Contains all global placeholders.
* *
@ -97,7 +106,6 @@ public final class PlaceholderEngine {
placeholders.add(new EngineGitDirty()); placeholders.add(new EngineGitDirty());
// engine_version* // engine_version*
placeholders.add(new EngineVersion()); placeholders.add(new EngineVersion());
placeholders.add(new EngineVersionCodename());
placeholders.add(new EngineVersionVersion()); placeholders.add(new EngineVersionVersion());
placeholders.add(new EngineVersionType()); placeholders.add(new EngineVersionType());
placeholders.add(new EngineVersionTyperelease()); placeholders.add(new EngineVersionTyperelease());

View file

@ -19,7 +19,7 @@
package de.staropensource.engine.base.utility; package de.staropensource.engine.base.utility;
import de.staropensource.engine.base.logging.Logger; import de.staropensource.engine.base.logging.LoggerInstance;
import de.staropensource.engine.base.type.Tristate; import de.staropensource.engine.base.type.Tristate;
import lombok.Getter; import lombok.Getter;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -49,6 +49,14 @@ public final class PropertiesReader {
@Getter @Getter
private static final @NotNull PropertiesReader instance = new PropertiesReader(System.getProperties()); private static final @NotNull PropertiesReader instance = new PropertiesReader(System.getProperties());
/**
* Contains the {@link LoggerInstance} for this instance.
*
* @see LoggerInstance
* @since v1-alpha0
*/
private final @NotNull LoggerInstance logger;
/** /**
* Contains the {@link Properties} used by this parser instance to read properties. * Contains the {@link Properties} used by this parser instance to read properties.
* *
@ -72,6 +80,7 @@ public final class PropertiesReader {
*/ */
public PropertiesReader(@NotNull Properties properties) { public PropertiesReader(@NotNull Properties properties) {
this.properties = properties; this.properties = properties;
this.logger = new LoggerInstance.Builder().setClazz(getClass()).setOrigin("ENGINE").setMetadata(String.valueOf(properties.hashCode())).build();
} }
/** /**
@ -85,7 +94,7 @@ public final class PropertiesReader {
*/ */
public @NotNull String getString(@NotNull String name) throws NullPointerException { public @NotNull String getString(@NotNull String name) throws NullPointerException {
if (properties.getProperty(name) == null) { if (properties.getProperty(name) == null) {
Logger.sarn("Unable to get String from property '" + name + "': Property does not exist"); logger.sarn("Unable to get String from property '" + name + "': Property does not exist");
throw new NullPointerException("Unable to get String from property '" + name + "': Property does not exist"); throw new NullPointerException("Unable to get String from property '" + name + "': Property does not exist");
} }
return properties.getProperty(name); return properties.getProperty(name);
@ -102,7 +111,7 @@ public final class PropertiesReader {
*/ */
public boolean getBoolean(@NotNull String name) throws NullPointerException { public boolean getBoolean(@NotNull String name) throws NullPointerException {
if (properties.getProperty(name) == null) { if (properties.getProperty(name) == null) {
Logger.sarn("Unable to get Boolean from property '" + name + "': Property does not exist"); logger.sarn("Unable to get Boolean from property '" + name + "': Property does not exist");
throw new NullPointerException("Unable to get Boolean from property '" + name + "': Property does not exist"); throw new NullPointerException("Unable to get Boolean from property '" + name + "': Property does not exist");
} }
@ -127,7 +136,7 @@ public final class PropertiesReader {
*/ */
public @NotNull Tristate getTristate(@NotNull String name) throws NullPointerException { public @NotNull Tristate getTristate(@NotNull String name) throws NullPointerException {
if (properties.getProperty(name) == null) { if (properties.getProperty(name) == null) {
Logger.sarn("Unable to get Tristate from property '" + name + "': Property does not exist"); logger.sarn("Unable to get Tristate from property '" + name + "': Property does not exist");
throw new NullPointerException("Unable to get Tristate from property '" + name + "': Property does not exist"); throw new NullPointerException("Unable to get Tristate from property '" + name + "': Property does not exist");
} }
@ -156,14 +165,14 @@ public final class PropertiesReader {
*/ */
public byte getByte(@NotNull String name) throws NullPointerException, NumberFormatException { public byte getByte(@NotNull String name) throws NullPointerException, NumberFormatException {
if (properties.getProperty(name) == null) { if (properties.getProperty(name) == null) {
Logger.sarn("Unable to get Byte from property '" + name + "': Property does not exist"); logger.sarn("Unable to get Byte from property '" + name + "': Property does not exist");
throw new NullPointerException("Unable to get Byte from property '" + name + "': Property does not exist"); throw new NullPointerException("Unable to get Byte from property '" + name + "': Property does not exist");
} }
try { try {
return Byte.parseByte(properties.getProperty(name)); return Byte.parseByte(properties.getProperty(name));
} catch (NumberFormatException exception) { } catch (NumberFormatException exception) {
Logger.sarn("Unable to get Byte from property '" + name + "': String cannot be parsed as a Byte."); logger.sarn("Unable to get Byte from property '" + name + "': String cannot be parsed as a Byte.");
throw exception; throw exception;
} }
} }
@ -180,14 +189,14 @@ public final class PropertiesReader {
*/ */
public short getShort(@NotNull String name) throws NullPointerException, NumberFormatException { public short getShort(@NotNull String name) throws NullPointerException, NumberFormatException {
if (properties.getProperty(name) == null) { if (properties.getProperty(name) == null) {
Logger.sarn("Unable to get Short from property '" + name + "': Property does not exist"); logger.sarn("Unable to get Short from property '" + name + "': Property does not exist");
throw new NullPointerException("Unable to get Short from property '" + name + "': Property does not exist"); throw new NullPointerException("Unable to get Short from property '" + name + "': Property does not exist");
} }
try { try {
return Short.parseShort(properties.getProperty(name)); return Short.parseShort(properties.getProperty(name));
} catch (NumberFormatException exception) { } catch (NumberFormatException exception) {
Logger.sarn("Unable to get Short from property '" + name + "': String cannot be parsed as a Short."); logger.sarn("Unable to get Short from property '" + name + "': String cannot be parsed as a Short.");
throw exception; throw exception;
} }
} }
@ -205,7 +214,7 @@ public final class PropertiesReader {
*/ */
public int getInteger(@NotNull String name, boolean unsigned) throws NullPointerException, NumberFormatException { public int getInteger(@NotNull String name, boolean unsigned) throws NullPointerException, NumberFormatException {
if (properties.getProperty(name) == null) { if (properties.getProperty(name) == null) {
Logger.sarn("Unable to get Integer from property '" + name + "': Property does not exist"); logger.sarn("Unable to get Integer from property '" + name + "': Property does not exist");
throw new NullPointerException("Unable to get Integer from property '" + name + "': Property does not exist"); throw new NullPointerException("Unable to get Integer from property '" + name + "': Property does not exist");
} }
@ -215,7 +224,7 @@ public final class PropertiesReader {
else else
return Integer.parseInt(properties.getProperty(name)); return Integer.parseInt(properties.getProperty(name));
} catch (NumberFormatException exception) { } catch (NumberFormatException exception) {
Logger.sarn("Unable to get Integer from property '" + name + "': String cannot be parsed as an Integer."); logger.sarn("Unable to get Integer from property '" + name + "': String cannot be parsed as an Integer.");
throw exception; throw exception;
} }
} }
@ -233,7 +242,7 @@ public final class PropertiesReader {
*/ */
public long getLong(@NotNull String name, boolean unsigned) throws NullPointerException, NumberFormatException { public long getLong(@NotNull String name, boolean unsigned) throws NullPointerException, NumberFormatException {
if (properties.getProperty(name) == null) { if (properties.getProperty(name) == null) {
Logger.sarn("Unable to get Long from property '" + name + "': Property does not exist"); logger.sarn("Unable to get Long from property '" + name + "': Property does not exist");
throw new NullPointerException("Unable to get Long from property '" + name + "': Property does not exist"); throw new NullPointerException("Unable to get Long from property '" + name + "': Property does not exist");
} }
@ -243,7 +252,7 @@ public final class PropertiesReader {
else else
return Long.parseLong(properties.getProperty(name)); return Long.parseLong(properties.getProperty(name));
} catch (NumberFormatException exception) { } catch (NumberFormatException exception) {
Logger.sarn("Unable to get Long from property '" + name + "': String cannot be parsed as a Long."); logger.sarn("Unable to get Long from property '" + name + "': String cannot be parsed as a Long.");
throw exception; throw exception;
} }
} }
@ -260,14 +269,14 @@ public final class PropertiesReader {
*/ */
public float getFloat(@NotNull String name) throws NullPointerException, NumberFormatException { public float getFloat(@NotNull String name) throws NullPointerException, NumberFormatException {
if (properties.getProperty(name) == null) { if (properties.getProperty(name) == null) {
Logger.sarn("Unable to get Float from property '" + name + "': Property does not exist"); logger.sarn("Unable to get Float from property '" + name + "': Property does not exist");
throw new NullPointerException("Unable to get Float from property '" + name + "': Property does not exist"); throw new NullPointerException("Unable to get Float from property '" + name + "': Property does not exist");
} }
try { try {
return Float.parseFloat(properties.getProperty(name)); return Float.parseFloat(properties.getProperty(name));
} catch (NumberFormatException exception) { } catch (NumberFormatException exception) {
Logger.sarn("Unable to get Float from property '" + name + "': String cannot be parsed as a Float."); logger.sarn("Unable to get Float from property '" + name + "': String cannot be parsed as a Float.");
throw exception; throw exception;
} }
} }
@ -284,17 +293,17 @@ public final class PropertiesReader {
*/ */
public double getDouble(@NotNull String name) throws NullPointerException, NumberFormatException { public double getDouble(@NotNull String name) throws NullPointerException, NumberFormatException {
if (properties.getProperty(name) == null) { if (properties.getProperty(name) == null) {
Logger.sarn("Unable to get Double from property '" + name + "': Property does not exist"); logger.sarn("Unable to get Double from property '" + name + "': Property does not exist");
throw new NullPointerException("Unable to get Double from property '" + name + "': Property does not exist"); throw new NullPointerException("Unable to get Double from property '" + name + "': Property does not exist");
} }
try { try {
return Double.parseDouble(properties.getProperty(name)); return Double.parseDouble(properties.getProperty(name));
} catch (NullPointerException exception) { } catch (NullPointerException exception) {
Logger.sarn("Unable to get Double from property '" + name + "': String is null."); logger.sarn("Unable to get Double from property '" + name + "': String is null.");
throw exception; throw exception;
} catch (NumberFormatException exception) { } catch (NumberFormatException exception) {
Logger.sarn("Unable to get Double from property '" + name + "': String cannot be parsed as a Double."); logger.sarn("Unable to get Double from property '" + name + "': String cannot be parsed as a Double.");
throw exception; throw exception;
} }
} }

View file

@ -19,10 +19,13 @@
package de.staropensource.engine.base.utility.information; package de.staropensource.engine.base.utility.information;
import de.staropensource.engine.base.logging.Logger; import de.staropensource.engine.base.Engine;
import de.staropensource.engine.base.logging.LoggerInstance;
import de.staropensource.engine.base.type.VersionType; import de.staropensource.engine.base.type.VersionType;
import de.staropensource.engine.base.utility.Miscellaneous;
import de.staropensource.engine.base.utility.PropertiesReader; import de.staropensource.engine.base.utility.PropertiesReader;
import lombok.Getter; import lombok.Getter;
import org.jetbrains.annotations.NotNull;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -41,6 +44,14 @@ import java.util.Properties;
*/ */
@SuppressWarnings({ "JavadocDeclaration" }) @SuppressWarnings({ "JavadocDeclaration" })
public final class EngineInformation { public final class EngineInformation {
/**
* Contains the {@link LoggerInstance} for this instance.
*
* @see LoggerInstance
* @since v1-alpha2
*/
private static final @NotNull LoggerInstance logger = new LoggerInstance.Builder().setClazz(EngineInformation.class).setOrigin("ENGINE").build();
/** /**
* Contains the engine's version codename. * Contains the engine's version codename.
* *
@ -337,14 +348,14 @@ public final class EngineInformation {
* @since v1-alpha1 * @since v1-alpha1
*/ */
public static synchronized void update() { public static synchronized void update() {
Logger.diag("Updating engine information"); logger.diag("Updating engine information");
// Load properties from bundled gradle.properties // Load properties from bundled gradle.properties
Properties gradleProperties = new Properties(); Properties gradleProperties = new Properties();
InputStream gradleStream = EngineInformation.class.getClassLoader().getResourceAsStream("sosengine-gradle.properties"); InputStream gradleStream = EngineInformation.class.getClassLoader().getResourceAsStream("sosengine-gradle.properties");
if (gradleStream == null) { if (gradleStream == null) {
Logger.crash("Unable to load build information: The bundled gradle.properties file could not be found."); logger.crash("Unable to load build information: The bundled gradle.properties file could not be found.");
return; return;
} }
@ -352,7 +363,7 @@ public final class EngineInformation {
gradleProperties.load(gradleStream); gradleProperties.load(gradleStream);
gradleStream.close(); gradleStream.close();
} catch (IOException exception) { } catch (IOException exception) {
Logger.crash("Unable to load build information: InputStream 'gradleStream' failed", exception); logger.crash("Unable to load build information: InputStream 'gradleStream' failed", exception);
return; return;
} }
@ -361,7 +372,7 @@ public final class EngineInformation {
Properties gitProperties = new Properties(); Properties gitProperties = new Properties();
InputStream gitStream = EngineInformation.class.getClassLoader().getResourceAsStream("sosengine-git.properties"); InputStream gitStream = EngineInformation.class.getClassLoader().getResourceAsStream("sosengine-git.properties");
if (gitStream == null) { if (gitStream == null) {
Logger.error("Unable to load build information: The bundled git.properties file could not be found. Did you download a tarball?"); logger.error("Unable to load build information: The bundled git.properties file could not be found. Did you download a tarball?");
// Fake information // Fake information
gitProperties.setProperty("git.total.commit.count", "0"); gitProperties.setProperty("git.total.commit.count", "0");
@ -379,7 +390,7 @@ public final class EngineInformation {
gitProperties.load(gitStream); gitProperties.load(gitStream);
gitStream.close(); gitStream.close();
} catch (IOException exception) { } catch (IOException exception) {
Logger.crash("Unable to load build information: InputStream 'gitStream' failed", exception); logger.crash("Unable to load build information: InputStream 'gitStream' failed", exception);
return; return;
} }
} }
@ -416,7 +427,9 @@ public final class EngineInformation {
calendar.setTime(date); calendar.setTime(date);
gitCommitTime = calendar.toZonedDateTime(); gitCommitTime = calendar.toZonedDateTime();
} catch (ParseException exception) { } catch (ParseException exception) {
Logger.crash("Unable to load build information: Can't parse \"" + gitParser.getString("git.commit.time") + "\" using format \"yyyy-MM-dd'T'HH:mmZ\"", exception); System.out.println("Unable to load build information: Can't parse \"" + gitParser.getString("git.commit.time") + "\" using format \"yyyy-MM-dd'T'HH:mmZ\"");
System.out.println(Miscellaneous.getStackTraceHeader(exception) + "\n" + Miscellaneous.getStackTraceAsString(exception, true));
Engine.getInstance().shutdown(69);
return; return;
} }
gitCommitterName = gitParser.getString("git.commit.user.name"); gitCommitterName = gitParser.getString("git.commit.user.name");

View file

@ -19,12 +19,11 @@
package de.staropensource.engine.base.utility.information; package de.staropensource.engine.base.utility.information;
import de.staropensource.engine.base.logging.Logger; import de.staropensource.engine.base.logging.LoggerInstance;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.lang.management.ManagementFactory; import java.lang.management.ManagementFactory;
import java.lang.management.MemoryUsage; import java.lang.management.MemoryUsage;
import java.util.Collections;
import java.util.List; import java.util.List;
/** /**
@ -34,6 +33,14 @@ import java.util.List;
*/ */
@SuppressWarnings({ "unused" }) @SuppressWarnings({ "unused" })
public final class JvmInformation { public final class JvmInformation {
/**
* Contains the {@link LoggerInstance} for this instance.
*
* @see LoggerInstance
* @since v1-alpha2
*/
private static final @NotNull LoggerInstance logger = new LoggerInstance.Builder().setClazz(JvmInformation.class).setOrigin("ENGINE").build();
/** /**
* Creates and initializes an instance of this class. * Creates and initializes an instance of this class.
* *
@ -61,7 +68,7 @@ public final class JvmInformation {
try { try {
return Integer.parseInt(version); return Integer.parseInt(version);
} catch (NumberFormatException exception) { } catch (NumberFormatException exception) {
Logger.crash("Could not parse Java version: Integer conversion failed for string \"" + version + "\"", exception, true); logger.crash("Could not parse Java version: Integer conversion failed for string \"" + version + "\"", exception, true);
throw exception; throw exception;
} }
} }
@ -110,11 +117,11 @@ public final class JvmInformation {
* Returns all arguments passed to the JVM. * Returns all arguments passed to the JVM.
* This excludes all arguments passed to the application. * This excludes all arguments passed to the application.
* *
* @return immutable list with all JVM arguments * @return JVM arguments
* @since v1-alpha0 * @since v1-alpha0
*/ */
public static @NotNull List<@NotNull String> getArguments() { public static @NotNull List<@NotNull String> getArguments() {
return Collections.unmodifiableList(ManagementFactory.getRuntimeMXBean().getInputArguments()); return ManagementFactory.getRuntimeMXBean().getInputArguments();
} }
/** /**

View file

@ -1,87 +0,0 @@
/*
* STAROPENSOURCE ENGINE SOURCE FILE
* Copyright (c) 2024 The StarOpenSource Engine Authors
* 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.engine.base.utility.misc;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Arrays;
import java.util.Collections;
/**
* Manipulates arrays.
*
* @since v1-alpha9
*/
public final class ArrayUtil {
/**
* Creates and initializes an
* instance of this class.
*
* @since v1-alpha9
*/
private ArrayUtil() {}
/**
* Removes the first {@code n} elements
* from the specified array.
* <p>
* If {@code n} is bigger than the
* amount of elements in the
* specified array, an empty array
* will be returned.
*
* @param <T> type
* @param array array to operate on
* @param n amount to remove
* @return array with the first elements removed
* @since v1-alpha9
*/
public static <T> @Nullable T @NotNull [] removeFirst(@Nullable T @NotNull [] array, int n) {
try {
return Arrays.copyOfRange(array, n, array.length);
} catch (ArrayIndexOutOfBoundsException | IllegalArgumentException exception) {
return Collections.emptyList().toArray(array);
}
}
/**
* Removes the last {@code n} elements
* from the specified array.
* <p>
* If {@code n} is bigger than the
* amount of elements in the
* specified array, an empty array
* will be returned.
*
* @param <T> type
* @param array array to operate on
* @param n amount to remove
* @return array with the last elements removed
* @since v1-alpha9
*/
public static <T> @Nullable T @NotNull [] removeLast(@Nullable T @NotNull [] array, int n) {
try {
return Arrays.copyOfRange(array, 0, array.length-n);
} catch (ArrayIndexOutOfBoundsException | IllegalArgumentException exception) {
return Collections.emptyList().toArray(array);
}
}
}

View file

@ -1,211 +0,0 @@
/*
* STAROPENSOURCE ENGINE SOURCE FILE
* Copyright (c) 2024 The StarOpenSource Engine Authors
* 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.engine.base.utility.misc;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.lang.ref.WeakReference;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.stream.Collectors;
/**
* Contains the most miscellaneous of all miscellaneous methods.
*
* @since v1-alpha9
*/
@SuppressWarnings({ "unused" })
public final class Miscellaneous {
/**
* Creates and initializes an instance of this class.
*
* @since v1-alpha9
*/
private Miscellaneous() {}
/**
* Searches for a value in a {@link Map}.
*
* @param map map to use
* @param value value to search for
* @return all keys matching the specified value
* @since v1-alpha9
*/
public static Set<?> searchForValueInMap(@NotNull Map<?, ?> map, @Nullable Object value) {
return map
.entrySet().stream()
.filter(entry -> Objects.equals(entry.getValue(), value))
.map(Map.Entry::getKey)
.collect(Collectors.toSet());
}
/**
* Measures the execution time of
* the specified {@link Runnable}.
*
* @param runnable {@link Runnable} to execute
* @return execution time in milliseconds
* @see Runnable
* @since v1-alpha9
*/
public static long measureExecutionTime(@NotNull Runnable runnable) {
long initTime = System.currentTimeMillis();
runnable.run();
return System.currentTimeMillis() - initTime;
}
/**
* Forcefully invokes the garbage collector
* and blocks execution until finished.
* If you want to run it in parallel to your
* program, consider running it in a thread.
* <p>
* This method does not guarantee full garbage collection,
* as the JVM only hints the garbage collector to do it's
* job. All garbage collectors are non-deterministic and
* cannot be invoked by force. And even if this method
* manages to invoke the garbage collector, a full garbage
* collection likely will never be archived. This is because
* the garbage collector will never throw everything into
* the trash. In addition, this method will only return if
* it's {@link WeakReference} becomes null. The garbage
* collector may or may not do more afterwards. This method
* just exists if someone wants to at least try to
* forcefully invoke the garbage collector. If you want
* a somewhat more reliable method, use Java Agents.
*
* @since v1-alpha9
*/
@SuppressWarnings("UnusedAssignment")
public static void invokeGarbageCollector() {
Object object = new Object();
WeakReference<Object> weakReference = new WeakReference<>(object);
object = null;
while(weakReference.get() != null) System.gc();
}
/**
* Returns if the code it is invoked by
* is running on the Java main thread.
*
* @return running on main thread?
* @since v1-alpha9
*/
public static boolean onMainThread() {
return Thread.currentThread().threadId() == 1;
}
/**
* Returns the {@code Caused by} message
* usually found in JVM stack traces.
*
* @param throwable {@link Throwable} to use
* @return {@link Throwable} header
* @since v1-alpha9
*/
public static @NotNull String throwableHeader(@NotNull Throwable throwable) {
return "Caused by: " + throwable.getClass().getName() + (throwable.getMessage() == null ? "" : ": " + throwable.getMessage());
}
/**
* Converts an array of {@link StackTraceElement}s into a regular string.
*
* @param stacktrace array of {@link StackTraceElement}s to convert
* @param indent if all lines shall be indented with a single tab.
* Enabling this will cause the output of this method
* to mimic the JVM's throwable handling output.
* @return specified stack trace as a String
* @since v1-alpha9
*/
public static @NotNull String stacktraceAsString(@NotNull StackTraceElement @NotNull [] stacktrace, boolean indent) {
StringBuilder output = new StringBuilder();
for (StackTraceElement element : stacktrace) {
if (!output.isEmpty())
output.append("\n");
if (indent)
output.append("\t");
output.append("at ").append(element);
}
return output.toString();
}
/**
* Converts an array of {@link StackTraceElement}s into a regular string.
*
* @param throwable throwable to derive the stacktrace from
* @param indent if all lines shall be indented with a single tab.
* Enabling this will cause the output of this method
* to mimic the JVM's throwable handling output.
* @return specified stack trace as a String
* @since v1-alpha9
*/
public static @NotNull String stacktraceAsString(@NotNull Throwable throwable, boolean indent) {
return stacktraceAsString(throwable.getStackTrace(), indent);
}
/**
* Returns the full stack trace of a {@link Throwable}.
* <p>
* This method recursively resolves, converts and then
* returns every {@link Throwable} and stacktrace found.
*
* @param throwable throwable to recursively convert
* @param indent if all lines shall be indented with a single tab.
* Enabling this will cause the output of this method
* to mimic the JVM's throwable handling output.
* @param includeHeader if to include the throwable header (see {@link #throwableHeader(Throwable)})
* @return full stack trace
* @since v1-alpha9
*/
public static @NotNull String stacktraceAsStringRecursive(@NotNull Throwable throwable, boolean indent, boolean includeHeader) {
StringBuilder output = new StringBuilder();
// Append header and stacktrace
if (includeHeader)
output
.append(throwableHeader(throwable))
.append("\n");
output.append(stacktraceAsString(throwable, indent));
// Process recursively
if (throwable.getCause() != null)
output
.append("\n")
.append(stacktraceAsStringRecursive(throwable.getCause(), indent, includeHeader));
if (throwable instanceof ClassNotFoundException exception && exception.getException() != null)
output
.append("\n")
.append(stacktraceAsStringRecursive(exception.getException(), indent, includeHeader));
if (throwable instanceof ExceptionInInitializerError exception && exception.getException() != null)
output
.append("\n")
.append(stacktraceAsStringRecursive(exception.getException(), indent, includeHeader));
if (throwable instanceof InvocationTargetException exception && exception.getTargetException() != null)
output
.append("\n")
.append(stacktraceAsStringRecursive(exception.getTargetException(), indent, includeHeader));
return output.toString();
}
}

View file

@ -1,93 +0,0 @@
/*
* STAROPENSOURCE ENGINE SOURCE FILE
* Copyright (c) 2024 The StarOpenSource Engine Authors
* 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.engine.base.utility.misc;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* Manipulates strings and characters.
*
* @since v1-alpha9
*/
public final class StringUtil {
/**
* Creates and initializes an
* instance of this class.
*
* @since v1-alpha9
*/
private StringUtil() {}
/**
* Counts the number of occurrences of the specified substring inside the specified string.
*
* @param string string to search in
* @param substring substring to count occurrences for
* @return number of times the specified substring was found inside the specified string
* @since v1-alpha9
*/
public static long count(@NotNull String string, @NotNull String substring) {
return (string.length() - string.replace(substring, "").length()) / substring.length();
}
/**
* Searches for the specified separators
* in the specified string and returns
* the first separator which matches the
* specified amount of required occurrences.
*
* @param string string to operate on
* @param separators separators to check for
* @param requiredOccurrences exact amount of occurrences for a separator to be deemed valid
* @return separator to use or {@code null}
* @since v1-alpha9
*/
public static @Nullable String getSeparatorRequired(@NotNull String string, @NotNull String @NotNull [] separators, int requiredOccurrences) {
if (string.isBlank() || separators.length == 0 || requiredOccurrences == 0)
return null;
for (String separator : separators)
if (count(string, separator) == requiredOccurrences)
return separator;
return null;
}
/**
* Searches for the specified separators
* in the specified string and returns
* the first separator which matches the
* specified amount of minimum occurrences.
*
* @param string string to operate on
* @param separators separators to check for
* @param minimumOccurrences minimum amount of occurrences for a separator to be deemed valid
* @return separator to use or {@code null}
* @since v1-alpha9
*/
public static @Nullable String getSeparatorMinimum(@NotNull String string, @NotNull String @NotNull [] separators, int minimumOccurrences) {
for (String separator : separators)
if (count(string, separator) >= minimumOccurrences)
return separator;
return null;
}
}

View file

@ -1,61 +0,0 @@
/*
* STAROPENSOURCE ENGINE SOURCE FILE
* Copyright (c) 2024 The StarOpenSource Engine Authors
* 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.engine.base.utility.misc;
import de.staropensource.engine.base.type.Tristate;
import org.jetbrains.annotations.Range;
/**
* Converts various data types into other data types.
*
* @since v1-alpha9
*/
public final class TypeConversion {
/**
* Creates and initializes an
* instance of this class.
*
* @since v1-alpha9
*/
private TypeConversion() {}
/**
* Converts a boolean into an integer.
*
* @param bool boolean to convert
* @return converted integer
* @since v1-alpha9
*/
@Range(from = 0, to = 1)
public static int booleanToInteger(boolean bool) {
return bool ? 1 : 0;
}
/**
* Converts an integer into a {@link Tristate} and then into a boolean.
*
* @param integer integer to convert
* @return booleanized integer
* @since v1-alpha9
*/
public static boolean integerToBoolean(@Range(from = 0, to = 1) int integer) throws IndexOutOfBoundsException {
return integer != 0;
}
}

View file

@ -20,7 +20,7 @@
/** /**
* Provides (utility) classes specifically made for one task. * Provides (utility) classes specifically made for one task.
* *
* @see de.staropensource.engine.base.utility.misc.Miscellaneous * @see de.staropensource.engine.base.utility.Miscellaneous
* @since v1-alpha0 * @since v1-alpha0
*/ */
package de.staropensource.engine.base.utility; package de.staropensource.engine.base.utility;

View file

@ -18,22 +18,23 @@ module sosengine.base {
exports de.staropensource.engine.base.implementable; exports de.staropensource.engine.base.implementable;
exports de.staropensource.engine.base.implementable.helper; exports de.staropensource.engine.base.implementable.helper;
exports de.staropensource.engine.base.utility.information; exports de.staropensource.engine.base.utility.information;
exports de.staropensource.engine.base.implementation.shortcode;
exports de.staropensource.engine.base.implementation.versioning; exports de.staropensource.engine.base.implementation.versioning;
exports de.staropensource.engine.base.event; exports de.staropensource.engine.base.event;
exports de.staropensource.engine.base.exception; exports de.staropensource.engine.base.exception;
exports de.staropensource.engine.base.exception.dependency; exports de.staropensource.engine.base.exception.dependency;
exports de.staropensource.engine.base.exception.reflection; exports de.staropensource.engine.base.exception.reflection;
exports de.staropensource.engine.base.exception.versioning; exports de.staropensource.engine.base.exception.versioning;
exports de.staropensource.engine.base.internal.event; // Internal: Required for subsystems
exports de.staropensource.engine.base.logging; exports de.staropensource.engine.base.logging;
exports de.staropensource.engine.base.implementation.logging; exports de.staropensource.engine.base.implementation.logging;
exports de.staropensource.engine.base.reflection; exports de.staropensource.engine.base.reflection;
exports de.staropensource.engine.base.type; exports de.staropensource.engine.base.type;
exports de.staropensource.engine.base.type.immutable;
exports de.staropensource.engine.base.type.logging; exports de.staropensource.engine.base.type.logging;
exports de.staropensource.engine.base.type.reflection; exports de.staropensource.engine.base.type.reflection;
exports de.staropensource.engine.base.type.vector; exports de.staropensource.engine.base.type.vector;
exports de.staropensource.engine.base.utility; exports de.staropensource.engine.base.utility;
exports de.staropensource.engine.base.utility.misc; exports de.staropensource.engine.base.implementation.shortcode;
// Reflection access // Reflection access
opens de.staropensource.engine.base; opens de.staropensource.engine.base;
@ -41,20 +42,21 @@ module sosengine.base {
opens de.staropensource.engine.base.implementable; opens de.staropensource.engine.base.implementable;
opens de.staropensource.engine.base.implementable.helper; opens de.staropensource.engine.base.implementable.helper;
opens de.staropensource.engine.base.utility.information; opens de.staropensource.engine.base.utility.information;
opens de.staropensource.engine.base.implementation.shortcode;
opens de.staropensource.engine.base.implementation.versioning; opens de.staropensource.engine.base.implementation.versioning;
opens de.staropensource.engine.base.event; opens de.staropensource.engine.base.event;
opens de.staropensource.engine.base.exception; opens de.staropensource.engine.base.exception;
opens de.staropensource.engine.base.exception.dependency; opens de.staropensource.engine.base.exception.dependency;
opens de.staropensource.engine.base.exception.reflection; opens de.staropensource.engine.base.exception.reflection;
opens de.staropensource.engine.base.exception.versioning; opens de.staropensource.engine.base.exception.versioning;
opens de.staropensource.engine.base.internal.event; // Internal: Required for subsystems
opens de.staropensource.engine.base.logging; opens de.staropensource.engine.base.logging;
opens de.staropensource.engine.base.implementation.logging; opens de.staropensource.engine.base.implementation.logging;
opens de.staropensource.engine.base.reflection; opens de.staropensource.engine.base.reflection;
opens de.staropensource.engine.base.type; opens de.staropensource.engine.base.type;
opens de.staropensource.engine.base.type.immutable;
opens de.staropensource.engine.base.type.logging; opens de.staropensource.engine.base.type.logging;
opens de.staropensource.engine.base.type.reflection; opens de.staropensource.engine.base.type.reflection;
opens de.staropensource.engine.base.type.vector; opens de.staropensource.engine.base.type.vector;
opens de.staropensource.engine.base.utility; opens de.staropensource.engine.base.utility;
opens de.staropensource.engine.base.utility.misc; opens de.staropensource.engine.base.implementation.shortcode;
} }

View file

@ -29,7 +29,6 @@ import org.junit.jupiter.api.Test;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
@ -54,10 +53,11 @@ class EngineConfigurationTest extends TestBase {
settings.put("debug", new Object[]{ "true", Boolean.TRUE }); settings.put("debug", new Object[]{ "true", Boolean.TRUE });
settings.put("debugEvents", new Object[]{ "true", Boolean.TRUE }); settings.put("debugEvents", new Object[]{ "true", Boolean.TRUE });
settings.put("errorShortcodeParser", new Object[]{ "false", Boolean.FALSE }); settings.put("errorShortcodeParser", new Object[]{ "false", Boolean.FALSE });
settings.put("logLevel", new Object[]{ "verbose", LogLevel.VERBOSE }); settings.put("loggerLevel", new Object[]{ "verbose", LogLevel.VERBOSE });
settings.put("logFeatures", new Object[]{ "formatting,runtime,time", Set.of("formatting", "runtime", "time") }); settings.put("loggerTemplate", new Object[]{ "%log_path% says: %message%", "%log_path% says: %message%" });
settings.put("logForceStandardOutput", new Object[]{ "true", Boolean.TRUE }); settings.put("loggerImmediateShutdown", new Object[]{ "true", Boolean.TRUE });
settings.put("logPollingSpeed", new Object[]{ "9999", 9999 }); settings.put("loggerForceStandardOutput", new Object[]{ "true", Boolean.TRUE });
settings.put("loggerPollingSpeed", new Object[]{ "9999", 9999 });
settings.put("optimizeLogging", new Object[]{ "false", Boolean.FALSE }); settings.put("optimizeLogging", new Object[]{ "false", Boolean.FALSE });
settings.put("optimizeEvents", new Object[]{ "false", Boolean.FALSE }); settings.put("optimizeEvents", new Object[]{ "false", Boolean.FALSE });

View file

@ -19,9 +19,13 @@
package de.staropensource.engine.base.srctests.utility; package de.staropensource.engine.base.srctests.utility;
import de.staropensource.engine.base.EngineConfiguration;
import de.staropensource.engine.base.annotation.EventListener;
import de.staropensource.engine.base.event.ThrowableCatchEvent;
import de.staropensource.engine.testing.TestBase; import de.staropensource.engine.testing.TestBase;
import de.staropensource.engine.base.utility.misc.NumberUtil; import de.staropensource.engine.base.utility.Math;
import de.staropensource.engine.base.utility.misc.Miscellaneous; import de.staropensource.engine.base.utility.Miscellaneous;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
@ -30,6 +34,7 @@ import org.junit.jupiter.params.provider.ValueSource;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Properties;
import java.util.Set; import java.util.Set;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
@ -60,7 +65,7 @@ public class MiscellaneousTest extends TestBase {
if (performMethodCalls("testPadNumbers", number, length, expected)) if (performMethodCalls("testPadNumbers", number, length, expected))
return; return;
String result = NumberUtil.padNumbers(number, length); String result = Math.padNumbers(number, length);
assertEquals(expected, result, "Result \"" + result + "\" does not match expected output \"" + expected + "\""); assertEquals(expected, result, "Result \"" + result + "\" does not match expected output \"" + expected + "\"");
} }
@ -92,7 +97,7 @@ public class MiscellaneousTest extends TestBase {
*/ */
@Test @Test
@DisplayName("getMapValues") @DisplayName("getMapValues")
void testSearchForValueInMap() { void testGetMapValues() {
if (performMethodCalls("testGetMapValues")) if (performMethodCalls("testGetMapValues"))
return; return;
@ -103,10 +108,72 @@ public class MiscellaneousTest extends TestBase {
testMap.put("keylast", "this is the last value"); testMap.put("keylast", "this is the last value");
testMap.put("keylast2", "this is the last value"); testMap.put("keylast2", "this is the last value");
Set<?> output = Miscellaneous.searchForValueInMap(testMap, "this value exists twice"); Set<?> output = Miscellaneous.getMapValues(testMap, "this value exists twice");
assertTrue(output.contains("keytwo"), "Map key \"keytwo\" not found in output Set"); assertTrue(output.contains("keytwo"), "Map key \"keytwo\" not found in output Set");
assertTrue(output.contains("keydrei"), "Map key \"keydrei\" not found in output Set"); assertTrue(output.contains("keydrei"), "Map key \"keydrei\" not found in output Set");
assertEquals(2, output.size(), "There are more or less than two keys in the output Set"); assertEquals(2, output.size(), "There are more or less than two keys in the output Set");
} }
/**
* Tests the method {@code executeSafely}.
*/
@Test
@DisplayName("executeSafely (test 0)")
void testExecuteSafely0() {
if (performMethodCalls("testExecuteSafely0"))
return;
throwableCaught = false;
Miscellaneous.executeSafely(() -> System.out.println("You can safely ignore this message (this comes from MiscellaneousTest#testExecuteSafely0)"), "MiscellaneousTest#testExecuteSafely0");
assertFalse(throwableCaught, "Event was triggered");
}
/**
* Tests the method {@code executeSafely}.
*/
@Test
@DisplayName("executeSafely (test 1)")
void testExecuteSafely1() {
if (performMethodCalls("testExecuteSafely1"))
return;
throwableCaught = false;
// Disable event optimization for instant results
Properties properties = new Properties();
properties.setProperty("sosengine.base.debug", "true");
properties.setProperty("sosengine.base.debugEvents", "true");
properties.setProperty("sosengine.base.loggerLevel", "diagnostic");
properties.setProperty("sosengine.base.optimizeLogging", "false");
properties.setProperty("sosengine.base.optimizeEvents", "false");
EngineConfiguration.getInstance().loadConfiguration(properties);
Miscellaneous.executeSafely(() -> {
throw new ArrayIndexOutOfBoundsException("This is not an accident!");
}, "MiscellaneousTest#testExecuteSafely1");
try {
Thread.sleep(200);
} catch (InterruptedException e) {
getLogger().error("Sleep was interrupted, test may fail");
throw new RuntimeException(e);
}
assertTrue(throwableCaught, "Event wasn't triggered");
}
/**
* Updates {@code throwableCaught}.
*
* @param throwable throwable
* @param identifier identifier
*/
@EventListener(event = ThrowableCatchEvent.class)
@SuppressWarnings({ "unused" })
public static void onCaughtThrowable(@NotNull Throwable throwable, @NotNull String identifier) {
getInstance().getLogger().diag("ThrowableCatchEvent received");
throwableCaught = true;
}
} }

View file

@ -30,7 +30,8 @@ tasks.register("javadocAll", Javadoc) {
":base", ":base",
":ansi", ":ansi",
":slf4j-compat", ":slf4j-compat",
":rendering", ":windowing",
":windowing:glfw",
":notification", ":notification",
] ]
@ -62,7 +63,7 @@ tasks.register("javadocAll", Javadoc) {
// Fix module collisions // Fix module collisions
doFirst { doFirst {
getLogger().log(LogLevel.WARN, "If this task fails, make sure to reset all module-info.java files using git or you may encounter issues.") logger.log(LogLevel.WARN, "If this task fails, make sure to reset all module-info.java files using git or you may encounter issues.")
for (String subproject : subprojects) { for (String subproject : subprojects) {
File source = new File(project(subproject).projectDir.getPath() + "/src/main/java/module-info.java") File source = new File(project(subproject).projectDir.getPath() + "/src/main/java/module-info.java")

Some files were not shown because too many files have changed in this diff Show more