about summary refs log tree commit diff stats
path: root/classes/debug.php
diff options
context:
space:
mode:
authorAndrew Dolgov <noreply@fakecake.org>2021-03-05 21:14:35 +0300
committerAndrew Dolgov <noreply@fakecake.org>2021-03-05 21:14:35 +0300
commit2b8b845abe7c13ecbb266613910484310cffe8e1 (patch)
tree90bd2e93737c2aad17cfb09496cc57cf3f9968cd /classes/debug.php
parentb2341679d53b227fc90fba34c3a7e6453e3cad6e (diff)
downloadthe-epube-2b8b845abe7c13ecbb266613910484310cffe8e1.tar.gz
the-epube-2b8b845abe7c13ecbb266613910484310cffe8e1.tar.xz
* use ORM for trivial queries
 * environment-based configuration
 * useradm.php -> update.php with new options
 * support for schema migrations
 * various fixes
Diffstat (limited to 'classes/debug.php')
-rw-r--r--classes/debug.php91
1 files changed, 91 insertions, 0 deletions
diff --git a/classes/debug.php b/classes/debug.php
new file mode 100644
index 0000000..a0dcac3
--- /dev/null
+++ b/classes/debug.php
@@ -0,0 +1,91 @@
+<?php
+class Debug {
+	const LOG_DISABLED = -1;
+    const LOG_NORMAL = 0;
+    const LOG_VERBOSE = 1;
+    const LOG_EXTENDED = 2;
+
+	public static $LOG_DISABLED = -1;
+    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) {
+        self::$logfile = $logfile;
+    }
+
+    public static function enabled() {
+        return self::$enabled;
+    }
+
+    public static function set_enabled($enable) {
+        self::$enabled = $enable;
+    }
+
+    public static function set_quiet($quiet) {
+        self::$quiet = $quiet;
+    }
+
+    public static function set_loglevel($level) {
+        self::$loglevel = $level;
+    }
+
+    public static function get_loglevel() {
+        return self::$loglevel;
+    }
+
+    public static function log($message, $level = 0) {
+
+        if (!self::$enabled || self::$loglevel < $level) return false;
+
+        $ts = strftime("%H:%M:%S", time());
+        if (function_exists('posix_getpid')) {
+            $ts = "$ts/" . posix_getpid();
+        }
+
+        if (self::$logfile) {
+            $fp = fopen(self::$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: " . self::$logfile, E_USER_WARNING);
+                        return;
+                    }
+                }
+
+                fputs($fp, "[$ts] $message\n");
+
+                if (function_exists("flock")) {
+                    flock($fp, LOCK_UN);
+                }
+
+                fclose($fp);
+
+                if (self::$quiet)
+                    return;
+
+            } else {
+                user_error("Unable to open debugging log file: " . self::$logfile, E_USER_WARNING);
+            }
+        }
+
+        print "[$ts] $message\n";
+    }
+}