#! /usr/bin/perl -w

# vim:syntax=perl

use strict;
use lib '/usr/local/share/perl5';
use Lire::DlfSchema;
use Lire::Syslog;
use Lire::Program qw( :msg :dlf );

my $schema = eval { Lire::DlfSchema::load_schema( "syslog" ) };
lr_err( "failed to load syslog schema: $@" ) if $@;
my $dlf_maker;

my $lines	= 0;
my $dlflines	= 0;
my $errorlines  = 0;

my $parser = new Lire::Syslog();
init_dlf_converter( "syslog" );
while ( <> ) {
    lire_chomp();
    $lines++;

    my $rec = eval { $parser->parse($_); };
    if ( $@ ) {
	lr_warn( $@ );
	$errorlines++;
	next;
    }

    unless (defined $dlf_maker ) {
	# Create the DLF schema function based on the available fields
	my @fields = qw/timestamp hostname process pid message /;

	push @fields, "facility" if exists $rec->{facility};
	push @fields, "level" if exists $rec->{level};

	$dlf_maker = $schema->make_hashref2asciidlf_func( @fields );
    }
    $rec->{message} = $rec->{content};

    my $dlf = $dlf_maker->( $rec );
    $dlflines++;
    print join( " ", @$dlf ), "\n";
}
end_dlf_converter( $lines, $dlflines, $errorlines );

__END__

=pod

=head1 NAME

syslog2dlf - convert syslog log files to DLF

=head1 SYNOPSIS

B<syslog2dlf>

=head1 DESCRIPTION

This script uses the Lire::Syslog module to create a DLF
representation of the syslog log files. It can process all the syslog
log formats supported by that module.

=head1 SEE ALSO

Lire::Syslog(3pm)

=head1 VERSION

$Id: syslog2dlf.in,v 1.1 2002/08/04 19:32:01 flacoste Exp $

=head1 AUTHOR

Francis J. Lacoste <flacoste@logreport.org>

=head1 COPYRIGHT

Copyright (C) 2002 Stichting LogReport Foundation LogReport@LogReport.org

This program is part of Lire.
 
Lire is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
 
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.
 
You should have received a copy of the GNU General Public License
along with this program (see COPYING); if not, check with
http://www.gnu.org/copyleft/gpl.html or write to the Free Software 
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.

=cut

# Local Variables:
# mode: cperl
# End:
