-
Notifications
You must be signed in to change notification settings - Fork 23
/
log.php
131 lines (120 loc) · 3.33 KB
/
log.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
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
<?php
/**
* Functions relating to logging for vip-go-ci.
*
* @package Automattic/vip-go-ci
*/
declare(strict_types=1);
/**
* Log information to the console.
* Will include timestamp, and any debug-data
* our caller might pass us.
*
* @param string $str Log message.
* @param array $debug_data Debug data accompanying the log message.
* @param int $debug_level Debug level of the message.
* @param bool $irc If to log to IRC.
*
* @return void
*
* @codeCoverageIgnore
*/
function vipgoci_log(
string $str,
array $debug_data = array(),
int $debug_level = 0,
bool $irc = false
) :void {
global $vipgoci_debug_level;
/*
* Determine if to log the message; if
* debug-level of the message is not high
* enough compared to the debug-level specified
* to be the threshold, do not print it, but
* otherwise, do print it,
*/
if ( $debug_level > $vipgoci_debug_level ) {
return;
}
echo '[ ' . gmdate( 'c' ) . ' GMT -- ' . (int) $debug_level . ' ] ' .
json_encode(
$str,
JSON_PRETTY_PRINT
) .
'; ' .
print_r(
json_encode(
$debug_data,
JSON_PRETTY_PRINT
),
true
) .
PHP_EOL;
/*
* Send to IRC API as well if asked
* to do so. Include debugging information as well.
*/
if ( true === $irc ) {
vipgoci_irc_api_alert_queue(
$str .
'; ' .
print_r(
json_encode(
$debug_data
),
true
)
);
}
}
/**
* Exit program, using vipgoci_log() to print a
* message before doing so.
*
* @param string $str Log message to print before exiting.
* @param array $debug_data Debug data to print along with log message.
* @param int $exit_status Exit status of program to use.
* @param bool $irc Whether to send log message to IRC or not.
*
* @return void|int Does not return when running normally, will return
* an integer value when running in unit-test mode.
*/
function vipgoci_sysexit(
string $str,
array $debug_data = array(),
int $exit_status = VIPGOCI_EXIT_USAGE_ERROR,
bool $irc = false
) {
if ( VIPGOCI_EXIT_USAGE_ERROR === $exit_status ) {
$str = 'Usage: ' . $str;
}
vipgoci_log(
$str,
$debug_data,
0,
$irc
);
/*
* If running certain unit-tests, return
* with exit status.
*/
if (
( function_exists( 'vipgoci_unittests_check_indication_for_test_id' ) ) &&
(
( vipgoci_unittests_check_indication_for_test_id( 'LogSysExitTest' ) ) ||
( vipgoci_unittests_check_indication_for_test_id( 'MiscSetMaximumExecTimeTest' ) ) ||
( vipgoci_unittests_check_indication_for_test_id( 'MainRunScanSkipExecutionTest' ) ) ||
( vipgoci_unittests_check_indication_for_test_id( 'MainRunScanMaxExecTimeTest' ) ) ||
( vipgoci_unittests_check_indication_for_test_id( 'MainRunInitGithubTokenOptionTest' ) ) ||
( vipgoci_unittests_check_indication_for_test_id( 'MainRunInitOptionsPhpcsTest' ) ) ||
( vipgoci_unittests_check_indication_for_test_id( 'OtherWebServicesIrcApiFilterIgnorableStringsTest' ) ) ||
( vipgoci_unittests_check_indication_for_test_id( 'GitRepoRepoOkTest' ) ) ||
( vipgoci_unittests_check_indication_for_test_id( 'WpscanReportCommentFormatResultTest' ) ) ||
( vipgoci_unittests_check_indication_for_test_id( 'WpscanReportEndTest' ) ) ||
( vipgoci_unittests_check_indication_for_test_id( 'WpscanReportStartTest' ) )
)
) {
return $exit_status;
}
exit( $exit_status );
}