From a0f941e7ae0e6935152e5ce42bfb8b45d224c25d Mon Sep 17 00:00:00 2001 From: Tom Smeding Date: Mon, 13 Jul 2020 19:53:05 +0200 Subject: weechat: Abstract debug file into separate module --- weechat/debug.c | 43 +++++++++++++++++++++++++++++++++ weechat/debug.h | 9 +++++++ weechat/net.c | 48 ++++++++++++++++++------------------- weechat/tomsg.c | 73 ++++++++++++++++++++++++++------------------------------- 4 files changed, 108 insertions(+), 65 deletions(-) create mode 100644 weechat/debug.c create mode 100644 weechat/debug.h diff --git a/weechat/debug.c b/weechat/debug.c new file mode 100644 index 0000000..969733c --- /dev/null +++ b/weechat/debug.c @@ -0,0 +1,43 @@ +#include +#include +#include +#include +#include "debug.h" + + +static FILE *g_dfile; + +void debug_init(void) { + const char *home = getenv("HOME"); + if (!home) home = "/tmp"; + + const char *suffix = "/Desktop/debugf.txt"; + + char *fname = malloc(strlen(home) + strlen(suffix) + 1); + if (!fname) return; // okay, fine, no debug file + + g_dfile = fopen(fname, "a"); + if (!g_dfile) { + g_dfile = NULL; + return; // fine! + } + + // Disable buffering + setvbuf(g_dfile, NULL, _IONBF, 0); + + fprintf(g_dfile, "--------\n"); +} + +void debug_deinit(void) { + if (g_dfile) fclose(g_dfile); +} + +__attribute__((format (printf, 1, 2))) +void debugf(const char *restrict format, ...) { + if (!g_dfile) return; + + va_list ap; + va_start(ap, format); + vfprintf(g_dfile, format, ap); + va_end(ap); +} diff --git a/weechat/debug.h b/weechat/debug.h new file mode 100644 index 0000000..95117a9 --- /dev/null +++ b/weechat/debug.h @@ -0,0 +1,9 @@ +#pragma once + + +void debug_init(void); + +void debug_deinit(void); + +__attribute__((format (printf, 1, 2))) +void debugf(const char *restrict format, ...); diff --git a/weechat/net.c b/weechat/net.c index 80d417b..897655a 100644 --- a/weechat/net.c +++ b/weechat/net.c @@ -7,9 +7,7 @@ #include #include #include "net.h" - - -extern FILE *debugf; +#include "debug.h" struct store_item{ @@ -57,7 +55,7 @@ bool net_sendf(int fd,net_callback_t *callback,void *payload,const char *format, } assert(store); - fprintf(debugf,"net_sendf(%d,%p,\"%s\",...)\n",fd,callback,format); + debugf("net_sendf(%d,%p,\"%s\",...)\n",fd,callback,format); va_list ap,ap2; va_start(ap,format); @@ -98,7 +96,7 @@ void net_handle_recv(int fd,const char *msg){ i64 msglen=strlen(msg); const char *p=strchr(msg,' '); if(p==NULL){ - fprintf(debugf,"net_handle_recv: no space in message <%s>\n",msg); + debugf("net_handle_recv: no space in message <%s>\n",msg); return; } i64 taglen=p-msg; @@ -111,7 +109,7 @@ void net_handle_recv(int fd,const char *msg){ cb=history_callback; } else { if(taglen!=8){ - fprintf(debugf,"net_handle_recv: tag not length 8 <%s>\n",msg); + debugf("net_handle_recv: tag not length 8 <%s>\n",msg); return; } for(i64 i=0;i\n",msg); + debugf("net_handle_recv: no viable callback found <%s>\n",msg); return; } } @@ -140,7 +138,7 @@ void net_handle_recv(int fd,const char *msg){ cb(fd,(struct net_response){.type=NET_OK},payload); } else if(cmdlen==6&&memcmp(cmd,"number",6)==0){ if(*p=='\0'){ - fprintf(debugf,"net_handle_recv: no number argument <%s>\n",msg); + debugf("net_handle_recv: no number argument <%s>\n",msg); return; } const char *nump=p+1; @@ -150,7 +148,7 @@ void net_handle_recv(int fd,const char *msg){ const char *endp; res.number=strtol(nump,(char**)&endp,10); if(nump[0]=='\0'||*endp!='\0'){ - fprintf(debugf,"net_handle_recv: invalid number argument <%s>\n",msg); + debugf("net_handle_recv: invalid number argument <%s>\n",msg); return; } cb(fd,res,payload); @@ -178,14 +176,14 @@ void net_handle_recv(int fd,const char *msg){ } else if(cmdlen==4&&memcmp(cmd,"list",4)==0){ struct net_response res=(struct net_response){.type=NET_LIST}; if(*p=='\0'){ - fprintf(debugf,"net_handle_recv: no list count <%s>\n",msg); + debugf("net_handle_recv: no list count <%s>\n",msg); return; } const char *cursor=p+1; p=strchr(cursor,' '); res.nitems=strtol(cursor,NULL,10); if(res.nitems<=0){ - fprintf(debugf,"net_handle_recv: -- 0 items <%s>\n",msg); + debugf("net_handle_recv: -- 0 items <%s>\n",msg); res.nitems=0; res.items=NULL; cb(fd,res,payload); @@ -197,7 +195,7 @@ void net_handle_recv(int fd,const char *msg){ for(i64 i=0;i\n",msg); + debugf("net_handle_recv: short list <%s>\n",msg); return; } cursor++; @@ -207,7 +205,7 @@ void net_handle_recv(int fd,const char *msg){ assert(res.items[i]); memcpy(res.items[i],cursor,p-cursor); res.items[i][p-cursor]='\0'; - fprintf(debugf,"net_handle_recv: -- item \"%s\" <%s>\n",res.items[i],msg); + debugf("net_handle_recv: -- item \"%s\" <%s>\n",res.items[i],msg); cursor=p; } cb(fd,res,payload); @@ -217,7 +215,7 @@ void net_handle_recv(int fd,const char *msg){ free(res.items); } else if(cmdlen==7&&memcmp(cmd,"message",7)==0){ if(*p=='\0'){ - fprintf(debugf,"net_handle_recv: no arguments to 'message' <%s>\n",msg); + debugf("net_handle_recv: no arguments to 'message' <%s>\n",msg); return; } const char *roomp=p+1; @@ -225,7 +223,7 @@ void net_handle_recv(int fd,const char *msg){ const char *q,*r,*s; if(p==NULL||(q=strchr(p+1,' '))==NULL||(r=strchr(q+1,' '))==NULL ||(s=strchr(r+1,' '))==NULL){ - fprintf(debugf,"net_handle_recv: not enough arguments to 'message' <%s>\n",msg); + debugf("net_handle_recv: not enough arguments to 'message' <%s>\n",msg); return; } i64 roomlen=p-roomp; @@ -245,7 +243,7 @@ void net_handle_recv(int fd,const char *msg){ const char *endp; res.timestamp=strtoll(stampp,(char**)&endp,10); if(endp-stampp!=stamplen){ - fprintf(debugf,"net_handle_recv: timestamp not a number in 'message' <%s>\n",msg); + debugf("net_handle_recv: timestamp not a number in 'message' <%s>\n",msg); return; } res.room=malloc(roomlen+1); @@ -268,12 +266,12 @@ void net_handle_recv(int fd,const char *msg){ cb(fd,res,payload); } else if(cmdlen==15&&memcmp(cmd,"history_message",15)==0){ if(*p=='\0'){ - fprintf(debugf,"net_handle_recv: no arguments to 'history_message' <%s>\n",msg); + debugf("net_handle_recv: no arguments to 'history_message' <%s>\n",msg); return; } p=strchr(p+1,' '); if(*p=='\0'){ - fprintf(debugf,"net_handle_recv: no arguments past index to 'history_message' <%s>\n",msg); + debugf("net_handle_recv: no arguments past index to 'history_message' <%s>\n",msg); return; } const char *roomp=p+1; @@ -281,7 +279,7 @@ void net_handle_recv(int fd,const char *msg){ const char *q,*r,*s; if(p==NULL||(q=strchr(p+1,' '))==NULL||(r=strchr(q+1,' '))==NULL ||(s=strchr(r+1,' '))==NULL){ - fprintf(debugf,"net_handle_recv: not enough arguments to 'history_message' <%s>\n",msg); + debugf("net_handle_recv: not enough arguments to 'history_message' <%s>\n",msg); return; } i64 roomlen=p-roomp; @@ -301,7 +299,7 @@ void net_handle_recv(int fd,const char *msg){ const char *endp; res.timestamp=strtoll(stampp,(char**)&endp,10); if(endp-stampp!=stamplen){ - fprintf(debugf,"net_handle_recv: timestamp not a number in 'history_message' <%s>\n",msg); + debugf("net_handle_recv: timestamp not a number in 'history_message' <%s>\n",msg); return; } res.room=malloc(roomlen+1); @@ -321,7 +319,7 @@ void net_handle_recv(int fd,const char *msg){ } else if(cmdlen==4&&memcmp(cmd,"join",4)==0){ const char *q; if(*p=='\0'||(q=strchr(p+1,' '))==NULL){ - fprintf(debugf,"net_handle_recv: not enough arguments to 'join' <%s>\n",msg); + debugf("net_handle_recv: not enough arguments to 'join' <%s>\n",msg); return; } const char *roomp=p+1; @@ -344,7 +342,7 @@ void net_handle_recv(int fd,const char *msg){ } else if(cmdlen==6&&memcmp(cmd,"invite",6)==0){ const char *q; if(*p=='\0'||(q=strchr(p+1,' '))==NULL){ - fprintf(debugf,"net_handle_recv: no arguments to 'invite' <%s>\n",msg); + debugf("net_handle_recv: no arguments to 'invite' <%s>\n",msg); return; } const char *roomp=p+1; @@ -366,7 +364,7 @@ void net_handle_recv(int fd,const char *msg){ } else if(cmdlen==6&&memcmp(cmd,"online",6)==0){ const char *q; if(*p=='\0'||(q=strchr(p+1,' '))==NULL){ - fprintf(debugf,"net_handle_recv: not enough arguments to 'online' <%s>\n",msg); + debugf("net_handle_recv: not enough arguments to 'online' <%s>\n",msg); return; } const char *nump=p+1; @@ -378,12 +376,12 @@ void net_handle_recv(int fd,const char *msg){ .online.num=strtol(nump,(char**)&endp,10) }; if(nump[0]==' '||*endp!=' '){ - fprintf(debugf,"net_handle_recv: invalid number argument to 'online' <%s>\n",msg); + debugf("net_handle_recv: invalid number argument to 'online' <%s>\n",msg); return; } cb(fd,res,payload); free(res.online.username); } else { - fprintf(debugf,"net_handle_recv: unknown command <%s>\n",msg); + debugf("net_handle_recv: unknown command <%s>\n",msg); } } diff --git a/weechat/tomsg.c b/weechat/tomsg.c index 5a71afa..374ae45 100644 --- a/weechat/tomsg.c +++ b/weechat/tomsg.c @@ -8,6 +8,7 @@ #include #include "weechat-plugin.h" #include "net.h" +#include "debug.h" WEECHAT_PLUGIN_NAME("tomsg") WEECHAT_PLUGIN_DESCRIPTION("tomsg client plugin") @@ -46,8 +47,6 @@ struct conndata{ }; -FILE *debugf; - static struct t_weechat_plugin *weechat_plugin; static struct t_hashtable *conntable; @@ -57,7 +56,7 @@ static void room_update_attributes(struct roomdata *room){ bool private=room->nmembers<=2; weechat_buffer_set(room->buffer,"localvar_set_type",private?"private":"channel"); weechat_buffer_set(room->buffer,"notify",private?"3":"1"); - fprintf(debugf,"room_update_attributes: set private for room %s to %d\n",room->name,private); + debugf("room_update_attributes: set private for room %s to %d\n",room->name,private); if(room->conn->username){ weechat_buffer_set(room->buffer,"localvar_set_nick",room->conn->username); } else { @@ -66,7 +65,7 @@ static void room_update_attributes(struct roomdata *room){ } static void close_room(struct roomdata *room){ - fprintf(debugf,"close_room(room=%p)\n",room); + debugf("close_room(room=%p)\n",room); free(room->name); if(room->buffer)weechat_buffer_close(room->buffer); free(room); @@ -74,13 +73,13 @@ static void close_room(struct roomdata *room){ static void message_net_callback(int fd,struct net_response res,void *payload){ (void)payload; - fprintf(debugf,"message_net_callback(fd=%d,res={.type=%d})\n",fd,res.type); + debugf("message_net_callback(fd=%d,res={.type=%d})\n",fd,res.type); struct conndata *conn=weechat_hashtable_get(conntable,&fd); assert(conn); if(res.type==NET_ERROR){ weechat_printf(conn->buffer,"tomsg: send threw error: %s",res.error); } else if(res.type!=NET_NUMBER){ - fprintf(debugf,"message_net_callback: res.type=%d\n",res.type); + debugf("message_net_callback: res.type=%d\n",res.type); } } @@ -93,7 +92,7 @@ static int room_input_cb(const void *room_vp,void *_d,struct t_gui_buffer *buffe bool skipfirst=input[0]=='/'&&input[1]=='/'; bool free_tosend=false; if(p!=NULL){ - fprintf(debugf,"room_input_cb: input contained newline <%s>\n",input); + debugf("room_input_cb: input contained newline <%s>\n",input); tosend=strdup(input+skipfirst); *strchr(tosend,'\n')='\0'; free_tosend=true; @@ -134,7 +133,7 @@ static void create_room_buffer(struct roomdata *room){ static void history_net_callback(int fd,struct net_response res,void *payload){ struct roomdata *room=(struct roomdata*)payload; assert(room); - fprintf(debugf,"history_net_callback(fd=%d,res={.type=%d})\n",fd,res.type); + debugf("history_net_callback(fd=%d,res={.type=%d})\n",fd,res.type); } struct room_and_name{ @@ -144,7 +143,7 @@ struct room_and_name{ static void isonline_net_callback(int fd,struct net_response res,void *payload){ (void)fd; - fprintf(debugf,"isonline_net_callback(fd=%d,res={.type=%d,.number=%" PRIi64 "})\n", + debugf("isonline_net_callback(fd=%d,res={.type=%d,.number=%" PRIi64 "})\n", fd,res.type,res.number); const char *color= res.type!=NET_NUMBER ? "red" : @@ -165,9 +164,9 @@ static void isonline_net_callback(int fd,struct net_response res,void *payload){ static void members_net_callback(int fd,struct net_response res,void *payload){ struct roomdata *room=(struct roomdata*)payload; assert(room); - fprintf(debugf,"members_net_callback(fd=%d,res={.type=%d})\n",fd,res.type); + debugf("members_net_callback(fd=%d,res={.type=%d})\n",fd,res.type); if(res.type!=NET_LIST){ - fprintf(debugf,"members_net_callback: res.type=%d\n",res.type); + debugf("members_net_callback: res.type=%d\n",res.type); return; } if(room->buffer_nickgroup!=NULL){ @@ -193,7 +192,7 @@ static void members_net_callback(int fd,struct net_response res,void *payload){ static void push_net_callback(int fd,struct net_response res,void *payload){ (void)payload; - fprintf(debugf,"push_net_callback(fd=%d,res={.type=%d})\n",fd,res.type); + debugf("push_net_callback(fd=%d,res={.type=%d})\n",fd,res.type); struct conndata *conn=weechat_hashtable_get(conntable,&fd); assert(conn); if(res.type==NET_MESSAGE||res.type==NET_HISTORY||res.type==NET_JOIN||res.type==NET_INVITE){ @@ -204,7 +203,7 @@ static void push_net_callback(int fd,struct net_response res,void *payload){ } } if(roomi==conn->nrooms){ - fprintf(debugf,"push_net_callback: message to unknown room '%s'\n",res.room); + debugf("push_net_callback: message to unknown room '%s'\n",res.room); return; } struct roomdata *room=conn->rooms[roomi]; @@ -241,7 +240,7 @@ static void push_net_callback(int fd,struct net_response res,void *payload){ } else if(res.type==NET_PONG){ // ok } else if(res.type==NET_ONLINE){ - fprintf(debugf," NET_ONLINE with username='%s' num='%" PRIi64 "'\n",res.online.username,res.online.num); + debugf(" NET_ONLINE with username='%s' num='%" PRIi64 "'\n",res.online.username,res.online.num); const char *color=res.online.num>0 ? "weechat.color.chat_nick" : "weechat.color.nicklist_away"; for(i64 i=0;inrooms;i++){ struct t_gui_nick *nickp=weechat_nicklist_search_nick( @@ -251,18 +250,18 @@ static void push_net_callback(int fd,struct net_response res,void *payload){ } } } else { - fprintf(debugf,"push_net_callback: unknown response type %d\n",res.type); + debugf("push_net_callback: unknown response type %d\n",res.type); } } static void history_push_net_callback(int fd,struct net_response res,void *payload){ - fprintf(debugf,"history_ -> "); + debugf("history_ -> "); push_net_callback(fd,res,payload); } static void roomlist_net_callback(int fd,struct net_response res,void *payload){ (void)payload; - fprintf(debugf,"roomlist_net_callback(fd=%d,res={.type=%d})\n",fd,res.type); + debugf("roomlist_net_callback(fd=%d,res={.type=%d})\n",fd,res.type); struct conndata *conn=weechat_hashtable_get(conntable,&fd); assert(conn); if(res.type==NET_LIST){ @@ -286,13 +285,13 @@ static void roomlist_net_callback(int fd,struct net_response res,void *payload){ net_sendf(fd,members_net_callback,room,"list_members %s",room->name); } } else { - fprintf(debugf,"roomlist_net_callback: res.type=%d\n",res.type); + debugf("roomlist_net_callback: res.type=%d\n",res.type); } } static void login_net_callback(int fd,struct net_response res,void *payload){ (void)payload; - fprintf(debugf,"login_net_callback(fd=%d,res={.type=%d})\n",fd,res.type); + debugf("login_net_callback(fd=%d,res={.type=%d})\n",fd,res.type); struct conndata *conn=weechat_hashtable_get(conntable,&fd); assert(conn); if(res.type==NET_OK){ @@ -305,24 +304,24 @@ static void login_net_callback(int fd,struct net_response res,void *payload){ } else if(res.type==NET_ERROR){ weechat_printf(conn->buffer,"Error logging in: %s",res.error); } else { - fprintf(debugf,"login_net_callback: res.type=%d\n",res.type); + debugf("login_net_callback: res.type=%d\n",res.type); } } static void pong_net_callback(int fd,struct net_response res,void *payload){ (void)payload; - fprintf(debugf,"pong_net_callback(fd=%d,res={.type=%d})\n",fd,res.type); + debugf("pong_net_callback(fd=%d,res={.type=%d})\n",fd,res.type); struct conndata *conn=weechat_hashtable_get(conntable,&fd); assert(conn); if(res.type==NET_PONG){ weechat_printf(conn->buffer,"pong"); } else { - fprintf(debugf,"pong_net_callback: res.type=%d\n",res.type); + debugf("pong_net_callback: res.type=%d\n",res.type); } } static void conn_destroy(struct conndata *conn){ - fprintf(debugf,"conn_destroy(conn=%p (fd=%d))\n",conn,conn->fd); + debugf("conn_destroy(conn=%p (fd=%d))\n",conn,conn->fd); weechat_unhook(conn->fd_hook); if(conntable)weechat_hashtable_remove(conntable,&conn->fd); for(int i=0;inrooms;i++){ @@ -339,7 +338,7 @@ static void conn_destroy(struct conndata *conn){ static int fd_hook_callback(const void *conn_vp,void *_d,int fd){ (void)_d; struct conndata *conn=(struct conndata*)conn_vp; - fprintf(debugf,"fd_hook_callback(conn=%p (fd=%d))\n",conn,fd); + debugf("fd_hook_callback(conn=%p (fd=%d))\n",conn,fd); assert(fd==conn->fd); if(conn->linebuf_len>conn->linebuf_sz/2){ @@ -354,9 +353,9 @@ static int fd_hook_callback(const void *conn_vp,void *_d,int fd){ if(nr<0){ if(errno==EINTR)continue; if(errno==EAGAIN)return WEECHAT_RC_OK; // next time around maybe? - fprintf(debugf,"fd_hook_callback: recv() < 0: %s\n",strerror(errno)); + debugf("fd_hook_callback: recv() < 0: %s\n",strerror(errno)); } else { - fprintf(debugf,"fd_hook_callback: recv() == 0 (EOF)\n"); + debugf("fd_hook_callback: recv() == 0 (EOF)\n"); } weechat_printf(NULL,"tomsg: Connection dropped"); weechat_buffer_close(conn->buffer); @@ -380,7 +379,7 @@ static int fd_hook_callback(const void *conn_vp,void *_d,int fd){ static int conn_input_cb(const void *conn_vp,void *_d,struct t_gui_buffer *buffer,const char *input){ (void)_d; struct conndata *conn=(struct conndata*)conn_vp; - fprintf(debugf,"conn_input_cb(conn=%p,buffer=%p,input=\"%s\")\n",conn,buffer,input); + debugf("conn_input_cb(conn=%p,buffer=%p,input=\"%s\")\n",conn,buffer,input); if(!conn->negotiation_complete){ weechat_printf(conn->buffer,"Server protocol version not yet negotiated, please wait..."); @@ -425,7 +424,7 @@ static char* password_hide_modifier(const void *_p,void *_d,const char *modifier (void)_p; (void)_d; (void)modifier; struct t_gui_buffer *buffer=(struct t_gui_buffer*)strtoll(bufstr,NULL,16); if(buffer==NULL){ - fprintf(debugf,"password_hide_modifier: bufstr is NULL: '%s'\n",bufstr); + debugf("password_hide_modifier: bufstr is NULL: '%s'\n",bufstr); return NULL; } password_hide_modifier__found=false; @@ -459,14 +458,14 @@ static char* password_hide_modifier(const void *_p,void *_d,const char *modifier static int conn_close_cb(const void *conn_vp,void *_d,struct t_gui_buffer *buffer){ (void)_d; (void)buffer; struct conndata *conn=(struct conndata*)conn_vp; - fprintf(debugf,"conn_close_cb(conn=%p,buffer=%p) fd=%d\n",conn,buffer,conn->fd); + debugf("conn_close_cb(conn=%p,buffer=%p) fd=%d\n",conn,buffer,conn->fd); conn_destroy(conn); return WEECHAT_RC_OK; } static void version_net_callback(int fd,struct net_response res,void *payload){ (void)payload; - fprintf(debugf,"version_net_callback(fd=%d,res={.type=%d})\n",fd,res.type); + debugf("version_net_callback(fd=%d,res={.type=%d})\n",fd,res.type); struct conndata *conn=weechat_hashtable_get(conntable,&fd); assert(conn); if(res.type==NET_OK){ @@ -543,7 +542,7 @@ static int cmd_tomsg_cb(const void *_p,void *_d,struct t_gui_buffer *buffer,int return WEECHAT_RC_ERROR; } - fprintf(debugf,"Connecting to %s:%d\n",hostname,port); + debugf("Connecting to %s:%d\n",hostname,port); char *hostname_copy=strdup(hostname); weechat_hook_connect( NULL, @@ -561,15 +560,9 @@ static int cmd_tomsg_cb(const void *_p,void *_d,struct t_gui_buffer *buffer,int int weechat_plugin_init(struct t_weechat_plugin *plugin,int argc,char **argv){ (void)argc; (void)argv; - weechat_plugin=plugin; - - char *fname; - asprintf(&fname,"%s/Desktop/debugf.txt",getenv("HOME")); - debugf=fopen(fname,"w"); - free(fname); - setvbuf(debugf,NULL,_IONBF,0); + weechat_plugin = plugin; - fprintf(debugf,"------\n"); + debug_init(); errpfx=weechat_prefix("error"); netpfx=weechat_prefix("network"); @@ -597,6 +590,6 @@ int weechat_plugin_end(struct t_weechat_plugin *plugin){ (void)plugin; weechat_hashtable_free(conntable); conntable=NULL; - fclose(debugf); + debug_deinit(); return WEECHAT_RC_OK; } -- cgit v1.2.3-54-g00ecf