pgduke65 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I am wanting to write a program that creates a log entry for a process start and end. What I would like is to have 1 entry per process per execution.

As an example:

Logged Job Start End

10/22/14 13:00:00 J1 10/22/14 13:00:00 10/22/14 13:05:00

10/23/14 13:00:00 J1 10/23/14 13:00:00 10/23/14 13:05:00

The question, I have is, If I write the start time at process start, how can I write the end time to the entry? Another process can write to the for its start. As a result, I can't just assume last entry in the file

Any advice is appreciated.

Replies are listed 'Best First'.
Re: Log Write position
by Loops (Curate) on Oct 22, 2014 at 05:21 UTC

    In order to match start and end lines in the log for interleaving jobs, you could include the process number in the log message. This is held in the perl variable $$.

Re: Log Write position
by McA (Priest) on Oct 22, 2014 at 06:42 UTC

    Hi,

    what you want to accomplish is a little hard and uncommon by the way. My advice: Write a log line with the start time, write later one with the end time. Identify the log lines by process number or other identifier like 'Loops' said before. If you want to see start and end time on one line you could remember the start time and print that together with the end time on the completion event. In this case you get an entry for the start event and an entry for the end event showing start and end time to see the duration (you could even calculate the duration for the reader of the log).

    Another approach whould be to log to a database row. But also there you need an unique identifier for the process. Be aware that process numbers get repeated at some time.

    Regards
    McA

Re: Log Write position
by perlron (Pilgrim) on Oct 22, 2014 at 07:45 UTC
    An option is to use perl's nested hash. The keys would be your start time / end time and other important tokens, with their values assigned according to the logic in your perl script. this is what first got me hooked to perl.:D
    1 #!/usr/bin/perl 2 3 %process_hash = ( 4 'pid_1' => { 5 'start_time' => '00:00:00', 6 'end_time' => '12:00:00' 7 }, 8 'pid_2' => { 9 'start_time' => '00:00:01', 10 'end_time' => '12:00:02' 11 } 12 ); 13 14 print $process_hash{'pid_2'}->{'start_time'};
    please check perlvar,perldata and perlreftut for further info.
Re: Log Write position
by clueless newbie (Curate) on Oct 22, 2014 at 11:31 UTC

    Use a database.

Re: Log Write position
by pgduke65 (Acolyte) on Oct 22, 2014 at 15:05 UTC

    Thanks for the feedback. I appreciate the help.