1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.core.helpers;
18
19 import java.io.IOException;
20 import java.io.InterruptedIOException;
21 import java.io.LineNumberReader;
22 import java.io.PrintWriter;
23 import java.io.StringReader;
24 import java.io.StringWriter;
25 import java.util.ArrayList;
26 import java.util.List;
27
28
29
30
31 public class Throwables {
32
33
34
35
36
37
38
39
40 public static List<String> toStringList(final Throwable throwable) {
41 final StringWriter sw = new StringWriter();
42 final PrintWriter pw = new PrintWriter(sw);
43 try {
44 throwable.printStackTrace(pw);
45 } catch (final RuntimeException ex) {
46
47 }
48 pw.flush();
49 final LineNumberReader reader = new LineNumberReader(new StringReader(sw.toString()));
50 final ArrayList<String> lines = new ArrayList<String>();
51 try {
52 String line = reader.readLine();
53 while (line != null) {
54 lines.add(line);
55 line = reader.readLine();
56 }
57 } catch (final IOException ex) {
58 if (ex instanceof InterruptedIOException) {
59 Thread.currentThread().interrupt();
60 }
61 lines.add(ex.toString());
62 }
63 return lines;
64 }
65
66 }