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

I've a c library thats been wrapped by SWIG. This is very handy in allowing me to make use of these routines in my scripts but I think I've come across a problem.

The funtion (swig wrapped remember) can send information to STDOUT. I want to take this and redirect STDOUT to various sockets. To do this I've tried a number of differeent methods including capturing STDOUT using IO::Scalar and tie, binding STDOUT to the socket using fileno and open STDOUT, ">&=$fd"; and so forth all to no avail.

I think I determined that the problem is that SWIG isn't actually piping its STDOUT back into perl's STDOUT. So I can do anything I like STDOUT in perl and it will have no effect because these environmental changes are not being inherited by the function/c API.

Does this sound likely? If so can anyone think of a way to get around this that doesn't involve rewriting the API?

Chris

Replies are listed 'Best First'.
(tye)Re: STDOUT and SWIG
by tye (Sage) on Aug 21, 2001 at 00:01 UTC

    None of what you tried will work for C code. You need to dup() the socket onto file descriptor 1 (which is C's stdout) via:

    open(STDOUT,">&$fd") or die ...
    you were close in one case but the "=" in your code causes STDOUT to be fdopen()ed to the existing file descriptor so that the socket does not get dup()ed.

            - tye (but my friends call me "Tye")
      tye,

      That was a problem on my part and I am now capturing some small percentage of the data. I'm not sure why I'm not getting all of it unless its overloading the buffers (I've increased sendspace and recvspace to 128k via sysctl - but that still might not be enough - either way I'm still losing the same amount of data).

      What bothers be is that I would think that capturing STDOUT via IO:Scalar should work and I wish I could figure out why its not.

      The partial failure of one method and the complete failure of the other is really annoying me.

        IO::Scalar works by telling Perl that its STDOUT is not an ordinary file handle and so it has to do special processing when Perl's output functions like print try to write to it. C knows nothing of Perl's STDOUT and doesn't use Perl's output functions like print, so IO::Scalar has absolutely no affect on C code (nor on output written by child processes, etc.).

        The order of the layers goes something like this: print, STDOUT, Perl's informal I/O abstraction layer, C's stdio (FILE handles), operating system I/O functions (file descriptors). The C code of SWIG is probably starting at the "C's stdio" layer but could also just be going to the Unix I/O layer of write() and file descriptors. In any case, the C code is not going to be affected by any of the layers above that.

        That is, unless it went out of its way to try to use Perl's I/O routines. I doubt doing that would be easy.

                - tye (but my friends call me "Tye")
Re: STDOUT and SWIG
by Cine (Friar) on Aug 20, 2001 at 23:25 UTC
    Sounds like the API reopens the original STDOUT, in that case you should rewrite the API.

    T I M T O W T D I