All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.slf4j.plus.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;

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
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE) // @checkstyle:ok
public class Throwables {
	
	/**
	 * Separator character
	 */
	private static final String SEPARATOR_CHARACTER = System.lineSeparator();
	
	/**
	 * Tab character
	 */
	private static final String TAB_CHARACTER = "\t";
	
	/**
	 * Frame line prefix
	 */
	public static final String FRAME_LINE_PREFIX = TAB_CHARACTER + "at ";
	
	/**
	 * Omitted line prefix
	 */
	public static final String OMITTED_LINE_PREFIX = TAB_CHARACTER + "... ";
	
	/**
	 * Omitted line suffix
	 */
	public static final String OMITTED_LINE_SUFFIX = " more";
	
	/**
	 * Get stack trace string
	 * 
	 * @param throwable {@link Throwable}
	 * @return stack trace string
	 */
	public static String getStackTraceString(@NonNull Throwable throwable) {
		
		StringWriter writer = new StringWriter();
		
		throwable.printStackTrace(new PrintWriter(writer));
		
		return writer.toString();
	}
	
	/**
	 * Get stack trace string
	 * 
	 * @param throwable {@link Throwable}
	 * @param maxFrameLength max frame length
	 * @return stack trace string
	 */
	public static String getStackTraceString(Throwable throwable, int maxFrameLength) {
		
		if (maxFrameLength <= 0) {
			
			throw new IllegalArgumentException("'maxFrameLength' must be positive");
		}
		
		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 stack trace lines
	 * 
	 * @param throwable {@link Throwable}
	 * @return stack trace lines
	 */
	private static List generateStackTraceLines(Throwable throwable) {
		
		List lines = new ArrayList<>();
		
		for (String line : getStackTraceString(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