#!/usr/bin/perl

use strict;
use warnings;
my $VERSION = 0.1;

=head1 NAME

color_tail.pl is a utility that works much like tail -f but colors lines containing pre-defined strings.

=head1 DESCRIPTION

Configuring which strings shall be colored is done like so: 

my @greens=('SUCCESS','OK','DEBUG');

my @reds=('ERROR');

my @yellows=('INFO','WARNING');

color_tail.pl can be used to monitor any log file provided the correct patterns are defined.

=head1 SYNOPSIS

./color_tail.pl </path/to/log> [path to other log] ..

If the COLOR_ERRS_ONLY var is set, only ERRORS will be outputted.

=head1 PREREQUISITES

This script requires the C<File::Tail::Multi> and C<Term::ANSIColor> modules. 

=head1 COREQUISITES

Pretty much any Perl version for which the modules stated in the PREREQUISITES section are installed.

=head1 COPYRIGHT

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.

=head1 SCRIPT CATEGORIES

UNIX/System_administration

=head1 AUTHOR

Jess Portnoy <kernel01@gmail.com>

=cut
use File::Tail::Multi;
use Term::ANSIColor;
use constant GREEN => 1;
use constant RED => 2;
use constant YELLOW => 3;
use constant BASE_LOG_DIR => '';
$SIG{__DIE__} = sub { print "@_"; exit 3; };
$SIG{INT} = \&unset_color;  # best strategy

my @files;
for (my $i=0;$i<$#ARGV+1;$i++){
	if (! -f $ARGV[$i]){
		my $try=BASE_LOG_DIR."$ARGV[$i]";
		if ( -f $try){
			$files[$i]=$try;
		}else{
			print "Couldn't find $ARGV[$i] or $try\n";
		}
	}else{
		$files[$i]=$ARGV[$i];
	}
}
my $tail1=File::Tail::Multi->new (
	OutputPrefix => "f", 
	Debug => 0, 
	RemoveDuplicate => 0,
	Files => [@files],
	Function => \&hook
);
print color 'reset';
while(1) {
	$tail1->read;
	sleep 1;
}

sub hook 
{
	my $lines_ref = shift;      
        foreach (@{$lines_ref}){
        	chomp;
			colorize($_);	
		}	
}

sub colorize
{
	my ($line)=@_;
    # the following are only examples of patterns to match, please replace with your own.
	#my @greens=('Installation finished','BUILD SUCCESSFUL','saved','version matched','Commit complete','Upgrade script finished','installation process completed','procedure successfully completed','Running upgrade script','Display ear file changes','Removing webloic temp directory','copy ','backup ',' Finished','Starting installation','OK');
	#my @reds=('No such file or directory','Build failed','Device or resource busy','the account is locked','NOT matched','cannot delete old','not found','Error:','cannot overwrite directory','not valid','ORA-','failed to match','Stale NFS','bad interpreter','another process is using port','Failed to load class');
	#my @yellows=('info','warning');
	my @greens=('New.*device found','I love Tally');
	my @reds=('ERROR');
	my @yellows=('was not an MTP device');
	if (! $ENV{'COLOR_ERRS_ONLY'}){	
		for my $green (@greens){
			if ($line =~ /$green/){
				print color 'green';
				print "$line\n";
				print color 'reset';
				return GREEN;
			}
		}
		for my $yellow (@yellows){
			if ($line =~ /$yellow/i){
				print color 'yellow';
				print "$line\n";
				print color 'reset';
				return YELLOW;
			}
		}
	}
	for my $red (@reds){
	    if ($line =~ /$red/i){
            print color 'red';
            print "$line\n";
            print color 'reset';
            return RED;
        } 
	}

    if (!$ENV{'COLOR_ERRS_ONLY'}){	
		print $line,"\n";
    }
}
sub unset_color
{
	print color 'reset';
	exit;
}
