diff options
-rwxr-xr-x | utils/rspamd_stats.pl | 65 |
1 files changed, 52 insertions, 13 deletions
diff --git a/utils/rspamd_stats.pl b/utils/rspamd_stats.pl index efb787358..3a8affce1 100755 --- a/utils/rspamd_stats.pl +++ b/utils/rspamd_stats.pl @@ -113,17 +113,16 @@ elsif ( -d "$log_file" ) { open( $rspamd_log, "-|", "$dc $log_dir/$_" ) or die "cannot execute $dc $log_dir/$_ : $!"; - if (!$json) { - printf "\033[J Parsing log files: [%d/%d] %s\033[G", $log_file_num++, scalar @logs, $_; - $spinner_update_time = 0; # Force spinner update - &spinner; - } + printf {interactive(*STDERR)} "\033[J Parsing log files: [%d/%d] %s\033[G", $log_file_num++, scalar @logs, $_; + $spinner_update_time = 0; # Force spinner update + &spinner; &ProcessLog; close($rspamd_log) or warn "cannot close $dc $log_dir/$_: $!"; } + print {interactive(*STDERR)} "\033[J\033[G"; # Progress indicator clean-up } else { my $ext = ($log_file =~ /[^.]+\.?([^.]*?)$/)[0]; @@ -630,13 +629,11 @@ sub GetLogfilesList { splice( @logs, $exclude_logs, $num_logs ||= @logs - $exclude_logs ); # Loop through array printing out filenames - if (!$json) { - print "\nLog files to process:\n"; - foreach my $file (@logs) { - print " $file\n"; - } - print "\n"; + print {interactive(*STDERR)} "\nLog files to process:\n"; + foreach my $file (@logs) { + print {interactive(*STDERR)} " $file\n"; } + print {interactive(*STDERR)} "\n"; return @logs; } @@ -701,9 +698,9 @@ sub numeric { sub spinner { my @spinner = qw{/ - \ |}; return - if ( $json || ( time - $spinner_update_time ) < 1 ); - printf "%s\r", $spinner[ $spinner_update_time % @spinner ]; + if ( ( time - $spinner_update_time ) < 1 ); $spinner_update_time = time; + printf {interactive(*STDERR)} "%s\r", $spinner[ $spinner_update_time % @spinner ]; select()->flush(); } @@ -723,6 +720,48 @@ sub syslog2iso { $month_map{$month} + 1, @t; } +### Imported from IO::Interactive 1.022 Perl module +sub is_interactive { + my ($out_handle) = (@_, select); # Default to default output handle + + # Not interactive if output is not to terminal... + return 0 if not -t $out_handle; + + # If *ARGV is opened, we're interactive if... + if ( tied(*ARGV) or defined(fileno(ARGV)) ) { # this is what 'Scalar::Util::openhandle *ARGV' boils down to + + # ...it's currently opened to the magic '-' file + return -t *STDIN if defined $ARGV && $ARGV eq '-'; + + # ...it's at end-of-file and the next file is the magic '-' file + return @ARGV>0 && $ARGV[0] eq '-' && -t *STDIN if eof *ARGV; + + # ...it's directly attached to the terminal + return -t *ARGV; + } + + # If *ARGV isn't opened, it will be interactive if *STDIN is attached + # to a terminal. + else { + return -t *STDIN; + } +} + +### Imported from IO::Interactive 1.022 Perl module +local (*DEV_NULL, *DEV_NULL2); +my $dev_null; +BEGIN { + pipe *DEV_NULL, *DEV_NULL2 + or die "Internal error: can't create null filehandle"; + $dev_null = \*DEV_NULL; +} + +### Imported from IO::Interactive 1.022 Perl module +sub interactive { + my ($out_handle) = (@_, \*STDOUT); # Default to STDOUT + return &is_interactive ? $out_handle : $dev_null; +} + __END__ =head1 NAME |