Reinsert initialiser for __UNUSED_loglevel
[yaz-moved-to-github.git] / src / statserv.c
index 968141c..2ab5b9e 100644 (file)
@@ -5,7 +5,7 @@
  * NT threaded server code by
  *   Chas Woodfield, Fretwell Downing Informatics.
  *
- * $Id: statserv.c,v 1.31 2005-06-25 15:46:05 adam Exp $
+ * $Id: statserv.c,v 1.36 2006-03-22 17:58:32 mike Exp $
  */
 
 /**
@@ -14,6 +14,7 @@
  */
 
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 #include <ctype.h>
 #ifdef WIN32
@@ -92,7 +93,7 @@ statserv_options_block control_block = {
     1,                          /* dynamic mode */
     0,                          /* threaded mode */
     0,                          /* one shot (single session) */
-    YLOG_DEFAULT_LEVEL,          /* log level */
+    0, /* __UNUSED_loglevel */
     "",                         /* no PDUs */
     "",                         /* diagnostic output to stderr */
     "tcp:@:9999",               /* default listener port */
@@ -173,13 +174,13 @@ static char *nmem_dup_xml_content(NMEM n, xmlNodePtr ptr)
     unsigned char *cp;
     xmlNodePtr p;
     int len = 1;  /* start with 1, because of trailing 0 */
-    char *str;
+    unsigned char *str;
     int first = 1; /* whitespace lead flag .. */
     /* determine length */
     for (p = ptr; p; p = p->next)
     {
         if (p->type == XML_TEXT_NODE)
-            len += strlen(p->content);
+            len += xmlStrlen(p->content);
     }
     /* now allocate for the string */
     str = nmem_malloc(n, len);
@@ -196,16 +197,16 @@ static char *nmem_dup_xml_content(NMEM n, xmlNodePtr ptr)
                 if (*cp)
                     first = 0;  /* reset if we got non-whitespace out */
             }
-            strcat(str, cp); /* append */
+            strcat((char *)str, (const char *)cp); /* append */
         }
     }
     /* remove trailing whitespace */
-    cp = strlen(str) + str;
-    while ((char*) cp != str && isspace(cp[-1]))
+    cp = strlen((const char *)str) + str;
+    while (cp != str && isspace(cp[-1]))
         cp--;
     *cp = '\0';
     /* return resulting string */
-    return str;
+    return (char *) str;
 }
 #endif
 
@@ -219,6 +220,8 @@ static struct gfs_server * gfs_server_new()
     n->cql_transform = 0;
     n->server_node_ptr = 0;
     n->directory = 0;
+    n->docpath = 0;
+    n->stylesheet = 0;
     return n;
 }
 
@@ -262,6 +265,10 @@ int control_association(association *assoc, const char *host, int force_open)
             *cp = '\0';
         host = vhost;
     }
+    assoc->cql_transform = 0;
+    assoc->server_node_ptr = 0;
+    assoc->docpath = 0;
+    assoc->stylesheet = 0;
     if (control_block.xml_config[0])
     {
         struct gfs_server *gfs;
@@ -289,6 +296,8 @@ int control_association(association *assoc, const char *host, int force_open)
                     xfree(assoc->init);
                     assoc->init = 0;
                 }
+                assoc->docpath = gfs->docpath;
+                assoc->stylesheet = gfs->stylesheet;
                 assoc->cql_transform = gfs->cql_transform;
                 assoc->server_node_ptr = gfs->server_node_ptr;
                 assoc->last_control = &gfs->cb;
@@ -300,8 +309,6 @@ int control_association(association *assoc, const char *host, int force_open)
         }
         statserv_setcontrol(0);
         assoc->last_control = 0;
-        assoc->cql_transform = 0;
-        assoc->server_node_ptr = 0;
         yaz_log(YLOG_DEBUG, "server select: no match");
         return 0;
     }
@@ -309,8 +316,6 @@ int control_association(association *assoc, const char *host, int force_open)
     {
         statserv_setcontrol(&control_block);
         assoc->last_control = &control_block;
-        assoc->cql_transform = 0;
-        assoc->server_node_ptr = 0;
         yaz_log(YLOG_DEBUG, "server select: config=%s", control_block.configname);
         return 1;
     }
@@ -340,7 +345,7 @@ static void xml_config_read()
             const char *address =
                 nmem_dup_xml_content(gfs_nmem, ptr->children);
             for ( ; attr; attr = attr->next)
