GlueGen: src/java/com/jogamp/common/ExceptionUtils.java Source File

GlueGen v2.6.0-rc-20250712 GlueGen, Native Binding Generator for Java™ (public API).

ExceptionUtils.java
Go to the documentation of this file.
1/**
2 * Copyright 2014 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28package com.jogamp.common;
29
30import java.io.PrintStream;
31
32/**
33 * @since 2.3.0
34 */
35 public class ExceptionUtils {
36 public static void dumpStack(final PrintStream out) {
37 dumpStack(out, 1, -1);
38 }
39 public static void dumpStack(final PrintStream out, final int skip, final int depth) {
40 dumpStack(out, new Exception(""), skip+1, depth);
41 }
42 public static void dumpStack(final PrintStream out, final Throwable t, final int skip, final int depth) {
43 dumpStack(out, t.getStackTrace(), skip, depth);
44 }
45 public static void dumpStack(final PrintStream out, final StackTraceElement[] stack, final int skip, final int depth) {
46 if( null == stack ) {
47 return;
48 }
49 final int maxDepth;
50 if( 0 > depth ) {
51 maxDepth = stack.length;
52 } else {
53 maxDepth = Math.min(depth+skip, stack.length);
54 }
55 for(int i=skip; i<maxDepth; i++) {
56 out.println(" ["+i+"]: "+stack[i]);
57 }
58 }
59
60 /**
61 * Interface allowing {@link Throwable} specializations to provide their custom stack trace presentation.
62 * @since 2.3.2
63 */
64 public static interface CustomStackTrace {
65 /**
66 * Prints this {@link Throwable} as a cause to the output {@link PrintStream} {@code s},
67 * not iterating over all inner causes!
68 * @param s output stream
69 * @param causeStr the cause title
70 * @param causeIdx the cause index over all causes known by caller
71 * @param stackDepth the maximum depth for stack entries, or {@code -1} for all
72 * @since 2.3.2
73 */
74 void printCauseStack(final PrintStream s, final String causeStr, final int causeIdx, final int stackDepth);
75 /**
76 * Custom {@code printStackTrace} method, similar to {@link Throwable#printStackTrace(PrintStream, int, int)}.
77 * @param s output stream
78 * @param causeDepth the maximum depth for causes, or {@code -1} for all
79 * @param stackDepth the maximum depth for stack entries, or {@code -1} for all
80 */
81 void printStackTrace(final PrintStream s, final int causeDepth, final int stackDepth);
82 }
83
84 /**
85 * Prints the given {@link Throwable} cause to the output {@link PrintStream} {@code s}.
86 * @param s output stream
87 * @param causeStr the cause title
88 * @param cause the {@link Throwable} cause for output
89 * @param causeIdx the cause index over all causes known by caller
90 * @param causeDepth the maximum depth for causes, or {@code -1} for all
91 * @param stackDepth the maximum depth for stack entries, or {@code -1} for all
92 * @since 2.3.2
93 */
94 public static int printCause(final PrintStream s, final String causeStr, Throwable cause, final int causeIdx, final int causeDepth, final int stackDepth) {
95 int i=causeIdx;
96 for(; null != cause && ( -1 == causeDepth || i < causeDepth ); cause = cause.getCause()) {
97 if( cause instanceof CustomStackTrace ) {
98 ((CustomStackTrace)cause).printCauseStack(s, causeStr, i, stackDepth);
99 } else {
100 s.println(causeStr+"["+i+"] by "+cause.getClass().getSimpleName()+": "+cause.getMessage()+" on thread "+Thread.currentThread().getName());
101 dumpStack(s, cause.getStackTrace(), 0, stackDepth);
102 }
103 i++;
104 }
105 return i;
106 }
107
108 /**
109 * Prints the given {@link Throwable} to the output {@link PrintStream} {@code s}.
110 * @param s output stream
111 * @param t the {@link Throwable} for output
112 * @param causeDepth the maximum depth for causes, or {@code -1} for all
113 * @param stackDepth the maximum depth for stack entries, or {@code -1} for all
114 * @since 2.3.2
115 */
116 public static void printStackTrace(final PrintStream s, final Throwable t, final int causeDepth, final int stackDepth) {
117 if( t instanceof CustomStackTrace ) {
118 ((CustomStackTrace)t).printStackTrace(s, causeDepth, stackDepth);
119 } else {
120 s.println(t.getClass().getSimpleName()+": "+t.getMessage()+" on thread "+Thread.currentThread().getName());
121 dumpStack(s, t.getStackTrace(), 0, stackDepth);
122 printCause(s, "Caused", t.getCause(), 0, causeDepth, stackDepth);
123 }
124 }
125
126 /**
127 * Dumps a {@link Throwable} to {@link System.err} in a decorating message including the current thread name,
128 * and its {@link #dumpStack(PrintStream, StackTraceElement[], int, int) stack trace}.
129 * <p>
130 * Implementation will iterate through all {@link Throwable#getCause() causes}.
131 * </p>
132 * @param additionalDescr additional text placed before the {@link Throwable} details.
133 * @param t the {@link Throwable} for output
134 */
135 public static void dumpThrowable(final String additionalDescr, final Throwable t) {
136 dumpThrowable(additionalDescr, t, -1, -1);
137 }
138 /**
139 * Dumps a {@link Throwable} to {@link System.err} in a decorating message including the current thread name,
140 * and its {@link #dumpStack(PrintStream, StackTraceElement[], int, int) stack trace}.
141 * <p>
142 * Implementation will iterate through all {@link Throwable#getCause() causes}.
143 * </p>
144 * @param additionalDescr additional text placed before the {@link Throwable} details.
145 * @param t the {@link Throwable} for output
146 * @param causeDepth the maximum depth for causes, or {@code -1} for all
147 * @param stackDepth the maximum depth for stack entries, or {@code -1} for all
148 * @since 2.3.2
149 */
150 public static void dumpThrowable(final String additionalDescr, final Throwable t, final int causeDepth, final int stackDepth) {
151 System.err.print("Caught "+additionalDescr+" ");
152 printStackTrace(System.err, t, causeDepth, stackDepth);
153 }
154}
com.jogamp.common.ExceptionUtils
Definition: ExceptionUtils.java:35
com.jogamp.common.ExceptionUtils.dumpStack
static void dumpStack(final PrintStream out, final StackTraceElement[] stack, final int skip, final int depth)
Definition: ExceptionUtils.java:45
com.jogamp.common.ExceptionUtils.dumpStack
static void dumpStack(final PrintStream out, final int skip, final int depth)
Definition: ExceptionUtils.java:39
com.jogamp.common.ExceptionUtils.printCause
static int printCause(final PrintStream s, final String causeStr, Throwable cause, final int causeIdx, final int causeDepth, final int stackDepth)
Prints the given Throwable cause to the output PrintStream s.
Definition: ExceptionUtils.java:94
com.jogamp.common.ExceptionUtils.dumpThrowable
static void dumpThrowable(final String additionalDescr, final Throwable t, final int causeDepth, final int stackDepth)
Dumps a Throwable to System.err in a decorating message including the current thread name,...
Definition: ExceptionUtils.java:150
com.jogamp.common.ExceptionUtils.dumpThrowable
static void dumpThrowable(final String additionalDescr, final Throwable t)
Dumps a Throwable to System.err in a decorating message including the current thread name,...
Definition: ExceptionUtils.java:135
com.jogamp.common.ExceptionUtils.dumpStack
static void dumpStack(final PrintStream out, final Throwable t, final int skip, final int depth)
Definition: ExceptionUtils.java:42
com.jogamp.common.ExceptionUtils.printStackTrace
static void printStackTrace(final PrintStream s, final Throwable t, final int causeDepth, final int stackDepth)
Prints the given Throwable to the output PrintStream s.
Definition: ExceptionUtils.java:116
com.jogamp.common.ExceptionUtils.dumpStack
static void dumpStack(final PrintStream out)
Definition: ExceptionUtils.java:36
com.jogamp.common.ExceptionUtils.CustomStackTrace
Interface allowing Throwable specializations to provide their custom stack trace presentation.
Definition: ExceptionUtils.java:64
com.jogamp.common.ExceptionUtils.CustomStackTrace.printStackTrace
void printStackTrace(final PrintStream s, final int causeDepth, final int stackDepth)
Custom printStackTrace method, similar to Throwable#printStackTrace(PrintStream, int,...
com.jogamp.common.ExceptionUtils.CustomStackTrace.printCauseStack
void printCauseStack(final PrintStream s, final String causeStr, final int causeIdx, final int stackDepth)
Prints this Throwable as a cause to the output PrintStream s, not iterating over all inner causes!