-
Notifications
You must be signed in to change notification settings - Fork 12
/
out.php
72 lines (63 loc) · 1.63 KB
/
out.php
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
<?php declare(strict_types=1);
/**
* Copyright (c) 2010-2020, Jos de Ruijter <jos@dutnie.nl>
*/
/**
* Class for handling output messages.
*/
class out
{
private static int $verbosity = 1;
private static string $message_prev = '';
/**
* This is a static class and should not be instantiated.
*/
private function __construct() {}
/**
* Output a given message to the console.
*/
public static function put(string $type, string $message): void
{
$datetime = preg_replace('/(?<=[a-z] )0/', ' ', date('M d H:i:s'));
/**
* Critical messages will always display and are followed by the termination of
* the program.
*/
if ($type === 'critical') {
exit($datetime.' [C] '.$message."\n");
}
/**
* Avoid repeating the same message multiple times in a row, e.g. repeated lines
* and errors related to mode changes.
*/
if ($message === self::$message_prev) {
return;
}
if ($type === 'notice') {
if (self::$verbosity >= 1) {
echo $datetime.' [ ] '.$message."\n";
}
} elseif ($type === 'debug') {
if (self::$verbosity >= 2) {
echo $datetime.' [D] '.$message."\n";
}
}
/**
* Remember the last message displayed.
*/
self::$message_prev = $message;
}
/**
* Set the level of output verbosity. All message types with a lower numeric
* value will also be displayed. Default value is 1 and can be overridden by
* using the command line flag -q (quiet) for 0 or -v (verbose) for 2.
*
* 0 Critical events (these will always display)
* 1 Notices
* 2 Debug messages
*/
public static function set_verbosity(int $verbosity): void
{
self::$verbosity = $verbosity;
}
}