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/tomsg.c | 73 ++++++++++++++++++++++++++------------------------------- 1 file changed, 33 insertions(+), 40 deletions(-) (limited to 'weechat/tomsg.c') 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