Skip to content
This repository
Browse code

[NEW] Simple error handling and verification of directories, issue #38

[CHANGED] Combined kind and humanKind variables, issue #39
  • Loading branch information...
commit b56c195ad580ed61ebdfa69acb8e4df1e92c928a 1 parent a565d32
Joakim Nygård authored
2  config.php
@@ -45,7 +45,7 @@ class Webgrind_Config{
45 45 # BELOW NOT FOR EDITING #
46 46 #########################
47 47
48   - static $webgrindVersion = '1.0';
  48 + static $webgrindVersion = '1.0.1';
49 49
50 50 /**
51 51 * Regex that matches the trace files generated by xdebug
0  img/call_green.png → img/call_class.png
File renamed without changes
0  img/call_grey.png → img/call_include.png
File renamed without changes
0  img/call_blue.png → img/call_internal.png
File renamed without changes
0  img/call_orange.png → img/call_procedural.png
File renamed without changes
0  img/gradient_green.png → img/gradient_class.png
File renamed without changes
0  img/gradient_grey.png → img/gradient_include.png
File renamed without changes
0  img/gradient_blue.png → img/gradient_internal.png
File renamed without changes
0  img/gradient_orange.png → img/gradient_procedural.png
File renamed without changes
204 index.php
@@ -8,7 +8,7 @@
8 8 require 'library/FileHandler.php';
9 9
10 10 // TODO: Errorhandling:
11   -// No files, outputdir not writabel
  11 +// No files, outputdir not writable
12 12
13 13 set_time_limit(0);
14 14
@@ -16,83 +16,78 @@
16 16 if (ini_get('date.timezone') == '')
17 17 date_default_timezone_set( Webgrind_Config::$defaultTimezone );
18 18
  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);
19 34
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);
37 37
38 38
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 + }
68 63
69   - }
70   - usort($functions,'costCmp');
  64 + }
  65 + usort($functions,'costCmp');
71 66
72   - $remainingCost = $shownTotal*get('showFraction');
  67 + $remainingCost = $shownTotal*get('showFraction');
73 68
74   - $result['functions'] = array();
75   - foreach($functions as $function){
  69 + $result['functions'] = array();
  70 + foreach($functions as $function){
76 71
77   - $remainingCost -= $function['summedSelfCost'];
  72 + $remainingCost -= $function['summedSelfCost'];
78 73 $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);
96 91
97 92 $result = array('calledFrom'=>array(), 'subCalls'=>array());
98 93 $foundInvocations = 0;
@@ -106,44 +101,57 @@
106 101 }
107 102 $result['calledByHost'] = ($foundInvocations<$function['invocationCount']);
108 103
109   - for($i=0;$i<$function['subCallInfoCount'];$i++){
  104 + for($i=0;$i<$function['subCallInfoCount'];$i++){
110 105 $invo = $reader->getSubCallInfo($functionNr, $i);
111 106 $callInfo = $reader->getFunctionInfo($invo['functionNr']);
112 107 $invo['file'] = urlencode($function['file']); // Sub call to $callInfo['file'] but from $function['file']
113 108 $invo['callerFunctionName'] = $callInfo['functionName'];
114 109 $result['subCalls'][] = $invo;
115 110 }
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');
122 116
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';
136 130
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;
144 153 }
145 154
146   -
147 155 function get($param, $default=false){
148 156 return (isset($_GET[$param])? $_GET[$param] : $default);
149 157 }
26 templates/index.phtml
@@ -30,6 +30,11 @@
30 30 $.getJSON("index.php",
31 31 vars,
32 32 function(data){
  33 + if (data.error) {
  34 + $("#hello_message").html(data.error);
  35 + $("#hello_message").show();
  36 + return;
  37 + }
33 38 callInfoLoaded = new Array();
34 39 $("#function_table tbody").empty();
35 40 for(i=0;i<data.functions.length;i++){
@@ -46,13 +51,13 @@
46 51 $("#runtime_sum").html(data.summedRunTime);
47 52 $("#runs").html(data.runs);
48 53
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'];
50 55 $("#breakdown").html(
51 56 '<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)+'">'+
56 61 '<img src="img/gradient_right.png" height="20" width="10">'+
57 62 '<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>'
58 63 );
@@ -85,7 +90,12 @@
85 90 $.getJSON("index.php",
86 91 {'op':'callinfo_list', 'file':currentDataFile, 'functionNr':functionNr, 'costFormat':$("#costFormat").val()},
87 92 function(data){
88   -
  93 + if (data.error) {
  94 + $("#hello_message").html(data.error);
  95 + $("#hello_message").show();
  96 + return;
  97 + }
  98 +
89 99 if(data.calledByHost)
90 100 $("#callinfo_area_"+functionNr).append('<b>Called from script host</b>');
91 101
@@ -169,7 +179,7 @@
169 179 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>';
170 180 return '<tr> \
171 181 <td> \
172   - <img src="img/call_'+data.kind+'.png" title="'+data.humanKind+'"> \
  182 + <img src="img/call_'+data.humanKind+'.png" title="'+data.humanKind+'"> \
173 183 </td> \
174 184 <td> \
175 185 <a href="javascript:toggleCallInfo('+data.nr+')"> \
@@ -317,7 +327,7 @@
317 327 </tbody>
318 328 </table>
319 329 </div>
320   - <h2 id="hello_message">Select a cachegrind file above</h2>
  330 + <h2 id="hello_message"><?=$welcome?></h2>
321 331 <div id="footer">
322 332 <?php if(Webgrind_Config::$checkVersion):?>
323 333 <div id="version_info">&nbsp;</div>

0 comments on commit b56c195

Please sign in to comment.
Something went wrong with that request. Please try again.