diff options
author | Andrew Dolgov <[email protected]> | 2018-11-30 08:34:29 +0300 |
---|---|---|
committer | Andrew Dolgov <[email protected]> | 2018-11-30 08:34:29 +0300 |
commit | c10a43069ec1e71b6608574a81fb29c76919e132 (patch) | |
tree | 69b94003a2512b14e488b0553cf944e2bbdba2a8 /classes/debug.php | |
parent | 758752684c68efd179071cd77c92f78879e68f6d (diff) |
debug logging system rework:
* support various logging levels per-message
* remove hacks like debug_suppress, DAEMON_EXTENDED_DEBUG, etc
* _debug() is kept as a compatibility shim for plugins
Diffstat (limited to 'classes/debug.php')
-rw-r--r-- | classes/debug.php | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/classes/debug.php b/classes/debug.php new file mode 100644 index 000000000..21b823c52 --- /dev/null +++ b/classes/debug.php @@ -0,0 +1,85 @@ +<?php +class Debug { + public static $LOG_NORMAL = 0; + public static $LOG_VERBOSE = 1; + public static $LOG_EXTENDED = 2; + + private static $enabled = false; + private static $quiet = false; + private static $logfile = false; + private static $loglevel = 0; + + public static function set_logfile($logfile) { + Debug::$logfile = $logfile; + } + + public static function enabled() { + return Debug::$enabled; + } + + public static function set_enabled($enable) { + Debug::$enabled = $enable; + } + + public static function set_quiet($quiet) { + Debug::$quiet = $quiet; + } + + public static function set_loglevel($level) { + Debug::$loglevel = $level; + } + + public static function get_loglevel() { + return Debug::$loglevel; + } + + public static function log($message, $level = 0) { + + if (!Debug::$enabled || Debug::$loglevel < $level) return false; + + $ts = strftime("%H:%M:%S", time()); + if (function_exists('posix_getpid')) { + $ts = "$ts/" . posix_getpid(); + } + + if (Debug::$logfile) { + $fp = fopen(Debug::$logfile, 'a+'); + + if ($fp) { + $locked = false; + + if (function_exists("flock")) { + $tries = 0; + + // try to lock logfile for writing + while ($tries < 5 && !$locked = flock($fp, LOCK_EX | LOCK_NB)) { + sleep(1); + ++$tries; + } + + if (!$locked) { + fclose($fp); + user_error("Unable to lock debugging log file: " . Debug::$logfile, E_USER_WARNING); + return; + } + } + + fputs($fp, "[$ts] $message\n"); + + if (function_exists("flock")) { + flock($fp, LOCK_UN); + } + + fclose($fp); + + if (Debug::$quiet) + return; + + } else { + user_error("Unable to open debugging log file: " . Debug::$logfile, E_USER_WARNING); + } + } + + print "[$ts] $message\n"; + } +}
\ No newline at end of file |