How to redirect fprintf output to C socket?

后端 未结 3 1215
慢半拍i
慢半拍i 2021-01-13 20:17

How can I redirect fprintf output to a socket?

I am editing source code which has a lot of fprintf calls. It logs to a file. But I would li

相关标签:
3条回答
  • 2021-01-13 20:43

    On Unix-like systems, open the socket (getting a file descriptor).

    Map socket to stderr

    If you've not yet used stderr, you can simply use dup2() to make the socket into the output channel. You may be able to do that even if you have sent some data to the original stderr. The suggestion in the comment about using fflush() may be relevant, though stderr is usually unbuffered rather than fully buffered (so there is not usually anything to flush).

    The freopen() function can be used to change the output of a stream such as stderr (in particular). Hmmm, could you use freopen() to map stderr to /dev/fd/N where N is the file descriptor of the socket? Yes, you probably could.

    Map socket to other stream

    Re-reading your question - I don't see stderr in it. So the fdopen() function creates a stream from a file descriptor. So if you can specify the stream for the logging to write to, you're home. If you can't specify it but can see it, then the freopen() trick above should work here too.

    0 讨论(0)
  • 2021-01-13 20:50

    I realize this isn't your question, but you might consider using a tool like netcat instead of messing with the code (which doesn't seem like a trivial change).

    You can do something like this (updated to use a log fifo):

    mkfifo /path/to/log
    
    nc <ip> <port> < /path/to/log
    

    http://netcat.sourceforge.net/

    0 讨论(0)
  • 2021-01-13 20:57

    There is no such thing as a "C socket" but you are probably referring to one of two things (WinSock or libsocket). Either way, the basic method I'd use would be to use a #define and a simple function, similar to this:

    #define fprintf(a,b,...) fprintfsock(a,b,__VA_ARGS__)
    
    void fprintfsock( SOCKET s, const char* f, ... )
    {
        va_list a;
        va_start( a, f );
        int l = vsnprintf( 0, 0, f, a );
        char* buf = (char*) malloc( l + 1 );
        va_start( a, f );
        vsnprintf( buf, l, f, a );
        send( s, buf, l, 0 );
        free( buf );
    }
    

    That will work in either, except with libsocket you should typedef or use int for SOCKET. Make sure you #undef fprintf after you're done logging to the socket!

    0 讨论(0)
提交回复
热议问题