in reply to How can I catch a Server message displayed in Sybase::CTlib module

You could redirect STDERR for the time you make calls into that module.

package Foo; sub foo { warn "blorf!" } 1;
#!/usr/bin/perl use Foo; use strict; use warnings; sub call (&); call { Foo::foo(); }; if ($@) { warn "foo reported: '$@'\n"; } ## subs # sub call (&) { my $coderef = shift; my $message; open my $fh , '>&', \*STDERR; close STDERR; open STDERR, '>', \$message; $coderef->(); open STDERR, ">&", $fh; $@ = $message; } __END__ foo reported: 'blorf! at Foo.pm line 3. '

That way all you have to do is wrap the places where you call into Sybase::CTlib into a block and dispatch that to the call() function.

Replies are listed 'Best First'.
Re^2: How can I catch a Server message displayed in Sybase::CTlib module
by goutam_monk (Initiate) on May 07, 2009 at 12:15 UTC
    Hi shmem,

    Thank you very much.
    I have executed the above code, and I have got the expected result.

    I still need some help. Sorry if it is silly.

    Still, my question is,
    where can I send the below err statements into log file. I have tried them and was not able to throw into log file.

    ct_callback(CS_CLIENTMSG_CB, \&msg_cb);
    ct_callback(CS_SERVERMSG_CB, "srv_cb");

    sub msg_cb {
    my($layer, $origin, $severity, $number, $msg, $osmsg, $dbh) = @_;

    printf STDERR "\nOpen Client Message: (In msg_cb)\n";
    printf STDERR "Message number: LAYER = (%ld) ORIGIN = (%ld) ",
    $layer, $origin;
    printf STDERR "SEVERITY = (%ld) NUMBER = (%ld)\n",
    $severity, $number;
    printf STDERR "Message String: %s\n", $msg;
    if (defined($osmsg)) {
    printf STDERR "Operating System Error: %s\n", $osmsg;
    }
    CS_SUCCEED;
    }

    sub srv_cb {
    my($dbh, $number, $severity, $state, $line, $server,
    $proc, $msg) = @_;

    # If $dbh is defined, then you can set or check attributes
    # in the callback, which can be tested in the main body
    # of the code.

    printf STDERR "\nServer message: (In srv_cb)\n";
    printf STDERR "Message number: %ld, Severity %ld, ",
    $number, $severity;

    printf STDERR "State %ld, Line %ld\n", $state, $line;

    if (defined($server)) {
    printf STDERR "Server '%s'\n", $server;
    }

    if (defined($proc)) {
    printf STDERR " Procedure '%s'\n", $proc;
    }

    printf STDERR "Message String: %s\n", $msg; CS_SUCCEED;
    }

    please, help required...

    Thanks in advance!!!

      open file, print to filehandle you opened, close filehandle