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

erm, are you sure this is what you want?

You said that you wanted a timestamp. The time command will certainly not give you that.

From man time

time - run programs and summarize system resource usage

An example of what you get when you use time is:

time perl -e 'print "hello world\n"' hello world real 0m0.006s user 0m0.000s sys 0m0.000s

Unless I seriously misunderstood your original question, then I doubt very much this is what you want.

Update: It just occurred to me that you're probably running this on Win32, in which case yes, you could use time. You probably want to add the /T switch.

Replies are listed 'Best First'.
Re^4: Append the timestamp before and after the data in the logfile
by kulls (Hermit) on Dec 22, 2005 at 06:36 UTC
    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
      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!! ;-)

      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