@@ -37,120 +37,7 @@ public static class Log {
37
37
38
38
/** Constructor. */
39
39
public LogPattern (String pattern ) {
40
- List <Section > sections = new LinkedList <>();
41
- int pos = 0 ;
42
- int len = pattern .length ();
43
- while (pos < len ) {
44
- int i = pattern .indexOf ('%' , pos );
45
- if (i > pos ) {
46
- sections .add (new StringSection (pattern .substring (pos , i )));
47
- pos = i ;
48
- } else if (i < 0 ) {
49
- sections .add (new StringSection (pattern .substring (pos )));
50
- break ;
51
- }
52
- if (pos == len - 1 ) {
53
- sections .add (new StringSection ("%" ));
54
- break ;
55
- }
56
- char c = pattern .charAt (pos + 1 );
57
- if (c == 'd' ) {
58
- if (pos + 3 >= len || pattern .charAt (pos + 2 ) != '{' ) {
59
- sections .add (new StringSection (pattern .substring (pos , pos + 2 )));
60
- pos += 2 ;
61
- } else {
62
- i = pattern .indexOf ('}' , pos + 3 );
63
- if (i < 0 ) {
64
- sections .add (new StringSection (pattern .substring (pos , pos + 2 )));
65
- pos += 2 ;
66
- } else {
67
- String format = pattern .substring (pos + 3 , i );
68
- sections .add (new DateSection (format ));
69
- pos = i + 1 ;
70
- }
71
- }
72
- } else if (c == 't' ) {
73
- sections .add (new ThreadNameSection ());
74
- needsThreadName = true ;
75
- pos += 2 ;
76
- } else if (c == '%' ) {
77
- sections .add (new StringSection ("%" ));
78
- pos += 2 ;
79
- } else if (c == 'l' ) {
80
- if (pos + 5 >= len ) {
81
- sections .add (new StringSection (pattern .substring (pos , pos + 2 )));
82
- pos += 2 ;
83
- } else {
84
- c = pattern .charAt (pos + 2 );
85
- if (c == 'e' ) {
86
- // can be level
87
- if (pattern .charAt (pos + 3 ) == 'v' &&
88
- pattern .charAt (pos + 4 ) == 'e' &&
89
- pattern .charAt (pos + 5 ) == 'l' ) {
90
- sections .add (new LevelSection ());
91
- pos += 6 ;
92
- } else {
93
- sections .add (new StringSection (pattern .substring (pos , pos + 2 )));
94
- pos += 2 ;
95
- }
96
- } else if (c == 'o' ) {
97
- // can be logger or location
98
- if (pos < len - 7 &&
99
- pattern .charAt (pos + 3 ) == 'g' &&
100
- pattern .charAt (pos + 4 ) == 'g' &&
101
- pattern .charAt (pos + 5 ) == 'e' &&
102
- pattern .charAt (pos + 6 ) == 'r' ) {
103
- if (pos < len - 8 && pattern .charAt (pos + 7 ) == '{' ) {
104
- i = pattern .indexOf ('}' , pos + 8 );
105
- if (i < 0 ) {
106
- sections .add (new LoggerSection (-1 ));
107
- pos += 7 ;
108
- } else {
109
- int size = -1 ;
110
- try { size = Integer .parseInt (pattern .substring (pos + 8 , i )); }
111
- catch (Exception t ) { /* ignore */ }
112
- sections .add (new LoggerSection (size ));
113
- pos = i + 1 ;
114
- }
115
- } else {
116
- sections .add (new LoggerSection (-1 ));
117
- pos += 7 ;
118
- }
119
- } else {
120
- sections .add (new StringSection (pattern .substring (pos , pos + 2 )));
121
- pos += 2 ;
122
- }
123
- } else {
124
- sections .add (new StringSection (pattern .substring (pos , pos + 2 )));
125
- pos += 2 ;
126
- }
127
- }
128
- } else if (c == 'm' ) {
129
- sections .add (new MessageSection ());
130
- pos += 2 ;
131
- } else if (c == 'C' ) {
132
- sections .add (new ClassNameSection ());
133
- needsLocation = true ;
134
- pos += 2 ;
135
- } else if (c == 'M' ) {
136
- sections .add (new MethodNameSection ());
137
- needsLocation = true ;
138
- pos += 2 ;
139
- } else if (c == 'L' ) {
140
- sections .add (new LineSection ());
141
- needsLocation = true ;
142
- pos += 2 ;
143
- } else if (c == 'f' ) {
144
- sections .add (new FileNameSection ());
145
- needsLocation = true ;
146
- pos += 2 ;
147
- } else {
148
- sections .add (new StringSection (pattern .substring (pos , pos + 2 )));
149
- pos += 2 ;
150
- }
151
- }
152
- // TODO concatenate successive StringSection
153
- parts = sections .toArray (new Section [sections .size ()]);
40
+ parsePattern (pattern );
154
41
}
155
42
156
43
private Section [] parts ;
@@ -281,4 +168,132 @@ else if (len < fixedSize) {
281
168
}
282
169
}
283
170
171
+ private void parsePattern (String pattern ) {
172
+ List <Section > sections = new LinkedList <>();
173
+ int pos = 0 ;
174
+ int len = pattern .length ();
175
+ while (pos < len ) {
176
+ int i = pattern .indexOf ('%' , pos );
177
+ if (i > pos ) {
178
+ sections .add (new StringSection (pattern .substring (pos , i )));
179
+ pos = i ;
180
+ } else if (i < 0 ) {
181
+ sections .add (new StringSection (pattern .substring (pos )));
182
+ break ;
183
+ }
184
+ if (pos == len - 1 ) {
185
+ sections .add (new StringSection ("%" ));
186
+ break ;
187
+ }
188
+ char c = pattern .charAt (pos + 1 );
189
+ switch (c ) {
190
+ case 'd' :
191
+ pos = parsePatternD (pattern , pos , len , sections );
192
+ break ;
193
+ case 'f' :
194
+ sections .add (new FileNameSection ());
195
+ needsLocation = true ;
196
+ pos += 2 ;
197
+ break ;
198
+ case 't' :
199
+ sections .add (new ThreadNameSection ());
200
+ needsThreadName = true ;
201
+ pos += 2 ;
202
+ break ;
203
+ case 'l' :
204
+ pos = parsePatternL (pattern , pos , len , sections );
205
+ break ;
206
+ case 'm' :
207
+ sections .add (new MessageSection ());
208
+ pos += 2 ;
209
+ break ;
210
+ case 'C' :
211
+ sections .add (new ClassNameSection ());
212
+ needsLocation = true ;
213
+ pos += 2 ;
214
+ break ;
215
+ case 'L' :
216
+ sections .add (new LineSection ());
217
+ needsLocation = true ;
218
+ pos += 2 ;
219
+ break ;
220
+ case 'M' :
221
+ sections .add (new MethodNameSection ());
222
+ needsLocation = true ;
223
+ pos += 2 ;
224
+ break ;
225
+ case '%' :
226
+ sections .add (new StringSection ("%" ));
227
+ pos += 2 ;
228
+ break ;
229
+ default :
230
+ sections .add (new StringSection (pattern .substring (pos , pos + 2 )));
231
+ pos += 2 ;
232
+ break ;
233
+ }
234
+ }
235
+ // TODO concatenate successive StringSection
236
+ parts = sections .toArray (new Section [sections .size ()]);
237
+ }
238
+
239
+ private static int parsePatternD (String pattern , int pos , int len , List <Section > sections ) {
240
+ if (pos + 3 >= len || pattern .charAt (pos + 2 ) != '{' ) {
241
+ sections .add (new StringSection (pattern .substring (pos , pos + 2 )));
242
+ return pos + 2 ;
243
+ }
244
+ int i = pattern .indexOf ('}' , pos + 3 );
245
+ if (i < 0 ) {
246
+ sections .add (new StringSection (pattern .substring (pos , pos + 2 )));
247
+ return pos + 2 ;
248
+ }
249
+ String format = pattern .substring (pos + 3 , i );
250
+ sections .add (new DateSection (format ));
251
+ return i + 1 ;
252
+ }
253
+
254
+ private static int parsePatternL (String pattern , int pos , int len , List <Section > sections ) {
255
+ if (pos + 5 >= len ) {
256
+ sections .add (new StringSection (pattern .substring (pos , pos + 2 )));
257
+ return pos + 2 ;
258
+ }
259
+ char c = pattern .charAt (pos + 2 );
260
+ if (c == 'e' ) {
261
+ // can be level
262
+ if (pattern .charAt (pos + 3 ) == 'v' &&
263
+ pattern .charAt (pos + 4 ) == 'e' &&
264
+ pattern .charAt (pos + 5 ) == 'l' ) {
265
+ sections .add (new LevelSection ());
266
+ return pos + 6 ;
267
+ }
268
+ sections .add (new StringSection (pattern .substring (pos , pos + 2 )));
269
+ return pos + 2 ;
270
+ }
271
+ if (c == 'o' ) {
272
+ // can be logger or location
273
+ if (pos <= len - 7 &&
274
+ pattern .charAt (pos + 3 ) == 'g' &&
275
+ pattern .charAt (pos + 4 ) == 'g' &&
276
+ pattern .charAt (pos + 5 ) == 'e' &&
277
+ pattern .charAt (pos + 6 ) == 'r' ) {
278
+ if (pos <= len - 8 && pattern .charAt (pos + 7 ) == '{' ) {
279
+ int i = pattern .indexOf ('}' , pos + 8 );
280
+ if (i < 0 ) {
281
+ sections .add (new LoggerSection (-1 ));
282
+ return pos + 7 ;
283
+ }
284
+ int size = -1 ;
285
+ try { size = Integer .parseInt (pattern .substring (pos + 8 , i )); }
286
+ catch (Exception t ) { /* ignore */ }
287
+ sections .add (new LoggerSection (size ));
288
+ return i + 1 ;
289
+ }
290
+ sections .add (new LoggerSection (-1 ));
291
+ return pos + 7 ;
292
+ }
293
+ sections .add (new StringSection (pattern .substring (pos , pos + 2 )));
294
+ return pos + 2 ;
295
+ }
296
+ sections .add (new StringSection (pattern .substring (pos , pos + 2 )));
297
+ return pos + 2 ;
298
+ }
284
299
}
0 commit comments