Make logger (optionally) asynchronous!

Finally! Multithreading!
This commit is contained in:
JeremyStar™ 2024-06-11 21:49:48 +02:00
parent 0a6ed79673
commit b0d740cd65
Signed by: JeremyStarTM
GPG key ID: E366BAEF67E4704D
2 changed files with 60 additions and 30 deletions

View file

@ -85,7 +85,7 @@ public final class EngineConfiguration implements SubsystemConfiguration {
/** /**
* If enabled, all called events will be logged. * If enabled, all called events will be logged.
* *
* @see de.staropensource.sosengine.base.classes.helpers.EventHelper#logCall(Class) * @see de.staropensource.sosengine.base.classes.helpers.EventHelper#logCall(Class, Object...)
* @since 1-alpha0 * @since 1-alpha0
*/ */
private boolean debugEvents; private boolean debugEvents;
@ -184,6 +184,20 @@ public final class EngineConfiguration implements SubsystemConfiguration {
*/ */
private boolean loggerForceStandardOutput; private boolean loggerForceStandardOutput;
/**
* Makes the {@link Logger} work asynchronous if enabled.
*
* @since 1-alpha0
*
* -- GETTER --
* Gets the value for {@code optimizeLogging}.
*
* @return variable value
* @see EngineConfiguration#optimizeLogging
* @since 1-alpha0
*/
private boolean optimizeLogging;
/** /**
* Constructor. * Constructor.
* *
@ -231,6 +245,8 @@ public final class EngineConfiguration implements SubsystemConfiguration {
case "loggerTemplate" -> loggerTemplate = parser.getString(group + property); case "loggerTemplate" -> loggerTemplate = parser.getString(group + property);
case "loggerImmediateShutdown" -> loggerImmediateShutdown = parser.getBoolean(group + property); case "loggerImmediateShutdown" -> loggerImmediateShutdown = parser.getBoolean(group + property);
case "loggerForceStandardOutput" -> loggerForceStandardOutput = parser.getBoolean(group + property); case "loggerForceStandardOutput" -> loggerForceStandardOutput = parser.getBoolean(group + property);
case "optimizeLogging" -> optimizeLogging = parser.getBoolean(group + property);
} }
} catch (NullPointerException ignored) {} } catch (NullPointerException ignored) {}
} }
@ -259,6 +275,8 @@ public final class EngineConfiguration implements SubsystemConfiguration {
loggerTemplate = "%log_color_primary%[%time_hour%:%time_minute%:%time_second%] [%log_level% %log_path%%log_info%] %log_color_secondary%%log_message%<reset>"; loggerTemplate = "%log_color_primary%[%time_hour%:%time_minute%:%time_second%] [%log_level% %log_path%%log_info%] %log_color_secondary%%log_message%<reset>";
loggerImmediateShutdown = false; loggerImmediateShutdown = false;
loggerForceStandardOutput = false; loggerForceStandardOutput = false;
optimizeLogging = false;
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
@ -274,9 +292,11 @@ public final class EngineConfiguration implements SubsystemConfiguration {
case "debugShortcodeConverter" -> { case "debugShortcodeConverter" -> {
return debugShortcodeConverter; return debugShortcodeConverter;
} }
case "errorShortcodeConverter" -> { case "errorShortcodeConverter" -> {
return errorShortcodeConverter; return errorShortcodeConverter;
} }
case "loggerLevel" -> { case "loggerLevel" -> {
return loggerLevel; return loggerLevel;
} }
@ -289,6 +309,10 @@ public final class EngineConfiguration implements SubsystemConfiguration {
case "loggerForceStandardOutput" -> { case "loggerForceStandardOutput" -> {
return loggerForceStandardOutput; return loggerForceStandardOutput;
} }
case "optimizeLogging" -> {
return optimizeLogging;
}
default -> { default -> {
return null; return null;
} }

View file

@ -86,8 +86,8 @@ public final class Logger {
* @since 1-alpha0 * @since 1-alpha0
*/ */
private static void log(@NotNull LogLevel level, @NotNull LogIssuer logIssuer, @NotNull String message) { private static void log(@NotNull LogLevel level, @NotNull LogIssuer logIssuer, @NotNull String message) {
Runnable loggingCode = () -> {
// Check if engine has initialized // Check if engine has initialized
if (Engine.getInstance() == null) return; if (Engine.getInstance() == null) return;
// Check if level is allowed // Check if level is allowed
@ -125,6 +125,12 @@ public final class Logger {
// Print log message // Print log message
loggerImplementation.print(level, logIssuer, base); loggerImplementation.print(level, logIssuer, base);
};
if (EngineConfiguration.getInstance().isOptimizeLogging())
Thread.ofVirtual().start(loggingCode);
else
loggingCode.run();
} }
/** /**