Skip to content

Commit

Permalink
SLF4j-466: Move two methods from MessageFormatter to Util so they can…
Browse files Browse the repository at this point in the history
… be used by the EventRecordingLogger as well
  • Loading branch information
delgurth committed Aug 12, 2019
1 parent b99ad9f commit a09d788
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 25 deletions.
25 changes: 2 additions & 23 deletions slf4j-api/src/main/java/org/slf4j/helpers/MessageFormatter.java
Expand Up @@ -153,34 +153,13 @@ final public static FormattingTuple format(final String messagePattern, Object a


final public static FormattingTuple arrayFormat(final String messagePattern, final Object[] argArray) {
Throwable throwableCandidate = getThrowableCandidate(argArray);
Throwable throwableCandidate = Util.getThrowableCandidate(argArray);
Object[] args = argArray;
if (throwableCandidate != null) {
args = trimmedCopy(argArray);
args = Util.trimmedCopy(argArray);
}
return arrayFormat(messagePattern, args, throwableCandidate);
}

static final Throwable getThrowableCandidate(Object[] argArray) {
if (argArray == null || argArray.length == 0) {
return null;
}

final Object lastEntry = argArray[argArray.length - 1];
if (lastEntry instanceof Throwable) {
return (Throwable) lastEntry;
}
return null;
}
private static Object[] trimmedCopy(Object[] argArray) {
if (argArray == null || argArray.length == 0) {
throw new IllegalStateException("non-sensical empty or null argument array");
}
final int trimemdLen = argArray.length - 1;
Object[] trimmed = new Object[trimemdLen];
System.arraycopy(argArray, 0, trimmed, 0, trimemdLen);
return trimmed;
}

final public static FormattingTuple arrayFormat(final String messagePattern, final Object[] argArray, Throwable throwable) {

Expand Down
43 changes: 41 additions & 2 deletions slf4j-api/src/main/java/org/slf4j/helpers/Util.java
Expand Up @@ -127,7 +127,46 @@ static final public void report(String msg, Throwable t) {
static final public void report(String msg) {
System.err.println("SLF4J: " + msg);
}



/**
* Helper method to determine if an {@link Object} array contains an {@link Throwable} as last element
*
* @param argArray
* The arguments off which we want to know if it contains a {@link Throwable} as last element
* @return if the last parameter in argArray is a Throwable this method will return it, otherwise it returns null
*/
public static Throwable getThrowableCandidate(final Object[] argArray) {
if (argArray == null || argArray.length == 0) {
return null;
}

final Object lastEntry = argArray[argArray.length - 1];
if (lastEntry instanceof Throwable) {
return (Throwable) lastEntry;
}

return null;
}

/**
* Helper method to get all but the last element of an array
*
* @param argArray
* The arguments from which we want to remove the last element
*
* @return a copy of the array without the last element
*/
public static Object[] trimmedCopy(final Object[] argArray) {
if (argArray == null || argArray.length == 0) {
throw new IllegalStateException("non-sensical empty or null argument array");
}

final int trimmedLen = argArray.length - 1;

Object[] trimmed = new Object[trimmedLen];
System.arraycopy(argArray, 0, trimmed, 0, trimmedLen);

return trimmed;
}

}

0 comments on commit a09d788

Please sign in to comment.