1 | <?php |
---|
2 | /** |
---|
3 | * $Header: /repository/pear/Log/Log/display.php,v 1.11 2008/03/20 16:03:57 jon Exp $ |
---|
4 | * |
---|
5 | * @version $Revision: 1.11 $ |
---|
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 | * Constructs a new Log_display object. |
---|
42 | * |
---|
43 | * @param string $name Ignored. |
---|
44 | * @param string $ident The identity string. |
---|
45 | * @param array $conf The configuration array. |
---|
46 | * @param int $level Log messages up to and including this level. |
---|
47 | * @access public |
---|
48 | */ |
---|
49 | function Log_display($name = '', $ident = '', $conf = array(), |
---|
50 | $level = PEAR_LOG_DEBUG) |
---|
51 | { |
---|
52 | $this->_id = md5(microtime()); |
---|
53 | $this->_ident = $ident; |
---|
54 | $this->_mask = Log::UPTO($level); |
---|
55 | |
---|
56 | /* Start by configuring the line format. */ |
---|
57 | if (!empty($conf['lineFormat'])) { |
---|
58 | $this->_lineFormat = str_replace(array_keys($this->_formatMap), |
---|
59 | array_values($this->_formatMap), |
---|
60 | $conf['lineFormat']); |
---|
61 | } |
---|
62 | |
---|
63 | /* We may need to prepend a string to our line format. */ |
---|
64 | $prepend = null; |
---|
65 | if (isset($conf['error_prepend'])) { |
---|
66 | $prepend = $conf['error_prepend']; |
---|
67 | } else { |
---|
68 | $prepend = ini_get('error_prepend_string'); |
---|
69 | } |
---|
70 | if (!empty($prepend)) { |
---|
71 | $this->_lineFormat = $prepend . $this->_lineFormat; |
---|
72 | } |
---|
73 | |
---|
74 | /* We may also need to append a string to our line format. */ |
---|
75 | $append = null; |
---|
76 | if (isset($conf['error_append'])) { |
---|
77 | $append = $conf['error_append']; |
---|
78 | } else { |
---|
79 | $append = ini_get('error_append_string'); |
---|
80 | } |
---|
81 | if (!empty($append)) { |
---|
82 | $this->_lineFormat .= $append; |
---|
83 | } |
---|
84 | |
---|
85 | /* Lastly, the line ending sequence is also configurable. */ |
---|
86 | if (isset($conf['linebreak'])) { |
---|
87 | $this->_lineFormat .= $conf['linebreak']; |
---|
88 | } else { |
---|
89 | $this->_lineFormat .= "<br />\n"; |
---|
90 | } |
---|
91 | |
---|
92 | /* The user can also change the time format. */ |
---|
93 | if (!empty($conf['timeFormat'])) { |
---|
94 | $this->_timeFormat = $conf['timeFormat']; |
---|
95 | } |
---|
96 | } |
---|
97 | |
---|
98 | /** |
---|
99 | * Opens the display handler. |
---|
100 | * |
---|
101 | * @access public |
---|
102 | * @since Log 1.9.6 |
---|
103 | */ |
---|
104 | function open() |
---|
105 | { |
---|
106 | $this->_opened = true; |
---|
107 | return true; |
---|
108 | } |
---|
109 | |
---|
110 | /** |
---|
111 | * Closes the display handler. |
---|
112 | * |
---|
113 | * @access public |
---|
114 | * @since Log 1.9.6 |
---|
115 | */ |
---|
116 | function close() |
---|
117 | { |
---|
118 | $this->_opened = false; |
---|
119 | return true; |
---|
120 | } |
---|
121 | |
---|
122 | /** |
---|
123 | * Writes $message to the text browser. Also, passes the message |
---|
124 | * along to any Log_observer instances that are observing this Log. |
---|
125 | * |
---|
126 | * @param mixed $message String or object containing the message to log. |
---|
127 | * @param string $priority The priority of the message. Valid |
---|
128 | * values are: PEAR_LOG_EMERG, PEAR_LOG_ALERT, |
---|
129 | * PEAR_LOG_CRIT, PEAR_LOG_ERR, PEAR_LOG_WARNING, |
---|
130 | * PEAR_LOG_NOTICE, PEAR_LOG_INFO, and PEAR_LOG_DEBUG. |
---|
131 | * @return boolean True on success or false on failure. |
---|
132 | * @access public |
---|
133 | */ |
---|
134 | function log($message, $priority = null) |
---|
135 | { |
---|
136 | /* If a priority hasn't been specified, use the default value. */ |
---|
137 | if ($priority === null) { |
---|
138 | $priority = $this->_priority; |
---|
139 | } |
---|
140 | |
---|
141 | /* Abort early if the priority is above the maximum logging level. */ |
---|
142 | if (!$this->_isMasked($priority)) { |
---|
143 | return false; |
---|
144 | } |
---|
145 | |
---|
146 | /* Extract the string representation of the message. */ |
---|
147 | $message = $this->_extractMessage($message); |
---|
148 | |
---|
149 | /* Build and output the complete log line. */ |
---|
150 | echo $this->_format($this->_lineFormat, |
---|
151 | strftime($this->_timeFormat), |
---|
152 | $priority, |
---|
153 | nl2br(htmlspecialchars($message))); |
---|
154 | |
---|
155 | /* Notify observers about this log message. */ |
---|
156 | $this->_announce(array('priority' => $priority, 'message' => $message)); |
---|
157 | |
---|
158 | return true; |
---|
159 | } |
---|
160 | |
---|
161 | } |
---|