/[cvs]/nfo/php/libs/net.php.pear/Log/file.php
ViewVC logotype

Diff of /nfo/php/libs/net.php.pear/Log/file.php

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1 by cvsjoko, Fri Oct 25 15:15:23 2002 UTC revision 1.3 by joko, Wed Feb 5 22:06:42 2003 UTC
# Line 1  Line 1 
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->_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    ?>

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.3

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed