#!/usr/bin/perl -w

use strict;
use warnings;

=head1 NAME

ExampleLog - short description

=cut

use constant VERSION => '0.0.1';

use diagnostics;
use Getopt::Long qw(:config no_ignore_case posix_default );
use Pod::Usage;
use Log::Log4perl qw(:easy);

=head1 SYNOPSIS

ExampleLog -run -help|-h -man -version [...]
                  -loglevel=<string> -logfile=<file> -loglayout=<layout string>

=cut

my %opt = ();
GetOptions( \%opt, 'run!', 'help|h!', 'man!', 'version!',
          'loglevel=s', 'logfile=s', 'loglayout=s'  )
  or pod2usage( -verbose => 0, -exitval => 1, -output  => \*STDERR );

=head1 OPTIONS

=over 4

=item B<-loglevel>

Set the loglevel=<DEBUG|INFO|WARN|ERROR|FATAL> default is WARN.

=cut

$opt{loglevel} ='INFO' unless (defined $opt{loglevel});

=item B<-logfile>

Set the logfile=<file> default is STDERR.

=cut

$opt{logfile}  ='STDERR' unless (defined $opt{logfile});

=item B<-loglayout>

Set the loglayout=<format> default is '%d %r %p %F %L %M %m%n'

The format string can contain a number of placeholders which will be
replaced by the logging engine when it's time
to log the message:

  %c Category of the logging event.
  %C Fully qualified package (or class) name of the caller
  %d Current date in yyyy/MM/dd hh:mm:ss format
  %F File where the logging event occurred
  %H Hostname
  %l Fully qualified name of the calling method followed by the
     callers source the file name and line number between
     parentheses.
  %L Line number within the file where the log statement was issued
  %m The message to be logged
  %M Method or function where the logging request was issued
  %n Newline (OS-independent)
  %p Priority of the logging event
  %P pid of the current process
  %r Number of milliseconds elapsed from program start to logging
     event
  %x The elements of the NDC stack (see below)
  %X{key} The entry 'key' of the MDC (see below)
  %% A literal percent (%) sign

=cut

$opt{loglayout}="%d $$ %p %F %L %M %m%n" unless (defined $opt{loglayout});

Log::Log4perl->easy_init(
                          {
                            category => __PACKAGE__,
                            level    => $opt{loglevel},
                            file     => $opt{logfile},
                            layout   => $opt{loglayout},
                          }
);
DEBUG("Option: \$opt{$_}=$opt{$_}") for ( keys %opt );

=item B<-help|-h>

Print a brief help message and exit.

=cut

if ( defined $opt{help} ) {
  pod2usage( -verbose => 1, -exitval => 0 );
}

=item B<-man>

Prints the manual page and exit.

=cut

if ( defined $opt{man} ) {
  pod2usage(-verbose => 2, -exitval => 0);
}

=item B<-version>

Prints the version number and exit.

=cut

if ( defined $opt{version} ) {
  print 'ExampleLog version ' . VERSION . "\n";
  exit 0;
}

=item B<-run>

This option is running your program.

=cut

unless ( defined( $opt{run} )) {
  pod2usage(-verbose => 0, -exitval => 1);
}

=back

=cut

# # # #

INFO "Program is running."

# # # #

__END__

=head1 DESCRIPTION

B<ExampleLog > will do something...

=head1 AUTHOR

Urs Stotz <stotz@gmx.ch>

=head1 COPYRIGHT

Copyright (c) 2005, Urs Stotz <stotz@gmx.ch>

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

=head1 SEE ALSO

L<perl(1)|perl> L<Log::Log4perl> 
=cut