1 |
<?php |
<?php |
2 |
// $Horde: horde/lib/Log/file.php,v 1.4 2000/06/28 21:36:13 jon Exp $ |
// +-----------------------------------------------------------------------+ |
3 |
|
// | Copyright (c) 2002 Richard Heyes | |
4 |
/** |
// | All rights reserved. | |
5 |
* The Log_file class is a concrete implementation of the Log:: |
// | | |
6 |
* abstract class which writes message to a text file. |
// | Redistribution and use in source and binary forms, with or without | |
7 |
* |
// | modification, are permitted provided that the following conditions | |
8 |
* @author Jon Parise <jon@csh.rit.edu> |
// | are met: | |
9 |
* @version $Revision$ |
// | | |
10 |
* @since Horde 1.3 |
// | o Redistributions of source code must retain the above copyright | |
11 |
*/ |
// | notice, this list of conditions and the following disclaimer. | |
12 |
class Log_file extends Log { |
// | o Redistributions in binary form must reproduce the above copyright | |
13 |
|
// | notice, this list of conditions and the following disclaimer in the | |
14 |
// {{{ properties |
// | 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 |
/** String holding the filename of the logfile. */ |
// | products derived from this software without specific prior written | |
17 |
var $filename = ''; |
// | permission. | |
18 |
|
// | | |
19 |
/** Integer holding the file handle. */ |
// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
20 |
var $fp = ''; |
// | "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 |
// {{{ constructor |
// | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
26 |
/** |
// | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
27 |
* Constructs a new logfile object. |
// | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
28 |
* |
// | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
29 |
* @param $log_name The filename of the logfile. |
// | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
30 |
* @param $ident (optional) The identity string. |
// | | |
31 |
* @param $conf (optional) The configuration array. |
// +-----------------------------------------------------------------------+ |
32 |
*/ |
// | Author: Richard Heyes <richard@phpguru.org> | |
33 |
function Log_file ($log_name, $ident = '', $conf = false) { |
// | Jon Parise <jon@php.net> | |
34 |
$this->filename = $log_name; |
// +-----------------------------------------------------------------------+ |
35 |
$this->ident = $ident; |
// |
36 |
} |
// $Id$ |
37 |
// }}} |
|
38 |
|
/** |
39 |
|
* The Log_file class is a concrete implementation of the Log:: |
40 |
// {{{ open() |
* abstract class which writes message to a text file. This is based |
41 |
/** |
* on the previous Log_file class by Jon Parise. |
42 |
* Opens the logfile for appending, if it has not already been opened. |
* |
43 |
* If the file doesn't already exist, attempt to create it. This is |
* @author Richard Heyes <richard@php.net> |
44 |
* implicitly called by log(), if necessary. |
* @version $Revision$ |
45 |
*/ |
* @package Log |
46 |
function open () { |
*/ |
47 |
if (!$this->opened) { |
class Log_file extends Log |
48 |
$this->fp = fopen($this->filename, 'a'); |
{ |
49 |
$this->opened = true; |
/** |
50 |
} |
* String holding the filename of the logfile. |
51 |
} |
* @var string |
52 |
// }}} |
*/ |
53 |
|
var $_filename; |
54 |
// {{{ close() |
|
55 |
/** |
/** |
56 |
* Closes the logfile, if it is open. |
* No idea what this does. |
57 |
*/ |
* @var string (maybe) |
58 |
function close () { |
*/ |
59 |
if ($this->opened) { |
var $_ident; |
60 |
fclose($this->fp); |
|
61 |
$this->opened = false; |
/** |
62 |
} |
* Maximum level to log |
63 |
} |
* @var integer |
64 |
// }}} |
*/ |
65 |
|
var $_maxLevel; |
66 |
// {{{ log() |
|
67 |
/** |
/** |
68 |
* Writes $message to the currently open logfile. Calls open(), if |
* Integer holding the file handle. |
69 |
* necessary. Also, passes the message along to any Log_observer |
* @var integer |
70 |
* instances that are observing this Log. |
*/ |
71 |
* |
var $_fp; |
72 |
* @param $message The textual message to be logged. |
|
73 |
* @param $priority (optional) The priority of the message. Valid |
/** |
74 |
* values are: LOG_EMERG, LOG_ALERT, LOG_CRIT, |
* Integer (in octal) containing the logfile's permissions mode. |
75 |
* LOG_ERR, LOG_WARNING, LOG_NOTICE, LOG_INFO, and |
* @var integer |
76 |
* LOG_DEBUG. The default is LOG_INFO. |
*/ |
77 |
*/ |
var $_mode = 0644; |
78 |
function log ($message, $priority = LOG_INFO) { |
|
79 |
if (!$this->opened) |
/** |
80 |
$this->open(); |
* Array holding the lines to log |
81 |
|
* @var array |
82 |
$this->ident && $ident = $this->ident . " "; |
*/ |
83 |
|
var $_logLines; |
84 |
$entry = sprintf("%s %s[%s] %s\n", |
|
85 |
strftime("%Y-%m-%d %H:%M:%S"), |
/** |
86 |
$ident, |
* Boolean which if true will mean |
87 |
Log::priorityToString($priority), |
* the lines are *NOT* written out. |
88 |
$message |
*/ |
89 |
); |
var $_writeOut; |
90 |
|
|
91 |
if ($this->fp) |
/** |
92 |
fwrite($this->fp, $entry); |
* Creates a new logfile object. |
93 |
|
* |
94 |
$this->notifyAll(array('priority' => $priority, 'message' => $message)); |
* @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 |
|
?> |