Problem in appending the correct log


 
Thread Tools Search this Thread
Operating Systems Solaris Problem in appending the correct log
# 1  
Old 05-26-2009
Problem in appending the correct log

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

Code:
#!/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 **************************

Last edited by vidyadhar85; 05-26-2009 at 03:31 AM.. Reason: code tag added
# 2  
Old 05-26-2009
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.

Code:
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.

Code:
$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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Appending 2 text files - Problem in windows

Hello, I have text files to append and am able to do with cat. cat file1 file2 >file3 and file3 works fine in UNIX (checked with vi and it looks fine) but when i open the same file in windows I see 2nd file appended as a single-line. In other words, all the lines of 2nd file appended to... (2 Replies)
Discussion started by: magnus29
2 Replies

2. Shell Programming and Scripting

sed appending problem

i have a number of java files containing eg: --------------myfile.java-------------- package zip.fun.myfiles; import java.* import something..; import sdfdfdsa; ... ... -------------------------------------------- Now I need to append / insert a line as follows: ... (10 Replies)
Discussion started by: linuxadmin
10 Replies

3. UNIX for Dummies Questions & Answers

Appending timestamp problem

Hi, I am trying to insert a timestamp after all the file names in a folder,after the timestamp is created in the filename the file size is becoming zero bytes. please tell me where I am doing it wrong. I have declared the variable in starting of my script. timestamp=`date... (1 Reply)
Discussion started by: shruthidwh
1 Replies

4. Solaris

Help with finding correct log files

Lets say i have files like the following format :- R0001.log R0002.log ... ... R00011.log upto R000n.log, there are also a lot of text files with different names now how can i find these files with in a range, i can do "ls R000*.log" and it will show me all the R000*.log files but what... (2 Replies)
Discussion started by: oopsalion
2 Replies

5. Shell Programming and Scripting

Problem with appending a file!

Hi All, I have a script which i use in office. This script is used to log the work in users name. For that in the script itself I have added that the infomration should append to the logfile everytime the script is run. LOGFILE=/data/log/request1.txt All these days it was... (3 Replies)
Discussion started by: smarty86
3 Replies

6. Programming

Problem in getting the correct number from the string.

I have string named texts which consist of section label “BOOK-SEC-“. Section starts from 1 to n, where n is a number. For this example conside the value of n is 9. That is, the string variable looks like “BOOK-SEC-1... (2 Replies)
Discussion started by: SamRoj
2 Replies

7. UNIX for Advanced & Expert Users

Correct format in a log file

I'm trying to read the output of a .sql script (simple insert and commit oracle pl/slq script) to a log file using a shell script. My problem is I end up with a log file that looks like this: sd12@phenix97:/detain/sd12/logs > cat 20071205_detain_20071206.log 12320496 rows created. Commit... (11 Replies)
Discussion started by: sd12
11 Replies

8. Shell Programming and Scripting

Can someone kindly help with appending problem!!!

Hi guys,i urgently need help in this problem,i need to append one sentence to another line when it meet a certain word. For example: root: root time_last_login = 1191232080 root tty_last_login = /dev/vty0 root host_last_login = rep1nim root unsuccessful_login_count = 0 root... (10 Replies)
Discussion started by: cyberray
10 Replies

9. Shell Programming and Scripting

AWK scripting problem - appending values

ABC:10:A1:ABCA110 ABC:10:A1:ABCA110 ABC:20:A1:ABCA120 DEF:20:D1:DEFD120 GHI:30:G1:GHIG130 GHI:40:G1:GHIG140 JKL:30:J1:JKLJ130 MNO:10:M1:MNOM110 What I'm trying to do is look through a file that consists of four columns (as above). As you can see there are duplicates in the file, i.e.... (2 Replies)
Discussion started by: rowntree
2 Replies

10. UNIX for Dummies Questions & Answers

will reinstalling correct my problem??

about 2 months ago i installed mandrake linux 8.0 however due to my current internet setup (dial up upsteam, and cable downstream) i was never able to get it configured properly so i gave up untill i could change to an all cable isp...well that day is tomorrow!!! what i would like to know is... (1 Reply)
Discussion started by: justchillin
1 Replies
Login or Register to Ask a Question