Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

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

by haukex (Archbishop)
on Jun 07, 2017 at 10:36 UTC ( [id://1192259]=note: print w/replies, xml ) Need Help??


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

Hmm, gnomon is a neat little tool. Just for the heck of it, here's a really simple Perl version (works on Linux, probably not on Windows):

#!/usr/bin/env perl use warnings; use strict; use IO::Select; use Time::HiRes qw/ gettimeofday tv_interval /; my $sel = IO::Select->new(\*STDIN); $|=1; my $t0 = [gettimeofday]; while (1) { my $line; if ($sel->can_read(.1)) { defined( $line = <STDIN> ) or last } printf "%7.3f | %s", tv_interval($t0), $line//"\r"; $t0 = [gettimeofday] if defined $line; }
  • Comment on Re: How to Prepending a timestamp to each line of output from a command
  • Download Code

Replies are listed 'Best First'.
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

    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?

      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://1192259]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (2)
As of 2024-04-16 21:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found