| 1 |
<?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.2 2003/02/05 21:48:31 joko 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.2 $ |
| 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->_timestampFormat = '%b %d %H:%M:%S'; |
| 120 |
|
| 121 |
/* If a timestamp format has been provided, use it. */ |
| 122 |
if (!empty($conf['timestampFormat'])) { |
| 123 |
$this->_timestampFormat = $conf['timestampFormat']; |
| 124 |
} |
| 125 |
|
| 126 |
$this->PEAR(); |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Destructor. This will write out any lines to the logfile, UNLESS the dontLog() |
| 131 |
* method has been called, in which case it won't. |
| 132 |
* |
| 133 |
* @access private |
| 134 |
*/ |
| 135 |
function _Log_File() |
| 136 |
{ |
| 137 |
$this->_PEAR(); |
| 138 |
|
| 139 |
if (!empty($this->_logLines) AND $this->_writeOut AND $this->_openLogfile()) { |
| 140 |
|
| 141 |
foreach ($this->_logLines as $line) { |
| 142 |
$this->_writeLine($line['message'], $line['priority'], $line['time']); |
| 143 |
} |
| 144 |
|
| 145 |
$this->_closeLogfile(); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Adds a line to be logged. Adds it to the internal array and will only |
| 151 |
* get written out when the destructor is called. |
| 152 |
* |
| 153 |
* @param string $message The textual message to be logged. |
| 154 |
* @param string $priority The priority of the message. Valid |
| 155 |
* values are: PEAR_LOG_EMERG, PEAR_LOG_ALERT, PEAR_LOG_CRIT, |
| 156 |
* PEAR_LOG_ERR, PEAR_LOG_WARNING, PEAR_LOG_NOTICE, PEAR_LOG_INFO, and |
| 157 |
* PEAR_LOG_DEBUG. The default is PEAR_LOG_INFO. |
| 158 |
* @return boolean True on success or false on failure. |
| 159 |
* @access public |
| 160 |
*/ |
| 161 |
function log($message, $priority = PEAR_LOG_INFO) |
| 162 |
{ |
| 163 |
// Abort early if the priority is above the maximum logging level. |
| 164 |
if ($priority > $this->_maxLevel) { |
| 165 |
return false; |
| 166 |
} |
| 167 |
|
| 168 |
// Add to loglines array |
| 169 |
$this->_logLines[] = array('message' => $message, 'priority' => $priority, 'time' => strftime($this->_timestampFormat)); |
| 170 |
|
| 171 |
// Notify observers |
| 172 |
$this->notifyAll(array('message' => $message, 'priority' => $priority)); |
| 173 |
|
| 174 |
return true; |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* This function will prevent the destructor from logging. |
| 179 |
* |
| 180 |
* @access public |
| 181 |
*/ |
| 182 |
function dontLog() |
| 183 |
{ |
| 184 |
$this->_writeOut = false; |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Function to force writing out of log *now*. Will clear the queue. |
| 189 |
* Using this function does not cancel the writeout in the destructor. |
| 190 |
* Handy for long running processes. |
| 191 |
* |
| 192 |
* @access public |
| 193 |
*/ |
| 194 |
function writeOut() |
| 195 |
{ |
| 196 |
if (!empty($this->_logLines) AND $this->_openLogfile()) { |
| 197 |
|
| 198 |
foreach ($this->_logLines as $line) { |
| 199 |
$this->_writeLine($line['message'], $line['priority'], $line['time']); |
| 200 |
} |
| 201 |
|
| 202 |
$this->_logLines = array(); |
| 203 |
$this->_closeLogfile(); |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* Opens the logfile for appending. File should always exist, as |
| 209 |
* constructor will create it if it doesn't. |
| 210 |
* |
| 211 |
* @access private |
| 212 |
*/ |
| 213 |
function _openLogfile() |
| 214 |
{ |
| 215 |
if (($this->_fp = @fopen($this->_filename, 'a')) == false) { |
| 216 |
return false; |
| 217 |
} |
| 218 |
|
| 219 |
chmod($this->_filename, $this->_mode); |
| 220 |
|
| 221 |
return true; |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* Closes the logfile file pointer. |
| 226 |
* |
| 227 |
* @access private |
| 228 |
*/ |
| 229 |
function _closeLogfile() |
| 230 |
{ |
| 231 |
return fclose($this->_fp); |
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* Writes a line to the logfile |
| 236 |
* |
| 237 |
* @param string $line The line to write |
| 238 |
* @param integer $priority The priority of this line/msg |
| 239 |
* @return integer Number of bytes written or -1 on error |
| 240 |
* @access private |
| 241 |
*/ |
| 242 |
function _writeLine($line, $priority, $time) |
| 243 |
{ |
| 244 |
return fwrite($this->_fp, sprintf("%s %s [%s] %s\r\n", $time, $this->_ident, $this->priorityToString($priority), $line)); |
| 245 |
} |
| 246 |
|
| 247 |
} // End of class |
| 248 |
?> |