package RDA::Log; # $Id: Log.pm,v 1.25 2008/02/25 14:55:25 mschenke Exp $ # ARCS: $Header: /home/cvs/repository/RDA_4/src/scripting/lib/RDA/Log.pm,v 1.25 2008/02/25 14:55:25 mschenke Exp $ # # Change History # 20080225 MSC Change the copyright notice. =head1 NAME RDA::Log - Class Used for Objects to Manage the Event Log =head1 SYNOPSIS require RDA::Log; =head1 DESCRIPTION The objects of the C class are used to manage the event log. The following methods are available: =cut use strict; BEGIN { use Exporter; use File::Spec; use IO::File; } # Define the global public variables use vars qw($VERSION); $VERSION = sprintf("%d.%02d", q$Revision: 1.25 $ =~ /(\d+)\.(\d+)/); # Define the global private variables # Report the module version number sub Version { $VERSION; } =head2 S<$h = RDA::Log-Enew($agt)> The object constructor. It takes the agent reference as an argument. C is represented by a blessed hash reference. The following special keys are used: =over 16 =item B< '_afl' > Autoflush indicator =item B< '_agt' > Reference to the agent object =item B< '_beg' > Start time =item B< '_nam' > Setup file name =item B< '_ofh' > Output file handle =item B< '_pth' > Log file path =item B< '_ver' > Software version =back Internal keys are prefixed by an underscore. It also tries to open the event log file and to add a start event (type 'b'). =cut sub new { my $slf = shift; my $cls = ref($slf) || $slf; my $agt = shift; my $pth; # Create the render object $slf = bless {}, $cls; # Get some setup information $slf->{'_agt'} = $agt; $slf->{'_beg'} = time; $slf->{'_nam'} = $agt->get_info('nam'); $slf->{'_ver'} = $agt->get_info('ver'); # Return the object reference $slf; } =head2 S<$h-Eend> This method logs an end event (type 'e') including the execution duration, and then closes the event log. =cut sub end { my ($slf) = @_; if (exists($slf->{'_ofh'})) { # Log the end record my $tim = time; $slf->_log($tim, 'e', $slf->{'_agt'}->get_setting('RDA_VERSION'), $tim - $slf->{'_beg'}, $slf->{'_agt'}->get_setting('RPT_GROUP', 'RDA')); # Close the event log file close($slf->{'_ofh'}); delete($slf->{'_ofh'}); } } =head2 S<$h-Einit($dir[,$flg])> This method initialises the log file in the specified directory. When the flag is set, it also creates the report directory. =cut sub init { my ($slf, $dir, $flg) = @_; unless (exists($slf->{'_ofh'})) { my $pth; # Create the report directory when needed. if ($flg && $dir) { die "RDA-00200: Cannot create the report directory $dir:\n $!\n" unless -d $dir || mkdir($dir, 0770); } # Open the event log file and add the start event $slf->_init($dir) if $dir && -d $dir; } } sub _init { my ($slf, $dir) = @_; my ($agt, $ofh, $pth); # Open the event log file and add the start event $agt = $slf->{'_agt'}; $ofh = IO::File->new; $pth = File::Spec->catfile($dir, $agt->get_setting('RDA_CASE') ? 'RDA.log' : 'rda.log'); if (open($ofh, ">>$pth")) { $ofh->autoflush(1) if ($slf->{'_afl'} = $agt->get_setting('RDA_FLUSH', 1)); $slf->{'_ofh'} = $ofh; $slf->{'_pth'} = $pth; $slf->_log($slf->{'_beg'}, 'b', $slf->{'_ver'}); } } =head2 S<$h-Elog($typ[,$arg...])> This method logs an event. It prefixes log records with a timestamp (GMT) and the setup name. Fields are separated by a C<|> character. =cut sub log { my $slf = shift; my $typ = shift; $slf->_log(time, $typ, @_) if exists($slf->{'_ofh'}) && $typ; } sub _log { my $slf = shift; my @tbl = gmtime(shift); print {$slf->{'_ofh'}} join('|', sprintf("%04d%02d%02d_%02d%02d%02d", 1900 + $tbl[5], 1 + $tbl[4], $tbl[3], $tbl[2], $tbl[1], $tbl[0]), $slf->{'_nam'}, @_, "\n"); } =head2 S<$h-Elogfile> This method returns the file handle that is associated with the event log. Othewise, it returns C when not possible. =cut sub logfile { my $slf = shift; exists($slf->{'_ofh'}) ? $slf->{'_ofh'} : undef; } =head2 S<$h-Eresume> This method reopens the log file and restarts the event logging. =cut sub resume { my $slf = shift; my $ofh; if (exists($slf->{'_pth'}) && !exists($slf->{'_ofh'})) { $ofh = IO::File->new; if (open($ofh, '>>'.$slf->{'_pth'})) { $ofh->autoflush(1) if $slf->{'afl'}; $slf->{'_ofh'} = $ofh; } } } =head2 S<$h-Esuspend> This method suspends event logging and closes the log file. =cut sub suspend { my $slf = shift; if (exists($slf->{'_ofh'})) { close($slf->{'_ofh'}); delete($slf->{'_ofh'}); } } 1; __END__ =head1 SEE ALSO See also L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L. =head1 COPYRIGHT NOTICE Copyright (c) 2002-2008, Oracle. All rights reserved. =head1 TRADEMARK NOTICE Oracle, JD Edwards, PeopleSoft, and Siebel are registered trademarks of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. =cut