#!/usr/bin/perl ##### Reply-To: "ADSM: Dist Stor Manager" ##### Date: Mon, 24 Aug 2009 09:02:07 -0400 ##### From: Andrew Raibeck ##### Subject: Re: Colorful console mode output #+wjm ##### ... original idea from: ##### Date: Sat, 22 Aug 2009 02:00:08 +0300 ##### From: Michael Green ##### Subject: Colorful console mode output # # ... and improved upon by w.j.m. (tested with 'xterm' only)... # # Usage: $0 [-id= [-pa=]] [-se=] # #.wjm use strict; use POSIX qw(strftime); # timestamp formatting routine use Term::ANSIColor qw(:constants); if($^O =~ m/^MSWin32/) { require Win32::Console::ANSI; } ## You may need to edit this line. my $dsmadmBin = "dsmadmc"; # command that invokes 'dsmadmc' ## This block is responsible for catching interrupts my $int_counter = 0; sub int_handler { $int_counter++ } $SIG{INT} = 'int_handler'; $SIG{HUP} = 'int_handler'; $SIG{QUIT} = 'int_handler'; $SIG{PIPE} = 'int_handler'; $SIG{STOP} = 'int_handler'; $SIG{ABRT} = 'int_handler'; $SIG{TRAP} = 'int_handler'; $SIG{TERM} = 'int_handler'; ## handle output & timestamps $| = 1; # aka. $OUTPUT_AUTOFLUSH sub doprint { if($#_ == 0) { # single arg => non-ANR msg ('dsmadmc' header) print RESET, BLUE, @_, RESET; } else { # timestamp + args (color attributes & ANR msg) #-wjm print WHITE, ON_BLACK, strftime "%m\/%d %H:%M:%S ", localtime; ... print CYAN, ON_BLACK, strftime("%Y-%m-%d %H:%M:%S ", localtime), RESET, @_, RESET; } } ## delay output until after 'dsmadmc' prompts my $init = 1; my $initoutput = ""; sub end_init { if($init) { $init = 0; print "\n"; doprint $initoutput; } } ## Invoke the 'dsmadmc' binary with our command line parameters my $dscl = "$dsmadmBin -consolemode @ARGV"; open DSCL, "$dscl|" or die "Cannot execute \'$dscl\': $!"; while () { if($int_counter) { last; } elsif(/^ANR\d{4}I\s+/) { end_init; doprint WHITE, ON_BLACK, $_; } elsif(/^ANR\d{4}W\s+/) { end_init; doprint YELLOW, BOLD, ON_BLACK, $_; } elsif(/^ANR\d{4}E\s+/) { end_init; doprint RED, BOLD, ON_BLACK, $_; } elsif(/^ANR\d{4}S\s+/) { #-wjm elsif (/(?:ANR\d{4}D)\s+(?:[\d\D]+)/) { end_init; doprint WHITE, BOLD, ON_RED, $_; } else { # Non-ANR message. Normally seen at 'dsmadmc' start-up only. if ($init) { # Delay output until "Session established" $initoutput .= $_; end_init if (/established/); } else { doprint $_; } } } close DSCL; end_init; print RESET, "\n";