merge updates in repository for HEAD
diff --git a/ChangeLog b/ChangeLog
index 063b7c2..dcc8c7f 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -5,7 +5,16 @@
 	* rivet/rivet-tcl/parray.tcl: ::rivet::parray now accepting alternative way to
 	return output.
 
-2023-05-14 Massimo Manghi <mxmanghi@apache.org>
+2023-05-18 Massimo Manghi <mxmanghi@apache.org>
+	* src/mod_rivet_ng/mod_rivet.c:
+	* src/mod_rivet_ng/mod_rivet.h:
+	* src/mod_rivet_ng/mod_rivet_common.c:
+	* src/mod_rivet_ng/rivetCore.c:
+	* src/mod_rivet_ng/worker_prefork_common.c: now storing the server record in the intepreter globals
+	to be used by Rivet_LogErrorCmd instead of module_globals->server when threads private data are
+	not available (like when ChildInitScript is executed)
+
+2023-05-04 Massimo Manghi <mxmanghi@apache.org>
 	* tests/rivet.test: more contributions provided by Scott Pitcher <scotty@svptechnicalservices.com.au>)
 	* tests/apachetest/apachetest.tcl: More improvements to the test script. Now
 	handling also Apache directive IncludeOptional and enabling execution control 
@@ -13,7 +22,6 @@
 	* tests/post.rvt:
 	* tests/post.test: add a test for ::rivet::raw_post
 
-	* 
 2023-04-11 Massimo Manghi <mxmanghi@apache.org>
 	* src/mod_rivet_ng/TclWebapache.c:
 	* src/mod_rivet_ng/rivetCore.c:
@@ -268,8 +276,7 @@
 	into their correct macro definitions
 
 2019-01-14 Massimo Manghi <mxmanghi@apache.org>
-	* src/mod_rivet_ng/rivetCore.c: add experimental ::rivet::thread_id
-   command
+	* src/mod_rivet_ng/rivetCore.c: add experimental ::rivet::thread_id command
 
 2019-01-05 Massimo Manghi <mxmanghi@apache.org>
 	* doc/: merging from 3.1 fixes to various broken docbook elements
