Skip to content

Commit b56c195

Browse files
committed
[NEW] Simple error handling and verification of directories, issue #38
[CHANGED] Combined kind and humanKind variables, issue #39
1 parent a565d32 commit b56c195

11 files changed

+125
-107
lines changed

config.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class Webgrind_Config{
4545
# BELOW NOT FOR EDITING #
4646
#########################
4747

48-
static $webgrindVersion = '1.0';
48+
static $webgrindVersion = '1.0.1';
4949

5050
/**
5151
* Regex that matches the trace files generated by xdebug
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

index.php

+106-98
Original file line numberDiff line numberDiff line change
@@ -8,91 +8,86 @@
88
require 'library/FileHandler.php';
99

1010
// TODO: Errorhandling:
11-
// No files, outputdir not writabel
11+
// No files, outputdir not writable
1212

1313
set_time_limit(0);
1414

1515
// Make sure we have a timezone for date functions.
1616
if (ini_get('date.timezone') == '')
1717
date_default_timezone_set( Webgrind_Config::$defaultTimezone );
1818

19+
try {
20+
switch(get('op')){
21+
case 'file_list':
22+
echo json_encode(Webgrind_FileHandler::getInstance()->getTraceList());
23+
break;
24+
case 'function_list':
25+
$dataFile = get('dataFile');
26+
if($dataFile=='0'){
27+
$files = Webgrind_FileHandler::getInstance()->getTraceList();
28+
$dataFile = $files[0]['filename'];
29+
}
30+
$reader = Webgrind_FileHandler::getInstance()->getTraceReader($dataFile, get('costFormat', Webgrind_Config::$defaultCostformat));
31+
$functions = array();
32+
$shownTotal = 0;
33+
$breakdown = array('internal' => 0, 'procedural' => 0, 'class' => 0, 'include' => 0);
1934

20-
switch(get('op')){
21-
case 'file_list':
22-
echo json_encode(Webgrind_FileHandler::getInstance()->getTraceList());
23-
break;
24-
case 'function_list':
25-
$dataFile = get('dataFile');
26-
if($dataFile=='0'){
27-
$files = Webgrind_FileHandler::getInstance()->getTraceList();
28-
$dataFile = $files[0]['filename'];
29-
}
30-
$reader = Webgrind_FileHandler::getInstance()->getTraceReader($dataFile, get('costFormat', Webgrind_Config::$defaultCostformat));
31-
$functions = array();
32-
$shownTotal = 0;
33-
$breakdown = array('internal' => 0, 'user' => 0, 'class' => 0, 'include' => 0);
34-
35-
for($i=0;$i<$reader->getFunctionCount();$i++) {
36-
$functionInfo = $reader->getFunctionInfo($i);
35+
for($i=0;$i<$reader->getFunctionCount();$i++) {
36+
$functionInfo = $reader->getFunctionInfo($i);
3737

3838

39-
if (false !== strpos($functionInfo['functionName'], 'php::')) {
40-
$breakdown['internal'] += $functionInfo['summedSelfCost'];
41-
$humanKind = 'internal';
42-
$kind = 'blue';
43-
} elseif (false !== strpos($functionInfo['functionName'], 'require_once::') ||
44-
false !== strpos($functionInfo['functionName'], 'require::') ||
45-
false !== strpos($functionInfo['functionName'], 'include_once::') ||
46-
false !== strpos($functionInfo['functionName'], 'include::')) {
47-
$breakdown['include'] += $functionInfo['summedSelfCost'];
48-
$humanKind = 'include';
49-
$kind = 'grey';
50-
} else {
51-
if (false !== strpos($functionInfo['functionName'], '->') || false !== strpos($functionInfo['functionName'], '::')) {
52-
$breakdown['class'] += $functionInfo['summedSelfCost'];
53-
$humanKind = 'class';
54-
$kind = 'green';
55-
} else {
56-
$breakdown['user'] += $functionInfo['summedSelfCost'];
57-
$humanKind = 'procedural';
58-
$kind = 'orange';
59-
}
60-
}
61-
if (!(int)get('hideInternals', 0) || strpos($functionInfo['functionName'], 'php::') === false) {
62-
$shownTotal += $functionInfo['summedSelfCost'];
63-
$functions[$i] = $functionInfo;
64-
$functions[$i]['nr'] = $i;
65-
$functions[$i]['kind'] = $kind;
66-
$functions[$i]['humanKind'] = $humanKind;
67-
}
39+
if (false !== strpos($functionInfo['functionName'], 'php::')) {
40+
$breakdown['internal'] += $functionInfo['summedSelfCost'];
41+
$humanKind = 'internal';
42+
} elseif (false !== strpos($functionInfo['functionName'], 'require_once::') ||
43+
false !== strpos($functionInfo['functionName'], 'require::') ||
44+
false !== strpos($functionInfo['functionName'], 'include_once::') ||
45+
false !== strpos($functionInfo['functionName'], 'include::')) {
46+
$breakdown['include'] += $functionInfo['summedSelfCost'];
47+
$humanKind = 'include';
48+
} else {
49+
if (false !== strpos($functionInfo['functionName'], '->') || false !== strpos($functionInfo['functionName'], '::')) {
50+
$breakdown['class'] += $functionInfo['summedSelfCost'];
51+
$humanKind = 'class';
52+
} else {
53+
$breakdown['procedural'] += $functionInfo['summedSelfCost'];
54+
$humanKind = 'procedural';
55+
}
56+
}
57+
if (!(int)get('hideInternals', 0) || strpos($functionInfo['functionName'], 'php::') === false) {
58+
$shownTotal += $functionInfo['summedSelfCost'];
59+
$functions[$i] = $functionInfo;
60+
$functions[$i]['nr'] = $i;
61+
$functions[$i]['humanKind'] = $humanKind;
62+
}
6863

69-
}
70-
usort($functions,'costCmp');
64+
}
65+
usort($functions,'costCmp');
7166

72-
$remainingCost = $shownTotal*get('showFraction');
67+
$remainingCost = $shownTotal*get('showFraction');
7368

74-
$result['functions'] = array();
75-
foreach($functions as $function){
69+
$result['functions'] = array();
70+
foreach($functions as $function){
7671

77-
$remainingCost -= $function['summedSelfCost'];
72+
$remainingCost -= $function['summedSelfCost'];
7873
$function['file'] = urlencode($function['file']);
79-
$result['functions'][] = $function;
80-
if($remainingCost<0)
81-
break;
82-
}
83-
$result['summedInvocationCount'] = $reader->getFunctionCount();
84-
$result['summedRunTime'] = $reader->formatCost($reader->getHeader('summary'), 'msec');
85-
$result['dataFile'] = $dataFile;
86-
$result['invokeUrl'] = $reader->getHeader('cmd');
87-
$result['runs'] = $reader->getHeader('runs');
88-
$result['breakdown'] = $breakdown;
89-
$result['mtime'] = date(Webgrind_Config::$dateFormat,filemtime(Webgrind_Config::xdebugOutputDir().$dataFile));
90-
echo json_encode($result);
91-
break;
92-
case 'callinfo_list':
93-
$reader = Webgrind_FileHandler::getInstance()->getTraceReader(get('file'), get('costFormat', Webgrind_Config::$defaultCostformat));
94-
$functionNr = get('functionNr');
95-
$function = $reader->getFunctionInfo($functionNr);
74+
$result['functions'][] = $function;
75+
if($remainingCost<0)
76+
break;
77+
}
78+
$result['summedInvocationCount'] = $reader->getFunctionCount();
79+
$result['summedRunTime'] = $reader->formatCost($reader->getHeader('summary'), 'msec');
80+
$result['dataFile'] = $dataFile;
81+
$result['invokeUrl'] = $reader->getHeader('cmd');
82+
$result['runs'] = $reader->getHeader('runs');
83+
$result['breakdown'] = $breakdown;
84+
$result['mtime'] = date(Webgrind_Config::$dateFormat,filemtime(Webgrind_Config::xdebugOutputDir().$dataFile));
85+
echo json_encode($result);
86+
break;
87+
case 'callinfo_list':
88+
$reader = Webgrind_FileHandler::getInstance()->getTraceReader(get('file'), get('costFormat', Webgrind_Config::$defaultCostformat));
89+
$functionNr = get('functionNr');
90+
$function = $reader->getFunctionInfo($functionNr);
9691

9792
$result = array('calledFrom'=>array(), 'subCalls'=>array());
9893
$foundInvocations = 0;
@@ -106,44 +101,57 @@
106101
}
107102
$result['calledByHost'] = ($foundInvocations<$function['invocationCount']);
108103

109-
for($i=0;$i<$function['subCallInfoCount'];$i++){
104+
for($i=0;$i<$function['subCallInfoCount'];$i++){
110105
$invo = $reader->getSubCallInfo($functionNr, $i);
111106
$callInfo = $reader->getFunctionInfo($invo['functionNr']);
112107
$invo['file'] = urlencode($function['file']); // Sub call to $callInfo['file'] but from $function['file']
113108
$invo['callerFunctionName'] = $callInfo['functionName'];
114109
$result['subCalls'][] = $invo;
115110
}
116-
echo json_encode($result);
117-
118-
break;
119-
case 'fileviewer':
120-
$file = get('file');
121-
$line = get('line');
111+
echo json_encode($result);
112+
break;
113+
case 'fileviewer':
114+
$file = get('file');
115+
$line = get('line');
122116

123-
if($file && $file!=''){
124-
$message = '';
125-
if(!file_exists($file)){
126-
$message = $file.' does not exist.';
127-
} else if(!is_readable($file)){
128-
$message = $file.' is not readable.';
129-
} else if(is_dir($file)){
130-
$message = $file.' is a directory.';
131-
}
132-
} else {
133-
$message = 'No file to view';
134-
}
135-
require 'templates/fileviewer.phtml';
117+
if($file && $file!=''){
118+
$message = '';
119+
if(!file_exists($file)){
120+
$message = $file.' does not exist.';
121+
} else if(!is_readable($file)){
122+
$message = $file.' is not readable.';
123+
} else if(is_dir($file)){
124+
$message = $file.' is a directory.';
125+
}
126+
} else {
127+
$message = 'No file to view';
128+
}
129+
require 'templates/fileviewer.phtml';
136130

137-
break;
138-
case 'version_info':
139-
$response = @file_get_contents('http://jokke.dk/webgrindupdate.json?version='.Webgrind_Config::$webgrindVersion);
140-
echo $response;
141-
break;
142-
default:
143-
require 'templates/index.phtml';
131+
break;
132+
case 'version_info':
133+
$response = @file_get_contents('http://jokke.dk/webgrindupdate.json?version='.Webgrind_Config::$webgrindVersion);
134+
echo $response;
135+
break;
136+
default:
137+
$welcome = '';
138+
if (!file_exists(Webgrind_Config::storageDir()) || !is_writable(Webgrind_Config::storageDir())) {
139+
$welcome .= 'Webgrind $storageDir does not exist or is not writeable: "'.Webgrind_Config::storageDir().'"<br>';
140+
}
141+
if (!file_exists(Webgrind_Config::xdebugOutputDir()) || !is_readable(Webgrind_Config::xdebugOutputDir())) {
142+
$welcome .= 'Webgrind $profilerDir does not exist or is not readable: "'.Webgrind_Config::xdebugOutputDir().'"<br>';
143+
}
144+
145+
if ($welcome == '') {
146+
$welcome = 'Select a cachegrind file above';
147+
}
148+
require 'templates/index.phtml';
149+
}
150+
} catch (Exception $e) {
151+
echo json_encode(array('error' => $e->getMessage().'<br>'.$e->getFile().', line '.$e->getLine()));
152+
return;
144153
}
145154

146-
147155
function get($param, $default=false){
148156
return (isset($_GET[$param])? $_GET[$param] : $default);
149157
}

templates/index.phtml

+18-8
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030
$.getJSON("index.php",
3131
vars,
3232
function(data){
33+
if (data.error) {
34+
$("#hello_message").html(data.error);
35+
$("#hello_message").show();
36+
return;
37+
}
3338
callInfoLoaded = new Array();
3439
$("#function_table tbody").empty();
3540
for(i=0;i<data.functions.length;i++){
@@ -46,13 +51,13 @@
4651
$("#runtime_sum").html(data.summedRunTime);
4752
$("#runs").html(data.runs);
4853

49-
var breakdown_sum = data.breakdown['internal']+data.breakdown['user']+data.breakdown['class']+data.breakdown['include'];
54+
var breakdown_sum = data.breakdown['internal']+data.breakdown['procedural']+data.breakdown['class']+data.breakdown['include'];
5055
$("#breakdown").html(
5156
'<img src="img/gradient_left.png" height="20" width="10">'+
52-
'<img src="img/gradient_blue.png" height="20" width="'+Math.floor(data.breakdown['internal']/breakdown_sum*300)+'">'+
53-
'<img src="img/gradient_grey.png" height="20" width="'+Math.floor(data.breakdown['include']/breakdown_sum*300)+'">'+
54-
'<img src="img/gradient_green.png" height="20" width="'+Math.floor(data.breakdown['class']/breakdown_sum*300)+'">'+
55-
'<img src="img/gradient_orange.png" height="20" width="'+Math.floor(data.breakdown['user']/breakdown_sum*300)+'">'+
57+
'<img src="img/gradient_internal.png" height="20" width="'+Math.floor(data.breakdown['internal']/breakdown_sum*300)+'">'+
58+
'<img src="img/gradient_include.png" height="20" width="'+Math.floor(data.breakdown['include']/breakdown_sum*300)+'">'+
59+
'<img src="img/gradient_class.png" height="20" width="'+Math.floor(data.breakdown['class']/breakdown_sum*300)+'">'+
60+
'<img src="img/gradient_procedural.png" height="20" width="'+Math.floor(data.breakdown['procedural']/breakdown_sum*300)+'">'+
5661
'<img src="img/gradient_right.png" height="20" width="10">'+
5762
'<div title="internal functions, include/require, class methods and procedural functions." style="background:url(img/gradient_markers.png);position:relative;top:-20px;left:10px;width:301px;height:19px"></div>'
5863
);
@@ -85,7 +90,12 @@
8590
$.getJSON("index.php",
8691
{'op':'callinfo_list', 'file':currentDataFile, 'functionNr':functionNr, 'costFormat':$("#costFormat").val()},
8792
function(data){
88-
93+
if (data.error) {
94+
$("#hello_message").html(data.error);
95+
$("#hello_message").show();
96+
return;
97+
}
98+
8999
if(data.calledByHost)
90100
$("#callinfo_area_"+functionNr).append('<b>Called from script host</b>');
91101

@@ -169,7 +179,7 @@
169179
openLink = (data.file=='php:internal')?'':'<a title="Open file" href="'+sprintf(fileUrlFormat,data.file,-1)+'" target="_blank"><img src="img/file.png" alt="O"></a>';
170180
return '<tr> \
171181
<td> \
172-
<img src="img/call_'+data.kind+'.png" title="'+data.humanKind+'"> \
182+
<img src="img/call_'+data.humanKind+'.png" title="'+data.humanKind+'"> \
173183
</td> \
174184
<td> \
175185
<a href="javascript:toggleCallInfo('+data.nr+')"> \
@@ -317,7 +327,7 @@
317327
</tbody>
318328
</table>
319329
</div>
320-
<h2 id="hello_message">Select a cachegrind file above</h2>
330+
<h2 id="hello_message"><?=$welcome?></h2>
321331
<div id="footer">
322332
<?php if(Webgrind_Config::$checkVersion):?>
323333
<div id="version_info">&nbsp;</div>

0 commit comments

Comments
 (0)