
org.slf4j.plus.util.Throwables Maven / Gradle / Ivy
/*
* Copyright 2015-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.slf4j.plus.util;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.NonNull;
/**
* {@link Throwable} utility methods
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE) // @checkstyle:ok
public class Throwables {
/**
* The character of the system separator
*/
private static final String SEPARATOR_CHARACTER = System.lineSeparator();
/**
* The character of tab
*/
private static final String TAB_CHARACTER = "\t";
/**
* The prefix of frame line
*/
public static final String FRAME_LINE_PREFIX = TAB_CHARACTER + "at ";
/**
* The prefix of omitted line
*/
public static final String OMITTED_LINE_PREFIX = TAB_CHARACTER + "... ";
/**
* The suffix of omitted line
*/
public static final String OMITTED_LINE_SUFFIX = " more";
/**
* Get the stack trace string.
*
* @param throwable {@link Throwable}
* @return the stack trace string
*/
public static String getStackTrace(@NonNull Throwable throwable) {
StringWriter writer = new StringWriter();
throwable.printStackTrace(new PrintWriter(writer));
return writer.toString();
}
/**
* Get the stack trace string.
*
* @param throwable {@link Throwable}
* @param maxFrameLength the max frame length
* @return the stack trace string
*/
public static String getStackTrace(@NonNull Throwable throwable, int maxFrameLength) {
List lines = new ArrayList<>();
int frameCount = 0;
int omittedCount = 0;
for (String line : generateStackTraceLines(throwable)) {
// Caused exception line or empty line
if (!line.startsWith(TAB_CHARACTER)) {
// Add omitted line
if (frameCount > maxFrameLength) {
omittedCount += frameCount - maxFrameLength;
}
if (omittedCount > 0) {
lines.add(OMITTED_LINE_PREFIX + omittedCount + OMITTED_LINE_SUFFIX);
}
frameCount = 0;
omittedCount = 0;
lines.add(line);
}
// Frame line
else if (line.startsWith(FRAME_LINE_PREFIX)) {
if (++frameCount <= maxFrameLength) {
lines.add(line);
}
}
// Omitted line
else if (line.startsWith(OMITTED_LINE_PREFIX)) {
Matcher matcher = Pattern.compile("^" + Pattern.quote(OMITTED_LINE_PREFIX) + "([0-9]+)").matcher(line);
if (matcher.find()) {
omittedCount = Integer.parseInt(matcher.group(1));
}
}
}
return String.join(SEPARATOR_CHARACTER, lines);
}
/**
* Generate the stack trace string lines.
*
* @param throwable {@link Throwable}
* @return the stack trace string lines
*/
private static List generateStackTraceLines(@NonNull Throwable throwable) {
List lines = new ArrayList<>();
for (String line : getStackTrace(throwable).split(SEPARATOR_CHARACTER)) {
lines.add(line);
}
// Add last empty line because split command remove it.
lines.add("");
return lines;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy