summaryrefslogtreecommitdiff
path: root/framework/Util/TLogger.php
blob: 6fd12ccfe763fef86409653c1b36b8647cec3540 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
/**
 * TLogger class file
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @link http://www.pradosoft.com/
 * @copyright Copyright &copy; 2005-2014 PradoSoft
 * @license http://www.pradosoft.com/license/
 * @package System.Util
 */

/**
 * TLogger class.
 *
 * TLogger records log messages in memory and implements the methods to
 * retrieve the messages with filter conditions, including log levels,
 * log categories, and by control.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @package System.Util
 * @since 3.0
 */
class TLogger extends TComponent
{
	/**
	 * Log levels.
	 */
	const DEBUG=0x01;
	const INFO=0x02;
	const NOTICE=0x04;
	const WARNING=0x08;
	const ERROR=0x10;
	const ALERT=0x20;
	const FATAL=0x40;
	/**
	 * @var array log messages
	 */
	private $_logs=array();
	/**
	 * @var integer log levels (bits) to be filtered
	 */
	private $_levels;
	/**
	 * @var array list of categories to be filtered
	 */
	private $_categories;
	/**
	 * @var array list of control client ids to be filtered
	 */
	private $_controls;
	/**
	 * @var float timestamp used to filter
	 */
	private $_timestamp;

	/**
	 * Logs a message.
	 * Messages logged by this method may be retrieved via {@link getLogs}.
	 * @param string message to be logged
	 * @param integer level of the message. Valid values include
	 * TLogger::DEBUG, TLogger::INFO, TLogger::NOTICE, TLogger::WARNING,
	 * TLogger::ERROR, TLogger::ALERT, TLogger::FATAL.
	 * @param string category of the message
	 * @param string|TControl control of the message
	 */
	public function log($message,$level,$category='Uncategorized', $ctl=null)
	{
		if($ctl) {
			if($ctl instanceof TControl)
				$ctl = $ctl->ClientId;
			else if(!is_string($ctl))
				$ctl = null;
		} else
			$ctl = null;
		$this->_logs[]=array($message,$level,$category,microtime(true),memory_get_usage(),$ctl);
	}

	/**
	 * Retrieves log messages.
	 * Messages may be filtered by log levels and/or categories and/or control client ids and/or timestamp.
	 * A level filter is specified by an integer, whose bits indicate the levels interested.
	 * For example, (TLogger::INFO | TLogger::WARNING) specifies INFO and WARNING levels.
	 * A category filter is specified by an array of categories to filter.
	 * A message whose category name starts with any filtering category
	 * will be returned. For example, a category filter array('System.Web','System.IO')
	 * will return messages under categories such as 'System.Web', 'System.IO',
	 * 'System.Web.UI', 'System.Web.UI.WebControls', etc.
	 * A control client id filter is specified by an array of control client id
	 * A message whose control client id starts with any filtering naming panels
	 * will be returned. For example, a category filter array('ctl0_body_header',
	 * 'ctl0_body_content_sidebar')
	 * will return messages under categories such as 'ctl0_body_header', 'ctl0_body_content_sidebar',
	 * 'ctl0_body_header_title', 'ctl0_body_content_sidebar_savebutton', etc.
	 * A timestamp filter is specified as an interger or float number.
	 * A message whose registered timestamp is less or equal the filter value will be returned.
	 * Level filter, category filter, control filter and timestamp filter are combinational, i.e., only messages
	 * satisfying all filter conditions will they be returned.
	 * @param integer level filter
	 * @param array category filter
	 * @param array control filter
	 * @return array list of messages. Each array elements represents one message
	 * with the following structure:
	 * array(
	 *   [0] => message
	 *   [1] => level
	 *   [2] => category
	 *   [3] => timestamp (by microtime(), float number));
	 *   [4] => memory in bytes
	 *   [5] => control client id
	 */
	public function getLogs($levels=null,$categories=null,$controls=null,$timestamp=null)
	{
		$this->_levels=$levels;
		$this->_categories=$categories;
		$this->_controls=$controls;
		$this->_timestamp=$timestamp;
		if(empty($levels) && empty($categories) && empty($controls) && is_null($timestamp))
			return $this->_logs;
		$logs = $this->_logs;
		if(!empty($levels))
			$logs = array_values(array_filter( array_filter($logs,array($this,'filterByLevels')) ));
		if(!empty($categories))
			$logs = array_values(array_filter( array_filter($logs,array($this,'filterByCategories')) ));
		if(!empty($controls))
			$logs = array_values(array_filter( array_filter($logs,array($this,'filterByControl')) ));
		if(!is_null($timestamp))
			$logs = array_values(array_filter( array_filter($logs,array($this,'filterByTimeStamp')) ));
		return $logs;
	}

