blob: 4441a9fbed3329b108ea31e88706368fc03c31a7 [file] [log] [blame]
Derek Allard2067d1a2008-11-13 22:59:24 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
Derek Jones7f3719f2010-01-05 13:35:37 +00009 * @copyright Copyright (c) 2008 - 2010, EllisLab, Inc.
Derek Allard2067d1a2008-11-13 22:59:24 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * CodeIgniter Profiler Class
20 *
21 * This class enables you to display benchmark, query, and other data
22 * in order to help with debugging and optimization.
23 *
24 * Note: At some point it would be good to move all the HTML in this class
25 * into a set of template files in order to allow customization.
26 *
27 * @package CodeIgniter
28 * @subpackage Libraries
29 * @category Libraries
30 * @author ExpressionEngine Dev Team
31 * @link http://codeigniter.com/user_guide/general/profiling.html
32 */
33class CI_Profiler {
34
35 var $CI;
Barry Mienydd671972010-10-04 16:33:58 +020036
Greg Aker5ac55942010-11-10 14:59:47 -060037 protected $_available_sections = array(
Derek Jonesee71c802010-03-10 10:05:05 -060038 'benchmarks',
Derek Jonesee71c802010-03-10 10:05:05 -060039 'get',
Derek Jonesee71c802010-03-10 10:05:05 -060040 'memory_usage',
41 'post',
Greg Akere6026832010-04-29 13:41:39 -050042 'uri_string',
43 'controller_info',
Derek Jonesee71c802010-03-10 10:05:05 -060044 'queries',
Greg Akere6026832010-04-29 13:41:39 -050045 'http_headers',
46 'config'
Derek Jonesee71c802010-03-10 10:05:05 -060047 );
48
Greg Aker5ac55942010-11-10 14:59:47 -060049 public function __construct($config = array())
Barry Mienydd671972010-10-04 16:33:58 +020050 {
51 $this->CI =& get_instance();
52 $this->CI->load->language('profiler');
53
Derek Jonesee71c802010-03-10 10:05:05 -060054 // default all sections to display
55 foreach ($this->_available_sections as $section)
56 {
57 if ( ! isset($config[$section]))
58 {
59 $this->_compile_{$section} = TRUE;
60 }
61 }
Barry Mienydd671972010-10-04 16:33:58 +020062
Derek Jonesee71c802010-03-10 10:05:05 -060063 $this->set_sections($config);
Barry Mienydd671972010-10-04 16:33:58 +020064 }
65
Derek Allard2067d1a2008-11-13 22:59:24 +000066 // --------------------------------------------------------------------
67
68 /**
Derek Jonesee71c802010-03-10 10:05:05 -060069 * Set Sections
70 *
71 * Sets the private _compile_* properties to enable/disable Profiler sections
72 *
Derek Jonesee71c802010-03-10 10:05:05 -060073 * @param mixed
74 * @return void
75 */
Greg Aker5ac55942010-11-10 14:59:47 -060076 public function set_sections($config)
Derek Jonesee71c802010-03-10 10:05:05 -060077 {
78 foreach ($config as $method => $enable)
79 {
80 if (in_array($method, $this->_available_sections))
81 {
Barry Mienydd671972010-10-04 16:33:58 +020082 $this->_compile_{$method} = ($enable !== FALSE) ? TRUE : FALSE;
Derek Jonesee71c802010-03-10 10:05:05 -060083 }
84 }
85 }
86
87 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020088
Derek Jonesee71c802010-03-10 10:05:05 -060089 /**
Derek Allard2067d1a2008-11-13 22:59:24 +000090 * Auto Profiler
91 *
92 * This function cycles through the entire array of mark points and
93 * matches any two points that are named identically (ending in "_start"
94 * and "_end" respectively). It then compiles the execution times for
95 * all points and returns it as an array
Greg Aker5ac55942010-11-10 14:59:47 -060096 *
Derek Allard2067d1a2008-11-13 22:59:24 +000097 * @return array
98 */
Greg Aker5ac55942010-11-10 14:59:47 -060099 protected function _compile_benchmarks()
Barry Mienydd671972010-10-04 16:33:58 +0200100 {
101 $profile = array();
102 foreach ($this->CI->benchmark->marker as $key => $val)
103 {
104 // We match the "end" marker so that the list ends
105 // up in the order that it was defined
106 if (preg_match("/(.+?)_end/i", $key, $match))
107 {
108 if (isset($this->CI->benchmark->marker[$match[1].'_end']) AND isset($this->CI->benchmark->marker[$match[1].'_start']))
109 {
110 $profile[$match[1]] = $this->CI->benchmark->elapsed_time($match[1].'_start', $key);
111 }
112 }
113 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000114
115 // Build a table containing the profile data.
116 // Note: At some point we should turn this into a template that can
117 // be modified. We also might want to make this data available to be logged
Barry Mienydd671972010-10-04 16:33:58 +0200118
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 $output = "\n\n";
Derek Allard76af4092010-01-16 19:20:49 +0000120 $output .= '<fieldset style="border:1px solid #900;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 $output .= "\n";
Derek Allard76af4092010-01-16 19:20:49 +0000122 $output .= '<legend style="color:#900;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_benchmarks').'&nbsp;&nbsp;</legend>';
Barry Mienydd671972010-10-04 16:33:58 +0200123 $output .= "\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 $output .= "\n\n<table cellpadding='4' cellspacing='1' border='0' width='100%'>\n";
Barry Mienydd671972010-10-04 16:33:58 +0200125
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 foreach ($profile as $key => $val)
127 {
128 $key = ucwords(str_replace(array('_', '-'), ' ', $key));
Greg Akerc2950702010-09-14 17:52:26 -0500129 $output .= "<tr><td style='width:50%;color:#000;font-weight:bold;background-color:#ddd;'>".$key."&nbsp;&nbsp;</td><td style='width:50%;color:#900;font-weight:normal;background-color:#ddd;'>".$val."</td></tr>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 }
Barry Mienydd671972010-10-04 16:33:58 +0200131
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 $output .= "</table>\n";
133 $output .= "</fieldset>";
Barry Mienydd671972010-10-04 16:33:58 +0200134
135 return $output;
136 }
137
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 // --------------------------------------------------------------------
139
140 /**
141 * Compile Queries
142 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200144 */
Greg Aker5ac55942010-11-10 14:59:47 -0600145 protected function _compile_queries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 {
147 $dbs = array();
Derek Jonesf0a9b332009-07-29 14:19:18 +0000148
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 // Let's determine which databases are currently connected to
150 foreach (get_object_vars($this->CI) as $CI_object)
151 {
Derek Jonesf0a9b332009-07-29 14:19:18 +0000152 if (is_object($CI_object) && is_subclass_of(get_class($CI_object), 'CI_DB') )
Derek Allard2067d1a2008-11-13 22:59:24 +0000153 {
154 $dbs[] = $CI_object;
155 }
156 }
Barry Mienydd671972010-10-04 16:33:58 +0200157
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 if (count($dbs) == 0)
159 {
160 $output = "\n\n";
161 $output .= '<fieldset style="border:1px solid #0000FF;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
162 $output .= "\n";
163 $output .= '<legend style="color:#0000FF;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_queries').'&nbsp;&nbsp;</legend>';
Barry Mienydd671972010-10-04 16:33:58 +0200164 $output .= "\n";
Greg Akerc2950702010-09-14 17:52:26 -0500165 $output .= "\n\n<table cellpadding='4' cellspacing='1' border='0' style='width:100%'>\n";
166 $output .="<tr><td style='width:100%;color:#0000FF;font-weight:normal;background-color:#eee;'>".$this->CI->lang->line('profiler_no_db')."</td></tr>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 $output .= "</table>\n";
168 $output .= "</fieldset>";
Barry Mienydd671972010-10-04 16:33:58 +0200169
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 return $output;
171 }
Barry Mienydd671972010-10-04 16:33:58 +0200172
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 // Load the text helper so we can highlight the SQL
174 $this->CI->load->helper('text');
175
176 // Key words we want bolded
Greg Aker3424bf72010-09-14 17:53:10 -0500177 $highlight = array('SELECT', 'DISTINCT', 'FROM', 'WHERE', 'AND', 'LEFT&nbsp;JOIN', 'ORDER&nbsp;BY', 'GROUP&nbsp;BY', 'LIMIT', 'INSERT', 'INTO', 'VALUES', 'UPDATE', 'OR&nbsp;', 'HAVING', 'OFFSET', 'NOT&nbsp;IN', 'IN', 'LIKE', 'NOT&nbsp;LIKE', 'COUNT', 'MAX', 'MIN', 'ON', 'AS', 'AVG', 'SUM', '(', ')');
Derek Allard2067d1a2008-11-13 22:59:24 +0000178
179 $output = "\n\n";
Barry Mienydd671972010-10-04 16:33:58 +0200180
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 foreach ($dbs as $db)
182 {
183 $output .= '<fieldset style="border:1px solid #0000FF;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
184 $output .= "\n";
185 $output .= '<legend style="color:#0000FF;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_database').':&nbsp; '.$db->database.'&nbsp;&nbsp;&nbsp;'.$this->CI->lang->line('profiler_queries').': '.count($this->CI->db->queries).'&nbsp;&nbsp;&nbsp;</legend>';
Barry Mienydd671972010-10-04 16:33:58 +0200186 $output .= "\n";
Greg Akerc2950702010-09-14 17:52:26 -0500187 $output .= "\n\n<table cellpadding='4' cellspacing='1' border='0' style='width:100%;'>\n";
Barry Mienydd671972010-10-04 16:33:58 +0200188
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 if (count($db->queries) == 0)
190 {
Greg Akerc2950702010-09-14 17:52:26 -0500191 $output .= "<tr><td style='width:100%;color:#0000FF;font-weight:normal;background-color:#eee;'>".$this->CI->lang->line('profiler_no_queries')."</td></tr>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 }
193 else
Barry Mienydd671972010-10-04 16:33:58 +0200194 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 foreach ($db->queries as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200196 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 $time = number_format($db->query_times[$key], 4);
198
199 $val = highlight_code($val, ENT_QUOTES);
Barry Mienydd671972010-10-04 16:33:58 +0200200
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 foreach ($highlight as $bold)
202 {
Barry Mienydd671972010-10-04 16:33:58 +0200203 $val = str_replace($bold, '<strong>'.$bold.'</strong>', $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 }
Barry Mienydd671972010-10-04 16:33:58 +0200205
Greg Akerc2950702010-09-14 17:52:26 -0500206 $output .= "<tr><td valign='top' style='width:1%;color:#900;font-weight:normal;background-color:#ddd;'>".$time."&nbsp;&nbsp;</td><td style='color:#000;font-weight:normal;background-color:#ddd;'>".$val."</td></tr>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 }
208 }
Barry Mienydd671972010-10-04 16:33:58 +0200209
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 $output .= "</table>\n";
211 $output .= "</fieldset>";
Barry Mienydd671972010-10-04 16:33:58 +0200212
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 }
Barry Mienydd671972010-10-04 16:33:58 +0200214
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 return $output;
216 }
217
Barry Mienydd671972010-10-04 16:33:58 +0200218
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 // --------------------------------------------------------------------
220
221 /**
222 * Compile $_GET Data
223 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200225 */
Greg Aker5ac55942010-11-10 14:59:47 -0600226 protected function _compile_get()
Barry Mienydd671972010-10-04 16:33:58 +0200227 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 $output = "\n\n";
229 $output .= '<fieldset style="border:1px solid #cd6e00;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
230 $output .= "\n";
231 $output .= '<legend style="color:#cd6e00;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_get_data').'&nbsp;&nbsp;</legend>';
232 $output .= "\n";
Barry Mienydd671972010-10-04 16:33:58 +0200233
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 if (count($_GET) == 0)
235 {
236 $output .= "<div style='color:#cd6e00;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->lang->line('profiler_no_get')."</div>";
237 }
238 else
239 {
240 $output .= "\n\n<table cellpadding='4' cellspacing='1' border='0' width='100%'>\n";
Barry Mienydd671972010-10-04 16:33:58 +0200241
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 foreach ($_GET as $key => $val)
243 {
244 if ( ! is_numeric($key))
245 {
246 $key = "'".$key."'";
247 }
Barry Mienydd671972010-10-04 16:33:58 +0200248
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 $output .= "<tr><td width='50%' style='color:#000;background-color:#ddd;'>&#36;_GET[".$key."]&nbsp;&nbsp; </td><td width='50%' style='color:#cd6e00;font-weight:normal;background-color:#ddd;'>";
250 if (is_array($val))
251 {
252 $output .= "<pre>" . htmlspecialchars(stripslashes(print_r($val, true))) . "</pre>";
253 }
254 else
255 {
256 $output .= htmlspecialchars(stripslashes($val));
257 }
258 $output .= "</td></tr>\n";
259 }
Barry Mienydd671972010-10-04 16:33:58 +0200260
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 $output .= "</table>\n";
262 }
263 $output .= "</fieldset>";
264
Barry Mienydd671972010-10-04 16:33:58 +0200265 return $output;
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 }
Barry Mienydd671972010-10-04 16:33:58 +0200267
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200269
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 /**
271 * Compile $_POST Data
272 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200274 */
Greg Aker5ac55942010-11-10 14:59:47 -0600275 protected function _compile_post()
Barry Mienydd671972010-10-04 16:33:58 +0200276 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 $output = "\n\n";
278 $output .= '<fieldset style="border:1px solid #009900;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
279 $output .= "\n";
280 $output .= '<legend style="color:#009900;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_post_data').'&nbsp;&nbsp;</legend>';
281 $output .= "\n";
Barry Mienydd671972010-10-04 16:33:58 +0200282
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 if (count($_POST) == 0)
284 {
285 $output .= "<div style='color:#009900;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->lang->line('profiler_no_post')."</div>";
286 }
287 else
288 {
289 $output .= "\n\n<table cellpadding='4' cellspacing='1' border='0' width='100%'>\n";
Barry Mienydd671972010-10-04 16:33:58 +0200290
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 foreach ($_POST as $key => $val)
292 {
293 if ( ! is_numeric($key))
294 {
295 $key = "'".$key."'";
296 }
Barry Mienydd671972010-10-04 16:33:58 +0200297
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 $output .= "<tr><td width='50%' style='color:#000;background-color:#ddd;'>&#36;_POST[".$key."]&nbsp;&nbsp; </td><td width='50%' style='color:#009900;font-weight:normal;background-color:#ddd;'>";
299 if (is_array($val))
300 {
Derek Jones511e3d72010-05-13 09:03:30 -0500301 $output .= "<pre>" . htmlspecialchars(stripslashes(print_r($val, TRUE))) . "</pre>";
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 }
303 else
304 {
305 $output .= htmlspecialchars(stripslashes($val));
306 }
307 $output .= "</td></tr>\n";
308 }
Barry Mienydd671972010-10-04 16:33:58 +0200309
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 $output .= "</table>\n";
311 }
312 $output .= "</fieldset>";
313
Barry Mienydd671972010-10-04 16:33:58 +0200314 return $output;
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 }
Barry Mienydd671972010-10-04 16:33:58 +0200316
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200318
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 /**
320 * Show query string
321 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200323 */
Greg Aker5ac55942010-11-10 14:59:47 -0600324 protected function _compile_uri_string()
Barry Mienydd671972010-10-04 16:33:58 +0200325 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 $output = "\n\n";
327 $output .= '<fieldset style="border:1px solid #000;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
328 $output .= "\n";
329 $output .= '<legend style="color:#000;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_uri_string').'&nbsp;&nbsp;</legend>';
330 $output .= "\n";
Barry Mienydd671972010-10-04 16:33:58 +0200331
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 if ($this->CI->uri->uri_string == '')
333 {
334 $output .= "<div style='color:#000;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->lang->line('profiler_no_uri')."</div>";
335 }
336 else
337 {
Barry Mienydd671972010-10-04 16:33:58 +0200338 $output .= "<div style='color:#000;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->uri->uri_string."</div>";
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 }
Barry Mienydd671972010-10-04 16:33:58 +0200340
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 $output .= "</fieldset>";
342
Barry Mienydd671972010-10-04 16:33:58 +0200343 return $output;
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 }
345
346 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200347
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 /**
349 * Show the controller and function that were called
350 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200352 */
Greg Aker5ac55942010-11-10 14:59:47 -0600353 protected function _compile_controller_info()
Barry Mienydd671972010-10-04 16:33:58 +0200354 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 $output = "\n\n";
356 $output .= '<fieldset style="border:1px solid #995300;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
357 $output .= "\n";
358 $output .= '<legend style="color:#995300;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_controller_info').'&nbsp;&nbsp;</legend>';
359 $output .= "\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000360
Barry Mienydd671972010-10-04 16:33:58 +0200361 $output .= "<div style='color:#995300;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->router->fetch_class()."/".$this->CI->router->fetch_method()."</div>";
362
363
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 $output .= "</fieldset>";
365
Barry Mienydd671972010-10-04 16:33:58 +0200366 return $output;
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 }
Derek Allard76af4092010-01-16 19:20:49 +0000368
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200370
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 /**
372 * Compile memory usage
373 *
374 * Display total used memory
375 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 * @return string
377 */
Greg Aker5ac55942010-11-10 14:59:47 -0600378 protected function _compile_memory_usage()
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 {
380 $output = "\n\n";
381 $output .= '<fieldset style="border:1px solid #5a0099;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
382 $output .= "\n";
383 $output .= '<legend style="color:#5a0099;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_memory_usage').'&nbsp;&nbsp;</legend>';
384 $output .= "\n";
Barry Mienydd671972010-10-04 16:33:58 +0200385
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 if (function_exists('memory_get_usage') && ($usage = memory_get_usage()) != '')
387 {
388 $output .= "<div style='color:#5a0099;font-weight:normal;padding:4px 0 4px 0'>".number_format($usage).' bytes</div>';
389 }
390 else
391 {
Barry Mienydd671972010-10-04 16:33:58 +0200392 $output .= "<div style='color:#5a0099;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->lang->line('profiler_no_memory_usage')."</div>";
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 }
Barry Mienydd671972010-10-04 16:33:58 +0200394
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 $output .= "</fieldset>";
396
397 return $output;
398 }
399
400 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200401
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 /**
Derek Allard76af4092010-01-16 19:20:49 +0000403 * Compile header information
404 *
405 * Lists HTTP headers
406 *
Derek Allard76af4092010-01-16 19:20:49 +0000407 * @return string
408 */
Greg Aker5ac55942010-11-10 14:59:47 -0600409 protected function _compile_http_headers()
Derek Allard76af4092010-01-16 19:20:49 +0000410 {
411 $output = "\n\n";
412 $output .= '<fieldset style="border:1px solid #000;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
413 $output .= "\n";
414 $output .= '<legend style="color:#000;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_headers').'&nbsp;&nbsp;</legend>';
415 $output .= "\n";
416
417 $output .= "\n\n<table cellpadding='4' cellspacing='1' border='0' width='100%'>\n";
418
Derek Jones079303e2010-03-02 18:16:23 -0600419 foreach(array('HTTP_ACCEPT', 'HTTP_USER_AGENT', 'HTTP_CONNECTION', 'SERVER_PORT', 'SERVER_NAME', 'REMOTE_ADDR', 'SERVER_SOFTWARE', 'HTTP_ACCEPT_LANGUAGE', 'SCRIPT_NAME', 'REQUEST_METHOD',' HTTP_HOST', 'REMOTE_HOST', 'CONTENT_TYPE', 'SERVER_PROTOCOL', 'QUERY_STRING', 'HTTP_ACCEPT_ENCODING', 'HTTP_X_FORWARDED_FOR') as $header)
Derek Allard76af4092010-01-16 19:20:49 +0000420 {
421 $val = (isset($_SERVER[$header])) ? $_SERVER[$header] : '';
422 $output .= "<tr><td valign='top' style='color:#900;background-color:#ddd;'>".$header."&nbsp;&nbsp;</td><td style='color:#000;background-color:#ddd;'>".$val."</td></tr>\n";
423 }
424
425 $output .= "</table>\n";
426 $output .= "</fieldset>";
427
Derek Allard76af4092010-01-16 19:20:49 +0000428 return $output;
429 }
430
431 // --------------------------------------------------------------------
432
433 /**
434 * Compile config information
435 *
436 * Lists developer config variables
437 *
Derek Allard76af4092010-01-16 19:20:49 +0000438 * @return string
439 */
Greg Aker5ac55942010-11-10 14:59:47 -0600440 protected function _compile_config()
Derek Allard76af4092010-01-16 19:20:49 +0000441 {
442 $output = "\n\n";
443 $output .= '<fieldset style="border:1px solid #000;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
444 $output .= "\n";
445 $output .= '<legend style="color:#000;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_config').'&nbsp;&nbsp;</legend>';
446 $output .= "\n";
447
448 $output .= "\n\n<table cellpadding='4' cellspacing='1' border='0' width='100%'>\n";
449
450 foreach($this->CI->config->config as $config=>$val)
451 {
Greg Akere6026832010-04-29 13:41:39 -0500452 if (is_array($val))
453 {
454 $val = print_r($val, TRUE);
455 }
Barry Mienydd671972010-10-04 16:33:58 +0200456
Derek Jones511e3d72010-05-13 09:03:30 -0500457 $output .= "<tr><td valign='top' style='color:#900;background-color:#ddd;'>".$config."&nbsp;&nbsp;</td><td style='color:#000;background-color:#ddd;'>".htmlspecialchars($val)."</td></tr>\n";
Derek Allard76af4092010-01-16 19:20:49 +0000458 }
459
460 $output .= "</table>\n";
461 $output .= "</fieldset>";
462
Derek Allard76af4092010-01-16 19:20:49 +0000463 return $output;
464 }
465
466 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200467
Derek Allard76af4092010-01-16 19:20:49 +0000468 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 * Run the Profiler
470 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200472 */
Greg Aker5ac55942010-11-10 14:59:47 -0600473 public function run()
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 {
475 $output = "<div id='codeigniter_profiler' style='clear:both;background-color:#fff;padding:10px;'>";
Derek Jonesee71c802010-03-10 10:05:05 -0600476 $fields_displayed = 0;
Barry Mienydd671972010-10-04 16:33:58 +0200477
Derek Jonesee71c802010-03-10 10:05:05 -0600478 foreach ($this->_available_sections as $section)
479 {
480 if ($this->_compile_{$section} !== FALSE)
481 {
482 $func = "_compile_{$section}";
483 $output .= $this->{$func}();
484 $fields_displayed++;
485 }
486 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000487
Derek Jonesee71c802010-03-10 10:05:05 -0600488 if ($fields_displayed == 0)
489 {
490 $output .= '<p style="border:1px solid #5a0099;padding:10px;margin:20px 0;background-color:#eee">'.$this->CI->lang->line('profiler_no_profiles').'</p>';
491 }
Barry Mienydd671972010-10-04 16:33:58 +0200492
Derek Allard2067d1a2008-11-13 22:59:24 +0000493 $output .= '</div>';
494
495 return $output;
496 }
497
498}
499
500// END CI_Profiler class
501
502/* End of file Profiler.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000503/* Location: ./system/libraries/Profiler.php */