output-monitoring module

Discussion in 'Perl Misc' started by Henry Townsend, Apr 6, 2005.

  1. Please forgive me for asking here, I have done some searches and come up
    empty but I still think the module I need may have been written and want
    to know for sure before I go trying to roll my own.

    I'm thinking of writing a module which would monitor the output of any
    script which "use"d it looking for errors. E.g. something like this

    use Output::Monitor STDOUT => qr/re1/, STDERR => qr/re2/;

    This would - perhaps by using tied filehandles - watch all data flowing
    to stdout and stderr, apply the specified REs to each line, and convert
    the exit status to nonzero if any matches occur. The data would still be
    delivered to the same place, just checked along the way.

    The idea is to replace a common technique where I work, which is to
    write build scripts which divert their own output to a log internally,
    then read that logfile to discover any unflagged errors. This makes it
    hard for external tools to redirect output as they choose, requires
    intimate knowledge of the script to know where it's writing its logfile
    to, etc. What I'd hope to achieve with the above is transparency, i.e.
    by simply including the module within a script it would be converted
    from unchecked to checked, without the script having to change anything
    else while the same data as before flows to stdout and stderr.

    Before anyone says "just check exit status" - we do but we're stuck with
    certain underlying build tools which have unreliable status so we must
    parse output as a belt-and-suspenders plan.

    Does anyone know of such a module or something close to use as a
    starting point?

    --
    Henry Townsend
     
    Henry Townsend, Apr 6, 2005
    #1
    1. Advertising

  2. Henry Townsend

    J. Gleixner Guest

    Henry Townsend wrote:

    > I'm thinking of writing a module which would monitor the output of any
    > script which "use"d it looking for errors. E.g. something like this
    >
    > use Output::Monitor STDOUT => qr/re1/, STDERR => qr/re2/;
    >
    > This would - perhaps by using tied filehandles - watch all data flowing
    > to stdout and stderr, apply the specified REs to each line, and convert
    > the exit status to nonzero if any matches occur. The data would still be
    > delivered to the same place, just checked along the way.


    > Does anyone know of such a module or something close to use as a
    > starting point?


    Filter::Handle should help.


    http://search.cpan.org/~btrott/Filter-Handle-0.03/Handle.pm
     
    J. Gleixner, Apr 7, 2005
    #2
    1. Advertising

  3. Henry Townsend

    Anno Siegel Guest

    Henry Townsend <> wrote in comp.lang.perl.misc:
    > Please forgive me for asking here, I have done some searches and come up
    > empty but I still think the module I need may have been written and want
    > to know for sure before I go trying to roll my own.
    >
    > I'm thinking of writing a module which would monitor the output of any
    > script which "use"d it looking for errors. E.g. something like this
    >
    > use Output::Monitor STDOUT => qr/re1/, STDERR => qr/re2/;
    >
    > This would - perhaps by using tied filehandles - watch all data flowing
    > to stdout and stderr, apply the specified REs to each line, and convert
    > the exit status to nonzero if any matches occur. The data would still be
    > delivered to the same place, just checked along the way.


    You can do that using the standard module Tie::Handle:

    warn "blubb"; # processed normally
    tie *STDERR, 'Monitor', '>&STDERR';
    warn "glugg"; # prefixed with "monitored: "
    exit;

    package Monitor;
    use Tie::Handle;
    use base "Tie::StdHandle";

    sub WRITE {
    my $ob = shift;
    my ( $scalar, $length) = @_;
    $ob->SUPER::WRITE(
    "monitored: $scalar", length( "monitored: ") + $length
    );
    }

    Note however that the ability of tying STDERR is new with Perl 5.8.1.
    If you need it to work with an earlier version, use $SIG{ __DIE_} and
    $SIG{ __WARN__}.

    [snip]

    Anno
     
    Anno Siegel, Apr 7, 2005
    #3
  4. Henry Townsend

    Anno Siegel Guest

    Henry Townsend <> wrote in comp.lang.perl.misc:
    > Anno Siegel wrote:
    > > Henry Townsend <> wrote in comp.lang.perl.misc:


    > >>This would - perhaps by using tied filehandles - watch all data flowing
    > >>to stdout and stderr, apply the specified REs to each line, and convert
    > >>the exit status to nonzero if any matches occur. The data would still be
    > >>delivered to the same place, just checked along the way.

    >
    > > You can do that using the standard module Tie::Handle:


    [snip code]

    > Thanks. Unfortunately, as I should have made clearer, because I'm
    > monitoring build scripts I need to inspect output flowing from
    > subprocesses as well...


    Sure, that's a show-stopper. You need the Perl runtime system for
    tied filehandles.

    Anno
     
    Anno Siegel, Apr 7, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wally

    Monitoring windows service

    Wally, Sep 30, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    4,925
    James D. Marshall
    Oct 3, 2004
  2. Carl Prothman [MVP]
    Replies:
    1
    Views:
    4,286
    Ali_ggl
    Mar 17, 2008
  3. KP
    Replies:
    0
    Views:
    320
  4. Maric Michaud
    Replies:
    0
    Views:
    7,221
    Maric Michaud
    Jun 24, 2006
  5. Caleb Marcus
    Replies:
    0
    Views:
    302
    Caleb Marcus
    Dec 13, 2007
Loading...

Share This Page