Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^2: How to Prepending a timestamp to each line of output from a command

by jnarayan81 (Sexton)
on Jun 07, 2017 at 13:08 UTC ( [id://1192270]=note: print w/replies, xml ) Need Help??


in reply to Re: How to Prepending a timestamp to each line of output from a command
in thread How to Prepending a timestamp to each line of output from a command

This code is magic. I am trying to implement in my program but fail due to little understanding.

#!/usr/bin/env perl use warnings; use strict; use IO::Select; use Time::HiRes qw/ gettimeofday tv_interval /; my @abc = ("a", "b", "c", "d", "e"); for my $v(@abc) { my $sel = IO::Select->new($v); $|=1; my $t0 = [gettimeofday]; while (1) { my $line; if ($sel->can_read(.1)) { defined( $line = $v ) or last } printf "%7.3f | %s", tv_interval($t0), $line//"\r"; $t0 = [gettimeofday] if defined $line; } sleep 1; }

Is this possible to make it a subroutine and call it by default in all STDOUT?

  • Comment on Re^2: How to Prepending a timestamp to each line of output from a command
  • Download Code

Replies are listed 'Best First'.
Re^3: How to Prepending a timestamp to each line of output from a command
by haukex (Archbishop) on Jun 07, 2017 at 13:50 UTC

    I'm sorry, but I don't fully understand what you're trying to do. It would be best if you could explain more, with a runnable SSCCE, example input and expected output. Otherwise I am left guessing, for example, based on your code it's not clear to me what @abc contains. Is this a list of commands you want to run, or is it the output of another command? If it's the latter, note that the other monks have already shown you how to handle that, like Discipulus's post here. How are you running the external command, from the command line with the input piped to the Perl script like in the gnomon example (which my code handles), or do you want to invoke the external command from inside the Perl script? If it's the latter, note that my code might not be the best way to do this, and you might need to switch to a module like IPC::Run.

    Is this possible to make it a subroutine and call it by default in all STDOUT?

    Are you asking how you can modify your own Perl script's output to prefix it with the current time (like a log), instead of another program's output?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1192270]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-19 14:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found