Skip to content

Instantly share code, notes, and snippets.

@moisseev
Created March 18, 2018 18:57
Show Gist options
  • Save moisseev/c012e46d8ae03d0fe941d62f63c843fa to your computer and use it in GitHub Desktop.
Save moisseev/c012e46d8ae03d0fe941d62f63c843fa to your computer and use it in GitHub Desktop.
Cache interactivity checks in rspamd_stat
diff --git a/utils/rspamd_stats.pl b/utils/rspamd_stats.pl
index 3a8affce1..a8d65e36b 100755
--- a/utils/rspamd_stats.pl
+++ b/utils/rspamd_stats.pl
@@ -81,6 +81,7 @@ my %scanTime = (
total => 0,
);
my %bidir_match;
+my %fh_cache; # Cache for sub interactive
foreach ( $startTime, $endTime ) { $_ = &normalized_time($_) }
@@ -113,7 +114,7 @@ elsif ( -d "$log_file" ) {
open( $rspamd_log, "-|", "$dc $log_dir/$_" )
or die "cannot execute $dc $log_dir/$_ : $!";
- printf {interactive(*STDERR)} "\033[J Parsing log files: [%d/%d] %s\033[G", $log_file_num++, scalar @logs, $_;
+ printf {interactive(\%fh_cache, *STDERR)} "\033[J Parsing log files: [%d/%d] %s\033[G", $log_file_num++, scalar @logs, $_;
$spinner_update_time = 0; # Force spinner update
&spinner;
@@ -122,7 +123,7 @@ elsif ( -d "$log_file" ) {
close($rspamd_log)
or warn "cannot close $dc $log_dir/$_: $!";
}
- print {interactive(*STDERR)} "\033[J\033[G"; # Progress indicator clean-up
+ print {interactive(\%fh_cache, *STDERR)} "\033[J\033[G"; # Progress indicator clean-up
}
else {
my $ext = ($log_file =~ /[^.]+\.?([^.]*?)$/)[0];
@@ -629,11 +630,11 @@ sub GetLogfilesList {
splice( @logs, $exclude_logs, $num_logs ||= @logs - $exclude_logs );
# Loop through array printing out filenames
- print {interactive(*STDERR)} "\nLog files to process:\n";
+ print {interactive(\%fh_cache, *STDERR)} "\nLog files to process:\n";
foreach my $file (@logs) {
- print {interactive(*STDERR)} " $file\n";
+ print {interactive(\%fh_cache, *STDERR)} " $file\n";
}
- print {interactive(*STDERR)} "\n";
+ print {interactive(\%fh_cache, *STDERR)} "\n";
return @logs;
}
@@ -700,7 +701,7 @@ sub spinner {
return
if ( ( time - $spinner_update_time ) < 1 );
$spinner_update_time = time;
- printf {interactive(*STDERR)} "%s\r", $spinner[ $spinner_update_time % @spinner ];
+ printf {interactive(\%fh_cache, *STDERR)} "%s\r", $spinner[ $spinner_update_time % @spinner ];
select()->flush();
}
@@ -754,12 +755,18 @@ 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
+### and modified to cache checks
sub interactive {
- my ($out_handle) = (@_, \*STDOUT); # Default to STDOUT
- return &is_interactive ? $out_handle : $dev_null;
+ my $cache = shift;
+ my $out_handle = shift // \*STDOUT; # Default to STDOUT
+
+ $cache->{$out_handle} = &is_interactive
+ unless exists($cache->{$out_handle});
+ return $cache->{$out_handle} ? $out_handle : $dev_null;
}
__END__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment