summaryrefslogtreecommitdiff
path: root/classes/debug.php
blob: e20126b86a6d564c02f3f4f28b6eaf6006ed729b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
class Debug {
	const LOG_DISABLED = -1;
	const LOG_NORMAL = 0;
	const LOG_VERBOSE = 1;
	const LOG_EXTENDED = 2;

	const ALL_LOG_LEVELS = [
		Debug::LOG_DISABLED,
		Debug::LOG_NORMAL,
		Debug::LOG_VERBOSE,
		Debug::LOG_EXTENDED,
	];

	// 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;

	/**
	 * @deprecated
	 * @var int
	*/
	public static $LOG_VERBOSE = self::LOG_VERBOSE;

	/**
	 * @deprecated
	 * @var int
	*/
	public static $LOG_EXTENDED = self::LOG_EXTENDED;

	/** @var bool */
	private static $enabled = false;

	/** @var bool */
	private static $quiet = false;

	/** @var string|null */
	private static $logfile = null;

	/**
	 * @var int Debug::LOG_*
	 */
    private static $loglevel = self::LOG_NORMAL;

	public static function set_logfile(string $logfile): void {
        self::$logfile = $logfile;
    }

    public static function enabled(): bool {
        return self::$enabled;
    }

    public static function set_enabled(bool $enable): void {
        self::$enabled = $enable;
    }

    public static function set_quiet(bool $quiet): void {
        self::$quiet = $quiet;
    }

	/**
	 * @param int $level Debug::LOG_*
	 */
    public static function set_loglevel(int $level): void {
        self::$loglevel = $level;
    }

	/**
	 * @return int Debug::LOG_*
	 */
    public static function get_loglevel(): int {
        return self::$loglevel;
    }

	/**
	 * @param int $level Debug::LOG_*
	 */
    public static function log(string $message, int $level = Debug::LOG_NORMAL): bool {

        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 false;
                    }
                }

                fputs($fp, "[$ts] $message\n");

                if (function_exists("flock")) {
                    flock($fp, LOCK_UN);
                }

                fclose($fp);

                if (self::$quiet)
                    return false;

            } else {
                user_error("Unable to open debugging log file: " . self::$logfile, E_USER_WARNING);
            }
        }

        print "[$ts] $message\n";

		return true;
    }
}