LoggerPatternConverter.php
3.42 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
<?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
*/
/**
* Array for fast space padding
* Used by {@link LoggerPatternConverter::spacePad()}.
*
* @package log4php
* @subpackage helpers
*/
$GLOBALS['log4php.LoggerPatternConverter.spaces'] = array(
" ", // 1 space
" ", // 2 spaces
" ", // 4 spaces
" ", // 8 spaces
" ", // 16 spaces
" " ); // 32 spaces
/**
* LoggerPatternConverter is an abstract class that provides the formatting
* functionality that derived classes need.
*
* <p>Conversion specifiers in a conversion patterns are parsed to
* individual PatternConverters. Each of which is responsible for
* converting a logging event in a converter specific manner.</p>
*
* @version $Revision: 822445 $
* @package log4php
* @subpackage helpers
* @abstract
* @since 0.3
*/
class LoggerPatternConverter {
/**
* @var LoggerPatternConverter next converter in converter chain
*/
public $next = null;
public $min = -1;
public $max = 0x7FFFFFFF;
public $leftAlign = false;
/**
* Constructor
*
* @param LoggerFormattingInfo $fi
*/
public function __construct($fi = null) {
if($fi !== null) {
$this->min = $fi->min;
$this->max = $fi->max;
$this->leftAlign = $fi->leftAlign;
}
}
/**
* Derived pattern converters must override this method in order to
* convert conversion specifiers in the correct way.
*
* @param LoggerLoggingEvent $event
*/
public function convert($event) {}
/**
* A template method for formatting in a converter specific way.
*
* @param string &$sbuf string buffer
* @param LoggerLoggingEvent $e
*/
public function format(&$sbuf, $e) {
$s = $this->convert($e);
if($s == null or empty($s)) {
if(0 < $this->min) {
$this->spacePad($sbuf, $this->min);
}
return;
}
$len = strlen($s);
if($len > $this->max) {
$sbuf .= substr($s , 0, ($len - $this->max));
} else if($len < $this->min) {
if($this->leftAlign) {
$sbuf .= $s;
$this->spacePad($sbuf, ($this->min - $len));
} else {
$this->spacePad($sbuf, ($this->min - $len));
$sbuf .= $s;
}
} else {
$sbuf .= $s;
}
}
/**
* Fast space padding method.
*
* @param string $sbuf string buffer
* @param integer $length pad length
*
* @todo reimplement using PHP string functions
*/
public function spacePad($sbuf, $length) {
while($length >= 32) {
$sbuf .= $GLOBALS['log4php.LoggerPatternConverter.spaces'][5];
$length -= 32;
}
for($i = 4; $i >= 0; $i--) {
if(($length & (1<<$i)) != 0) {
$sbuf .= $GLOBALS['log4php.LoggerPatternConverter.spaces'][$i];
}
}
// $sbuf = str_pad($sbuf, $length);
}
}