LoggerPatternParser.php
17.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
<?php
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
* @package log4php
* @subpackage helpers
*/
/**
* @ignore
*/
if (!defined('LOG4PHP_DIR')) define('LOG4PHP_DIR', dirname(__FILE__) . '/..');
if (!defined('LOG4PHP_LINE_SEP')) {
if (substr(php_uname(), 0, 7) == "Windows") {
/**
* @ignore
*/
define('LOG4PHP_LINE_SEP', "\r\n");
} else {
/**
* @ignore
*/
define('LOG4PHP_LINE_SEP', "\n");
}
}
/**
*/
require_once(LOG4PHP_DIR . '/helpers/LoggerFormattingInfo.php');
require_once(LOG4PHP_DIR . '/helpers/LoggerPatternConverter.php');
require_once(LOG4PHP_DIR . '/LoggerLog.php');
define('LOG4PHP_LOGGER_PATTERN_PARSER_ESCAPE_CHAR', '%');
define('LOG4PHP_LOGGER_PATTERN_PARSER_LITERAL_STATE', 0);
define('LOG4PHP_LOGGER_PATTERN_PARSER_CONVERTER_STATE', 1);
define('LOG4PHP_LOGGER_PATTERN_PARSER_MINUS_STATE', 2);
define('LOG4PHP_LOGGER_PATTERN_PARSER_DOT_STATE', 3);
define('LOG4PHP_LOGGER_PATTERN_PARSER_MIN_STATE', 4);
define('LOG4PHP_LOGGER_PATTERN_PARSER_MAX_STATE', 5);
define('LOG4PHP_LOGGER_PATTERN_PARSER_FULL_LOCATION_CONVERTER', 1000);
define('LOG4PHP_LOGGER_PATTERN_PARSER_METHOD_LOCATION_CONVERTER', 1001);
define('LOG4PHP_LOGGER_PATTERN_PARSER_CLASS_LOCATION_CONVERTER', 1002);
define('LOG4PHP_LOGGER_PATTERN_PARSER_FILE_LOCATION_CONVERTER', 1003);
define('LOG4PHP_LOGGER_PATTERN_PARSER_LINE_LOCATION_CONVERTER', 1004);
define('LOG4PHP_LOGGER_PATTERN_PARSER_RELATIVE_TIME_CONVERTER', 2000);
define('LOG4PHP_LOGGER_PATTERN_PARSER_THREAD_CONVERTER', 2001);
define('LOG4PHP_LOGGER_PATTERN_PARSER_LEVEL_CONVERTER', 2002);
define('LOG4PHP_LOGGER_PATTERN_PARSER_NDC_CONVERTER', 2003);
define('LOG4PHP_LOGGER_PATTERN_PARSER_MESSAGE_CONVERTER', 2004);
define('LOG4PHP_LOGGER_PATTERN_PARSER_DATE_FORMAT_ISO8601', 'Y-m-d H:i:s,u');
define('LOG4PHP_LOGGER_PATTERN_PARSER_DATE_FORMAT_ABSOLUTE', 'H:i:s');
define('LOG4PHP_LOGGER_PATTERN_PARSER_DATE_FORMAT_DATE', 'd M Y H:i:s,u');
/**
* Most of the work of the {@link LoggerPatternLayout} class
* is delegated to the {@link LoggerPatternParser} class.
*
* <p>It is this class that parses conversion patterns and creates
* a chained list of {@link LoggerPatternConverter} converters.</p>
*
* @author Marco Vassura
* @version $Revision: 635069 $
* @package log4php
* @subpackage helpers
*
* @since 0.3
*/
class LoggerPatternParser {
var $state;
var $currentLiteral;
var $patternLength;
var $i;
/**
* @var LoggerPatternConverter
*/
var $head = null;
/**
* @var LoggerPatternConverter
*/
var $tail = null;
/**
* @var LoggerFormattingInfo
*/
var $formattingInfo;
/**
* @var string pattern to parse
*/
var $pattern;
/**
* Constructor
*
* @param string $pattern
*/
function LoggerPatternParser($pattern)
{
LoggerLog::debug("LoggerPatternParser::LoggerPatternParser() pattern='$pattern'");
$this->pattern = $pattern;
$this->patternLength = strlen($pattern);
$this->formattingInfo = new LoggerFormattingInfo();
$this->state = LOG4PHP_LOGGER_PATTERN_PARSER_LITERAL_STATE;
}
/**
* @param LoggerPatternConverter $pc
*/
function addToList($pc)
{
// LoggerLog::debug("LoggerPatternParser::addToList()");
if($this->head == null) {
$this->head = $pc;
$this->tail =& $this->head;
} else {
$this->tail->next = $pc;
$this->tail =& $this->tail->next;
}
}
/**
* @return string
*/
function extractOption()
{
if(($this->i < $this->patternLength) and ($this->pattern{$this->i} == '{')) {
$end = strpos($this->pattern, '}' , $this->i);
if ($end !== false) {
$r = substr($this->pattern, ($this->i + 1), ($end - $this->i - 1));
$this->i= $end + 1;
return $r;
}
}
return null;
}
/**
* The option is expected to be in decimal and positive. In case of
* error, zero is returned.
*/
function extractPrecisionOption()
{
$opt = $this->extractOption();
$r = 0;
if ($opt !== null) {
if (is_numeric($opt)) {
$r = (int)$opt;
if($r <= 0) {
LoggerLog::warn("Precision option ({$opt}) isn't a positive integer.");
$r = 0;
}
} else {
LoggerLog::warn("Category option \"{$opt}\" not a decimal integer.");
}
}
return $r;
}
function parse()
{
LoggerLog::debug("LoggerPatternParser::parse()");
$c = '';
$this->i = 0;
$this->currentLiteral = '';
while ($this->i < $this->patternLength) {
$c = $this->pattern{$this->i++};
// LoggerLog::debug("LoggerPatternParser::parse() char is now '$c' and currentLiteral is '{$this->currentLiteral}'");
switch($this->state) {
case LOG4PHP_LOGGER_PATTERN_PARSER_LITERAL_STATE:
// LoggerLog::debug("LoggerPatternParser::parse() state is 'LOG4PHP_LOGGER_PATTERN_PARSER_LITERAL_STATE'");
// In literal state, the last char is always a literal.
if($this->i == $this->patternLength) {
$this->currentLiteral .= $c;
continue;
}
if($c == LOG4PHP_LOGGER_PATTERN_PARSER_ESCAPE_CHAR) {
// LoggerLog::debug("LoggerPatternParser::parse() char is an escape char");
// peek at the next char.
switch($this->pattern{$this->i}) {
case LOG4PHP_LOGGER_PATTERN_PARSER_ESCAPE_CHAR:
// LoggerLog::debug("LoggerPatternParser::parse() next char is an escape char");
$this->currentLiteral .= $c;
$this->i++; // move pointer
break;
case 'n':
// LoggerLog::debug("LoggerPatternParser::parse() next char is 'n'");
$this->currentLiteral .= LOG4PHP_LINE_SEP;
$this->i++; // move pointer
break;
default:
if(strlen($this->currentLiteral) != 0) {
$this->addToList(new LoggerLiteralPatternConverter($this->currentLiteral));
LoggerLog::debug("LoggerPatternParser::parse() Parsed LITERAL converter: \"{$this->currentLiteral}\".");
}
$this->currentLiteral = $c;
$this->state = LOG4PHP_LOGGER_PATTERN_PARSER_CONVERTER_STATE;
$this->formattingInfo->reset();
}
} else {
$this->currentLiteral .= $c;
}
break;
case LOG4PHP_LOGGER_PATTERN_PARSER_CONVERTER_STATE:
// LoggerLog::debug("LoggerPatternParser::parse() state is 'LOG4PHP_LOGGER_PATTERN_PARSER_CONVERTER_STATE'");
$this->currentLiteral .= $c;
switch($c) {
case '-':
$this->formattingInfo->leftAlign = true;
break;
case '.':
$this->state = LOG4PHP_LOGGER_PATTERN_PARSER_DOT_STATE;
break;
default:
if(ord($c) >= ord('0') and ord($c) <= ord('9')) {
$this->formattingInfo->min = ord($c) - ord('0');
$this->state = LOG4PHP_LOGGER_PATTERN_PARSER_MIN_STATE;
} else {
$this->finalizeConverter($c);
}
} // switch
break;
case LOG4PHP_LOGGER_PATTERN_PARSER_MIN_STATE:
// LoggerLog::debug("LoggerPatternParser::parse() state is 'LOG4PHP_LOGGER_PATTERN_PARSER_MIN_STATE'");
$this->currentLiteral .= $c;
if(ord($c) >= ord('0') and ord($c) <= ord('9')) {
$this->formattingInfo->min = ($this->formattingInfo->min * 10) + (ord($c) - ord('0'));
} elseif ($c == '.') {
$this->state = LOG4PHP_LOGGER_PATTERN_PARSER_DOT_STATE;
} else {
$this->finalizeConverter($c);
}
break;
case LOG4PHP_LOGGER_PATTERN_PARSER_DOT_STATE:
// LoggerLog::debug("LoggerPatternParser::parse() state is 'LOG4PHP_LOGGER_PATTERN_PARSER_DOT_STATE'");
$this->currentLiteral .= $c;
if(ord($c) >= ord('0') and ord($c) <= ord('9')) {
$this->formattingInfo->max = ord($c) - ord('0');
$this->state = LOG4PHP_LOGGER_PATTERN_PARSER_MAX_STATE;
} else {
LoggerLog::warn("LoggerPatternParser::parse() Error occured in position {$this->i}. Was expecting digit, instead got char \"{$c}\".");
$this->state = LOG4PHP_LOGGER_PATTERN_PARSER_LITERAL_STATE;
}
break;
case LOG4PHP_LOGGER_PATTERN_PARSER_MAX_STATE:
// LoggerLog::debug("LoggerPatternParser::parse() state is 'LOG4PHP_LOGGER_PATTERN_PARSER_MAX_STATE'");
$this->currentLiteral .= $c;
if(ord($c) >= ord('0') and ord($c) <= ord('9')) {
$this->formattingInfo->max = ($this->formattingInfo->max * 10) + (ord($c) - ord('0'));
} else {
$this->finalizeConverter($c);
$this->state = LOG4PHP_LOGGER_PATTERN_PARSER_LITERAL_STATE;
}
break;
} // switch
} // while
if(strlen($this->currentLiteral) != 0) {
$this->addToList(new LoggerLiteralPatternConverter($this->currentLiteral));
// LoggerLog::debug("LoggerPatternParser::parse() Parsed LITERAL converter: \"{$this->currentLiteral}\".");
}
return $this->head;
}
function finalizeConverter($c)
{
LoggerLog::debug("LoggerPatternParser::finalizeConverter() with char '$c'");
$pc = null;
switch($c) {
case 'c':
$pc = new LoggerCategoryPatternConverter($this->formattingInfo, $this->extractPrecisionOption());
LoggerLog::debug("LoggerPatternParser::finalizeConverter() CATEGORY converter.");
$this->currentLiteral = '';
break;
case 'C':
$pc = new LoggerClassNamePatternConverter($this->formattingInfo, $this->extractPrecisionOption());
LoggerLog::debug("LoggerPatternParser::finalizeConverter() CLASSNAME converter.");
$this->currentLiteral = '';
break;
case 'd':
$dateFormatStr = LOG4PHP_LOGGER_PATTERN_PARSER_DATE_FORMAT_ISO8601; // ISO8601_DATE_FORMAT;
$dOpt = $this->extractOption();
if($dOpt !== null)
$dateFormatStr = $dOpt;
if ($dateFormatStr == 'ISO8601') {
$df = LOG4PHP_LOGGER_PATTERN_PARSER_DATE_FORMAT_ISO8601;
} elseif($dateFormatStr == 'ABSOLUTE') {
$df = LOG4PHP_LOGGER_PATTERN_PARSER_DATE_FORMAT_ABSOLUTE;
} elseif($dateFormatStr == 'DATE') {
$df = LOG4PHP_LOGGER_PATTERN_PARSER_DATE_FORMAT_DATE;
} else {
$df = $dateFormatStr;
if ($df == null) {
$df = LOG4PHP_LOGGER_PATTERN_PARSER_DATE_FORMAT_ISO8601;
}
}
$pc = new LoggerDatePatternConverter($this->formattingInfo, $df);
$this->currentLiteral = '';
break;
case 'F':
$pc = new LoggerLocationPatternConverter($this->formattingInfo, LOG4PHP_LOGGER_PATTERN_PARSER_FILE_LOCATION_CONVERTER);
LoggerLog::debug("LoggerPatternParser::finalizeConverter() File name converter.");
$this->currentLiteral = '';
break;
case 'l':
$pc = new LoggerLocationPatternConverter($this->formattingInfo, LOG4PHP_LOGGER_PATTERN_PARSER_FULL_LOCATION_CONVERTER);
LoggerLog::debug("LoggerPatternParser::finalizeConverter() Location converter.");
$this->currentLiteral = '';
break;
case 'L':
$pc = new LoggerLocationPatternConverter($this->formattingInfo, LOG4PHP_LOGGER_PATTERN_PARSER_LINE_LOCATION_CONVERTER);
LoggerLog::debug("LoggerPatternParser::finalizeConverter() LINE NUMBER converter.");
$this->currentLiteral = '';
break;
case 'm':
$pc = new LoggerBasicPatternConverter($this->formattingInfo, LOG4PHP_LOGGER_PATTERN_PARSER_MESSAGE_CONVERTER);
LoggerLog::debug("LoggerPatternParser::finalizeConverter() MESSAGE converter.");
$this->currentLiteral = '';
break;
case 'M':
$pc = new LoggerLocationPatternConverter($this->formattingInfo, LOG4PHP_LOGGER_PATTERN_PARSER_METHOD_LOCATION_CONVERTER);
$this->currentLiteral = '';
break;
case 'p':
$pc = new LoggerBasicPatternConverter($this->formattingInfo, LOG4PHP_LOGGER_PATTERN_PARSER_LEVEL_CONVERTER);
$this->currentLiteral = '';
break;
case 'r':
$pc = new LoggerBasicPatternConverter($this->formattingInfo, LOG4PHP_LOGGER_PATTERN_PARSER_RELATIVE_TIME_CONVERTER);
LoggerLog::debug("LoggerPatternParser::finalizeConverter() RELATIVE TIME converter.");
$this->currentLiteral = '';
break;
case 't':
$pc = new LoggerBasicPatternConverter($this->formattingInfo, LOG4PHP_LOGGER_PATTERN_PARSER_THREAD_CONVERTER);
LoggerLog::debug("LoggerPatternParser::finalizeConverter() THREAD converter.");
$this->currentLiteral = '';
break;
case 'u':
if($this->i < $this->patternLength) {
$cNext = $this->pattern{$this->i};
if(ord($cNext) >= ord('0') and ord($cNext) <= ord('9')) {
$pc = new LoggerUserFieldPatternConverter($this->formattingInfo, (string)(ord($cNext) - ord('0')));
LoggerLog::debug("LoggerPatternParser::finalizeConverter() USER converter [{$cNext}].");
$this->currentLiteral = '';
$this->i++;
} else {
LoggerLog::warn("LoggerPatternParser::finalizeConverter() Unexpected char '{$cNext}' at position {$this->i}.");
}
}
break;
case 'x':
$pc = new LoggerBasicPatternConverter($this->formattingInfo, LOG4PHP_LOGGER_PATTERN_PARSER_NDC_CONVERTER);
LoggerLog::debug("LoggerPatternParser::finalizeConverter() NDC converter.");
$this->currentLiteral = '';
break;
case 'X':
$xOpt = $this->extractOption();
$pc = new LoggerMDCPatternConverter($this->formattingInfo, $xOpt);
LoggerLog::debug("LoggerPatternParser::finalizeConverter() MDC converter.");
$this->currentLiteral = '';
break;
default:
LoggerLog::warn("LoggerPatternParser::finalizeConverter() Unexpected char [$c] at position {$this->i} in conversion pattern.");
$pc = new LoggerLiteralPatternConverter($this->currentLiteral);
$this->currentLiteral = '';
}
$this->addConverter($pc);
}
function addConverter($pc)
{
$this->currentLiteral = '';
// Add the pattern converter to the list.
$this->addToList($pc);
// Next pattern is assumed to be a literal.
$this->state = LOG4PHP_LOGGER_PATTERN_PARSER_LITERAL_STATE;
// Reset formatting info
$this->formattingInfo->reset();
}
}