diff src/dnsbl.cpp @ 86:c1280cd3e248

add multiple debug syslog levels, remove duplicate dnsbl definitions
author carl
date Tue, 19 Jul 2005 22:47:15 -0700
parents 091d3fe3db46
children 946fc1bcfb2c
line wrap: on
line diff
--- a/src/dnsbl.cpp	Sun Jul 17 08:56:33 2005 -0700
+++ b/src/dnsbl.cpp	Tue Jul 19 22:47:15 2005 -0700
@@ -13,7 +13,7 @@
 -c       Check the config, and print a copy to stdout. Don't start the
          milter or do anything with the socket.
 -s       Stress test by loading and deleting the current config in a loop.
--d       Add debug syslog entries
+-d       increase debug level
 -e f|t   Print the results of looking up from address f and to address
          t in the current config
 
@@ -81,7 +81,7 @@
     void sig_chld(int signo);
 }
 
-bool debug_syslog  = false;
+int  debug_syslog  = 0;
 bool syslog_opened = false;
 bool use_syslog    = true;  // false to printf
 bool loader_run    = true;  // used to stop the config loader thread
@@ -673,15 +673,17 @@
         if ((cnt > limit) && (limit > 0) && random) {
             int r = rand() % cnt;
             if (r >= limit) {
+                if (debug_syslog > 2) {
                 char buf[maxlen];
                 snprintf(buf, sizeof(buf), "host %s skipped", host);
                 my_syslog(&priv, buf);
+                }
                 continue;
             }
         }
         count++;
         ip = dns_interface(priv, host, true, &nameservers);
-        if (debug_syslog) {
+        if (debug_syslog > 2) {
             char buf[maxlen];
             if (ip) {
                 char adr[sizeof "255.255.255.255"];
@@ -714,7 +716,7 @@
         host = (*i).first;  // a transient reference that needs to be replaced before we return it
         ip   = (*i).second;
         if (!ip) ip = dns_interface(priv, host, false, NULL);
-        if (debug_syslog) {
+        if (debug_syslog > 2) {
             char buf[maxlen];
             if (ip) {
                 char adr[sizeof "255.255.255.255"];
@@ -803,7 +805,7 @@
     char *rcptaddr  = rcpt[0];
     char *loto      = to_lower_string(rcptaddr);
     CONTEXT    &con = *(dc.find_context(loto)->find_context(priv.mailaddr));
-    if (debug_syslog) {
+    if (debug_syslog > 1) {
         char buf[maxlen];
         char msg[maxlen];
         snprintf(msg, sizeof(msg), "from <%s> to <%s> using context %s", priv.mailaddr, loto, con.get_full_name(buf,maxlen));
@@ -967,9 +969,11 @@
     pthread_mutex_lock(&config_mutex);
         newc->generation = generation++;
     pthread_mutex_unlock(&config_mutex);
+    if (debug_syslog) {
     char buf[maxlen];
     snprintf(buf, sizeof(buf), "loading configuration generation %d", newc->generation);
     my_syslog(buf);
+    }
     if (load_conf(*newc, "dnsbl.conf")) {
         newc->load_time = time(NULL);
         return newc;
@@ -1014,9 +1018,11 @@
         for (configp_set::iterator i=old_configs.begin(); i!=old_configs.end(); ) {
             CONFIG *old = *i;
             if (!old->reference_count) {
+                if (debug_syslog) {
                 char buf[maxlen];
                 snprintf(buf, sizeof(buf), "freeing memory for old configuration generation %d", old->generation);
                 my_syslog(buf);
+                }
                 delete old; // destructor does all the work
                 old_configs.erase(i++);
             }
@@ -1030,7 +1036,7 @@
 void usage(char *prog);
 void usage(char *prog)
 {
-    fprintf(stderr, "Usage: %s  [-d] [-c] [-s] [-e from|to] -r port -p sm-sock-addr [-t timeout]\n", prog);
+    fprintf(stderr, "Usage: %s  [-d [level]] [-c] [-s] [-e from|to] -r port -p sm-sock-addr [-t timeout]\n", prog);
     fprintf(stderr, "where port is for the connection to our own dns resolver processes\n");
     fprintf(stderr, "    and should be local-domain-socket-file-name\n");
     fprintf(stderr, "where sm-sock-addr is for the connection to sendmail\n");
@@ -1040,7 +1046,7 @@
     fprintf(stderr, "-c will load and dump the config to stdout\n");
     fprintf(stderr, "-s will stress test the config loading code by repeating the load/free cycle\n");
     fprintf(stderr, "        in an infinte loop.\n");
-    fprintf(stderr, "-d will add some syslog debug messages\n");
+    fprintf(stderr, "-d will set the syslog message level, currently 0 to 3");
     fprintf(stderr, "-e will print the results of looking up the from and to addresses in the\n");
     fprintf(stderr, "        current config. The | character is used to separate the from and to\n");
     fprintf(stderr, "        addresses in the argument to the -e switch\n");
@@ -1084,7 +1090,7 @@
     bool setreso = false;
     char *email = NULL;
     int c;
-    const char *args = "r:p:t:e:cdhs";
+    const char *args = "r:p:t:e:d:chs";
     extern char *optarg;
 
     // Process command line options
@@ -1139,7 +1145,8 @@
                 break;
 
             case 'd':
-                debug_syslog = true;
+                if (optarg == NULL || *optarg == '\0') debug_syslog = 1;
+                else                                   debug_syslog = atoi(optarg);
                 break;
 
             case 'h':
@@ -1151,7 +1158,7 @@
 
     if (check) {
         use_syslog   = false;
-        debug_syslog = true;
+        debug_syslog = 10;
         CONFIG *conf = new_conf();
         if (conf) {
             conf->dump();
@@ -1313,9 +1320,7 @@
                     // this is the worker process
                     // child does not need the listening socket
                     close(resolver_socket);
-                  //my_syslog("child forked a worker process");
                     process_resolver_requests(s);
-                  //my_syslog("child terminated a worker process");
                     exit(0);
                 }
                 else {