| 1 | package Csistck::Oper; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 17 | ▶ | use 5.010; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 17 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 17 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 17 | ▶ | use base 'Exporter'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 17 | our @EXPORT_OK = qw/ | |||||||||||||||||||||||||||
| 10 | debug error info | ||||||||||||||||||||||||||||
| 11 | repair | ||||||||||||||||||||||||||||
| 12 | /; | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 17 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 17 | ▶ | use Getopt::Long; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 17 | ▶ | use Term::ANSIColor; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | # Logging levels, default boolean | ||||||||||||||||||||||||||||
| 19 | 17 | our $Modes = { | |||||||||||||||||||||||||||
| 20 | debug => 0, | ||||||||||||||||||||||||||||
| 21 | error => 1, | ||||||||||||||||||||||||||||
| 22 | info => 0 | ||||||||||||||||||||||||||||
| 23 | }; | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | # Boolean options | ||||||||||||||||||||||||||||
| 26 | 17 | 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 | 17 | our $Roles = []; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | # Set up colored output | ||||||||||||||||||||||||||||
| 39 | 17 | $Term::ANSIColor::EACHLINE = "\n"; | |||||||||||||||||||||||||||
| 40 | 17 | my $Colors = { | |||||||||||||||||||||||||||
| 41 | 'debug' => 'black', | ||||||||||||||||||||||||||||
| 42 | 'info' => 'yellow', | ||||||||||||||||||||||||||||
| 43 | 'error' => 'red' | ||||||||||||||||||||||||||||
| 44 | }; | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | # Dynamic setup of reporting functions | ||||||||||||||||||||||||||||
| 47 | 17 | for my $level (keys %{$Modes}) { | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 17 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 51 | ▶ | *{"Csistck\::Oper\::$level"} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||
| 52 | 73 | 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 | ▶ | if (defined $func and $Modes->{$level}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $func and $Csistck::Oper::Modes->{$level}
MC/DC50%1/2
defined $func and $Csistck::Oper::Modes->{$level}
defined $func
$Csistck::Oper::Modes->{$level}
Truth table66%2/3
A and B
A= defined $funcB= $Csistck::Oper::Modes->{$level}
| |||||||||||||||||||||||||||||
| 58 | 8 | given (ref $func) { | |||||||||||||||||||||||||||
| 59 | 0 | when ("CODE") { return &$func; }; | |||||||||||||||||||||||||||
| 60 | 8 | when ("") { return log_message($level, $func) }; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | else { | ||||||||||||||||||||||||||||
| 64 | # Return mode | ||||||||||||||||||||||||||||
| 65 | 65 | return $Modes->{$level}; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | 51 | }; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | # Repair mode accessor | ||||||||||||||||||||||||||||
| 71 | 1 | ▶ | sub repair { | ||||||||||||||||||||||||||
|
Subroutine
repair: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 72 | 1 | return $Options->{repair}; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | # Set up mode via command line options | ||||||||||||||||||||||||||||
| 76 | 5 | ▶ | sub set_mode_by_cli { | ||||||||||||||||||||||||||
|
Subroutine
set_mode_by_cli: called CC=4 SCAR=14.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | # Map options (as getopt negatable option) to $Options | ||||||||||||||||||||||||||||
| 79 | # TODO more options | ||||||||||||||||||||||||||||
| 80 | 5 | my %opts = map { +"$_!" => \$Options->{$_} } keys %{$Options}; | |||||||||||||||||||||||||||
| 81 | 5 | my $result = GetOptions(%{{ | |||||||||||||||||||||||||||
| 82 | 5 | 'role=s' => \@{$Roles}, | |||||||||||||||||||||||||||
| 83 | %opts | ||||||||||||||||||||||||||||
| 84 | }}); | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | # Set reporting mode based on options | ||||||||||||||||||||||||||||
| 87 | 5 | ▶ | $Modes->{info} = ($Options->{verbose}) ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 5 | ▶ | $Modes->{debug} = ($Options->{debug}) ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 5 | ▶ | $Modes->{error} = ($Options->{quiet}) ? 0 : 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | # Display usage | ||||||||||||||||||||||||||||
| 93 | 5 | ▶ | sub usage { | ||||||||||||||||||||||||||
|
Subroutine
usage: called CC=2 SCAR=10.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 94 | 5 | ▶ | return undef unless ($Options->{help}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 0 | print <<EOF; | |||||||||||||||||||||||||||
| 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 | return 1; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 8 | ▶ | sub log_message { | ||||||||||||||||||||||||||
|
Subroutine
log_message: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 114 | 8 | my $level = shift; | |||||||||||||||||||||||||||
| 115 | 8 | my $msg = shift; | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 8 | my $log_line = sprintf("[%s]\ %s\n", uc($level), $msg); | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 8 | ▶ | if ($Options->{color}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 8 | print(colored($log_line, $Colors->{$level})); | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | else { | ||||||||||||||||||||||||||||
| 123 | 0 | print($log_line); | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 17 | 1; | |||||||||||||||||||||||||||