Problem in appending the correct log

Hi All,
I have a perl module TrxLog.pm and following are codes in it

#!/usr/local/bin/perl
package TrxLog;
%log_begin="";
%log_end="";
%log_msg="";
%log_start_time="";
%log_end_time="";
$ix=0;
@arr_msg="";
if (! -e "TrxLog.txt"){
        open (TRX,">TrxLog.txt");
    }else{
                open (TRX,">>TrxLog.txt");
                }

sub begin{
        my($parm)=@_;
        $log_start_time{$parm}=localtime(time());
        $log_begin{$parm}=time();
        $log_msg{$parm}="** $parm Process **";

        }

sub end{
        my($parm)=@_;
        $log_end_time{$parm}=localtime(time());
        $log_end{$parm}=time();
        my($msg)=$log_msg{$parm};
        my($diff)=$log_end{$parm} - $log_begin{$parm};

        my ($tsec,$tmin,$thour,$tmday,$tmon,$tyear,$twday,$tyday,$tisdst) =
        gmtime($diff);

        print TRX "$msg\n\n";
        print TRX "\n";
        print TRX "\tStarted  at $log_start_time{$parm}\n";
        print TRX "\tFinished at $log_end_time{$parm}\n";
        print TRX "\tElapsed Time Hours:$thour";
        print TRX " Minutes:$tmin";
        print TRX " Seconds:$tsec\n";

        $ix=0;
        while ($arr_msg[$ix]){
                print TRX "$arr_msg[$ix]\n";
                $ix++;
        }

        &close_log;
}
sub log_trx{
        my($msg)=@_;
        $arr_msg[$ix]=$msg;
        $ix++;
}
sub close_log{
        print TRX "**************** End of Session **************************\n\n";
        close TRX;
}

1;

My problem is
The below log produced by a perl script is correctly appended into the TrxLog.txt file

** DNB Load Process **

    Started  at Mon Nov 10 14:09:13 2008
    Finished at Mon Nov 10 14:11:04 2008
    Elapsed Time Hours:0 Minutes:1 Seconds:51

archival of dnb process started

archival dnb_20081101 table created & total rows :520940
62507 DNB inserted
**************** End of Session **************************

But When I used the same perl module TrxLog.pm in another perl script it produced the following log.Which is not complete.

    Started  at
    Finished at Mon Nov 10 15:53:08 2008
    Elapsed Time Hours:20 Minutes:53 Seconds:8

**************** End of Session **************************

Considering that you are passing a parameter to the begin function and using it as a key... if you pass anything that's an invalid key, then the begin function will break, which will break the rest of the script.

sub begin{
my($parm)=@_;
$log_start_time{$parm}=localtime(time());
$log_begin{$parm}=time();
$log_msg{$parm}="** $parm Process **";
}

It would probably be easier/more robust to use objects to accomplish this task.

$dnbLog = new TrxLogObj("DNB Process");
$otherLog = new TrxLogObj("Other Process");

$dnbLog->start();
$dnbLog->log("Something interesting happened here");

$otherLog->start();
# Do your process stuff here...
$otherLog->stop();

$dnbLog->stop();

 

This way each object is it's own logging session, with it's own instance variables, one of which is a text string indicating the name of the session, the others are the message array, the filehandle and start and stop times.

This allows you great freedom to start and stop multiple logging sessions, and insulates you from having to use specific keys in your scripts to reference those sessions.

It's relatively trivial to convert your code to the object format, but also might be easier to use one of the many pre-existing Log classes and adapt for your use, just search CPAN.

-FilmJ