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

I have redirected STDOUT to print to a log file. I make system calls, which return with text info, and want to prefix this with scalar(localtime()). Can I use the STDOUT format to somehow add this to the text?

Replies are listed 'Best First'.
Re: formatting STDOUT
by salva (Canon) on May 31, 2005 at 14:16 UTC
    select STDOUT; $|=1; unless (open STDOUT, '|-') { eval { while(<>) { print scalar(localtime), $_; } }; exit(0); }
    it's not the most efficient aproach, but it's bullet proof. It will catch any output, generated from perl, from C libraries or even from external commands called via system.
      Spot on. Thanks Salva!
      Hi, this code looks interesting, but I can't seem to figure out how to make it work. Could you please post a simple working example using it? Thanks.

      I'm not really a human, but I play one on earth. flash japh
        well, it's very simple to use, after it is called on a perl script, every line written to STDOUT will get the datestamp prepended.

        for instance, create a module like:

        package MyApp::Logger; select STDOUT; $|=1; unless (open STDOUT, '|-') { eval { while(<>) { print scalar(localtime), ' ', $_; } }; exit(0); } 1;
        and then from your script, just use the module, and then print whatever you want:
        #!/usr/bin/perl use MyApp::Logger; print "foo\nbar\n";
Re: formatting STDOUT
by Fletch (Bishop) on May 31, 2005 at 13:14 UTC
    open( CMD, "mycmd arguments wubba |" ) or die "Can't open pipe from my +cmd: $!\n"; while( <CMD> ) { print scalar localtime, ": ", $_; } close( CMD );

    If you need fancier you might want to look at IPC::Run or the like.

    --
    We're looking for people in ATL

      I call an ARSPerl function e.g. login, which only returns a structure, but opens its own stream to STDOUT. I have redirected STDOUT to a log file which i also write to using print statements in the format e.g.

      Sun May 29 11:15:46 2005 Successfully generated query.

      but my redirect of STDOUT does not allow formatting. e.g. my line of perl code:

      ($control = ars_Login($arsserver, $arsuser, $arspass))
      produces this text, sent directly to STDOUT
      [ARS.xs 201] XS_ARS_ars_Login : ars_Login(arsprod, hp_servicecall, [nu +ll]) [ARS.xs 292] XS_ARS_ars_Login : finished.

      I want to enforce formatting rules so any print statement, even ones from the library call, have to obey i.e. including datetime.

      Janitored by holli - fixed formatting

Re: formatting STDOUT
by thcsoft (Monk) on May 31, 2005 at 11:20 UTC
    try
    use IO::Scalar;
    language is a virus from outer space.
Re: formatting STDOUT
by lupey (Monk) on May 31, 2005 at 11:43 UTC
    I'm not sure what you mean by 'STDOUT format' and maybe I'm missing something, but will a simple concatenation operator work for you?

    my $output = scalar(localtime()).$system_call; print $output,"\n";

    lupey

      Im running a binary which returns text - without me being able to do any handling e.g. concatenating strings. I want to change the default formatting to include a timestamp.
        You can capture the output from an executable file by using backticks ``. Then, you can do whatever you want to the output: add a prefix, a suffix, or change it completely with regular expressions. In the following code, I'm capturing the output of echo and prefixing the date. Replace echo with the binary of your choice.
        use strict; my $system_call = `echo hello world`; my $output = scalar(localtime())." $system_call"; print STDOUT $output,"\n"; # output # Tue May 31 09:04:22 2005 hello world

        If this isn't what you are looking for, please be more explicit in your posts.

        lupey