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