2018-11-30 05:34:29 +00:00
|
|
|
<?php
|
|
|
|
class Debug {
|
2021-03-04 05:30:52 +00:00
|
|
|
const LOG_DISABLED = -1;
|
2021-11-12 21:17:31 +00:00
|
|
|
const LOG_NORMAL = 0;
|
|
|
|
const LOG_VERBOSE = 1;
|
|
|
|
const LOG_EXTENDED = 2;
|
2021-03-04 05:30:52 +00:00
|
|
|
|
2021-11-12 04:48:06 +00:00
|
|
|
const ALL_LOG_LEVELS = [
|
|
|
|
Debug::LOG_DISABLED,
|
|
|
|
Debug::LOG_NORMAL,
|
|
|
|
Debug::LOG_VERBOSE,
|
|
|
|
Debug::LOG_EXTENDED,
|
|
|
|
];
|
|
|
|
|
2021-11-12 21:17:31 +00:00
|
|
|
// TODO: class properties can be switched to PHP typing if/when the minimum PHP_VERSION is raised to 7.4.0+
|
|
|
|
/**
|
|
|
|
* @deprecated
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
public static $LOG_DISABLED = self::LOG_DISABLED;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @deprecated
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
public static $LOG_NORMAL = self::LOG_NORMAL;
|
2021-03-06 12:19:31 +00:00
|
|
|
|
2021-11-12 21:17:31 +00:00
|
|
|
/**
|
|
|
|
* @deprecated
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
public static $LOG_VERBOSE = self::LOG_VERBOSE;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @deprecated
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
public static $LOG_EXTENDED = self::LOG_EXTENDED;
|
2021-03-06 12:19:31 +00:00
|
|
|
|
2021-11-12 21:17:31 +00:00
|
|
|
/** @var bool */
|
|
|
|
private static $enabled = false;
|
2021-03-06 12:19:31 +00:00
|
|
|
|
2021-11-12 21:17:31 +00:00
|
|
|
/** @var bool */
|
|
|
|
private static $quiet = false;
|
2018-11-30 05:34:29 +00:00
|
|
|
|
2021-11-12 21:17:31 +00:00
|
|
|
/** @var string|null */
|
|
|
|
private static $logfile = null;
|
2018-11-30 05:34:29 +00:00
|
|
|
|
2021-11-11 21:02:06 +00:00
|
|
|
/**
|
2021-11-12 05:24:02 +00:00
|
|
|
* @var int Debug::LOG_*
|
2021-11-11 21:02:06 +00:00
|
|
|
*/
|
2021-11-12 21:17:31 +00:00
|
|
|
private static $loglevel = self::LOG_NORMAL;
|
2021-11-11 21:02:06 +00:00
|
|
|
|
|
|
|
public static function set_logfile(string $logfile): void {
|
2020-09-22 11:54:15 +00:00
|
|
|
self::$logfile = $logfile;
|
2018-11-30 05:34:29 +00:00
|
|
|
}
|
|
|
|
|
2021-11-11 21:02:06 +00:00
|
|
|
public static function enabled(): bool {
|
2020-09-22 11:54:15 +00:00
|
|
|
return self::$enabled;
|
2018-11-30 05:34:29 +00:00
|
|
|
}
|
|
|
|
|
2021-11-11 21:02:06 +00:00
|
|
|
public static function set_enabled(bool $enable): void {
|
2020-09-22 11:54:15 +00:00
|
|
|
self::$enabled = $enable;
|
2018-11-30 05:34:29 +00:00
|
|
|
}
|
|
|
|
|
2021-11-11 21:02:06 +00:00
|
|
|
public static function set_quiet(bool $quiet): void {
|
2020-09-22 11:54:15 +00:00
|
|
|
self::$quiet = $quiet;
|
2018-11-30 05:34:29 +00:00
|
|
|
}
|
|
|
|
|
2021-11-11 21:02:06 +00:00
|
|
|
/**
|
2021-11-12 04:48:06 +00:00
|
|
|
* @param int $level Debug::LOG_*
|
2021-11-11 21:02:06 +00:00
|
|
|
*/
|
2021-11-12 04:48:06 +00:00
|
|
|
public static function set_loglevel(int $level): void {
|
2020-09-22 11:54:15 +00:00
|
|
|
self::$loglevel = $level;
|
2018-11-30 05:34:29 +00:00
|
|
|
}
|
|
|
|
|
2021-11-11 21:02:06 +00:00
|
|
|
/**
|
2021-11-12 04:48:06 +00:00
|
|
|
* @return int Debug::LOG_*
|
2021-11-11 21:02:06 +00:00
|
|
|
*/
|
|
|
|
public static function get_loglevel(): int {
|
2020-09-22 11:54:15 +00:00
|
|
|
return self::$loglevel;
|
2018-11-30 05:34:29 +00:00
|
|
|
}
|
|
|
|
|
2021-11-11 21:02:06 +00:00
|
|
|
/**
|
2021-11-12 04:48:06 +00:00
|
|
|
* @param int $level Debug::LOG_*
|
2021-11-11 21:02:06 +00:00
|
|
|
*/
|
|
|
|
public static function log(string $message, int $level = Debug::LOG_NORMAL): bool {
|
2018-11-30 05:34:29 +00:00
|
|
|
|
2020-09-22 11:54:15 +00:00
|
|
|
if (!self::$enabled || self::$loglevel < $level) return false;
|
2018-11-30 05:34:29 +00:00
|
|
|
|
|
|
|
$ts = strftime("%H:%M:%S", time());
|
|
|
|
if (function_exists('posix_getpid')) {
|
|
|
|
$ts = "$ts/" . posix_getpid();
|
|
|
|
}
|
|
|
|
|
2020-09-22 11:54:15 +00:00
|
|
|
if (self::$logfile) {
|
|
|
|
$fp = fopen(self::$logfile, 'a+');
|
2018-11-30 05:34:29 +00:00
|
|
|
|
|
|
|
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);
|
2020-09-22 11:54:15 +00:00
|
|
|
user_error("Unable to lock debugging log file: " . self::$logfile, E_USER_WARNING);
|
2021-11-11 21:02:06 +00:00
|
|
|
return false;
|
2018-11-30 05:34:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fputs($fp, "[$ts] $message\n");
|
|
|
|
|
|
|
|
if (function_exists("flock")) {
|
|
|
|
flock($fp, LOCK_UN);
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose($fp);
|
|
|
|
|
2020-09-22 11:54:15 +00:00
|
|
|
if (self::$quiet)
|
2021-11-11 21:02:06 +00:00
|
|
|
return false;
|
2018-11-30 05:34:29 +00:00
|
|
|
|
|
|
|
} else {
|
2020-09-22 11:54:15 +00:00
|
|
|
user_error("Unable to open debugging log file: " . self::$logfile, E_USER_WARNING);
|
2018-11-30 05:34:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
print "[$ts] $message\n";
|
2021-11-11 21:02:06 +00:00
|
|
|
|
|
|
|
return true;
|
2018-11-30 05:34:29 +00:00
|
|
|
}
|
2020-09-22 11:54:15 +00:00
|
|
|
}
|