in reply to Re^3: Append the timestamp before and after the data in the logfile
in thread Append the timestamp before and after the data in the logfile

hi,
yes.This too also i needed. I updated my question too. But how can i append this information to the log file.As of now ,i just replace the 'date' with 'time' and start working in that.
Ok.Shall i do like this,
%usr>(perl test.pl; time perl test.pl;) > test.log
this is correct ?

-kulls
  • Comment on Re^4: Append the timestamp before and after the data in the logfile
  • Download Code

Replies are listed 'Best First'.
Re^5: Append the timestamp before and after the data in the logfile
by McDarren (Abbot) on Dec 22, 2005 at 07:02 UTC
    Honestly, I think you are going about this the wrong way. You really should be looking at something like File::Log

    Failing that, you could do something like this:

    #!/usr/bin/perl -w use strict; use POSIX qw(strftime); my $start = time(); print "Started at ", (strftime "%a %b %e %H:%M:%S %Y", localtime($star +t)), "\n"; for (1 .. 5) { print "Logging some stuff...\n"; sleep 1; } my $end = time(); print "Ended at ", (strftime "%a %b %e %H:%M:%S %Y", localtime($end)), + "\n"; print "Total run time:", $end-$start, " seconds\n";

    Which when run, gives you:

    Started at Thu Dec 22 14:59:20 2005 Logging some stuff... Logging some stuff... Logging some stuff... Logging some stuff... Logging some stuff... Ended at Thu Dec 22 14:59:25 2005 Total run time:5 seconds

    Then you simply redirect that to your logfile in the same way as you originally did.

      print "Started at ", (strftime "%a %b %e %H:%M:%S %Y", localtime($star +t)), "\n";

      Also

      scalar localtime

      (See what localtime does in scalar context) - Oh, this bad habit of Perl having the right dwimmery built in!! ;-)

Re^5: Append the timestamp before and after the data in the logfile
by blazar (Canon) on Dec 27, 2005 at 14:07 UTC
    There's an important difference. AFAIK 'time' is not a program, but a shell command. It prints to STDERR, so you can't capture it with a plain '>' redirection, but even a '2>' won't do, since you have to capture the output of the whole pipeline:
    $ time perl -le 'print "foo"; sleep 1' >/dev/null real 0m1.017s user 0m0.003s sys 0m0.002s $ time perl -le 'print "foo"; sleep 1' 2>/dev/null foo real 0m1.012s user 0m0.003s sys 0m0.004s $ (time perl -le 'print "foo"; sleep 1') >/dev/null real 0m1.012s user 0m0.002s sys 0m0.004s $ (time perl -le 'print "foo"; sleep 1') 2>/dev/null foo $ { time perl -le 'print "foo"; sleep 1'; } 2>/dev/null foo