PageRenderTime 3ms CodeModel.GetById 1ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/php/Log/console.php

https://bitbucket.org/adarshj/convenient_website
PHP | 190 lines | 71 code | 23 blank | 96 comment | 11 complexity | 68dd4ca5107df00d9901c1eef1112803 MD5 | raw file
  1<?php
  2/**
  3 * $Header: /repository/pear/Log/Log/console.php,v 1.19 2004/01/19 08:02:40 jon Exp $
  4 *
  5 * @version $Revision: 1.19 $
  6 * @package Log
  7 */
  8
  9/**
 10 * The Log_console class is a concrete implementation of the Log::
 11 * abstract class which writes message to the text console.
 12 * 
 13 * @author  Jon Parise <jon@php.net>
 14 * @since   Log 1.1
 15 * @package Log
 16 *
 17 * @example console.php     Using the console handler.
 18 */
 19class Log_console extends Log
 20{
 21    /**
 22     * Handle to the current output stream.
 23     * @var resource
 24     * @access private
 25     */
 26    var $_stream = STDOUT;
 27
 28    /**
 29     * Should the output be buffered or displayed immediately?
 30     * @var string
 31     * @access private
 32     */
 33    var $_buffering = false;
 34
 35    /**
 36     * String holding the buffered output.
 37     * @var string
 38     * @access private
 39     */
 40    var $_buffer = '';
 41
 42    /**
 43     * String containing the format of a log line.
 44     * @var string
 45     * @access private
 46     */
 47    var $_lineFormat = '%1$s %2$s [%3$s] %4$s';
 48
 49    /**
 50     * String containing the timestamp format.  It will be passed directly to
 51     * strftime().  Note that the timestamp string will generated using the
 52     * current locale.
 53     * @var string
 54     * @access private
 55     */
 56    var $_timeFormat = '%b %d %H:%M:%S';
 57
 58    /**
 59     * Hash that maps canonical format keys to position arguments for the
 60     * "line format" string.
 61     * @var array
 62     * @access private
 63     */
 64    var $_formatMap = array('%{timestamp}'  => '%1$s',
 65                            '%{ident}'      => '%2$s',
 66                            '%{priority}'   => '%3$s',
 67                            '%{message}'    => '%4$s',
 68                            '%\{'           => '%%{');
 69
 70    /**
 71     * Constructs a new Log_console object.
 72     * 
 73     * @param string $name     Ignored.
 74     * @param string $ident    The identity string.
 75     * @param array  $conf     The configuration array.
 76     * @param int    $level    Log messages up to and including this level.
 77     * @access public
 78     */
 79    function Log_console($name, $ident = '', $conf = array(),
 80                         $level = PEAR_LOG_DEBUG)
 81    {
 82        $this->_id = md5(microtime());
 83        $this->_ident = $ident;
 84        $this->_mask = Log::UPTO($level);
 85
 86        if (!empty($conf['stream'])) {
 87            $this->_stream = $conf['stream'];
 88        }
 89
 90        if (isset($conf['buffering'])) {
 91            $this->_buffering = $conf['buffering'];
 92        }
 93
 94        if (!empty($conf['lineFormat'])) {
 95            $this->_lineFormat = str_replace(array_keys($this->_formatMap),
 96                                             array_values($this->_formatMap),
 97                                             $conf['lineFormat']);
 98        }
 99
100        if (!empty($conf['timeFormat'])) {
101            $this->_timeFormat = $conf['timeFormat'];
102        }
103
104        /*
105         * If output buffering has been requested, we need to register a
106         * shutdown function that will dump the buffer upon termination.
107         */
108        if ($this->_buffering) {
109            register_shutdown_function(array(&$this, '_Log_console'));
110        }
111    }
112
113    /**
114     * Destructor
115     */
116    function _Log_console()
117    {
118        $this->flush();
119    }
120
121    /**
122     * Flushes all pending ("buffered") data to the output stream.
123     *
124     * @access public
125     * @since Log 1.8.2
126     */
127    function flush()
128    {
129        /*
130         * If output buffering is enabled, dump the contents of the buffer to
131         * the output stream.
132         */
133        if ($this->_buffering && (strlen($this->_buffer) > 0)) {
134            fwrite($this->_stream, $this->_buffer);
135            $this->_buffer = '';
136        }
137 
138        return fflush($this->_stream);
139    }
140
141    /**
142     * Writes $message to the text console. Also, passes the message
143     * along to any Log_observer instances that are observing this Log.
144     * 
145     * @param mixed  $message    String or object containing the message to log.
146     * @param string $priority The priority of the message.  Valid
147     *                  values are: PEAR_LOG_EMERG, PEAR_LOG_ALERT,
148     *                  PEAR_LOG_CRIT, PEAR_LOG_ERR, PEAR_LOG_WARNING,
149     *                  PEAR_LOG_NOTICE, PEAR_LOG_INFO, and PEAR_LOG_DEBUG.
150     * @return boolean  True on success or false on failure.
151     * @access public
152     */
153    function log($message, $priority = null)
154    {
155        /* If a priority hasn't been specified, use the default value. */
156        if ($priority === null) {
157            $priority = $this->_priority;
158        }
159
160        /* Abort early if the priority is above the maximum logging level. */
161        if (!$this->_isMasked($priority)) {
162            return false;
163        }
164
165        /* Extract the string representation of the message. */
166        $message = $this->_extractMessage($message);
167
168        /* Build the string containing the complete log line. */
169        $line = sprintf($this->_lineFormat, strftime($this->_timeFormat),
170                $this->_ident, $this->priorityToString($priority),
171                $message) . "\n";
172
173        /*
174         * If buffering is enabled, append this line to the output buffer.
175         * Otherwise, print the line to the output stream immediately.
176         */
177        if ($this->_buffering) {
178            $this->_buffer .= $line;
179        } else {
180            fwrite($this->_stream, $line);
181        }
182
183        /* Notify observers about this log message. */
184        $this->_announce(array('priority' => $priority, 'message' => $message));
185
186        return true;
187    }
188}
189
190?>