4618 |
30 Oct 08 |
nicklas |
1 |
/** |
4618 |
30 Oct 08 |
nicklas |
$Id$ |
4618 |
30 Oct 08 |
nicklas |
3 |
|
4618 |
30 Oct 08 |
nicklas |
Copyright (C) 2008 Nicklas Nordborg |
4618 |
30 Oct 08 |
nicklas |
5 |
|
4618 |
30 Oct 08 |
nicklas |
This file is part of BASE - BioArray Software Environment. |
4618 |
30 Oct 08 |
nicklas |
Available at http://base.thep.lu.se/ |
4618 |
30 Oct 08 |
nicklas |
8 |
|
4618 |
30 Oct 08 |
nicklas |
BASE is free software; you can redistribute it and/or |
4618 |
30 Oct 08 |
nicklas |
modify it under the terms of the GNU General Public License |
4618 |
30 Oct 08 |
nicklas |
as published by the Free Software Foundation; either version 3 |
4618 |
30 Oct 08 |
nicklas |
of the License, or (at your option) any later version. |
4618 |
30 Oct 08 |
nicklas |
13 |
|
4618 |
30 Oct 08 |
nicklas |
BASE is distributed in the hope that it will be useful, |
4618 |
30 Oct 08 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
4618 |
30 Oct 08 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
4618 |
30 Oct 08 |
nicklas |
GNU General Public License for more details. |
4618 |
30 Oct 08 |
nicklas |
18 |
|
4618 |
30 Oct 08 |
nicklas |
You should have received a copy of the GNU General Public License |
4618 |
30 Oct 08 |
nicklas |
along with BASE. If not, see <http://www.gnu.org/licenses/>. |
4618 |
30 Oct 08 |
nicklas |
21 |
*/ |
4618 |
30 Oct 08 |
nicklas |
22 |
package net.sf.basedb.util.error; |
4618 |
30 Oct 08 |
nicklas |
23 |
|
5114 |
02 Oct 09 |
nicklas |
24 |
import java.awt.Color; |
5114 |
02 Oct 09 |
nicklas |
25 |
import java.awt.Font; |
5114 |
02 Oct 09 |
nicklas |
26 |
import java.awt.Graphics2D; |
5114 |
02 Oct 09 |
nicklas |
27 |
import java.awt.font.FontRenderContext; |
5114 |
02 Oct 09 |
nicklas |
28 |
import java.awt.geom.Rectangle2D; |
5114 |
02 Oct 09 |
nicklas |
29 |
import java.awt.image.BufferedImage; |
4618 |
30 Oct 08 |
nicklas |
30 |
import java.io.PrintWriter; |
4618 |
30 Oct 08 |
nicklas |
31 |
import java.io.StringWriter; |
4618 |
30 Oct 08 |
nicklas |
32 |
|
5114 |
02 Oct 09 |
nicklas |
33 |
import net.sf.basedb.core.StringUtil; |
5114 |
02 Oct 09 |
nicklas |
34 |
|
4618 |
30 Oct 08 |
nicklas |
35 |
/** |
4618 |
30 Oct 08 |
nicklas |
Utility class for working with {@link Throwable}:s. |
4618 |
30 Oct 08 |
nicklas |
37 |
|
4618 |
30 Oct 08 |
nicklas |
@author Nicklas |
4618 |
30 Oct 08 |
nicklas |
@version 2.9 |
4618 |
30 Oct 08 |
nicklas |
@base.modified $Date$ |
4618 |
30 Oct 08 |
nicklas |
41 |
*/ |
4618 |
30 Oct 08 |
nicklas |
42 |
public class ThrowableUtil |
4618 |
30 Oct 08 |
nicklas |
43 |
{ |
4618 |
30 Oct 08 |
nicklas |
44 |
|
4618 |
30 Oct 08 |
nicklas |
45 |
/** |
4618 |
30 Oct 08 |
nicklas |
Convert a stacktrace to a string that is usually printed |
4618 |
30 Oct 08 |
nicklas |
with the {@link Throwable#printStackTrace()} method. |
4618 |
30 Oct 08 |
nicklas |
@param t The throwable |
4618 |
30 Oct 08 |
nicklas |
@return The string representation of the stack trace |
4618 |
30 Oct 08 |
nicklas |
50 |
*/ |
4618 |
30 Oct 08 |
nicklas |
51 |
public static String stackTraceToString(Throwable t) |
4618 |
30 Oct 08 |
nicklas |
52 |
{ |
4618 |
30 Oct 08 |
nicklas |
53 |
StringWriter sw = new StringWriter(); |
4618 |
30 Oct 08 |
nicklas |
54 |
PrintWriter pw = new PrintWriter(sw); |
4618 |
30 Oct 08 |
nicklas |
55 |
t.printStackTrace(pw); |
6451 |
23 Apr 14 |
nicklas |
56 |
pw.flush(); |
4618 |
30 Oct 08 |
nicklas |
57 |
pw.close(); |
6451 |
23 Apr 14 |
nicklas |
58 |
sw.flush(); |
5911 |
15 Dec 11 |
nicklas |
59 |
return sw.toString().replaceAll("\t", " "); |
4618 |
30 Oct 08 |
nicklas |
60 |
} |
5114 |
02 Oct 09 |
nicklas |
61 |
|
5114 |
02 Oct 09 |
nicklas |
62 |
/** |
5114 |
02 Oct 09 |
nicklas |
Prints as much of a stacktrace as possible to an image. |
5114 |
02 Oct 09 |
nicklas |
@param t The throwable |
5114 |
02 Oct 09 |
nicklas |
@param width The width of the image |
5114 |
02 Oct 09 |
nicklas |
@param height The height of the image |
5114 |
02 Oct 09 |
nicklas |
@since 2.14 |
5114 |
02 Oct 09 |
nicklas |
68 |
*/ |
5114 |
02 Oct 09 |
nicklas |
69 |
public static BufferedImage stackTraceToImage(Throwable t, int width, int height) |
5114 |
02 Oct 09 |
nicklas |
70 |
{ |
5114 |
02 Oct 09 |
nicklas |
71 |
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); |
5114 |
02 Oct 09 |
nicklas |
72 |
Graphics2D graphics = img.createGraphics(); |
5114 |
02 Oct 09 |
nicklas |
73 |
graphics.setBackground(Color.WHITE); |
5114 |
02 Oct 09 |
nicklas |
74 |
graphics.clearRect(0, 0, width, height); |
5114 |
02 Oct 09 |
nicklas |
75 |
graphics.setColor(Color.RED); |
5114 |
02 Oct 09 |
nicklas |
76 |
FontRenderContext context = graphics.getFontRenderContext(); |
5114 |
02 Oct 09 |
nicklas |
77 |
|
5114 |
02 Oct 09 |
nicklas |
78 |
Font topFont = new Font("SansSerif", Font.BOLD, 12); |
5114 |
02 Oct 09 |
nicklas |
79 |
Font stackTraceFont = new Font("SansSerif", Font.PLAIN, 12); |
5114 |
02 Oct 09 |
nicklas |
80 |
|
5114 |
02 Oct 09 |
nicklas |
81 |
float x = .0f; |
5114 |
02 Oct 09 |
nicklas |
82 |
float y = .0f; |
5114 |
02 Oct 09 |
nicklas |
83 |
String causedBy = ""; |
5114 |
02 Oct 09 |
nicklas |
84 |
do |
5114 |
02 Oct 09 |
nicklas |
85 |
{ |
5114 |
02 Oct 09 |
nicklas |
86 |
graphics.setFont(topFont); |
5114 |
02 Oct 09 |
nicklas |
87 |
String msg = causedBy + t.getClass().getSimpleName() + ": " + t.getMessage(); |
5114 |
02 Oct 09 |
nicklas |
88 |
x = 5.0f; |
5114 |
02 Oct 09 |
nicklas |
89 |
Rectangle2D bounds = topFont.getStringBounds(msg, context); |
5114 |
02 Oct 09 |
nicklas |
90 |
y += bounds.getHeight()+5; |
5114 |
02 Oct 09 |
nicklas |
91 |
if (bounds.getWidth() > width - x) |
5114 |
02 Oct 09 |
nicklas |
92 |
{ |
5114 |
02 Oct 09 |
nicklas |
93 |
msg = StringUtil.trimStringMiddle(msg,(int)(msg.length() * (width - x) / bounds.getWidth())); |
5114 |
02 Oct 09 |
nicklas |
94 |
} |
5114 |
02 Oct 09 |
nicklas |
95 |
graphics.drawString(msg, x, y); |
5114 |
02 Oct 09 |
nicklas |
96 |
|
5114 |
02 Oct 09 |
nicklas |
97 |
graphics.setFont(stackTraceFont); |
5114 |
02 Oct 09 |
nicklas |
98 |
x = 15.0f; |
5114 |
02 Oct 09 |
nicklas |
99 |
for (StackTraceElement st : t.getStackTrace()) |
5114 |
02 Oct 09 |
nicklas |
100 |
{ |
5114 |
02 Oct 09 |
nicklas |
101 |
msg = "at " + st.getClassName() + "." + st.getMethodName() + |
5114 |
02 Oct 09 |
nicklas |
102 |
":" + st.getLineNumber(); |
5114 |
02 Oct 09 |
nicklas |
103 |
bounds = stackTraceFont.getStringBounds(msg, context); |
5114 |
02 Oct 09 |
nicklas |
104 |
y += bounds.getHeight(); |
5114 |
02 Oct 09 |
nicklas |
105 |
if (bounds.getWidth() > width - x) |
5114 |
02 Oct 09 |
nicklas |
106 |
{ |
5114 |
02 Oct 09 |
nicklas |
107 |
msg = StringUtil.trimStringMiddle(msg,(int)(msg.length() * (width - x) / bounds.getWidth())); |
5114 |
02 Oct 09 |
nicklas |
108 |
} |
5114 |
02 Oct 09 |
nicklas |
109 |
graphics.drawString(msg, x, y); |
5114 |
02 Oct 09 |
nicklas |
110 |
} |
5114 |
02 Oct 09 |
nicklas |
111 |
t = t.getCause(); |
5114 |
02 Oct 09 |
nicklas |
112 |
causedBy = "Caused by: "; |
5114 |
02 Oct 09 |
nicklas |
113 |
} while (t != null && y < height); |
5114 |
02 Oct 09 |
nicklas |
114 |
return img; |
5114 |
02 Oct 09 |
nicklas |
115 |
} |
5114 |
02 Oct 09 |
nicklas |
116 |
|
4618 |
30 Oct 08 |
nicklas |
117 |
} |