[mapserver-commits] r12675 - in trunk/mapserver/mapcache: include src

svn at osgeo.org svn at osgeo.org
Wed Oct 19 08:30:06 EDT 2011


Author: tbonfort
Date: 2011-10-19 05:30:06 -0700 (Wed, 19 Oct 2011)
New Revision: 12675

Modified:
   trunk/mapserver/mapcache/include/errors.h
   trunk/mapserver/mapcache/include/mapcache.h
   trunk/mapserver/mapcache/src/configuration.c
   trunk/mapserver/mapcache/src/configuration_xml.c
   trunk/mapserver/mapcache/src/core.c
   trunk/mapserver/mapcache/src/fastcgi_mapcache.c
   trunk/mapserver/mapcache/src/mod_mapcache.c
Log:
add loglevel and auto config file reloading for the fastcgi/cgi implementation

honor the requested loglevel for the apache module implementation



Modified: trunk/mapserver/mapcache/include/errors.h
===================================================================
--- trunk/mapserver/mapcache/include/errors.h	2011-10-18 18:18:28 UTC (rev 12674)
+++ trunk/mapserver/mapcache/include/errors.h	2011-10-19 12:30:06 UTC (rev 12675)
@@ -29,10 +29,14 @@
 #define MAPCACHE_ERRORS_H_
 
 typedef enum {
-MAPCACHE_DEBUG,
-MAPCACHE_INFO,
-MAPCACHE_WARNING,
-MAPCACHE_ERROR
+   MAPCACHE_DEBUG,
+   MAPCACHE_INFO,
+   MAPCACHE_NOTICE,
+   MAPCACHE_WARN,
+   MAPCACHE_ERROR,
+   MAPCACHE_CRIT,
+   MAPCACHE_ALERT,
+   MAPCACHE_EMERG
 } mapcache_log_level;
 
 typedef enum {

Modified: trunk/mapserver/mapcache/include/mapcache.h
===================================================================
--- trunk/mapserver/mapcache/include/mapcache.h	2011-10-18 18:18:28 UTC (rev 12674)
+++ trunk/mapserver/mapcache/include/mapcache.h	2011-10-19 12:30:06 UTC (rev 12675)
@@ -929,6 +929,13 @@
 
     const char *lockdir;
     const char *endpoint; /**< the uri where the base of the service is mapped */
+
+    /* for fastcgi only */
+    int autoreload; /* should the modification time of the config file be recorded
+                       and the file be reparsed if it is modified. */
+    mapcache_log_level loglevel; /* logging verbosity. Ignored for the apache module
+                                    as in that case the apache LogLevel directive is
+                                    used. */
 };
 
 /**

Modified: trunk/mapserver/mapcache/src/configuration.c
===================================================================
--- trunk/mapserver/mapcache/src/configuration.c	2011-10-18 18:18:28 UTC (rev 12674)
+++ trunk/mapserver/mapcache/src/configuration.c	2011-10-19 12:30:06 UTC (rev 12675)
@@ -53,7 +53,7 @@
       while ((apr_dir_read(&finfo, APR_FINFO_DIRENT|APR_FINFO_TYPE|APR_FINFO_NAME, lockdir)) == APR_SUCCESS) {
          if(finfo.filetype == APR_REG) {
             if(!strncmp(finfo.name, MAPCACHE_LOCKFILE_PREFIX, strlen(MAPCACHE_LOCKFILE_PREFIX))) {
-               ctx->log(ctx,MAPCACHE_WARNING,"found old lockfile %s/%s, deleting it",config->lockdir,
+               ctx->log(ctx,MAPCACHE_WARN,"found old lockfile %s/%s, deleting it",config->lockdir,
                      finfo.name);
                rv = apr_file_remove(apr_psprintf(ctx->pool,"%s/%s",config->lockdir, finfo.name),ctx->pool);
                if(rv != APR_SUCCESS) {
@@ -231,6 +231,9 @@
    }
    mapcache_configuration_add_grid(cfg,grid,"g");
 
+   cfg->loglevel = MAPCACHE_WARN;
+   cfg->autoreload = 0;
+
    return cfg;
 }
 

Modified: trunk/mapserver/mapcache/src/configuration_xml.c
===================================================================
--- trunk/mapserver/mapcache/src/configuration_xml.c	2011-10-18 18:18:28 UTC (rev 12674)
+++ trunk/mapserver/mapcache/src/configuration_xml.c	2011-10-19 12:30:06 UTC (rev 12675)
@@ -879,7 +879,7 @@
       }
    }
    else if ((node = ezxml_child(doc,"services")) != NULL) {
-      ctx->log(ctx,MAPCACHE_WARNING,"<services> tag is deprecated, use <service type=\"wms\" enabled=\"true|false\">");
+      ctx->log(ctx,MAPCACHE_WARN,"<services> tag is deprecated, use <service type=\"wms\" enabled=\"true|false\">");
       parseServices(ctx, node, config);
    } else {
       ctx->set_error(ctx, 400, "no <services> configured");
@@ -925,6 +925,39 @@
    } else {
       config->lockdir = apr_pstrdup(ctx->pool,"/tmp");
    }
+   
+   if((node = ezxml_child(doc,"log_level")) != NULL) {
+      if(!strcasecmp(node->txt,"debug")) {
+         config->loglevel = MAPCACHE_DEBUG;
+      } else if(!strcasecmp(node->txt,"info")) {
+         config->loglevel = MAPCACHE_INFO;
+      } else if(!strcasecmp(node->txt,"notice")) {
+         config->loglevel = MAPCACHE_NOTICE;
+      } else if(!strcasecmp(node->txt,"warn")) {
+         config->loglevel = MAPCACHE_WARN;
+      } else if(!strcasecmp(node->txt,"error")) {
+         config->loglevel = MAPCACHE_ERROR;
+      } else if(!strcasecmp(node->txt,"crit")) {
+         config->loglevel = MAPCACHE_CRIT;
+      } else if(!strcasecmp(node->txt,"alert")) {
+         config->loglevel = MAPCACHE_ALERT;
+      } else if(!strcasecmp(node->txt,"emerg")) {
+         config->loglevel = MAPCACHE_EMERG;
+      } else {
+         ctx->set_error(ctx,500,"failed to parse <log_level> \"%s\". Expecting debug, info, notice, warn, error, crit, alert or emerg",node->txt);
+         return;
+      }
+   }
+   if((node = ezxml_child(doc,"auto_reload")) != NULL) {
+      if(!strcasecmp(node->txt,"true")) {
+         config->autoreload = 1;
+      } else if(!strcasecmp(node->txt,"false")) {
+         config->autoreload = 0;
+      } else {
+         ctx->set_error(ctx,500,"failed to parse <auto_reload> \"%s\". Expecting true or false",node->txt);
+         return;
+      }
+   }
 
 
 cleanup:

Modified: trunk/mapserver/mapcache/src/core.c
===================================================================
--- trunk/mapserver/mapcache/src/core.c	2011-10-18 18:18:28 UTC (rev 12674)
+++ trunk/mapserver/mapcache/src/core.c	2011-10-19 12:30:06 UTC (rev 12675)
@@ -337,7 +337,7 @@
    if(!msg) {
       msg = "an unspecified error has occured";
    }
-   ctx->log(ctx,MAPCACHE_INFO,msg);
+   ctx->log(ctx,MAPCACHE_ERROR,msg);
   
 
    if(ctx->config && ctx->config->reporting == MAPCACHE_REPORT_MSG) {

Modified: trunk/mapserver/mapcache/src/fastcgi_mapcache.c
===================================================================
--- trunk/mapserver/mapcache/src/fastcgi_mapcache.c	2011-10-18 18:18:28 UTC (rev 12674)
+++ trunk/mapserver/mapcache/src/fastcgi_mapcache.c	2011-10-19 12:30:06 UTC (rev 12675)
@@ -32,6 +32,7 @@
 #include <apr_strings.h>
 #include <apr_pools.h>
 #include <apr_file_io.h>
+#include <signal.h>
 #include <apr_date.h>
 #ifdef USE_FASTCGI
 #include <fcgi_stdio.h>
@@ -46,7 +47,7 @@
 static char *err501 = "Not Implemented";
 static char *err502 = "Bad Gateway";
 static char *errother = "No Description";
-apr_pool_t *global_pool;
+apr_pool_t *global_pool = NULL,*config_pool, *tmp_config_pool;
 
 static char* err_msg(int code) {
    switch(code) {
@@ -71,15 +72,17 @@
    apr_file_t *mutex_file;
 };
 
-void fcgi_context_log(mapcache_context *c, mapcache_log_level level, char *message, ...) {
+static void fcgi_context_log(mapcache_context *c, mapcache_log_level level, char *message, ...) {
    va_list args;
-   va_start(args,message);
-   fprintf(stderr,"%s\n",apr_pvsprintf(c->pool,message,args));
-   va_end(args);
+   if(!c->config || level >= c->config->loglevel) {
+      va_start(args,message);
+      fprintf(stderr,"%s\n",apr_pvsprintf(c->pool,message,args));
+      va_end(args);
+   }
 }
 
 
-void mapcache_fcgi_mutex_aquire(mapcache_context *gctx) {
+static void mapcache_fcgi_mutex_aquire(mapcache_context *gctx) {
    mapcache_context_fcgi *ctx = (mapcache_context_fcgi*)gctx;
    int ret;
 #ifdef DEBUG
@@ -101,7 +104,12 @@
    }
 }
 
-void mapcache_fcgi_mutex_release(mapcache_context *gctx) {
+static void handle_signal(int signal) {
+   apr_pool_destroy(global_pool);
+   exit(signal);
+}
+
+static void mapcache_fcgi_mutex_release(mapcache_context *gctx) {
    int ret;
    mapcache_context_fcgi *ctx = (mapcache_context_fcgi*)gctx;
 #ifdef DEBUG
@@ -132,6 +140,7 @@
    ctx->mutex_fname="/tmp/mapcache.fcgi.lock";
    ctx->ctx.global_lock_aquire = mapcache_fcgi_mutex_aquire;
    ctx->ctx.global_lock_release = mapcache_fcgi_mutex_release;
+   ctx->ctx.config = NULL;
    return ctx;
 }
 
@@ -172,44 +181,106 @@
 }
 
 
-int main(int argc, char **argv) {
+apr_time_t mtime;
+char *conffile;
+
+static void load_config(mapcache_context *ctx, char *filename) {
+   apr_file_t *f;
+   apr_finfo_t finfo;
+   if((apr_file_open(&f, filename, APR_FOPEN_READ, APR_UREAD | APR_GREAD,
+               global_pool)) == APR_SUCCESS) {
+      apr_file_info_get(&finfo, APR_FINFO_MTIME, f);
+      apr_file_close(f);
+   } else {
+      ctx->set_error(ctx,500,"failed to open config file %s",filename);
+      return;
+   }
+   if(ctx->config) {
+      //we already have a loaded configuration, check that the config file hasn't changed
+      if(finfo.mtime > mtime) {
+         ctx->log(ctx,MAPCACHE_INFO,"config file has changed, reloading");
+      } else {
+         return;
+      }
+   }
+   mtime = finfo.mtime;
+
+   /* either we have no config, or it has changed */
+
+   mapcache_cfg *old_cfg = ctx->config;
+   apr_pool_create(&tmp_config_pool,global_pool);
+
+   mapcache_cfg *cfg = mapcache_configuration_create(tmp_config_pool);
+   ctx->config = cfg;
+   ctx->pool = tmp_config_pool;
+
+   mapcache_configuration_parse(ctx,conffile,cfg,1);
+   if(GC_HAS_ERROR(ctx)) goto failed_load;
+   mapcache_configuration_post_config(ctx, cfg);
+   if(GC_HAS_ERROR(ctx)) goto failed_load;
+
+   /* no error, destroy the previous pool if we are reloading the config */
+   if(config_pool) {
+      apr_pool_destroy(config_pool);
+   }
+   config_pool = tmp_config_pool;
+
+   return;
+
+failed_load:
+   /* we failed to load the config file */
+   if(config_pool) {
+      /* we already have a running configuration, keep it and only log the error to not
+       * interrupt the already running service */
+      ctx->log(ctx,MAPCACHE_ERROR,"failed to reload config file %s: %s", conffile,ctx->get_error_message(ctx));
+      ctx->clear_errors(ctx);
+      ctx->config = old_cfg;
+      ctx->pool = config_pool;
+      apr_pool_destroy(tmp_config_pool);
+   }
+
+}
+
+int main(int argc, const char **argv) {
+   (void) signal(SIGTERM,handle_signal);
+   (void) signal(SIGUSR1,handle_signal);
+   apr_initialize();
+   atexit(apr_terminate);
    apr_pool_initialize();
    if(apr_pool_create(&global_pool,NULL) != APR_SUCCESS) {
       return 1;
    }
+   config_pool = NULL;
    mapcache_context_fcgi* globalctx = fcgi_context_create();
    mapcache_context* ctx = (mapcache_context*)globalctx;
-   mapcache_cfg *cfg = mapcache_configuration_create(ctx->pool);
-   ctx->config = cfg;
-
-   char *conffile  = getenv("MAPCACHE_CONFIG_FILE");
+   
+   conffile  = getenv("MAPCACHE_CONFIG_FILE");
    if(!conffile) {
       ctx->log(ctx,MAPCACHE_ERROR,"no config file found in MAPCACHE_CONFIG_FILE envirronement");
       return 1;
    }
-   ctx->log(ctx,MAPCACHE_DEBUG,"mapcache fcgi conf file: %s",conffile);
-   mapcache_configuration_parse(ctx,conffile,cfg,1);
-   if(GC_HAS_ERROR(ctx)) {
-      ctx->log(ctx,500,"failed to parse %s: %s",conffile,ctx->get_error_message(ctx));
-      return 1;
-   }
-   mapcache_configuration_post_config(ctx, cfg);
-   if(GC_HAS_ERROR(ctx)) {
-      ctx->log(ctx,500,"post-config failed for %s: %s",conffile,ctx->get_error_message(ctx));
-      return 1;
-   }
+   ctx->log(ctx,MAPCACHE_INFO,"mapcache fcgi conf file: %s",conffile);
    
+   
 #ifdef USE_FASTCGI
    while (FCGI_Accept() >= 0) {
 #endif
       apr_table_t *params;
-      apr_pool_create(&(ctx->pool),global_pool);
+      ctx->pool = config_pool;
+      if(!ctx->config || ctx->config->autoreload) {
+         load_config(ctx,conffile);
+         if(GC_HAS_ERROR(ctx)) {
+            fcgi_write_response(globalctx, mapcache_core_respond_to_error(ctx,NULL));
+            goto cleanup;
+         }
+      }
+      apr_pool_create(&(ctx->pool),config_pool);
       mapcache_request *request = NULL;
       char *pathInfo = getenv("PATH_INFO");
       
 
       params = mapcache_http_parse_param_string(ctx, getenv("QUERY_STRING"));
-      mapcache_service_dispatch_request(ctx,&request,pathInfo,params,cfg);
+      mapcache_service_dispatch_request(ctx,&request,pathInfo,params,ctx->config);
       if(GC_HAS_ERROR(ctx) || !request) {
          fcgi_write_response(globalctx, mapcache_core_respond_to_error(ctx,(request)?request->service:NULL));
          goto cleanup;
@@ -239,7 +310,7 @@
                fullhost,
                getenv("SCRIPT_NAME")
                );
-         http_response = mapcache_core_get_capabilities(ctx,request->service,req,url,pathInfo,cfg);
+         http_response = mapcache_core_get_capabilities(ctx,request->service,req,url,pathInfo,ctx->config);
       } else if( request->type == MAPCACHE_REQUEST_GET_TILE) {
          mapcache_request_get_tile *req_tile = (mapcache_request_get_tile*)request;
          http_response = mapcache_core_get_tile(ctx,req_tile);

Modified: trunk/mapserver/mapcache/src/mod_mapcache.c
===================================================================
--- trunk/mapserver/mapcache/src/mod_mapcache.c	2011-10-18 18:18:28 UTC (rev 12674)
+++ trunk/mapserver/mapcache/src/mod_mapcache.c	2011-10-19 12:30:06 UTC (rev 12675)
@@ -75,15 +75,73 @@
    va_start(args,message);
    char *msg = apr_pvsprintf(c->pool,message,args);
    va_end(args);
-   ap_log_error(APLOG_MARK, APLOG_INFO, 0, ctx->server,"%s",msg);
+   int ap_log_level;
+   switch(level) {
+      case MAPCACHE_DEBUG:
+         ap_log_level = APLOG_DEBUG;
+         break;
+      case MAPCACHE_INFO:
+         ap_log_level = APLOG_INFO;
+         break;
+      case MAPCACHE_NOTICE:
+         ap_log_level = APLOG_NOTICE;
+         break;
+      case MAPCACHE_WARN:
+         ap_log_level = APLOG_WARNING;
+         break;
+      case MAPCACHE_ERROR:
+         ap_log_level = APLOG_ERR;
+         break;
+      case MAPCACHE_CRIT:
+         ap_log_level = APLOG_CRIT;
+         break;
+      case MAPCACHE_ALERT:
+         ap_log_level = APLOG_ALERT;
+         break;
+      case MAPCACHE_EMERG:
+         ap_log_level = APLOG_EMERG;
+         break;
+      default:
+         ap_log_level = APLOG_WARNING;
+   }
+   ap_log_error(APLOG_MARK, ap_log_level, 0, ctx->server,"%s",msg);
 }
 
 void apache_context_request_log(mapcache_context *c, mapcache_log_level level, char *message, ...) {
    mapcache_context_apache_request *ctx = (mapcache_context_apache_request*)c;
    va_list args;
    va_start(args,message);
-   ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, ctx->request, "%s", apr_pvsprintf(c->pool,message,args));
    va_end(args);
+   int ap_log_level;
+   switch(level) {
+      case MAPCACHE_DEBUG:
+         ap_log_level = APLOG_DEBUG;
+         break;
+      case MAPCACHE_INFO:
+         ap_log_level = APLOG_INFO;
+         break;
+      case MAPCACHE_NOTICE:
+         ap_log_level = APLOG_NOTICE;
+         break;
+      case MAPCACHE_WARN:
+         ap_log_level = APLOG_WARNING;
+         break;
+      case MAPCACHE_ERROR:
+         ap_log_level = APLOG_ERR;
+         break;
+      case MAPCACHE_CRIT:
+         ap_log_level = APLOG_CRIT;
+         break;
+      case MAPCACHE_ALERT:
+         ap_log_level = APLOG_ALERT;
+         break;
+      case MAPCACHE_EMERG:
+         ap_log_level = APLOG_EMERG;
+         break;
+      default:
+         ap_log_level = APLOG_WARNING;
+   }
+   ap_log_rerror(APLOG_MARK, ap_log_level, 0, ctx->request, "%s", apr_pvsprintf(c->pool,message,args));
 }
 
 void mapcache_util_mutex_aquire(mapcache_context *gctx) {



More information about the mapserver-commits mailing list