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: 305290 $
6
 * @package Log
7
 */
8
 
9
/**
10
 * The Log_display class is a concrete implementation of the Log::
11
 * abstract class which writes message into browser in usual PHP maner.
12
 * This may be useful because when you use PEAR::setErrorHandling in
13
 * PEAR_ERROR_CALLBACK mode error messages are not displayed by
14
 * PHP error handler.
15
 *
16
 * @author  Paul Yanchenko <pusher@inaco.ru>
17
 * @since   Log 1.8.0
18
 * @package Log
19
 *
20
 * @example display.php     Using the display handler.
21
 */
22
class Log_display extends Log
23
{
24
    /**
25
     * String containing the format of a log line.
26
     * @var string
27
     * @access private
28
     */
29
    var $_lineFormat = '<b>%3$s</b>: %4$s';
30
 
31
    /**
32
     * String containing the timestamp format.  It will be passed directly to
33
     * strftime().  Note that the timestamp string will generated using the
34
     * current locale.
35
     * @var string
36
     * @access private
37
     */
38
    var $_timeFormat = '%b %d %H:%M:%S';
39
 
40
    /**
41
     * Flag indicating whether raw message text should be passed directly to
42
     * the log system.  Otherwise, the text will be converted to an HTML-safe
43
     * representation.
44
     * @var boolean
45
     * @access private
46
     */
47
    var $_rawText = false;
48
 
49
    /**
50
     * Constructs a new Log_display object.
51
     *
52
     * @param string $name     Ignored.
53
     * @param string $ident    The identity string.
54
     * @param array  $conf     The configuration array.
55
     * @param int    $level    Log messages up to and including this level.
56
     * @access public
57
     */
58
    function Log_display($name = '', $ident = '', $conf = array(),
59
                         $level = PEAR_LOG_DEBUG)
60
    {
61
        $this->_id = md5(microtime());
62
        $this->_ident = $ident;
63
        $this->_mask = Log::UPTO($level);
64
 
65
        /* Start by configuring the line format. */
66
        if (!empty($conf['lineFormat'])) {
67
            $this->_lineFormat = str_replace(array_keys($this->_formatMap),
68
                                             array_values($this->_formatMap),
69
                                             $conf['lineFormat']);
70
        }
71
 
72
        /* We may need to prepend a string to our line format. */
73
        $prepend = null;
74
        if (isset($conf['error_prepend'])) {
75
            $prepend = $conf['error_prepend'];
76
        } else {
77
            $prepend = ini_get('error_prepend_string');
78
        }
79
        if (!empty($prepend)) {
80
            $this->_lineFormat = $prepend . $this->_lineFormat;
81
        }
82
 
83
        /* We may also need to append a string to our line format. */
84
        $append = null;
85
        if (isset($conf['error_append'])) {
86
            $append = $conf['error_append'];
87
        } else {
88
            $append = ini_get('error_append_string');
89
        }
90
        if (!empty($append)) {
91
            $this->_lineFormat .= $append;
92
        }
93
 
94
        /* Lastly, the line ending sequence is also configurable. */
95
        if (isset($conf['linebreak'])) {
96
            $this->_lineFormat .= $conf['linebreak'];
97
        } else {
98
            $this->_lineFormat .= "<br />\n";
99
        }
100
 
101
        /* The user can also change the time format. */
102
        if (!empty($conf['timeFormat'])) {
103
            $this->_timeFormat = $conf['timeFormat'];
104
        }
105
 
106
        /* Message text conversion can be disabled. */
107
        if (isset($conf['rawText'])) {
108
            $this->_rawText = $conf['rawText'];
109
        }
110
    }
111
 
112
    /**
113
     * Opens the display handler.
114
     *
115
     * @access  public
116
     * @since   Log 1.9.6
117
     */
118
    function open()
119
    {
120
        $this->_opened = true;
121
        return true;
122
    }
123
 
124
    /**
125
     * Closes the display handler.
126
     *
127
     * @access  public
128
     * @since   Log 1.9.6
129
     */
130
    function close()
131
    {
132
        $this->_opened = false;
133
        return true;
134
    }
135
 
136
    /**
137
     * Writes $message to the text browser. Also, passes the message
138
     * along to any Log_observer instances that are observing this Log.
139
     *
140
     * @param mixed  $message    String or object containing the message to log.
141
     * @param string $priority The priority of the message.  Valid
142
     *                  values are: PEAR_LOG_EMERG, PEAR_LOG_ALERT,
143
     *                  PEAR_LOG_CRIT, PEAR_LOG_ERR, PEAR_LOG_WARNING,
144
     *                  PEAR_LOG_NOTICE, PEAR_LOG_INFO, and PEAR_LOG_DEBUG.
145
     * @return boolean  True on success or false on failure.
146
     * @access public
147
     */
148
    function log($message, $priority = null)
149
    {
150
        /* If a priority hasn't been specified, use the default value. */
151
        if ($priority === null) {
152
            $priority = $this->_priority;
153
        }
154
 
155
        /* Abort early if the priority is above the maximum logging level. */
156
        if (!$this->_isMasked($priority)) {
157
            return false;
158
        }
159
 
160
        /* Extract the string representation of the message. */
161
        $message = $this->_extractMessage($message);
162
 
163
        /* Convert the message to an HTML-friendly represention unless raw
164
         * text has been requested. */
165
        if ($this->_rawText === false) {
166
            $message = nl2br(htmlspecialchars($message));
167
        }
168
 
169
        /* Build and output the complete log line. */
170
        echo $this->_format($this->_lineFormat,
171
                            strftime($this->_timeFormat),
172
                            $priority,
173
                            $message);
174
 
175
        /* Notify observers about this log message. */
176
        $this->_announce(array('priority' => $priority, 'message' => $message));
177
 
178
        return true;
179
    }
180
 
181
}