Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * $Header$
4
 *
5
 * @version $Revision: 306594 $
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
 */
19
class Log_console extends Log
20
{
21
    /**
22
     * Handle to the current output stream.
23
     * @var resource
24
     * @access private
25
     */
26
    var $_stream = null;
27
 
28
    /**
29
     * Is this object responsible for closing the stream resource?
30
     * @var bool
31
     * @access private
32
     */
33
    var $_closeResource = false;
34
 
35
    /**
36
     * Should the output be buffered or displayed immediately?
37
     * @var string
38
     * @access private
39
     */
40
    var $_buffering = false;
41
 
42
    /**
43
     * String holding the buffered output.
44
     * @var string
45
     * @access private
46
     */
47
    var $_buffer = '';
48
 
49
    /**
50
     * String containing the format of a log line.
51
     * @var string
52
     * @access private
53
     */
54
    var $_lineFormat = '%1$s %2$s [%3$s] %4$s';
55
 
56
    /**
57
     * String containing the timestamp format.  It will be passed directly to
58
     * strftime().  Note that the timestamp string will generated using the
59
     * current locale.
60
     * @var string
61
     * @access private
62
     */
63
    var $_timeFormat = '%b %d %H:%M:%S';
64
 
65
    /**
66
     * Constructs a new Log_console object.
67
     *
68
     * @param string $name     Ignored.
69
     * @param string $ident    The identity string.
70
     * @param array  $conf     The configuration array.
71
     * @param int    $level    Log messages up to and including this level.
72
     * @access public
73
     */
74
    function Log_console($name, $ident = '', $conf = array(),
75
                         $level = PEAR_LOG_DEBUG)
76
    {
77
        $this->_id = md5(microtime());
78
        $this->_ident = $ident;
79
        $this->_mask = Log::UPTO($level);
80
 
81
        if (!empty($conf['stream'])) {
82
            $this->_stream = $conf['stream'];
83
        } elseif (defined('STDOUT')) {
84
            $this->_stream = STDOUT;
85
        } else {
86
            $this->_stream = fopen('php://output', 'a');
87
            $this->_closeResource = true;
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->close();
119
    }
120
 
121
    /**
122
     * Open the output stream.
123
     *
124
     * @access public
125
     * @since Log 1.9.7
126
     */
127
    function open()
128
    {
129
        $this->_opened = true;
130
        return true;
131
    }
132
 
133
    /**
134
     * Closes the output stream.
135
     *
136
     * This results in a call to flush().
137
     *
138
     * @access public
139
     * @since Log 1.9.0
140
     */
141
    function close()
142
    {
143
        $this->flush();
144
        $this->_opened = false;
145
        if ($this->_closeResource === true && is_resource($this->_stream)) {
146
            fclose($this->_stream);
147
        }
148
        return true;
149
    }
150
 
151
    /**
152
     * Flushes all pending ("buffered") data to the output stream.
153
     *
154
     * @access public
155
     * @since Log 1.8.2
156
     */
157
    function flush()
158
    {
159
        /*
160
         * If output buffering is enabled, dump the contents of the buffer to
161
         * the output stream.
162
         */
163
        if ($this->_buffering && (strlen($this->_buffer) > 0)) {
164
            fwrite($this->_stream, $this->_buffer);
165
            $this->_buffer = '';
166
        }
167
 
168
        if (is_resource($this->_stream)) {
169
            return fflush($this->_stream);
170
        }
171
 
172
        return false;
173
    }
174
 
175
    /**
176
     * Writes $message to the text console. Also, passes the message
177
     * along to any Log_observer instances that are observing this Log.
178
     *
179
     * @param mixed  $message    String or object containing the message to log.
180
     * @param string $priority The priority of the message.  Valid
181
     *                  values are: PEAR_LOG_EMERG, PEAR_LOG_ALERT,
182
     *                  PEAR_LOG_CRIT, PEAR_LOG_ERR, PEAR_LOG_WARNING,
183
     *                  PEAR_LOG_NOTICE, PEAR_LOG_INFO, and PEAR_LOG_DEBUG.
184
     * @return boolean  True on success or false on failure.
185
     * @access public
186
     */
187
    function log($message, $priority = null)
188
    {
189
        /* If a priority hasn't been specified, use the default value. */
190
        if ($priority === null) {
191
            $priority = $this->_priority;
192
        }
193
 
194
        /* Abort early if the priority is above the maximum logging level. */
195
        if (!$this->_isMasked($priority)) {
196
            return false;
197
        }
198
 
199
        /* Extract the string representation of the message. */
200
        $message = $this->_extractMessage($message);
201
 
202
        /* Build the string containing the complete log line. */
203
        $line = $this->_format($this->_lineFormat,
204
                               strftime($this->_timeFormat),
205
                               $priority, $message) . "\n";
206
 
207
        /*
208
         * If buffering is enabled, append this line to the output buffer.
209
         * Otherwise, print the line to the output stream immediately.
210
         */
211
        if ($this->_buffering) {
212
            $this->_buffer .= $line;
213
        } else {
214
            fwrite($this->_stream, $line);
215
        }
216
 
217
        /* Notify observers about this log message. */
218
        $this->_announce(array('priority' => $priority, 'message' => $message));
219
 
220
        return true;
221
    }
222
}