1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2025-03-03 10:15:45 +00:00

Add debug mode to daemon.

This commit is contained in:
Josh Elsasser 2007-04-26 07:03:36 +00:00
parent 9aaa36a431
commit 3ef81c7977
4 changed files with 54 additions and 15 deletions

View file

@ -49,7 +49,7 @@
#include "version.h" #include "version.h"
static void usage ( const char *, ... ); static void usage ( const char *, ... );
static int readargs ( int, char ** ); static void readargs ( int, char **, int *, int * );
static int trylocksock ( void ); static int trylocksock ( void );
static int getlock ( const char * ); static int getlock ( const char * );
static int getsock ( const char * ); static int getsock ( const char * );
@ -66,10 +66,10 @@ int
main( int argc, char ** argv ) main( int argc, char ** argv )
{ {
struct event_base * evbase; struct event_base * evbase;
int nofork, sockfd; int nofork, debug, sockfd;
setmyname( argv[0] ); setmyname( argv[0] );
nofork = readargs( argc, argv ); readargs( argc, argv, &nofork, &debug );
if( !nofork ) if( !nofork )
{ {
@ -92,6 +92,7 @@ main( int argc, char ** argv )
setupsigs( evbase ); setupsigs( evbase );
torrent_init( evbase ); torrent_init( evbase );
server_init( evbase ); server_init( evbase );
server_debug( debug );
server_listen( sockfd ); server_listen( sockfd );
event_dispatch(); event_dispatch();
@ -115,11 +116,12 @@ usage( const char * msg, ... )
} }
printf( printf(
"usage: %s [-fh]\n" "usage: %s [-dfh]\n"
"\n" "\n"
"Transmission %s (r%d) http://transmission.m0k.org/\n" "Transmission %s (r%d) http://transmission.m0k.org/\n"
"A free, lightweight BitTorrent client with a simple, intuitive interface\n" "A free, lightweight BitTorrent client with a simple, intuitive interface\n"
"\n" "\n"
" -d --debug Print data send and received, implies -f\n"
" -f --foreground Run in the foreground and log to stderr\n" " -f --foreground Run in the foreground and log to stderr\n"
" -h --help Display this message and exit\n" " -h --help Display this message and exit\n"
"\n" "\n"
@ -128,34 +130,37 @@ usage( const char * msg, ... )
exit( 0 ); exit( 0 );
} }
int void
readargs( int argc, char ** argv ) readargs( int argc, char ** argv, int * nofork, int * debug )
{ {
char optstr[] = "fh"; char optstr[] = "dfh";
struct option longopts[] = struct option longopts[] =
{ {
{ "debug", no_argument, NULL, 'd' },
{ "foreground", no_argument, NULL, 'f' }, { "foreground", no_argument, NULL, 'f' },
{ "help", no_argument, NULL, 'h' }, { "help", no_argument, NULL, 'h' },
{ NULL, 0, NULL, 0 } { NULL, 0, NULL, 0 }
}; };
int opt, nofork; int opt;
nofork = 0; *nofork = 0;
*debug = 0;
while( 0 <= ( opt = getopt_long( argc, argv, optstr, longopts, NULL ) ) ) while( 0 <= ( opt = getopt_long( argc, argv, optstr, longopts, NULL ) ) )
{ {
switch( opt ) switch( opt )
{ {
case 'd':
*debug = 1;
/* FALLTHROUGH */
case 'f': case 'f':
nofork = 1; *nofork = 1;
break; break;
default: default:
usage( NULL ); usage( NULL );
break; break;
} }
} }
return nofork;
} }
int int

View file

@ -84,6 +84,7 @@ INTCMP_FUNC( clientcmp, client, ev )
static struct event_base * gl_base = NULL; static struct event_base * gl_base = NULL;
static struct ipc_funcs * gl_tree = NULL; static struct ipc_funcs * gl_tree = NULL;
static int gl_debug = 0;
static int gl_exiting = 0; static int gl_exiting = 0;
static struct allclients gl_clients = RB_INITIALIZER( &gl_clients ); static struct allclients gl_clients = RB_INITIALIZER( &gl_clients );
@ -137,6 +138,12 @@ server_init( struct event_base * base )
return 0; return 0;
} }
void
server_debug( int enable )
{
gl_debug = enable;
}
int int
server_listen( int fd ) server_listen( int fd )
{ {
@ -233,6 +240,11 @@ newclient( int fd, short event UNUSED, void * arg )
old = RB_INSERT( allclients, &gl_clients, client ); old = RB_INSERT( allclients, &gl_clients, client );
assert( NULL == old ); assert( NULL == old );
if( gl_debug )
{
printf( "*** new client %i\n", clfd );
}
bufferevent_enable( clev, EV_READ ); bufferevent_enable( clev, EV_READ );
buf = ipc_mkvers( &buflen ); buf = ipc_mkvers( &buflen );
if( 0 > queuemsg( client, buf, buflen ) ) if( 0 > queuemsg( client, buf, buflen ) )
@ -286,6 +298,10 @@ byebye( struct bufferevent * ev, short what, void * arg UNUSED )
RB_REMOVE( allclients, &gl_clients, client ); RB_REMOVE( allclients, &gl_clients, client );
bufferevent_free( ev ); bufferevent_free( ev );
close( client->fd ); close( client->fd );
if( gl_debug )
{
printf( "*** client %i went bye-bye\n", client->fd );
}
free( client ); free( client );
} }
@ -302,6 +318,13 @@ doread( struct bufferevent * ev, void * arg )
buf = EVBUFFER_DATA( EVBUFFER_INPUT( ev ) ); buf = EVBUFFER_DATA( EVBUFFER_INPUT( ev ) );
len = EVBUFFER_LENGTH( EVBUFFER_INPUT( ev ) ); len = EVBUFFER_LENGTH( EVBUFFER_INPUT( ev ) );
if( gl_debug )
{
printf( "<<< %zu bytes from client %i: ", len, client->fd );
fwrite( buf, 1, len, stdout );
putc( '\n', stdout );
}
if( IPC_MIN_MSG_LEN > len ) if( IPC_MIN_MSG_LEN > len )
{ {
return; return;
@ -349,6 +372,13 @@ queuemsg( struct client * client, uint8_t * buf, size_t buflen )
return -1; return -1;
} }
if( gl_debug )
{
printf( ">>> %zu bytes to client %i: ", buflen, client->fd );
fwrite( buf, 1, buflen, stdout );
putc( '\n', stdout );
}
if( 0 > bufferevent_write( client->ev, buf, buflen ) ) if( 0 > bufferevent_write( client->ev, buf, buflen ) )
{ {
errnomsg( "failed to buffer %zd bytes of data for write", buflen ); errnomsg( "failed to buffer %zd bytes of data for write", buflen );

View file

@ -27,7 +27,8 @@
struct event_base; struct event_base;
int server_init( struct event_base * ); int server_init( struct event_base * );
int server_listen( int ); void server_debug( int );
int server_listen( int );
#endif /* TR_DAEMON_SERVER_H */ #endif /* TR_DAEMON_SERVER_H */

View file

@ -30,7 +30,7 @@
.Nm transmission-daemon .Nm transmission-daemon
.Fl h .Fl h
.Nm .Nm
.Op Fl f .Op Fl df
.Sh DESCRIPTION .Sh DESCRIPTION
The The
.Nm .Nm
@ -38,6 +38,9 @@ program cures baldness, obesity and gout.
.Pp .Pp
The options are as follows: The options are as follows:
.Bl -tag -width Ds .Bl -tag -width Ds
.It Fl d Fl -debug
Print data send and received to and from clients to stdout, also implies
.Fl f .
.It Fl f Fl -foreground .It Fl f Fl -foreground
Run in the foreground and print errors to stderr instead of forking Run in the foreground and print errors to stderr instead of forking
and logging errors with syslog. and logging errors with syslog.