use
5.010;
our
@EXPORT_OK
=
qw/
debug error info
repair
/
;
our
$Modes
= {
debug
=> 0,
error
=> 1,
info
=> 0
};
our
$Options
= {
verbose
=> 0,
debug
=> 0,
quiet
=> 0,
repair
=> 0,
help
=> 0,
color
=> 1
};
$Term::ANSIColor::EACHLINE
=
"\n"
;
my
$Colors
= {
'debug'
=>
'black'
,
'info'
=>
'yellow'
,
'error'
=>
'red'
};
for
my
$level
(
keys
%{
$Modes
}) {
no
strict
'refs'
;
*{
"Csistck\::Oper\::$level"
} =
sub
{
my
$func
=
shift
;
if
(
defined
$func
and
$Modes
->{
$level
}) {
given
(
ref
$func
) {
when
(
"CODE"
) {
return
&$func
; };
when
(
""
) {
return
log_message(
$level
,
$func
) };
}
}
else
{
return
$Modes
->{
$level
};
}
};
}
sub
repair {
return
$Options
->{repair};
}
sub
set_mode_by_cli {
my
%opts
=
map
{ +
"$_!"
=> \
$Options
->{
$_
} }
keys
%{
$Options
};
my
$result
= GetOptions(
%opts
);
$Modes
->{info} = (
$Options
->{verbose}) ? 1 : 0;
$Modes
->{debug} = (
$Options
->{debug}) ? 1 : 0;
$Modes
->{error} = (
$Options
->{quiet}) ? 0 : 1;
}
sub
usage {
return
undef
unless
(
$Options
->{help});
print
<<EOF;
Usage: $0 [OPTION]...
Arguments:
--help Display usage
--verbose Verbose output
--debug Debug output
--repair Run repair operations
--quiet Less output
--nocolor Turn off colored output
EOF
return
1;
}
sub
log_message {
my
$level
=
shift
;
my
$msg
=
shift
;
my
$log_line
=
sprintf
(
"[%s]\ %s\n"
,
uc
(
$level
),
$msg
);
if
(
$Options
->{color}) {
print
(colored(
$log_line
,
$Colors
->{
$level
}));
}
else
{
print
(
$log_line
);
}
}
1;