1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.core.impl;
18
19 import java.util.ArrayList;
20 import java.util.List;
21 import java.util.Scanner;
22
23 import org.apache.logging.log4j.core.util.Constants;
24 import org.apache.logging.log4j.core.util.Patterns;
25 import org.apache.logging.log4j.util.Strings;
26
27
28
29
30 public final class ThrowableFormatOptions {
31
32 private static final int DEFAULT_LINES = Integer.MAX_VALUE;
33
34
35
36
37 protected static final ThrowableFormatOptions DEFAULT = new ThrowableFormatOptions();
38
39
40
41
42 private static final String FULL = "full";
43
44
45
46
47 private static final String NONE = "none";
48
49
50
51
52 private static final String SHORT = "short";
53
54
55
56
57 private final int lines;
58
59
60
61
62 private final String separator;
63
64
65
66
67 private final List<String> packages;
68
69 public static final String CLASS_NAME = "short.className";
70 public static final String METHOD_NAME = "short.methodName";
71 public static final String LINE_NUMBER = "short.lineNumber";
72 public static final String FILE_NAME = "short.fileName";
73 public static final String MESSAGE = "short.message";
74 public static final String LOCALIZED_MESSAGE = "short.localizedMessage";
75
76
77
78
79
80
81
82 protected ThrowableFormatOptions(final int lines, final String separator, final List<String> packages) {
83 this.lines = lines;
84 this.separator = separator == null ? Constants.LINE_SEPARATOR : separator;
85 this.packages = packages;
86 }
87
88
89
90
91
92 protected ThrowableFormatOptions(final List<String> packages) {
93 this(DEFAULT_LINES, null, packages);
94 }
95
96
97
98
99 protected ThrowableFormatOptions() {
100 this(DEFAULT_LINES, null, null);
101 }
102
103
104
105
106
107 public int getLines() {
108 return this.lines;
109 }
110
111
112
113
114
115 public String getSeparator() {
116 return this.separator;
117 }
118
119
120
121
122
123 public List<String> getPackages() {
124 return this.packages;
125 }
126
127
128
129
130
131 public boolean allLines() {
132 return this.lines == DEFAULT_LINES;
133 }
134
135
136
137
138
139 public boolean anyLines() {
140 return this.lines > 0;
141 }
142
143
144
145
146
147
148 public int minLines(final int maxLines) {
149 return this.lines > maxLines ? maxLines : this.lines;
150 }
151
152
153
154
155
156 public boolean hasPackages() {
157 return this.packages != null && !this.packages.isEmpty();
158 }
159
160
161
162
163 @Override
164 public String toString() {
165 final StringBuilder s = new StringBuilder();
166 s.append('{').append(allLines() ? FULL : this.lines == 2 ? SHORT : anyLines() ? String.valueOf(this.lines) : NONE).append('}');
167 s.append("{separator(").append(this.separator).append(")}");
168 if (hasPackages()) {
169 s.append("{filters(");
170 for (final String p : this.packages) {
171 s.append(p).append(',');
172 }
173 s.deleteCharAt(s.length() - 1);
174 s.append(")}");
175 }
176 return s.toString();
177 }
178
179
180
181
182
183 public static ThrowableFormatOptions newInstance(String[] options) {
184 if (options == null || options.length == 0) {
185 return DEFAULT;
186 }
187
188
189
190
191
192
193 if (options.length == 1 && Strings.isNotEmpty(options[0])) {
194 final String[] opts = options[0].split(Patterns.COMMA_SEPARATOR, 2);
195 final String first = opts[0].trim();
196 try (final Scanner scanner = new Scanner(first)) {
197 if (opts.length > 1
198 && (first.equalsIgnoreCase(FULL) || first.equalsIgnoreCase(SHORT)
199 || first.equalsIgnoreCase(NONE) || scanner.hasNextInt())) {
200 options = new String[] {
201 first,
202 opts[1].trim() };
203 }
204 }
205 }
206
207 int lines = DEFAULT.lines;
208 String separator = DEFAULT.separator;
209 List<String> packages = DEFAULT.packages;
210 for (final String rawOption : options) {
211 if (rawOption != null) {
212 final String option = rawOption.trim();
213 if (option.isEmpty()) {
214
215 } else if (option.startsWith("separator(") && option.endsWith(")")) {
216 separator = option.substring("separator(".length(), option.length() - 1);
217 } else if (option.startsWith("filters(") && option.endsWith(")")) {
218 final String filterStr = option.substring("filters(".length(), option.length() - 1);
219 if (filterStr.length() > 0) {
220 final String[] array = filterStr.split(Patterns.COMMA_SEPARATOR);
221 if (array.length > 0) {
222 packages = new ArrayList<>(array.length);
223 for (String token : array) {
224 token = token.trim();
225 if (token.length() > 0) {
226 packages.add(token);
227 }
228 }
229 }
230 }
231 } else if (option.equalsIgnoreCase(NONE)) {
232 lines = 0;
233 } else if (option.equalsIgnoreCase(SHORT) || option.equalsIgnoreCase(CLASS_NAME) ||
234 option.equalsIgnoreCase(METHOD_NAME) || option.equalsIgnoreCase(LINE_NUMBER) ||
235 option.equalsIgnoreCase(FILE_NAME) || option.equalsIgnoreCase(MESSAGE) ||
236 option.equalsIgnoreCase(LOCALIZED_MESSAGE)) {
237 lines = 2;
238 } else if (!option.equalsIgnoreCase(FULL)) {
239 lines = Integer.parseInt(option);
240 }
241 }
242 }
243 return new ThrowableFormatOptions(lines, separator, packages);
244 }
245 }