-                if (!strcmp(attr->name, "id")
+                if (!xmlStrcmp(attr->name, BAD_CAST "id")
                     && attr->children && attr->children->type == XML_TEXT_NODE)
                     id = nmem_dup_xml_content(gfs_nmem, attr->children);
             if (address)
@@ -358,11 +363,12 @@ static void xml_config_read()
             const char *id = 0;
 
             for ( ; attr; attr = attr->next)
-                if (!strcmp(attr->name, "listenref") && attr->children &&
-                    attr->children->type == XML_TEXT_NODE)
+                if (!xmlStrcmp(attr->name, BAD_CAST "listenref") 
+                    && attr->children && attr->children->type == XML_TEXT_NODE)
                     listenref = nmem_dup_xml_content(gfs_nmem, attr->children);
-                else if (!strcmp(attr->name, "id") && attr->children &&
-                         attr->children->type == XML_TEXT_NODE)
+                else if (!xmlStrcmp(attr->name, BAD_CAST "id")
+                         && attr->children
+                         && attr->children->type == XML_TEXT_NODE)
                     id = nmem_dup_xml_content(gfs_nmem, attr->children);
                 else
                     yaz_log(YLOG_WARN, "Unknown attribute '%s' for server",
@@ -408,6 +414,28 @@ static void xml_config_read()
                     (*gfsp)->directory = 
                         nmem_dup_xml_content(gfs_nmem, ptr->children);
                 }
+                else if (!strcmp((const char *) ptr->name, "docpath"))
+                {
+                    (*gfsp)->docpath = 
+                        nmem_dup_xml_content(gfs_nmem, ptr->children);
+                }
+                else if (!strcmp((const char *) ptr->name, "stylesheet"))
+                {
+                    char *s = nmem_dup_xml_content(gfs_nmem, ptr->children);
+                    (*gfsp)->stylesheet = 
+                        nmem_malloc(gfs_nmem, strlen(s) + 2);
+                    sprintf((*gfsp)->stylesheet, "/%s", s);
+                }
+                else if (!strcmp((const char *) ptr->name, "explain"))
+                {
+                    ; /* being processed separately */
+                }
+                else
+                {
+                    yaz_log(YLOG_FATAL, "Unknown element '%s' in config %s",
+                            ptr->name, control_block.xml_config);
+                    exit(1);
+                }
             }
             gfsp = &(*gfsp)->next;
         }
@@ -884,7 +912,7 @@ static void listener(IOCHAN h, int event)
                     iochan_destroy(pp);
                 }
                 sprintf(nbuf, "%s(%d)", me, no_sessions);
-                yaz_log_init(control_block.loglevel, nbuf, 0);
+                yaz_log_init_prefix(nbuf);
                 /* ensure that bend_stop is not called when each child exits -
                    only for the main process ..  */
                 control_block.bend_stop = 0;
@@ -1293,12 +1321,19 @@ int check_options(int argc, char **argv)
     int ret = 0, r;
     char *arg;
 
-    /* set default log level */
-    control_block.loglevel = yaz_log_mask_str(STAT_DEFAULT_LOG_LEVEL);
-    yaz_log_init_level(control_block.loglevel);
+    if (getenv("YAZ_LOG") == 0) {
+        /*
+         * Set default log level.  We want to avoid doing this if the
+         * user has already explicitly specified a preferred default
+         * log-level, hence the inelegant peek at the YAZ_LOG
+         * environment variable that will subsequently be interpreted
+         * by the YAZ logging module itself.
+         */
+        yaz_log_init_level(yaz_log_mask_str(STAT_DEFAULT_LOG_LEVEL));
+    }
 
     get_logbits(1); 
-    while ((ret = options("1a:iszSTl:v:u:c:w:t:k:d:A:p:DC:f:",
+    while ((ret = options("1a:iszSTl:v:u:c:w:t:k:d:A:p:DC:f:m:",
                           argv, argc, &arg)) != -2)
     {
         switch (ret)
@@ -1335,14 +1370,21 @@ int check_options(int argc, char **argv)
             break;
         case 'l':
             option_copy(control_block.logfile, arg);
-            yaz_log_init(control_block.loglevel, me, control_block.logfile);
+            yaz_log_init_file(control_block.logfile);
             break;
-        case 'v':
-            control_block.loglevel =
-                yaz_log_mask_str_x(arg,control_block.loglevel);
-            yaz_log_init(control_block.loglevel, me, control_block.logfile);
+        case 'm':
+            if (!arg) {
+                fprintf(stderr, "%s: Specify time format for log file.\n", me);
+                return(1);
+            }
+            yaz_log_time_format(arg);
+            break;
+        case 'v': {
+            int default_level = yaz_log_mask_str(STAT_DEFAULT_LOG_LEVEL);
+            yaz_log_init_level(yaz_log_mask_str_x(arg, default_level));
             get_logbits(1); 
             break;
+        }
         case 'a':
             option_copy(control_block.apdufile, arg);
             break;
@@ -1405,7 +1447,7 @@ int check_options(int argc, char **argv)
             fprintf(stderr, "Usage: %s [ -a <pdufile> -v <loglevel>"
                     " -l <logfile> -u <user> -c <config> -t <minutes>"
                     " -k <kilobytes> -d <daemon> -p <pidfile> -C certfile"
-                        " -ziDST1 -w <directory> <listener-addr>... ]\n", me);
+                        " -ziDST1 -m <time-format> -w <directory> <listener-addr>... ]\n", me);
             return 1;
         }
     }