	/**
	 * Deletes log messages from the queue.
	 * Messages may be filtered by log levels and/or categories and/or control client ids and/or timestamp.
	 * A level filter is specified by an integer, whose bits indicate the levels interested.
	 * For example, (TLogger::INFO | TLogger::WARNING) specifies INFO and WARNING levels.
	 * A category filter is specified by an array of categories to filter.
	 * A message whose category name starts with any filtering category
	 * will be deleted. For example, a category filter array('System.Web','System.IO')
	 * will delete messages under categories such as 'System.Web', 'System.IO',
	 * 'System.Web.UI', 'System.Web.UI.WebControls', etc.
	 * A control client id filter is specified by an array of control client id
	 * A message whose control client id starts with any filtering naming panels
	 * will be deleted. For example, a category filter array('ctl0_body_header',
	 * 'ctl0_body_content_sidebar')
	 * will delete messages under categories such as 'ctl0_body_header', 'ctl0_body_content_sidebar',
	 * 'ctl0_body_header_title', 'ctl0_body_content_sidebar_savebutton', etc.
	 * A timestamp filter is specified as an interger or float number.
	 * A message whose registered timestamp is less or equal the filter value will be returned.
	 * Level filter, category filter, control filter and timestamp filter are combinational, i.e., only messages
	 * satisfying all filter conditions will they be returned.
	 * @param integer level filter
	 * @param array category filter
	 * @param array control filter
	 */
	public function deleteLogs($levels=null,$categories=null,$controls=null,$timestamp=null)
	{
		$this->_levels=$levels;
		$this->_categories=$categories;
		$this->_controls=$controls;
		$this->_timestamp=$timestamp;
		if(empty($levels) && empty($categories) && empty($controls) && is_null($timestamp))
		{
			$this->_logs=array();
			return;
		}
		$logs = $this->_logs;
		if(!empty($levels))
			$logs = array_filter( array_filter($logs,array($this,'filterByLevels')) );
		if(!empty($categories))
			$logs = array_filter( array_filter($logs,array($this,'filterByCategories')) );
		if(!empty($controls))
			$logs = array_filter( array_filter($logs,array($this,'filterByControl')) );
		if(!is_null($timestamp))
			$logs = array_filter( array_filter($logs,array($this,'filterByTimeStamp')) );
		$this->_logs = array_values( array_diff_key($this->_logs, $logs) );
	}

	/**
	 * Filter function used by {@link getLogs}.
	 * @param array element to be filtered
	 */
	private function filterByCategories($value)
	{
		foreach($this->_categories as $category)
		{
			// element 2 is the category
			if($value[2]===$category || strpos($value[2],$category.'.')===0)
				return $value;
		}
		return false;
	}

	/**
	 * Filter function used by {@link getLogs}
	 * @param array element to be filtered
	 */
	private function filterByLevels($value)
	{
		// element 1 are the levels
		if($value[1] & $this->_levels)
			return $value;
		else
			return false;
	}

	/**
	 * Filter function used by {@link getLogs}
	 * @param array element to be filtered
	 */
	private function filterByControl($value)
	{
		// element 5 are the control client ids
		foreach($this->_controls as $control)
		{
			if($value[5]===$control || strpos($value[5],$control)===0)
				return $value;
		}
		return false;
	}

	/**
	 * Filter function used by {@link getLogs}
	 * @param array element to be filtered
	 */
	private function filterByTimeStamp($value)
	{
		// element 3 is the timestamp
		if($value[3] <= $this->_timestamp)
			return $value;
		else
			return false;
	}
}