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