line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Csistck::Oper; |
2
|
|
|
|
|
|
|
|
3
|
17
|
|
|
17
|
|
292
|
use 5.010; |
|
17
|
|
|
|
|
56
|
|
|
17
|
|
|
|
|
1496
|
|
4
|
17
|
|
|
17
|
|
95
|
use strict; |
|
17
|
|
|
|
|
41
|
|
|
17
|
|
|
|
|
621
|
|
5
|
17
|
|
|
17
|
|
78
|
use warnings; |
|
17
|
|
|
|
|
29
|
|
|
17
|
|
|
|
|
1055
|
|
6
|
|
|
|
|
|
|
|
7
|
17
|
|
|
17
|
|
94
|
use base 'Exporter'; |
|
17
|
|
|
|
|
33
|
|
|
17
|
|
|
|
|
2204
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our @EXPORT_OK = qw/ |
10
|
|
|
|
|
|
|
debug error info |
11
|
|
|
|
|
|
|
repair |
12
|
|
|
|
|
|
|
/; |
13
|
|
|
|
|
|
|
|
14
|
17
|
|
|
17
|
|
98
|
use Carp; |
|
17
|
|
|
|
|
33
|
|
|
17
|
|
|
|
|
2754
|
|
15
|
17
|
|
|
17
|
|
26096
|
use Getopt::Long; |
|
17
|
|
|
|
|
346649
|
|
|
17
|
|
|
|
|
120
|
|
16
|
17
|
|
|
17
|
|
31113
|
use Term::ANSIColor; |
|
17
|
|
|
|
|
1859997
|
|
|
17
|
|
|
|
|
7159
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
# Logging levels, default boolean |
19
|
|
|
|
|
|
|
our $Modes = { |
20
|
|
|
|
|
|
|
debug => 0, |
21
|
|
|
|
|
|
|
error => 1, |
22
|
|
|
|
|
|
|
info => 0 |
23
|
|
|
|
|
|
|
}; |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
# Boolean options |
26
|
|
|
|
|
|
|
our $Options = { |
27
|
|
|
|
|
|
|
verbose => 0, |
28
|
|
|
|
|
|
|
debug => 0, |
29
|
|
|
|
|
|
|
quiet => 0, |
30
|
|
|
|
|
|
|
repair => 0, |
31
|
|
|
|
|
|
|
help => 0, |
32
|
|
|
|
|
|
|
color => 1 |
33
|
|
|
|
|
|
|
}; |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
# String options |
36
|
|
|
|
|
|
|
our $Roles = []; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
# Set up colored output |
39
|
|
|
|
|
|
|
$Term::ANSIColor::EACHLINE = "\n"; |
40
|
|
|
|
|
|
|
my $Colors = { |
41
|
|
|
|
|
|
|
'debug' => 'black', |
42
|
|
|
|
|
|
|
'info' => 'yellow', |
43
|
|
|
|
|
|
|
'error' => 'red' |
44
|
|
|
|
|
|
|
}; |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
# Dynamic setup of reporting functions |
47
|
|
|
|
|
|
|
for my $level (keys %{$Modes}) { |
48
|
|
|
|
|
|
|
|
49
|
17
|
|
|
17
|
|
218
|
no strict 'refs'; |
|
17
|
|
|
|
|
32
|
|
|
17
|
|
|
|
|
23563
|
|
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
*{"Csistck\::Oper\::$level"} = sub { |
52
|
73
|
|
|
73
|
|
134
|
my $func = shift; |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
# Maybe this isn't the best way. If func is passed and |
55
|
|
|
|
|
|
|
# is code, execute. If func is passed and is a scalar, |
56
|
|
|
|
|
|
|
# debug it?! |
57
|
73
|
100
|
66
|
|
|
525
|
if (defined $func and $Modes->{$level}) { |
58
|
8
|
|
|
|
|
39
|
given (ref $func) { |
59
|
8
|
|
|
|
|
27
|
when ("CODE") { return &$func; }; |
|
0
|
|
|
|
|
0
|
|
60
|
8
|
|
|
|
|
27
|
when ("") { return log_message($level, $func) }; |
|
8
|
|
|
|
|
83
|
|
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
else { |
64
|
|
|
|
|
|
|
# Return mode |
65
|
65
|
|
|
|
|
189
|
return $Modes->{$level}; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
}; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
# Repair mode accessor |
71
|
|
|
|
|
|
|
sub repair { |
72
|
1
|
|
|
1
|
0
|
5
|
return $Options->{repair}; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
# Set up mode via command line options |
76
|
|
|
|
|
|
|
sub set_mode_by_cli { |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
# Map options (as getopt negatable option) to $Options |
79
|
|
|
|
|
|
|
# TODO more options |
80
|
5
|
|
|
5
|
0
|
6
|
my %opts = map { +"$_!" => \$Options->{$_} } keys %{$Options}; |
|
30
|
|
|
|
|
92
|
|
|
5
|
|
|
|
|
18
|
|
81
|
5
|
|
|
|
|
16
|
my $result = GetOptions(%{{ |
|
5
|
|
|
|
|
48
|
|
82
|
5
|
|
|
|
|
7
|
'role=s' => \@{$Roles}, |
83
|
|
|
|
|
|
|
%opts |
84
|
|
|
|
|
|
|
}}); |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
# Set reporting mode based on options |
87
|
5
|
50
|
|
|
|
2468
|
$Modes->{info} = ($Options->{verbose}) ? 1 : 0; |
88
|
5
|
50
|
|
|
|
17
|
$Modes->{debug} = ($Options->{debug}) ? 1 : 0; |
89
|
5
|
50
|
|
|
|
29
|
$Modes->{error} = ($Options->{quiet}) ? 0 : 1; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
# Display usage |
93
|
|
|
|
|
|
|
sub usage { |
94
|
5
|
50
|
|
5
|
0
|
30
|
return undef unless ($Options->{help}); |
95
|
|
|
|
|
|
|
|
96
|
0
|
|
|
|
|
0
|
print <
|
97
|
|
|
|
|
|
|
Usage: $0 [OPTION]... |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
Arguments: |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
--help Display usage |
102
|
|
|
|
|
|
|
--verbose Verbose output |
103
|
|
|
|
|
|
|
--debug Debug output |
104
|
|
|
|
|
|
|
--repair Run repair operations |
105
|
|
|
|
|
|
|
--role=ROLE Force check on weak role ROLE |
106
|
|
|
|
|
|
|
--quiet Less output |
107
|
|
|
|
|
|
|
--nocolor Turn off colored output |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
EOF |
110
|
0
|
|
|
|
|
0
|
return 1; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
sub log_message { |
114
|
8
|
|
|
8
|
0
|
30
|
my $level = shift; |
115
|
8
|
|
|
|
|
22
|
my $msg = shift; |
116
|
|
|
|
|
|
|
|
117
|
8
|
|
|
|
|
56
|
my $log_line = sprintf("[%s]\ %s\n", uc($level), $msg); |
118
|
|
|
|
|
|
|
|
119
|
8
|
50
|
|
|
|
52
|
if ($Options->{color}) { |
120
|
8
|
|
|
|
|
104
|
print(colored($log_line, $Colors->{$level})); |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
else { |
123
|
0
|
|
|
|
|
|
print($log_line); |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
1; |