From: ian Date: Sat, 7 Jan 2006 17:08:04 +0000 (+0000) Subject: @@ -1,4 +1,4 @@ X-Git-Tag: wip.base.getaddrinfo~80 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/adns/commitdiff_plain/86ea5e62c7d3bc1e17715811b2047b7d1b06862d @@ -1,4 +1,4 @@ -adns (1.2); urgency=medium +adns (1.1.999.0.1); urgency=medium * Fix error in prototype in definition of adns__parse_domain. * New LICENCE.WAIVERS file for GPL-incompatility workarounds. @@ -16,6 +16,8 @@ * Add bind(2) and listen(2) to test harness (for epithet's benefit, but harmless in adns). * Add missing ENOTSOCK to hcommon.c.m4 (was already in hcommon.c!) + * New adns_init_logfn etc. for having logging done with a callback + function. -- --- diff --git a/changelog b/changelog index 058436d..a4bdd97 100644 --- a/changelog +++ b/changelog @@ -1,4 +1,4 @@ -adns (1.2); urgency=medium +adns (1.1.999.0.1); urgency=medium * Fix error in prototype in definition of adns__parse_domain. * New LICENCE.WAIVERS file for GPL-incompatility workarounds. @@ -16,6 +16,8 @@ adns (1.2); urgency=medium * Add bind(2) and listen(2) to test harness (for epithet's benefit, but harmless in adns). * Add missing ENOTSOCK to hcommon.c.m4 (was already in hcommon.c!) + * New adns_init_logfn etc. for having logging done with a callback + function. -- diff --git a/src/adns.h b/src/adns.h index 71a013d..f41bb3f 100644 --- a/src/adns.h +++ b/src/adns.h @@ -51,13 +51,14 @@ * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * - * $Id: adns.h,v 1.86 2005/04/05 20:34:38 ian Exp $ + * $Id: adns.h,v 1.87 2006/01/07 17:08:04 ian Exp $ */ #ifndef ADNS_H_INCLUDED #define ADNS_H_INCLUDED #include +#include #include #include @@ -360,6 +361,19 @@ int adns_init(adns_state *newstate_r, adns_initflags flags, int adns_init_strcfg(adns_state *newstate_r, adns_initflags flags, FILE *diagfile /*0=>discard*/, const char *configtext); +typedef void adns_logcallbackfn(adns_state ads, void *logfndata, + const char *fmt, va_list al); + /* Will be called perhaps several times for each message; when the + * message is complete, the string implied by fmt and al will end in + * a newline. Log messages start with `adns debug:' or `adns + * warning:' or `adns:' (for errors), or `adns debug [PID]:' + * etc. if adns_if_logpid is set. */ + +int adns_init_logfn(adns_state *newstate_r, adns_initflags flags, + const char *configtext /*0=>use default config files*/, + adns_logcallbackfn *logfn /*0=>logfndata is a FILE* */, + void *logfndata /*0 with logfn==0 => discard*/); + /* Configuration: * adns_init reads /etc/resolv.conf, which is expected to be (broadly * speaking) in the format expected by libresolv, and then @@ -672,10 +686,10 @@ void adns_beforeselect(adns_state ads, int *maxfd, fd_set *readfds, * for adns_firsttimeout. readfds, writefds, exceptfds and maxfd_io may * not be 0. * - * If now is not 0 then this will never actually do any I/O, or change - * the fds that adns is using or the timeouts it wants. In any case - * it won't block, and it will set the timeout to zero if a query - * finishes in _beforeselect. + * If tv_mod is 0 on entry then this will never actually do any I/O, + * or change the fds that adns is using or the timeouts it wants. In + * any case it won't block, and it will set the timeout to zero if a + * query finishes in _beforeselect. */ void adns_afterselect(adns_state ads, int maxfd, const fd_set *readfds, diff --git a/src/general.c b/src/general.c index 83232d7..da0b8d0 100644 --- a/src/general.c +++ b/src/general.c @@ -38,46 +38,57 @@ /* Core diagnostic functions */ +void adns__vlprintf(adns_state ads, const char *fmt, va_list al) { + ads->logfn(ads,ads->logfndata,fmt,al); +} + +void adns__lprintf(adns_state ads, const char *fmt, ...) { + va_list al; + va_start(al,fmt); + adns__vlprintf(ads,fmt,al); + va_end(al); +} + void adns__vdiag(adns_state ads, const char *pfx, adns_initflags prevent, int serv, adns_query qu, const char *fmt, va_list al) { const char *bef, *aft; vbuf vb; - if (!ads->diagfile || + if (!ads->logfn || (!(ads->iflags & adns_if_debug) && (!prevent || (ads->iflags & prevent)))) return; if (ads->iflags & adns_if_logpid) { - fprintf(ads->diagfile,"adns%s [%ld]: ",pfx,(long)getpid()); + adns__lprintf(ads,"adns%s [%ld]: ",pfx,(long)getpid()); } else { - fprintf(ads->diagfile,"adns%s: ",pfx); + adns__lprintf(ads,"adns%s: ",pfx); } - vfprintf(ads->diagfile,fmt,al); + adns__vlprintf(ads,fmt,al); bef= " ("; aft= "\n"; if (qu && qu->query_dgram) { adns__vbuf_init(&vb); - fprintf(ads->diagfile,"%sQNAME=%s, QTYPE=%s", + adns__lprintf(ads,"%sQNAME=%s, QTYPE=%s", bef, adns__diag_domain(qu->ads,-1,0, &vb, qu->query_dgram,qu->query_dglen,DNS_HDRSIZE), qu->typei ? qu->typei->rrtname : ""); if (qu->typei && qu->typei->fmtname) - fprintf(ads->diagfile,"(%s)",qu->typei->fmtname); + adns__lprintf(ads,"(%s)",qu->typei->fmtname); bef=", "; aft=")\n"; adns__vbuf_free(&vb); } if (serv>=0) { - fprintf(ads->diagfile,"%sNS=%s",bef,inet_ntoa(ads->servers[serv].addr)); + adns__lprintf(ads,"%sNS=%s",bef,inet_ntoa(ads->servers[serv].addr)); bef=", "; aft=")\n"; } - fputs(aft,ads->diagfile); + adns__lprintf(ads,"%s",aft); } void adns__debug(adns_state ads, int serv, adns_query qu, diff --git a/src/internal.h b/src/internal.h index f8d443d..95e5276 100644 --- a/src/internal.h +++ b/src/internal.h @@ -283,7 +283,8 @@ struct query_queue { adns_query head, tail; }; struct adns__state { adns_initflags iflags; - FILE *diagfile; + adns_logcallbackfn *logfn; + void *logfndata; int configerrno; struct query_queue udpw, tcpw, childw, output; adns_query forallnext; @@ -318,6 +319,10 @@ int adns__setnonblock(adns_state ads, int fd); /* => errno value */ /* From general.c: */ +void adns__vlprintf(adns_state ads, const char *fmt, va_list al); +void adns__lprintf(adns_state ads, const char *fmt, + ...) PRINTFFORMAT(2,3); + void adns__vdiag(adns_state ads, const char *pfx, adns_initflags prevent, int serv, adns_query qu, const char *fmt, va_list al); diff --git a/src/setup.c b/src/setup.c index 38be621..21aded0 100644 --- a/src/setup.c +++ b/src/setup.c @@ -77,14 +77,14 @@ static void configparseerr(adns_state ads, const char *fn, int lno, va_list al; saveerr(ads,EINVAL); - if (!ads->diagfile || (ads->iflags & adns_if_noerrprint)) return; + if (!ads->logfn || (ads->iflags & adns_if_noerrprint)) return; - if (lno==-1) fprintf(ads->diagfile,"adns: %s: ",fn); - else fprintf(ads->diagfile,"adns: %s:%d: ",fn,lno); + if (lno==-1) adns__lprintf(ads,"adns: %s: ",fn); + else adns__lprintf(ads,"adns: %s:%d: ",fn,lno); va_start(al,fmt); - vfprintf(ads->diagfile,fmt,al); + adns__vlprintf(ads,fmt,al); va_end(al); - fputc('\n',ads->diagfile); + adns__lprintf(ads,"\n"); } static int nextword(const char **bufp_io, const char **word_r, int *l_r) { @@ -507,13 +507,14 @@ int adns__setnonblock(adns_state ads, int fd) { } static int init_begin(adns_state *ads_r, adns_initflags flags, - FILE *diagfile) { + adns_logcallbackfn *logfn, void *logfndata) { adns_state ads; ads= malloc(sizeof(*ads)); if (!ads) return errno; ads->iflags= flags; - ads->diagfile= diagfile; + ads->logfn= logfn; + ads->logfndata= logfndata; ads->configerrno= 0; LIST_INIT(ads->udpw); LIST_INIT(ads->tcpw); @@ -541,8 +542,8 @@ static int init_finish(adns_state ads) { int r; if (!ads->nservers) { - if (ads->diagfile && ads->iflags & adns_if_debug) - fprintf(ads->diagfile,"adns: no nameservers, using localhost\n"); + if (ads->logfn && ads->iflags & adns_if_debug) + adns__lprintf(ads,"adns: no nameservers, using localhost\n"); ia.s_addr= htonl(INADDR_LOOPBACK); addserver(ads,ia); } @@ -571,12 +572,18 @@ static void init_abort(adns_state ads) { free(ads); } -int adns_init(adns_state *ads_r, adns_initflags flags, FILE *diagfile) { +static void logfn_file(adns_state ads, void *logfndata, + const char *fmt, va_list al) { + vfprintf(logfndata,fmt,al); +} + +static int init_files(adns_state *ads_r, adns_initflags flags, + adns_logcallbackfn *logfn, void *logfndata) { adns_state ads; const char *res_options, *adns_res_options; int r; - r= init_begin(&ads, flags, diagfile ? diagfile : stderr); + r= init_begin(&ads, flags, logfn, logfndata); if (r) return r; res_options= instrum_getenv(ads,"RES_OPTIONS"); @@ -612,12 +619,18 @@ int adns_init(adns_state *ads_r, adns_initflags flags, FILE *diagfile) { return 0; } -int adns_init_strcfg(adns_state *ads_r, adns_initflags flags, - FILE *diagfile, const char *configtext) { +int adns_init(adns_state *ads_r, adns_initflags flags, FILE *diagfile) { + return init_files(ads_r, flags, logfn_file, diagfile ? diagfile : stderr); +} + +static int init_strcfg(adns_state *ads_r, adns_initflags flags, + adns_logcallbackfn *logfn, void *logfndata, + const char *configtext) { adns_state ads; int r; - r= init_begin(&ads, flags, diagfile); if (r) return r; + r= init_begin(&ads, flags, logfn, logfndata); + if (r) return r; readconfigtext(ads,configtext,""); if (ads->configerrno) { @@ -632,6 +645,24 @@ int adns_init_strcfg(adns_state *ads_r, adns_initflags flags, return 0; } +int adns_init_strcfg(adns_state *ads_r, adns_initflags flags, + FILE *diagfile, const char *configtext) { + return init_strcfg(ads_r, flags, + diagfile ? logfn_file : 0, diagfile, + configtext); +} + +int adns_init_logfn(adns_state *newstate_r, adns_initflags flags, + const char *configtext /*0=>use default config files*/, + adns_logcallbackfn *logfn /*0=>logfndata is a FILE* */, + void *logfndata /*0 with logfn==0 => discard*/) { + if (!logfn && logfndata) + logfn= logfn_file; + if (configtext) + return init_strcfg(newstate_r, flags, logfn, logfndata, configtext); + else + return init_files(newstate_r, flags, logfn, logfndata); +} void adns_finish(adns_state ads) { adns__consistency(ads,0,cc_entex);