1
2
3
4
5
6
7
8
9
10
11 package ch.qos.logback.core.util;
12
13 import java.io.PrintStream;
14 import java.text.SimpleDateFormat;
15 import java.util.Date;
16 import java.util.Iterator;
17 import java.util.List;
18
19 import ch.qos.logback.core.Context;
20 import ch.qos.logback.core.CoreConstants;
21 import ch.qos.logback.core.helpers.ThrowableToStringArray;
22 import ch.qos.logback.core.status.ErrorStatus;
23 import ch.qos.logback.core.status.Status;
24 import ch.qos.logback.core.status.StatusManager;
25
26 public class StatusPrinter {
27
28 private static PrintStream ps = System.out;
29
30 static SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
31 "HH:mm:ss,SSS");
32
33 public static void setPrintStream(PrintStream printStream) {
34 ps = printStream;
35 }
36
37
38
39
40
41
42
43 public static void printInCaseOfErrorsOrWarnings(Context context) {
44 if (context == null) {
45 throw new IllegalArgumentException("Context argument cannot be null");
46 }
47
48 StatusManager sm = context.getStatusManager();
49 if (sm == null) {
50 ps.println("WARN: Context named \"" + context.getName()
51 + "\" has no status manager");
52 } else {
53 if (sm.getLevel() == ErrorStatus.WARN || (sm.getLevel() == ErrorStatus.ERROR) ) {
54 print(sm);
55 }
56 }
57 }
58
59
60
61
62
63
64
65 public static void printIfErrorsOccured(Context context) {
66 if (context == null) {
67 throw new IllegalArgumentException("Context argument cannot be null");
68 }
69
70 StatusManager sm = context.getStatusManager();
71 if (sm == null) {
72 ps.println("WARN: Context named \"" + context.getName()
73 + "\" has no status manager");
74 } else {
75 if (sm.getLevel() == ErrorStatus.ERROR) {
76 print(sm);
77 }
78 }
79 }
80
81
82
83
84
85
86 public static void print(Context context) {
87 if (context == null) {
88 throw new IllegalArgumentException("Context argument cannot be null");
89 }
90
91 StatusManager sm = context.getStatusManager();
92 if (sm == null) {
93 ps.println("WARN: Context named \"" + context.getName()
94 + "\" has no status manager");
95 } else {
96 print(sm);
97 }
98
99 }
100
101 public static void print(StatusManager sm) {
102 StringBuilder sb = new StringBuilder();
103 buildStrFromStatusManager(sb, sm);
104 ps.println(sb.toString());
105 }
106
107 public static void print(List<Status> statusList) {
108 StringBuilder sb = new StringBuilder();
109 buildStrFromStatusList(sb, statusList);
110 ps.println(sb.toString());
111 }
112
113
114 private static void buildStrFromStatusList(StringBuilder sb, List<Status> statusList) {
115 if(statusList == null)
116 return;
117 for(Status s : statusList) {
118 buildStr(sb, "", s);
119 }
120 }
121
122 private static void buildStrFromStatusManager(StringBuilder sb, StatusManager sm) {
123 buildStrFromStatusList(sb, sm.getCopyOfStatusList());
124 }
125
126 private static void appendThrowable(StringBuilder sb, Throwable t) {
127 String[] stringRep = ThrowableToStringArray.convert(t);
128
129 for (String s : stringRep) {
130 if (s.startsWith(CoreConstants.CAUSED_BY)) {
131
132 } else if (Character.isDigit(s.charAt(0))) {
133
134 sb.append("\t... ");
135 } else {
136
137 sb.append("\tat ");
138 }
139 sb.append(s).append(CoreConstants.LINE_SEPARATOR);
140 }
141 }
142
143 public static void buildStr(StringBuilder sb, String indentation, Status s) {
144 String prefix;
145 if (s.hasChildren()) {
146 prefix = indentation + "+ ";
147 } else {
148 prefix = indentation + "|-";
149 }
150
151 if (simpleDateFormat != null) {
152 Date date = new Date(s.getDate());
153 String dateStr = simpleDateFormat.format(date);
154 sb.append(dateStr).append(" ");
155 }
156 sb.append(prefix).append(s).append(CoreConstants.LINE_SEPARATOR);
157
158 if (s.getThrowable() != null) {
159 appendThrowable(sb, s.getThrowable());
160 }
161 if (s.hasChildren()) {
162 Iterator<Status> ite = s.iterator();
163 while (ite.hasNext()) {
164 Status child = ite.next();
165 buildStr(sb, indentation + " ", child);
166 }
167 }
168 }
169
170 }