1 : <?php
2 : /**
3 : * Licensed to the Apache Software Foundation (ASF) under one or more
4 : * contributor license agreements. See the NOTICE file distributed with
5 : * this work for additional information regarding copyright ownership.
6 : * The ASF licenses this file to You under the Apache License, Version 2.0
7 : * (the "License"); you may not use this file except in compliance with
8 : * the License. You may obtain a copy of the License at
9 : *
10 : * http://www.apache.org/licenses/LICENSE-2.0
11 : *
12 : * Unless required by applicable law or agreed to in writing, software
13 : * distributed under the License is distributed on an "AS IS" BASIS,
14 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 : * See the License for the specific language governing permissions and
16 : * limitations under the License.
17 : *
18 : * @package log4php
19 : */
20 :
21 :
22 : /**
23 : * LoggerPatternConverter is an abstract class that provides the formatting
24 : * functionality that derived classes need.
25 : *
26 : * <p>Conversion specifiers in a conversion patterns are parsed to
27 : * individual PatternConverters. Each of which is responsible for
28 : * converting a logging event in a converter specific manner.</p>
29 : *
30 : * @version $Revision: 1166187 $
31 : * @package log4php
32 : * @subpackage helpers
33 : * @since 0.3
34 : */
35 : class LoggerPatternConverter {
36 :
37 : /**
38 : * Array for fast space padding
39 : * Used by {@link LoggerPatternConverter::spacePad()}.
40 : */
41 : private $spaces = array(
42 : " ", // 1 space
43 : " ", // 2 spaces
44 : " ", // 4 spaces
45 : " ", // 8 spaces
46 : " ", // 16 spaces
47 : " "); // 32 spaces
48 :
49 : /**
50 : * @var LoggerPatternConverter next converter in converter chain
51 : */
52 : public $next = null;
53 :
54 : public $min = -1;
55 : public $max = 0x7FFFFFFF;
56 : public $leftAlign = false;
57 :
58 : /**
59 : * Constructor
60 : *
61 : * @param LoggerFormattingInfo $fi
62 : */
63 : public function __construct($fi = null) {
64 16 : if($fi !== null) {
65 16 : $this->min = $fi->min;
66 16 : $this->max = $fi->max;
67 16 : $this->leftAlign = $fi->leftAlign;
68 16 : }
69 16 : }
70 :
71 : /**
72 : * Derived pattern converters must override this method in order to
73 : * convert conversion specifiers in the correct way.
74 : *
75 : * @param LoggerLoggingEvent $event
76 : */
77 0 : public function convert($event) {}
78 :
79 : /**
80 : * A template method for formatting in a converter specific way.
81 : *
82 : * @param string &$sbuf string buffer
83 : * @param LoggerLoggingEvent $e
84 : */
85 : public function format(&$sbuf, $e) {
86 16 : $s = $this->convert($e);
87 :
88 16 : if($s == null or empty($s)) {
89 1 : if(0 < $this->min) {
90 0 : $this->spacePad($sbuf, $this->min);
91 0 : }
92 1 : return;
93 : }
94 :
95 16 : $len = strlen($s);
96 :
97 16 : if($len > $this->max) {
98 0 : $sbuf .= substr($s , 0, ($len - $this->max));
99 16 : } else if($len < $this->min) {
100 5 : if($this->leftAlign) {
101 5 : $sbuf .= $s;
102 5 : $this->spacePad($sbuf, ($this->min - $len));
103 5 : } else {
104 0 : $this->spacePad($sbuf, ($this->min - $len));
105 0 : $sbuf .= $s;
106 : }
107 5 : } else {
108 16 : $sbuf .= $s;
109 : }
110 16 : }
111 :
112 : /**
113 : * Fast space padding method.
114 : *
115 : * @param string &$sbuf string buffer
116 : * @param integer $length pad length
117 : *
118 : * @todo reimplement using PHP string functions
119 : */
120 : public function spacePad(&$sbuf, $length) {
121 5 : while($length >= 32) {
122 0 : $sbuf .= $this->spaces[5];
123 0 : $length -= 32;
124 0 : }
125 :
126 5 : for($i = 4; $i >= 0; $i--) {
127 5 : if(($length & (1<<$i)) != 0) {
128 5 : $sbuf .= $this->spaces[$i];
129 5 : }
130 5 : }
131 :
132 : // $sbuf = str_pad($sbuf, $length);
133 5 : }
134 : }
|