forked from 3thirty/wemo-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDebug.class.php
More file actions
93 lines (80 loc) · 2.08 KB
/
Debug.class.php
File metadata and controls
93 lines (80 loc) · 2.08 KB
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
<?
class Debug {
private $fh; // file handle to write errors to
private $on = true;
private $logFile;
private function openFileHandle (){
if (!is_resource($this->fh))
$this->fh = fopen ($this->logFile, "a");
}
private function closeFileHandle (){
if (!is_resource($this->fh))
$this->fh = fclose ($this->logFile);
}
/**
* Constructor. Open file handle to write data to
*
* @param on If set to true, enable debugging. If false, disable debugging
*/
public function __construct (
$on = true,
$logFile = "/tmp/log"
){
date_default_timezone_set("America/Los_Angeles");
if ($on !== true){
$this->on = false;
return;
}
$this->logFile = $logFile;
$this->log("Debug::__construct()");
}
public function __destruct (){
$this->log("Debug::__destruct()");
if ($this->on)
fclose ($this->fh);
}
/**
* Write a log message
* @param data The data to write out. If a string, just write out,
* otherwise, var_dump it
* @return void
*/
public function log (
$data
){
if ($this->on === false)
return;
if (!is_array($data))
$data = array($data);
$out = "";
foreach ($data as $d){
if (is_string($d)){
$out .= $d;
} else {
ob_start();
var_dump($d);
$out .= ob_get_contents();
ob_end_clean();
}
}
$out = rtrim ($out);
$out = date("Y-m-d H:i:s") . "\t" . $out . "\n";
$this->openFileHandle();
fwrite($this->fh, $out);
$this->closeFileHandle();
}
}
/**
* Standalone function to provide stack traces. Not really used much, but
* useful for diagnosing specific issues
*/
function stacktrace (
$errno = null,
$errstr = null,
$errfile = null,
$errline = null
){
echo "stacktrace:\n";
var_dump(debug_backtrace());
}
?>