Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to redirect fprintf output to C socket?

Tags:

c

printf

sockets

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 like to redirect this log to a socket.

How can this be done?

like image 878
Behrouz.M Avatar asked Dec 21 '22 03:12

Behrouz.M


2 Answers

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.

like image 154
Jonathan Leffler Avatar answered Dec 28 '22 08:12

Jonathan Leffler


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!

like image 24
Kaslai Avatar answered Dec 28 '22 06:12

Kaslai