#!/usr/bin/perl -w
#=============================================================================
# $Id: vtrace 31182 2007-02-01 14:36:21Z wsnyder $
#=============================================================================
#
# This program creates a define group file for waves
# 
# Copyright 2000-2007 by Wilson Snyder.  This program 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.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
#=============================================================================

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

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

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

# 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,
		 "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,
		 )) {
    usage();
}

($#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 '$Id: vtrace 31182 2007-02-01 14:36:21Z wsnyder $ ', "\n";
    $SIG{__WARN__} = sub{};	#pod2text isn't clean.
    pod2text($0);
    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.com/> free EDA software
tool suite.  The latest version is available from CPAN and from
L<http://www.veripool.com/>.

Copyright 2000-2007 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
