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