chiark / gitweb /
@@ -1,4 +1,4 @@
authorian <ian>
Sat, 7 Jan 2006 17:08:04 +0000 (17:08 +0000)
committerian <ian>
Sat, 7 Jan 2006 17:08:04 +0000 (17:08 +0000)
-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.

  --

changelog
src/adns.h
src/general.c
src/internal.h
src/setup.c

index 058436de13707701688f6a9d71d61489c3a0bd81..a4bdd9725ecde97b689ac4e345bde744dc261040 100644 (file)
--- 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.
 
  --
 
index 71a013d38dc66b24da3795326cb7f9bf2af5c29e..f41bb3fc4f20a87e60019dfb993018f08063247b 100644 (file)
  *  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 <stdio.h>
+#include <stdarg.h>
 
 #include <sys/types.h>
 #include <sys/socket.h>
@@ -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,
index 83232d7172dfed2a81b74fd0443d4f9dadf1179f..da0b8d09e77cfcf95a2176fc82aa50dc2a6ecd56 100644 (file)
 
 /* 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 : "<unknown>");
     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,
index f8d443db7413e2c8fa1514cca01d1a45f9d4ef9c..95e5276bc65a0ca9a068dc2fac010334c67e2d16 100644 (file)
@@ -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);
 
index 38be6217670df8f073e68d341ab5fa3b04b6a133..21aded08a4790f501716d90519790f159f6f4bff 100644 (file)
@@ -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,"<supplied configuration text>");
   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);