|
1
|
|
|
<?php |
|
2
|
|
|
class Debug { |
|
3
|
|
|
public static $LOG_DISABLED = -1; |
|
4
|
|
|
public static $LOG_NORMAL = 0; |
|
5
|
|
|
public static $LOG_VERBOSE = 1; |
|
6
|
|
|
public static $LOG_EXTENDED = 2; |
|
7
|
|
|
|
|
8
|
|
|
private static $enabled = false; |
|
9
|
|
|
private static $quiet = false; |
|
10
|
|
|
private static $logfile = false; |
|
11
|
|
|
private static $loglevel = 0; |
|
12
|
|
|
|
|
13
|
|
|
public static function set_logfile($logfile) { |
|
14
|
|
|
Debug::$logfile = $logfile; |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
public static function enabled() { |
|
18
|
|
|
return Debug::$enabled; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
public static function set_enabled($enable) { |
|
22
|
|
|
Debug::$enabled = $enable; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public static function set_quiet($quiet) { |
|
26
|
|
|
Debug::$quiet = $quiet; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public static function set_loglevel($level) { |
|
30
|
|
|
Debug::$loglevel = $level; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public static function get_loglevel() { |
|
34
|
|
|
return Debug::$loglevel; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public static function log($message, $level = 0) { |
|
38
|
|
|
|
|
39
|
|
|
if (!Debug::$enabled || Debug::$loglevel < $level) { |
|
40
|
|
|
return false; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
$ts = strftime("%H:%M:%S", time()); |
|
44
|
|
|
if (function_exists('posix_getpid')) { |
|
45
|
|
|
$ts = "$ts/".posix_getpid(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
if (Debug::$logfile) { |
|
49
|
|
|
$fp = fopen(Debug::$logfile, 'a+'); |
|
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
if ($fp) { |
|
|
|
|
|
|
52
|
|
|
$locked = false; |
|
53
|
|
|
|
|
54
|
|
|
if (function_exists("flock")) { |
|
55
|
|
|
$tries = 0; |
|
56
|
|
|
|
|
57
|
|
|
// try to lock logfile for writing |
|
58
|
|
|
while ($tries < 5 && !$locked = flock($fp, LOCK_EX | LOCK_NB)) { |
|
59
|
|
|
sleep(1); |
|
60
|
|
|
++$tries; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
if (!$locked) { |
|
64
|
|
|
fclose($fp); |
|
65
|
|
|
user_error("Unable to lock debugging log file: ".Debug::$logfile, E_USER_WARNING); |
|
66
|
|
|
return; |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
fputs($fp, "[$ts] $message\n"); |
|
71
|
|
|
|
|
72
|
|
|
if (function_exists("flock")) { |
|
73
|
|
|
flock($fp, LOCK_UN); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
fclose($fp); |
|
77
|
|
|
|
|
78
|
|
|
if (Debug::$quiet) { |
|
79
|
|
|
return; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
} else { |
|
83
|
|
|
user_error("Unable to open debugging log file: ".Debug::$logfile, E_USER_WARNING); |
|
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
print "[$ts] $message\n"; |
|
88
|
|
|
} |
|
89
|
|
|
} |