Add config setting for toggling newline support
All checks were successful
build-and-test / test (push) Successful in 1m58s
build-and-test / build (push) Successful in 2m7s
build-and-test / generate-javadoc (push) Successful in 2m21s

This commit is contained in:
JeremyStar™ 2024-08-19 20:28:40 +02:00
parent 48c326ad71
commit ad7b3568cf
Signed by: JeremyStarTM
GPG key ID: E366BAEF67E4704D
2 changed files with 42 additions and 25 deletions

View file

@ -216,19 +216,19 @@ public final class EngineConfiguration extends Configuration {
private String loggerTemplate; private String loggerTemplate;
/** /**
* If enabled, the JVM will immediately shutdown on an engine crash. This will prevent shutdown hooks from executing. * Contains how fast the logging thread will poll for queued messages.
* Note: This will also prevent Jansi and potentially other libraries from removing temporary native libraries at shutdown. * Only applies if {@code optimizeLogging} is turned on.
* *
* @see CrashHandler * @see #optimizeLogging
* @since v1-alpha0 * @since v1-alpha1
* -- GETTER -- * -- GETTER --
* Gets the value for {@link #loggerImmediateShutdown}. * Gets the value for {@link #loggerPollingSpeed}.
* *
* @return variable value * @return variable value
* @see #loggerImmediateShutdown * @see #loggerPollingSpeed
* @since v1-alpha0 * @since v1-alpha1
*/ */
private boolean loggerImmediateShutdown; private int loggerPollingSpeed;
/** /**
* If enabled, will force the {@link Logger} and {@link CrashHandler} to use <a href="https://www.man7.org/linux/man-pages/man3/stderr.3.html">the standard output</a> * If enabled, will force the {@link Logger} and {@link CrashHandler} to use <a href="https://www.man7.org/linux/man-pages/man3/stderr.3.html">the standard output</a>
@ -245,19 +245,34 @@ public final class EngineConfiguration extends Configuration {
private boolean loggerForceStandardOutput; private boolean loggerForceStandardOutput;
/** /**
* Contains how fast the logging thread will poll for queued messages. * If enabled, will enable support for printing log messages on multiple lines.
* Only applies if {@code optimizeLogging} is turned on. * 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}).
* *
* @see #optimizeLogging * @since v1-alpha4
* @since v1-alpha1
* -- GETTER -- * -- GETTER --
* Gets the value for {@link #loggerForceStandardOutput}. * Gets the value for {@link #loggerEnableNewlineSupport}.
* *
* @return variable value * @return variable value
* @see #loggerForceStandardOutput * @see #loggerEnableNewlineSupport
* @since v1-alpha1 * @since v1-alpha4
*/ */
private int loggerPollingSpeed; private boolean loggerEnableNewlineSupport;
/**
* If enabled, the JVM will immediately shutdown on an engine crash. 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;
/** /**
* Will truncate the path of types when using their {@code toString} method. * Will truncate the path of types when using their {@code toString} method.
@ -325,9 +340,10 @@ public final class EngineConfiguration extends Configuration {
} }
} }
case "loggerTemplate" -> loggerTemplate = parser.getString(group + property); case "loggerTemplate" -> loggerTemplate = parser.getString(group + property);
case "loggerImmediateShutdown" -> loggerImmediateShutdown = parser.getBoolean(group + property);
case "loggerForceStandardOutput" -> loggerForceStandardOutput = parser.getBoolean(group + property);
case "loggerPollingSpeed" -> loggerPollingSpeed = parser.getInteger(group + property, true); case "loggerPollingSpeed" -> loggerPollingSpeed = parser.getInteger(group + property, true);
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);
} }
@ -359,9 +375,10 @@ public final class EngineConfiguration extends Configuration {
loggerLevel = LogLevel.INFORMATIONAL; loggerLevel = LogLevel.INFORMATIONAL;
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>"; 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>";
loggerImmediateShutdown = false;
loggerForceStandardOutput = false;
loggerPollingSpeed = 5; loggerPollingSpeed = 5;
loggerForceStandardOutput = false;
loggerEnableNewlineSupport = true;
loggerImmediateShutdown = false;
hideFullTypePath = false; hideFullTypePath = false;
} }
@ -382,9 +399,10 @@ public final class EngineConfiguration extends Configuration {
case "loggerLevel" -> { return loggerLevel; } case "loggerLevel" -> { return loggerLevel; }
case "loggerTemplate" -> { return loggerTemplate; } case "loggerTemplate" -> { return loggerTemplate; }
case "loggerImmediateShutdown" -> { return loggerImmediateShutdown; }
case "loggerForceStandardOutput" -> { return loggerForceStandardOutput; }
case "loggerPollingSpeed" -> { return loggerPollingSpeed; } case "loggerPollingSpeed" -> { return loggerPollingSpeed; }
case "loggerForceStandardOutput" -> { return loggerForceStandardOutput; }
case "loggerEnableNewlineSupport" -> { return loggerEnableNewlineSupport; }
case "loggerImmediateShutdown" -> { return loggerImmediateShutdown; }
case "hideFullTypePath" -> { return hideFullTypePath; } case "hideFullTypePath" -> { return hideFullTypePath; }
default -> { return null; } default -> { return null; }

View file

@ -212,7 +212,7 @@ public final class Logger {
format = new LogPath(issuerClass).replace(format); format = new LogPath(issuerClass).replace(format);
// Handle newlines // Handle newlines
if (message.contains("\n")) if (EngineConfiguration.getInstance().isLoggerEnableNewlineSupport() && message.contains("\n"))
try (Scanner scanner = new Scanner(message)) { try (Scanner scanner = new Scanner(message)) {
int indexPrefix = format.indexOf("%log_message_prefix%"); int indexPrefix = format.indexOf("%log_message_prefix%");
int indexMessage = format.indexOf("%log_message%"); int indexMessage = format.indexOf("%log_message%");
@ -233,7 +233,6 @@ public final class Logger {
.replace("%log_color_secondary%", ""), false .replace("%log_color_secondary%", ""), false
).getClean().length()); ).getClean().length());
while (scanner.hasNextLine()) { while (scanner.hasNextLine()) {
if (formatNew.isEmpty()) if (formatNew.isEmpty())
formatNew.append(format, 0, indexPrefix == -1 ? indexMessage : indexPrefix); formatNew.append(format, 0, indexPrefix == -1 ? indexMessage : indexPrefix);
@ -254,7 +253,7 @@ public final class Logger {
// No newline found, use performance-efficient replacing // No newline found, use performance-efficient replacing
format = format format = format
.replace("%log_message_prefix%", "") .replace("%log_message_prefix%", "")
.replace("%log_message%", message); .replace("%log_message%", message.replace("\n", "\\n"));
// Replace placeholders involving colors // Replace placeholders involving colors
format = new LogColorPrimary(level).replace(format); format = new LogColorPrimary(level).replace(format);