Make logger adapters more better, do the thing with the vararg throwable

This commit is contained in:
Mumfrey committed 2021-08-19 18:57:30 +01:00
1 parent 212d5d1b35
commit b79b42a460
3 files changed
+115 -49

No files matched your search

@@ -25,11 +25,77 @@
package org.spongepowered.asm.logging;
/**
* Abstract base adapter which contains convenience methods for formatting
* Abstract base adapter which contains a convenience class for formatting
* log4j2-style messages and also routes all level-specific overloads to calls
* to <tt>log</tt>, which can simplify some implementations.
*/
public abstract class LoggerAdapterAbstract implements ILogger {
/**
* This is a very naive implementation of log4j2's <tt>ParameterizedMessage
* </tt> which is less efficient and less defensive because it doesn't need
* to handle all the cases that the log4j2 formatter does. All we're really
* doing here is substituting in the values for <tt>{}</tt> placeholders
* because I know that in mixin there aren't any cases where we need to
* handle anything else, such as escaped <tt>{</tt> characters or whatever.
* It also handles the case where the last param of the varargs is a
* <tt>Throwable</tt>, and makes it available to consumers.
*/
public static class FormattedMessage {
private String message;
private Throwable t;
/**
* Create a new formatted message
*
* @param message Message patterm
* @param params Message parameters
*/
public FormattedMessage(String message, Object... params) {
if (params.length == 0) {
this.message = message;
return;
}
StringBuilder sb = new StringBuilder();
int pos = 0, param = 0;
for (; pos < message.length() && param < params.length; param++) {
int delimPos = message.indexOf("{}", pos);
if (delimPos < 0) {
break;
}
sb.append(message.substring(pos, delimPos)).append(params[param]);
pos = delimPos + 2;
}
if (pos < message.length()) {
sb.append(message.substring(pos));
}
// If last arg is throwable and not consumed by the message, store it
if (param < params.length && params[params.length - 1] instanceof Throwable) {
this.t = (Throwable)params[params.length - 1];
}
this.message = sb.toString();
}
@Override
public String toString() {
return this.message;
}
public String getMessage() {
return this.message;
}
public boolean hasThrowable() {
return this.t != null;
}
public Throwable getThrowable() {
return this.t;
}
}
/**
* Logger id
@@ -51,39 +117,6 @@ public abstract class LoggerAdapterAbstract implements ILogger {
return this.id;
}
/**
* This is a very naive implementation of log4j2's
* ParameterizedMessage::format method which is less efficient and less
* defensive because it doesn't need to handle all the cases that the log4j2
* formatter does. All we're really doing here is substituting in the values
* for <tt>{}</tt> placeholders because I know that in mixin there aren't
* any cases where we need to handle anything else, such as escaped <tt>{
* </tt> characters or whatever.
*
* @param message Message patterm
* @param params Message parameters
* @return Message with {} placeholders filled in
*/
protected static String getFormattedMessage(String message, Object... params) {
if (params.length == 0) {
return message;
}
StringBuilder sb = new StringBuilder();
int pos = 0;
for (int param = 0; pos < message.length() && param < params.length; param++) {
int delimPos = message.indexOf("{}", pos);
if (delimPos < 0) {
return sb.append(message.substring(pos)).toString();
}
sb.append(message.substring(pos, delimPos)).append(params[param]);
pos = delimPos + 2;
}
if (pos < message.length()) {
sb.append(message.substring(pos));
}
return sb.toString();
}
@Override
public void catching(Throwable t) {
this.catching(Level.WARN, t);