1 <?php
2
3 4 5 6
7
8 9 10 11
12 function Debug($this){
13 MagratheaDebugger::Instance()->Add($this);
14 }
15
16 17 18 19
20 function Info($this){
21 MagratheaDebugger::Instance()->Info($this);
22 }
23
24 25 26 27
28 class MagratheaDebugger {
29
30 const NONE = 0;
31 const LOG = 2;
32 const DEBUG = 3;
33 const DEV = 4;
34
35 private $logFile = null;
36 private $debugItems = array();
37 private $debugType = self::LOG;
38 private $queries = false;
39 private $oldDebugType = self::NONE;
40 protected static $inst = null;
41
42
43 private function __construct(){ }
44
45 46 47
48 public static function Instance(){
49 if (self::$inst === null) {
50 self::$inst = new MagratheaDebugger();
51 }
52 return self::$inst;
53 }
54 public function MagratheaDebugger(){ }
55
56 57 58 59 60 61 62 63 64 65 66 67
68 public function SetType($dType){
69 $this->debugType = strtolower($dType);
70 return $this;
71 }
72
73 74 75 76 77
78 public function LogQueries($q){
79 $this->queries = $q;
80 return $this;
81 }
82
83 84 85 86 87 88
89 public function SetLogFile($file){
90 $this->logFile = $file;
91 return $this;
92 }
93
94 95 96
97 public function Trace(){
98 echo "<pre>"; debug_backtrace(); echo "</pre>";
99 }
100
101 102 103 104 105 106 107
108 public function SetTemp($dType){
109 $this->oldDebugType = $this->debugType;
110 $this->debugType = $dType;
111 return $this;
112 }
113
114 115 116 117 118
119 public function BackTemp(){
120 $this->debugType = $this->oldDebugType;
121 return $this;
122 }
123
124 125 126 127
128 public function GetType(){
129 return $this->debugType;
130 }
131
132 133 134 135
136 public function Add($debug){
137 switch ($this->debugType) {
138 case self::NONE:
139 return;
140 break;
141 case self::LOG:
142 MagratheaLogger::Log($debug, $this->logFile);
143 return;
144 break;
145 case self::DEV:
146 echo "<pre>".$debug."</pre>";
147 case self::DEBUG:
148 array_push($this->debugItems, $debug);
149 break;
150 }
151 }
152
153 154 155 156
157 public function Info($debug){
158 switch ($this->debugType) {
159 case self::NONE:
160 case self::LOG:
161 MagratheaLogger::Log($debug, $this->logFile);
162 return;
163 break;
164 case self::DEV:
165 echo "<pre>INFO: ".$debug."</pre>";
166 case self::DEBUG:
167 array_push($this->debugItems, "<pre>".$debug."</pre>");
168 break;
169 }
170 }
171
172 173 174 175
176 public function AddError($err){
177 $this->Add($err->getMessage());
178 }
179
180 181 182 183 184 185
186 public function AddQuery($sql, $values){
187 if($this->debugType == @NONE || !$this->queries) return;
188 $logThis = "query run: [".$sql."]";
189 if(!is_null($values)){
190 $logThis .= " - values: [".implode(',', $values)."]";
191 }
192 $this->Add($logThis);
193 }
194
195 196 197 198
199 public function Error($err){
200 $this->Add($err->getMessage());
201 }
202
203 204 205
206 public function Show(){
207 if(empty($this->debugItems)) return;
208 echo "<div style='width: 95%; border: 2px solid red; background-color: yellow; padding: 10px; margin: 10px;'>";
209 echo "MAGRATHEA DEBUG<br/><br/>";
210 foreach ($this->debugItems as $item) {
211 echo $this->printsDebug($item);
212 }
213 echo "</div>";
214 }
215
216 217 218 219
220 private function printsDebug($deb){
221 $html = "";
222 if(is_a($deb, "MagratheaException")){
223 $html .= "<div style='padding: 10px; border: 1px dotted black; margin-bottom: 5px; line-height: 120%;'>";
224 $html .= "<span style='color: red; font-weight: bold;'>".get_class($deb)."</span><br/>";
225 $html .= "<span>".$deb->getMessage()."</span><br/>";
226 $html .= "<span style='padding-left: 20px;'><b>at: </b> ==> ";
227 $html .= "<b>File: </b>".$deb->getFile()." / <b>line: </b>[".$deb->getLine()."]</span><br/>";
228 $html .= "<div style='padding-left: 20px;'><b>trace: </b> ==> <br/>";
229 $html .= "<div style='padding-left: 10px;'>".$this->printTrace($deb->getTrace())."</div></div>";
230 $html .= "</div>";
231 } else if (is_array($deb)) {
232 $html .= "<div style='padding: 10px; border: 1px dotted black; margin-bottom: 5px; line-height: 120%;'>";
233 $html .= "<span>".nice_p_r($deb)."</span><br/>";
234 $html .= "</div>";
235 } else {
236 $html .= "<div style='padding: 10px; border: 1px dotted black; margin-bottom: 5px; line-height: 120%;'>";
237 $html .= "<span>".$deb."</span><br/>";
238 $html .= "</div>";
239 }
240 return $html;
241 }
242
243 244 245 246 247
248 private function printTrace($trace){
249 $html = "";
250 foreach ($trace as $key => $val) {
251 $html .= "[".$key."]<br/>";
252 $html .= " <b>[file]</b> = ".@$val["file"]." <b>[line]</b> = ".@$val["line"]."<br/>";
253 $html .= " <b>[class]</b> = ".@$val["class"]." <b>[function]</b> = ".@$val["function"]."<br/>";
254 }
255 return $html;
256 }
257
258
259 }
260
261
262
263
264 ?>