diff --git a/src/mod_rivet_ng/mod_rivet.c b/src/mod_rivet_ng/mod_rivet.c
index 19b5dc4..3cf4f4c 100644
--- a/src/mod_rivet_ng/mod_rivet.c
+++ b/src/mod_rivet_ng/mod_rivet.c
@@ -238,7 +238,7 @@
 Rivet_RunServerInit (apr_pool_t *pPool, apr_pool_t *pLog, apr_pool_t *pTemp, server_rec *s)
 {
 #ifdef WIN32
-	char*	parent_pid_var = NULL;
+	char* parent_pid_var = NULL;
 #endif
 
     FILEDEBUGINFO;
@@ -262,8 +262,8 @@
 	 * (https://wiki.apache.org/httpd/ModuleLife)
 	 */
 	
-	#ifdef WIN32
-	
+#ifdef WIN32
+
 	/* if the environment variable AP_PARENT_PID is set
      * we know we are in a child process of the winnt MPM
      */
@@ -278,8 +278,8 @@
 		ap_log_perror(APLOG_MARK,APLOG_INFO,0,pPool,
 				 "AP_PARENT_PID undefined, we proceed with server initialization");
 	}
-	
-	#endif
+
+#endif
 	
     /* We don't create the cache here: it would make sense for prefork MPM
      * but threaded MPM bridges have their pool of threads. Each of them
@@ -444,9 +444,9 @@
     Tcl_InitNotifier();
 #endif
 
-    /* We can rely on the existence of module_globals only we are
+    /* We can rely on the existence of module_globals only when
      * running the prefork MPM, otherwise the pointer is NULL and
-     * the structure has to be filled with data
+     * the structure has to be allocated and filled with data
      */
 
     if (module_globals == NULL)
diff --git a/src/mod_rivet_ng/mod_rivet.h b/src/mod_rivet_ng/mod_rivet.h
index 68fa9f7..94d40fa 100644
--- a/src/mod_rivet_ng/mod_rivet.h
+++ b/src/mod_rivet_ng/mod_rivet.h
@@ -251,6 +251,7 @@
 
 typedef struct _rivet_interp_globals {
     Tcl_Namespace*      rivet_ns;           /* Rivet commands namespace             */
+    server_rec*         server;             /* Virtual host server rec              */
 } rivet_interp_globals;
 
 rivet_server_conf *Rivet_GetConf(request_rec *r);
diff --git a/src/mod_rivet_ng/mod_rivet_common.c b/src/mod_rivet_ng/mod_rivet_common.c
index 56908d9..c11131b 100644
--- a/src/mod_rivet_ng/mod_rivet_common.c
+++ b/src/mod_rivet_ng/mod_rivet_common.c
@@ -220,7 +220,7 @@
  *
  * Rivet_PerInterpInit --
  *
- *  Do the initialization that needs to happen to every interpreter.
+ *  Do the interpreter environment creation and initialization.
  *
  * Results:
  *  None.
@@ -249,11 +249,11 @@
     Tcl_SetAssocData (interp,"rivet",NULL,globals);
 
     /*
-     * the ::rivet namespace is the only information still stored
-     * in the interpreter global data
+     * we store in the globals some information relevant to
+     * the embedded interpreter work
      */
 
-    /* Rivet commands namespace is created */
+    /* the ::rivet namespace is created */
 
     globals->rivet_ns = Tcl_CreateNamespace (interp,RIVET_NS,NULL,
                                             (Tcl_NamespaceDeleteProc *)NULL);
diff --git a/src/mod_rivet_ng/rivetCore.c b/src/mod_rivet_ng/rivetCore.c
index ffa4381..b54c8c1 100644
--- a/src/mod_rivet_ng/rivetCore.c
+++ b/src/mod_rivet_ng/rivetCore.c
@@ -1766,7 +1766,7 @@
  *-----------------------------------------------------------------------------
  */
 
-TCL_CMD_HEADER( Rivet_LogErrorCmd )
+TCL_CMD_HEADER(Rivet_LogErrorCmd)
 {
     char *message = NULL;
 
@@ -1853,9 +1853,16 @@
      * root server name stored in the module_globals
      */
 
-    serverRec = ((private == NULL) || (private->r == NULL)) ? module_globals->server : private->r->server;
-
-    ap_log_error (APLOG_MARK, apLogLevel, 0, serverRec, "%s", message);
+    if ((private == NULL) || (private->r == NULL))
+    {
+        rivet_interp_globals* globals = Tcl_GetAssocData(interp, "rivet", NULL);
+        serverRec = globals->server;
+    }
+    else
+    {
+        serverRec = private->r->server;
+    }
+    ap_log_error (APLOG_MARK,apLogLevel,0,serverRec,"%s",message);
     return TCL_OK;
 }
 
@@ -2159,7 +2166,7 @@
 DLLEXPORT int
 Rivet_InitCore(Tcl_Interp *interp,rivet_thread_private* private)
 {
-    rivet_server_conf*      server_conf;
+    rivet_server_conf* server_conf;
 
     RIVET_OBJ_CMD ("makeurl",Rivet_MakeURL,private);
     RIVET_OBJ_CMD ("headers",Rivet_Headers,private);
diff --git a/src/mod_rivet_ng/worker_prefork_common.c b/src/mod_rivet_ng/worker_prefork_common.c
index a08f993..06ae464 100644
--- a/src/mod_rivet_ng/worker_prefork_common.c
+++ b/src/mod_rivet_ng/worker_prefork_common.c
@@ -72,7 +72,7 @@
 
 /* -- Rivet_VirtualHostsInterps
  *
- * The server_rec chain is walked through and server configurations is read to
+ * The server_rec chain is walked through and server configurations are read to
  * set up the thread private configuration and interpreters database
  *
  *  Arguments:
@@ -91,7 +91,7 @@
 
 rivet_thread_private* Rivet_VirtualHostsInterps (rivet_thread_private* private)
 {
-    server_rec*         s;
+    server_rec*         vhost_server;
     server_rec*         root_server = module_globals->server;
     rivet_server_conf*  root_server_conf;
     rivet_server_conf*  myrsc;
@@ -145,11 +145,11 @@
 
     parentfunction = root_server_conf->rivet_child_init_script;
 
-    for (s = root_server; s != NULL; s = s->next)
+    for (vhost_server = root_server; vhost_server != NULL; vhost_server = vhost_server->next)
     {
-        rivet_thread_interp*   rivet_interp;
+        rivet_thread_interp*  rivet_interp;
 
-        myrsc = RIVET_SERVER_CONF(s->module_config);
+        myrsc = RIVET_SERVER_CONF(vhost_server->module_config);
 
         /* by default we assign the root_interpreter as
          * interpreter of the virtual host. In case of separate
@@ -159,7 +159,7 @@
 
         rivet_interp = root_interp;
 
-        if (s == root_server)
+        if (vhost_server == root_server)
         {
             Tcl_RegisterChannel(rivet_interp->interp,*rivet_interp->channel);
         }
@@ -187,16 +187,13 @@
         /* interpreter base running scripts definition and initialization */
 
         rivet_interp->scripts = Rivet_RunningScripts (private->pool,rivet_interp->scripts,myrsc);
-
-        //private->ext->interps[myrsc->idx] = rivet_interp;
-
         RIVET_POKE_INTERP(private,myrsc,rivet_interp);
 
         /* Basic Rivet packages and libraries are loaded here */
 
         if ((rivet_interp->flags & RIVET_INTERP_INITIALIZED) == 0)
         {
-            Rivet_PerInterpInit(rivet_interp, private, s, private->pool);
+            Rivet_PerInterpInit(rivet_interp,private,vhost_server,private->pool);
         }
 
         /* It seems that allocating from a shared APR memory pool is not thread safe,
@@ -209,21 +206,21 @@
          */
 
         apr_thread_mutex_lock(module_globals->pool_mutex);
-        myrsc->server_name = (char*) apr_pstrdup (private->pool, s->server_hostname);
+        myrsc->server_name = (char*) apr_pstrdup (private->pool,vhost_server->server_hostname);
         apr_thread_mutex_unlock(module_globals->pool_mutex);
 
         /* when configured a child init script gets evaluated */
 
         function = myrsc->rivet_child_init_script;
         if (function &&
-            (s == root_server || module_globals->separate_virtual_interps || function != parentfunction))
+            (vhost_server == root_server || module_globals->separate_virtual_interps || function != parentfunction))
         {
             char*       errmsg = MODNAME ": Error in Child init script: %s";
-            Tcl_Interp* interp = rivet_interp->interp;
             Tcl_Obj*    tcl_child_init = Tcl_NewStringObj(function,-1);
+            rivet_interp_globals* globals = NULL;
 
             Tcl_IncrRefCount(tcl_child_init);
-            Tcl_Preserve (interp);
+            Tcl_Preserve (rivet_interp->interp);
 
             /* There is a lot of passing pointers around among various structures.
              * We should understand if this is all that necessary.
@@ -238,17 +235,24 @@
              * prepared TODO
              */
 
+            globals = Tcl_GetAssocData(rivet_interp->interp, "rivet", NULL);
+
+            /*
+             * The current server record is stored to enable ::rivet::apache_log_error and
+             * other commands to log error messages in the virtual host's designated log file
+             */
+
+            globals->server = vhost_server;
             private->running_conf = myrsc;
 
-            if (Tcl_EvalObjEx(interp,tcl_child_init, 0) != TCL_OK) {
-                ap_log_error(APLOG_MARK, APLOG_ERR, APR_EGENERAL, root_server,
-                             errmsg, function);
-                ap_log_error(APLOG_MARK, APLOG_ERR, APR_EGENERAL, root_server,
-                             "errorCode: %s", Tcl_GetVar(interp, "errorCode", 0));
-                ap_log_error(APLOG_MARK, APLOG_ERR, APR_EGENERAL, root_server,
-                             "errorInfo: %s", Tcl_GetVar(interp, "errorInfo", 0));
+            if (Tcl_EvalObjEx(rivet_interp->interp,tcl_child_init, 0) != TCL_OK) {
+                ap_log_error(APLOG_MARK, APLOG_ERR,APR_EGENERAL,vhost_server,errmsg, function);
+                ap_log_error(APLOG_MARK, APLOG_ERR, APR_EGENERAL,vhost_server,
+                             "errorCode: %s", Tcl_GetVar(rivet_interp->interp, "errorCode", 0));
+                ap_log_error(APLOG_MARK, APLOG_ERR, APR_EGENERAL,vhost_server,
+                             "errorInfo: %s", Tcl_GetVar(rivet_interp->interp, "errorInfo", 0));
             }
-            Tcl_Release (interp);
+            Tcl_Release (rivet_interp->interp);
             Tcl_DecrRefCount(tcl_child_init);
         }
     }