Debug::set_enabled()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 2
rs 10
1
<?php
2
class Debug {
3
    public static $LOG_DISABLED = -1;
4
    public static $LOG_NORMAL = 0;
5
    public static $LOG_VERBOSE = 1;
6
    public static $LOG_EXTENDED = 2;
7
8
    private static $enabled = false;
9
    private static $quiet = false;
10
    private static $logfile = false;
11
    private static $loglevel = 0;
12
13
    public static function set_logfile($logfile) {
14
        Debug::$logfile = $logfile;
15
    }
16
17
    public static function enabled() {
18
        return Debug::$enabled;
19
    }
20
21
    public static function set_enabled($enable) {
22
        Debug::$enabled = $enable;
23
    }
24
25
    public static function set_quiet($quiet) {
26
        Debug::$quiet = $quiet;
27
    }
28
29
    public static function set_loglevel($level) {
30
        Debug::$loglevel = $level;
31
    }
32
33
    public static function get_loglevel() {
34
        return Debug::$loglevel;
35
    }
36
37
    public static function log($message, $level = 0) {
38
39
        if (!Debug::$enabled || Debug::$loglevel < $level) {
40
            return false;
41
        }
42
43
        $ts = strftime("%H:%M:%S", time());
44
        if (function_exists('posix_getpid')) {
45
            $ts = "$ts/".posix_getpid();
46
        }
47
48
        if (Debug::$logfile) {
49
            $fp = fopen(Debug::$logfile, 'a+');
0 ignored issues
show
Bug introduced by
Debug::logfile of type true is incompatible with the type string expected by parameter $filename of fopen(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

49
            $fp = fopen(/** @scrutinizer ignore-type */ Debug::$logfile, 'a+');
Loading history...
50
51
            if ($fp) {
0 ignored issues
show
introduced by
$fp is of type false|resource, thus it always evaluated to false.
Loading history...
52
                $locked = false;
53
54
                if (function_exists("flock")) {
55
                    $tries = 0;
56
57
                    // try to lock logfile for writing
58
                    while ($tries < 5 && !$locked = flock($fp, LOCK_EX | LOCK_NB)) {
59
                        sleep(1);
60
                        ++$tries;
61
                    }
62
63
                    if (!$locked) {
64
                        fclose($fp);
65
                        user_error("Unable to lock debugging log file: ".Debug::$logfile, E_USER_WARNING);
66
                        return;
67
                    }
68
                }
69
70
                fputs($fp, "[$ts] $message\n");
71
72
                if (function_exists("flock")) {
73
                    flock($fp, LOCK_UN);
74
                }
75
76
                fclose($fp);
77
78
                if (Debug::$quiet) {
79
                                    return;
80
                }
81
82
            } else {
83
                user_error("Unable to open debugging log file: ".Debug::$logfile, E_USER_WARNING);
0 ignored issues
show
Bug introduced by
Are you sure Debug::logfile of type true can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

83
                user_error("Unable to open debugging log file: "./** @scrutinizer ignore-type */ Debug::$logfile, E_USER_WARNING);
Loading history...
84
            }
85
        }
86
87
        print "[$ts] $message\n";
88
    }
89
}