1 |
joko |
1.2 |
<?php |
2 |
|
|
// +-----------------------------------------------------------------------+ |
3 |
|
|
// | Copyright (c) 2002 Richard Heyes | |
4 |
|
|
// | All rights reserved. | |
5 |
|
|
// | | |
6 |
|
|
// | Redistribution and use in source and binary forms, with or without | |
7 |
|
|
// | modification, are permitted provided that the following conditions | |
8 |
|
|
// | are met: | |
9 |
|
|
// | | |
10 |
|
|
// | o Redistributions of source code must retain the above copyright | |
11 |
|
|
// | notice, this list of conditions and the following disclaimer. | |
12 |
|
|
// | o Redistributions in binary form must reproduce the above copyright | |
13 |
|
|
// | notice, this list of conditions and the following disclaimer in the | |
14 |
|
|
// | documentation and/or other materials provided with the distribution.| |
15 |
|
|
// | o The names of the authors may not be used to endorse or promote | |
16 |
|
|
// | products derived from this software without specific prior written | |
17 |
|
|
// | permission. | |
18 |
|
|
// | | |
19 |
|
|
// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
20 |
|
|
// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
21 |
|
|
// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
22 |
|
|
// | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
23 |
|
|
// | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
24 |
|
|
// | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
25 |
|
|
// | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
26 |
|
|
// | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
27 |
|
|
// | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
28 |
|
|
// | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
29 |
|
|
// | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
30 |
|
|
// | | |
31 |
|
|
// +-----------------------------------------------------------------------+ |
32 |
|
|
// | Author: Richard Heyes <richard@phpguru.org> | |
33 |
|
|
// | Jon Parise <jon@php.net> | |
34 |
|
|
// +-----------------------------------------------------------------------+ |
35 |
|
|
// |
36 |
|
|
// $Id: file.php,v 1.14 2002/12/02 05:23:00 jon Exp $ |
37 |
|
|
|
38 |
|
|
/** |
39 |
|
|
* The Log_file class is a concrete implementation of the Log:: |
40 |
|
|
* abstract class which writes message to a text file. This is based |
41 |
|
|
* on the previous Log_file class by Jon Parise. |
42 |
|
|
* |
43 |
|
|
* @author Richard Heyes <richard@php.net> |
44 |
|
|
* @version $Revision: 1.14 $ |
45 |
|
|
* @package Log |
46 |
|
|
*/ |
47 |
|
|
class Log_file extends Log |
48 |
|
|
{ |
49 |
|
|
/** |
50 |
|
|
* String holding the filename of the logfile. |
51 |
|
|
* @var string |
52 |
|
|
*/ |
53 |
|
|
var $_filename; |
54 |
|
|
|
55 |
|
|
/** |
56 |
|
|
* No idea what this does. |
57 |
|
|
* @var string (maybe) |
58 |
|
|
*/ |
59 |
|
|
var $_ident; |
60 |
|
|
|
61 |
|
|
/** |
62 |
|
|
* Maximum level to log |
63 |
|
|
* @var integer |
64 |
|
|
*/ |
65 |
|
|
var $_maxLevel; |
66 |
|
|
|
67 |
|
|
/** |
68 |
|
|
* Integer holding the file handle. |
69 |
|
|
* @var integer |
70 |
|
|
*/ |
71 |
|
|
var $_fp; |
72 |
|
|
|
73 |
|
|
/** |
74 |
|
|
* Integer (in octal) containing the logfile's permissions mode. |
75 |
|
|
* @var integer |
76 |
|
|
*/ |
77 |
|
|
var $_mode = 0644; |
78 |
|
|
|
79 |
|
|
/** |
80 |
|
|
* Array holding the lines to log |
81 |
|
|
* @var array |
82 |
|
|
*/ |
83 |
|
|
var $_logLines; |
84 |
|
|
|
85 |
|
|
/** |
86 |
|
|
* Boolean which if true will mean |
87 |
|
|
* the lines are *NOT* written out. |
88 |
|
|
*/ |
89 |
|
|
var $_writeOut; |
90 |
|
|
|
91 |
|
|
/** |
92 |
|
|
* Creates a new logfile object. |
93 |
|
|
* |
94 |
|
|
* @param string $name The filename of the logfile. |
95 |
|
|
* @param string $ident The identity string. |
96 |
|
|
* @param array $conf The configuration array. |
97 |
|
|
* @param int $maxLevel Maximum level at which to log. |
98 |
|
|
* @access public |
99 |
|
|
*/ |
100 |
|
|
function Log_File($name, $ident = '', $conf = array(), $maxLevel = PEAR_LOG_DEBUG) |
101 |
|
|
{ |
102 |
|
|
/* If a file mode has been provided, use it. */ |
103 |
|
|
if (!empty($conf['mode'])) { |
104 |
|
|
$this->_mode = $conf['mode']; |
105 |
|
|
} |
106 |
|
|
|
107 |
|
|
if (!file_exists($name)) { |
108 |
|
|
touch($name); |
109 |
|
|
chmod($name, $this->_mode); |
110 |
|
|
} |
111 |
|
|
|
112 |
|
|
$this->_filename = realpath($name); |
113 |
|
|
$this->_ident = $ident; |
114 |
|
|
$this->_maxLevel = $maxLevel; |
115 |
|
|
|
116 |
|
|
$this->_logLines = array(); |
117 |
|
|
$this->_writeOut = true; |
118 |
|
|
|
119 |
|
|
$this->PEAR(); |
120 |
|
|
} |
121 |
|
|
|
122 |
|
|
/** |
123 |
|
|
* Destructor. This will write out any lines to the logfile, UNLESS the dontLog() |
124 |
|
|
* method has been called, in which case it won't. |
125 |
|
|
* |
126 |
|
|
* @access private |
127 |
|
|
*/ |
128 |
|
|
function _Log_File() |
129 |
|
|
{ |
130 |
|
|
$this->_PEAR(); |
131 |
|
|
|
132 |
|
|
if (!empty($this->_logLines) AND $this->_writeOut AND $this->_openLogfile()) { |
133 |
|
|
|
134 |
|
|
foreach ($this->_logLines as $line) { |
135 |
|
|
$this->_writeLine($line['message'], $line['priority'], $line['time']); |
136 |
|
|
} |
137 |
|
|
|
138 |
|
|
$this->_closeLogfile(); |
139 |
|
|
} |
140 |
|
|
} |
141 |
|
|
|
142 |
|
|
/** |
143 |
|
|
* Adds a line to be logged. Adds it to the internal array and will only |
144 |
|
|
* get written out when the destructor is called. |
145 |
|
|
* |
146 |
|
|
* @param string $message The textual message to be logged. |
147 |
|
|
* @param string $priority The priority of the message. Valid |
148 |
|
|
* values are: PEAR_LOG_EMERG, PEAR_LOG_ALERT, PEAR_LOG_CRIT, |
149 |
|
|
* PEAR_LOG_ERR, PEAR_LOG_WARNING, PEAR_LOG_NOTICE, PEAR_LOG_INFO, and |
150 |
|
|
* PEAR_LOG_DEBUG. The default is PEAR_LOG_INFO. |
151 |
|
|
* @return boolean True on success or false on failure. |
152 |
|
|
* @access public |
153 |
|
|
*/ |
154 |
|
|
function log($message, $priority = PEAR_LOG_INFO) |
155 |
|
|
{ |
156 |
|
|
// Abort early if the priority is above the maximum logging level. |
157 |
|
|
if ($priority > $this->_maxLevel) { |
158 |
|
|
return false; |
159 |
|
|
} |
160 |
|
|
|
161 |
|
|
// Add to loglines array |
162 |
|
|
$this->_logLines[] = array('message' => $message, 'priority' => $priority, 'time' => strftime('%b %d %H:%M:%S')); |
163 |
|
|
|
164 |
|
|
// Notify observers |
165 |
|
|
$this->notifyAll(array('message' => $message, 'priority' => $priority)); |
166 |
|
|
|
167 |
|
|
return true; |
168 |
|
|
} |
169 |
|
|
|
170 |
|
|
/** |
171 |
|
|
* This function will prevent the destructor from logging. |
172 |
|
|
* |
173 |
|
|
* @access public |
174 |
|
|
*/ |
175 |
|
|
function dontLog() |
176 |
|
|
{ |
177 |
|
|
$this->_writeOut = false; |
178 |
|
|
} |
179 |
|
|
|
180 |
|
|
/** |
181 |
|
|
* Function to force writing out of log *now*. Will clear the queue. |
182 |
|
|
* Using this function does not cancel the writeout in the destructor. |
183 |
|
|
* Handy for long running processes. |
184 |
|
|
* |
185 |
|
|
* @access public |
186 |
|
|
*/ |
187 |
|
|
function writeOut() |
188 |
|
|
{ |
189 |
|
|
if (!empty($this->_logLines) AND $this->_openLogfile()) { |
190 |
|
|
|
191 |
|
|
foreach ($this->_logLines as $line) { |
192 |
|
|
$this->_writeLine($line['message'], $line['priority'], $line['time']); |
193 |
|
|
} |
194 |
|
|
|
195 |
|
|
$this->_logLines = array(); |
196 |
|
|
$this->_closeLogfile(); |
197 |
|
|
} |
198 |
|
|
} |
199 |
|
|
|
200 |
|
|
/** |
201 |
|
|
* Opens the logfile for appending. File should always exist, as |
202 |
|
|
* constructor will create it if it doesn't. |
203 |
|
|
* |
204 |
|
|
* @access private |
205 |
|
|
*/ |
206 |
|
|
function _openLogfile() |
207 |
|
|
{ |
208 |
|
|
if (($this->_fp = @fopen($this->_filename, 'a')) == false) { |
209 |
|
|
return false; |
210 |
|
|
} |
211 |
|
|
|
212 |
|
|
chmod($this->_filename, $this->_mode); |
213 |
|
|
|
214 |
|
|
return true; |
215 |
|
|
} |
216 |
|
|
|
217 |
|
|
/** |
218 |
|
|
* Closes the logfile file pointer. |
219 |
|
|
* |
220 |
|
|
* @access private |
221 |
|
|
*/ |
222 |
|
|
function _closeLogfile() |
223 |
|
|
{ |
224 |
|
|
return fclose($this->_fp); |
225 |
|
|
} |
226 |
|
|
|
227 |
|
|
/** |
228 |
|
|
* Writes a line to the logfile |
229 |
|
|
* |
230 |
|
|
* @param string $line The line to write |
231 |
|
|
* @param integer $priority The priority of this line/msg |
232 |
|
|
* @return integer Number of bytes written or -1 on error |
233 |
|
|
* @access private |
234 |
|
|
*/ |
235 |
|
|
function _writeLine($line, $priority, $time) |
236 |
|
|
{ |
237 |
|
|
return fwrite($this->_fp, sprintf("%s %s [%s] %s\r\n", $time, $this->_ident, $this->priorityToString($priority), $line)); |
238 |
|
|
} |
239 |
|
|
|
240 |
|
|
} // End of class |
241 |
|
|
?> |