#!/usr/bin/perl -w
# See copyright, etc in below POD section.
######################################################################

require 5.005;
use Getopt::Long;
use IO::File;
use Pod::Usage;

use lib "./lib";
use Log::Detect;
use Log::Delayed;
use strict;
use vars qw($Debug @More_Warnings $Detect $VERSION);

$VERSION = '1.423';

#======================================================================

# Warnings to add to Detect's:
@More_Warnings =
    ( ignore => qr/Warning ?: (License|ACC\/CLI capabilities have been enabled)/,
      );

#======================================================================
# main

$Detect = new Log::Detect
    (dino => undef,
     simvision => undef,
     warn_fatal => 1,
     warn_finish => 1,
     );
$Detect->add_regexp(@More_Warnings);

$Debug = 0;
my $opt_stderr;
my $opt_append;
my $result_filename = undef;
my @files = ();

if (!GetOptions (
		 "help"		=> \&usage,
		 "debug"	=> \&debug,
		 "version"	=> sub { print "Version $VERSION\n"; exit(0); },
		 "stderr!"	=> \$opt_stderr,
		 "append!"	=> \$opt_append,
		 "warnfatal!"	=> sub {shift; $Detect->set(warn_fatal => shift);},
		 "warnfinish!"	=> sub {shift; $Detect->set(warn_finish => shift);},
		 "dino=s"	=> sub {shift; $Detect->set(dino => shift);},
		 "simvision=s"	=> sub {shift; $Detect->set(simvision => shift);},
		 "result=s"	=> \$result_filename,
		 "<>"		=> \&parameter,
		 )) {
    die "%Error: Bad usage, try 'vtrace --help'\n";
}

($#files >= 0) or die "%Error: A .log file must be provided.\n";

foreach my $file (@files) {
    $Detect->read (filename=>$file);
}

# Write outputs
$Detect->write_append () if ($opt_append);
$Detect->write_stderr () if ($opt_stderr);
$Detect->write_dino () if $Detect->{dino};
$Detect->write_simvision () if $Detect->{simvision};

my ($stat,$sum) = $Detect->summary();
if ($result_filename) {
    my $Delayed = new Log::Delayed (filename=>$result_filename,
				    global=>1,);
    $Delayed->status($stat) if $stat;
    print "vtrace: fatal_exit: $stat\n" if $Debug;
}
exit(defined $stat?10:0);

#----------------------------------------------------------------------

sub usage {
    print "Version $VERSION\n";
    pod2usage(-verbose=>2, -exitval => 2);
    exit (1);
}

sub debug {
    $Debug = 1;
}

sub parameter {
    my $param = shift;
    push @files, $param;
}

######################################################################
1;
__END__

=pod

=head1 NAME

vtrace - read log files to check for errors and create Dinotrace cursors

=head1 SYNOPSIS

  vtrace <filename.log[.gz]>

=head1 DESCRIPTION

Vtrace reads the log files requested and optionally creates a Dinotrace or
Simvision file that places cursors at appropriate error or warning points.
The exit status will tell if any error or warnings were encountered in the
file.

=head1 ARGUMENTS

vtrace takes the following arguments:

=over 4

=item --help

Displays this message and program version and exits.

=item --append

Appends warning and error summary to end of the log file.

=item --dino I<name>

Filename to write a Dinotrace config file to.

=item --simvision I<name>

Filename to write a Simvision config file to.

=item --stderr

Shows errors and warnings in the log file onto stderr.

=item --warnfatal

Returns fatal exit status on warnings

=item --warnfinish

Returns fatal exit status if a magic line indicating test completion
is missing.

=back

=head1 DISTRIBUTION

Log-Detect is part of the L<http://www.veripool.org/> free EDA software
tool suite.  The latest version is available from CPAN and from
L<http://www.veripool.org/>.

Copyright 2000-2009 by Wilson Snyder.  This package is free software; you
can redistribute it and/or modify it under the terms of either the GNU
Lesser General Public License or the Perl Artistic License.

=head1 AUTHORS

Wilson Snyder <wsnyder@wsnyder.org>

=head1 SEE ALSO

L<Log::Detect>

=cut
