Index: /trunk/FACT++/src/EventBuilder.c
===================================================================
--- /trunk/FACT++/src/EventBuilder.c	(revision 15340)
+++ /trunk/FACT++/src/EventBuilder.c	(revision 15341)
@@ -7,4 +7,5 @@
 #include <stdlib.h>
 #include <stdint.h>
+#include <stdarg.h>
 #include <unistd.h>
 #include <stdio.h>
@@ -126,6 +127,19 @@
 WRK_DATA mBuffer[MAX_EVT * MAX_RUN];    //local working space
 
-#define MXSTR 1000
-char str[MXSTR];
+//#define MXSTR 1000
+//char str[MXSTR];
+
+void factPrintf(int severity, int id, const char *fmt, ...)
+{
+    char str[1000];
+
+    va_list ap;
+    va_start(ap, fmt);
+    vsnprintf(str, 1000, fmt, ap);
+    va_end(ap);
+
+    factOut(severity, id, str);
+}
+
 
 #define THOMAS_MALLOC
@@ -227,6 +241,5 @@
                     if (EtiMemoryChunks[i].nFreeSlots < 0)
                     {
-                        snprintf(str, MXSTR, "Number of free slot in chunk %d went below zero (%d) slot: %d", i, EtiMemoryChunks[i].nFreeSlots, j);
-                        factOut(kError, 000, str);
+                        factPrintf(kError, 0, "Number of free slot in chunk %d went below zero (%d) slot: %d", i, EtiMemoryChunks[i].nFreeSlots, j);
                         return NULL;
                     }
@@ -239,6 +252,5 @@
             //If I reach this point then we have a problem because it should have found
             //a free spot just above.
-            snprintf(str, MXSTR, "Could not find a free slot in a chunk that's supposed to have some. chunk=%d", i);
-            factOut(kError, 000, str);
+            factPrintf(kError, 0, "Could not find a free slot in a chunk that's supposed to have some. chunk=%d", i);
             return NULL;
         }
@@ -252,6 +264,5 @@
     if (EtiMemoryChunks[numAllocatedChunks].pointers[0] == NULL)
     {
-        snprintf (str, MXSTR, "Allocation of %lu bytes failed. %d chunks are currently allocated (max allowed %lu bytes)", MAX_CHUNK_SIZE, numAllocatedChunks, g_maxMem);
-        factOut (kError, 000, str);
+        factPrintf(kError, 0, "Allocation of %lu bytes failed. %d chunks are currently allocated (max allowed %lu bytes)", MAX_CHUNK_SIZE, numAllocatedChunks, g_maxMem);
         return NULL;
     }
@@ -279,6 +290,5 @@
     if (currentChunk->events[mBufferMapping[evtIndex].slot] != evtId)
     {
-        snprintf (str, MXSTR, "Mismatch in chunk mapping table. Expected evtId %d. Got %d. No memory was freed.", evtId, currentChunk->events[mBufferMapping[evtIndex].slot]);
-        factOut (kError, 000, str);
+        factPrintf(kError, 0, "Mismatch in chunk mapping table. Expected evtId %d. Got %d. No memory was freed.", evtId, currentChunk->events[mBufferMapping[evtIndex].slot]);
         return;
     }
@@ -296,6 +306,5 @@
         if (EtiMemoryChunks[chunkIndex].pointers[0] == NULL)
         {
-            snprintf(str, MXSTR, "Chunk %d not allocated as it ought to be. Skipping memory release.", chunkIndex);
-            factOut(kError, 000, str);
+            factPrintf(kError, 0, "Chunk %d not allocated as it ought to be. Skipping memory release.", chunkIndex);
             return;
         }
@@ -385,10 +394,10 @@
 /*-----------------------------------------------------------------*/
 
+
+
 int
 runFinish1 (uint32_t runnr)
 {
-   snprintf (str, MXSTR, "Should finish run %d (but not yet possible)",
-             runnr);
-   factOut (kInfo, 173, str);   //but continue anyhow
+   factPrintf(kInfo, 173, "Should finish(1) run %d (but not yet possible)", runnr);
    return 0;
 }
@@ -396,8 +405,6 @@
 runFinish (uint32_t runnr)
 {
-   snprintf (str, MXSTR, "Should finish run %d (but not yet possible)",
-             runnr);
-   factOut (kInfo, 173, str);   //but continue anyhow
-   return 0;
+    factPrintf(kInfo, 173, "Should finish run %d (but not yet possible)", runnr);
+    return 0;
 }
 
@@ -431,9 +438,7 @@
       j = close (rd->socket);
       if (j > 0) {
-          snprintf (str, MXSTR, "Closing socket %d failed: %m (close,rc=%d)", sid, errno);
-         factOut (kFatal, 771, str);
+          factPrintf(kFatal, 771, "Closing socket %d failed: %m (close,rc=%d)", sid, errno);
       } else {
-         snprintf (str, MXSTR, "Succesfully closed socket %d", sid);
-         factOut (kInfo, 771, str);
+          factPrintf(kInfo, 771, "Succesfully closed socket %d", sid);
       }
    }
@@ -462,6 +467,5 @@
       rd->rBuf = malloc (sizeof (CNV_FACT));
       if (rd->rBuf == NULL) {
-         snprintf (str, MXSTR, "Could not create local buffer %d (malloc failed)", sid);
-         factOut (kFatal, 774, str);
+         factPrintf(kFatal, 774, "Could not create local buffer %d (malloc failed)", sid);
          rd->sockStat = 77;
          return -3;
@@ -471,6 +475,5 @@
 
    if ((rd->socket = socket (PF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0)) <= 0) {
-       snprintf (str, MXSTR, "Generating socket %d failed: %m (socket,rc=%d)", sid, errno);
-      factOut (kFatal, 773, str);
+       factPrintf(kFatal, 773, "Generating socket %d failed: %m (socket,rc=%d)", sid, errno);
       rd->sockStat = 88;
       return -2;
@@ -478,26 +481,21 @@
    optval = 1;
    if (setsockopt (rd->socket, SOL_SOCKET, SO_KEEPALIVE, &optval, optlen) < 0) {
-       snprintf (str, MXSTR, "Setting SO_KEEPALIVE for socket %d failed: %m (setsockopt,rc=%d)", sid, errno);
-      factOut (kInfo, 173, str);        //but continue anyhow
+       factPrintf(kInfo, 173, "Setting SO_KEEPALIVE for socket %d failed: %m (setsockopt,rc=%d)", sid, errno);
    }
    optval = 10;                 //start after 10 seconds
    if (setsockopt (rd->socket, SOL_TCP, TCP_KEEPIDLE, &optval, optlen) < 0) {
-      snprintf (str, MXSTR, "Setting TCP_KEEPIDLE for socket %d failed: %m (setsockopt,rc=%d)", sid, errno);
-      factOut (kInfo, 173, str);        //but continue anyhow
+       factPrintf(kInfo, 173, "Setting TCP_KEEPIDLE for socket %d failed: %m (setsockopt,rc=%d)", sid, errno);
    }
    optval = 10;                 //do every 10 seconds
    if (setsockopt (rd->socket, SOL_TCP, TCP_KEEPINTVL, &optval, optlen) < 0) {
-      snprintf (str, MXSTR, "Setting TCP_KEEPINTVL for socket %d failed: %m (setsockopt,rc=%d)", sid, errno);
-      factOut (kInfo, 173, str);        //but continue anyhow
+      factPrintf(kInfo, 173, "Setting TCP_KEEPINTVL for socket %d failed: %m (setsockopt,rc=%d)", sid, errno);
    }
    optval = 2;                  //close after 2 unsuccessful tries
    if (setsockopt (rd->socket, SOL_TCP, TCP_KEEPCNT, &optval, optlen) < 0) {
-      snprintf (str, MXSTR, "Setting TCP_KEEPCNT for socket %d failed: %m (setsockopt,rc=%d)", sid, errno);
-      factOut (kInfo, 173, str);        //but continue anyhow
-   }
-
-
-   snprintf (str, MXSTR, "Successfully generated socket %d", sid);
-   factOut (kInfo, 773, str);
+      factPrintf(kInfo, 173, "Setting TCP_KEEPCNT for socket %d failed: %m (setsockopt,rc=%d)", sid, errno);
+   }
+
+   factPrintf(kInfo, 773, "Successfully generated socket %d", sid);
+
    rd->sockStat = -1;           //try to (re)open socket
    rd->errCnt = 0;
@@ -576,6 +574,5 @@
 
    if (nRoi[0] < 0 || nRoi[0] > 1024) {
-       snprintf (str, MXSTR, "Illegal roi in channel 0: %d (allowed: 0<=roi<=1024)", nRoi[0]);
-      factOut (kError, 999, str);
+       factPrintf(kError, 999, "Illegal roi in channel 0: %d (allowed: 0<=roi<=1024)", nRoi[0]);
       gj.badRoiR++;
       gj.badRoi[b]++;
@@ -585,6 +582,5 @@
    for (int jr = 1; jr < 8; jr++) {
       if (nRoi[jr] != nRoi[0]) {
-         snprintf (str, MXSTR, "Mismatch of roi (%d) in channel %d with roi (%d) in channel 0.", nRoi[jr], jr, nRoi[0]);
-         factOut (kError, 711, str);
+         factPrintf(kError, 711, "Mismatch of roi (%d) in channel %d with roi (%d) in channel 0.", nRoi[jr], jr, nRoi[0]);
          gj.badRoiB++;
          gj.badRoi[b]++;
@@ -593,6 +589,5 @@
    }
    if (nRoi[8] < nRoi[0]) {
-      snprintf (str, MXSTR, "Mismatch of roi (%d) in channel 8. Should be larger or equal than the roi (%d) in channel 0.", nRoi[8], nRoi[0]);
-      factOut (kError, 712, str);
+      factPrintf(kError, 712, "Mismatch of roi (%d) in channel 8. Should be larger or equal than the roi (%d) in channel 0.", nRoi[8], nRoi[0]);
       gj.badRoiB++;
       gj.badRoi[b]++;
@@ -609,7 +604,6 @@
          if (mBuffer[i].nRoi != nRoi[0]
              || mBuffer[i].nRoiTM != nRoi[8]) {
-            snprintf (str, MXSTR, "Mismatch of roi within event. Expected roi=%d and roi_tm=%d, got %d and %d.",
-                      mBuffer[i].nRoi, mBuffer[i].nRoiTM, nRoi[0], nRoi[8]);
-            factOut (kError, 821, str);
+            factPrintf(kError, 821, "Mismatch of roi within event. Expected roi=%d and roi_tm=%d, got %d and %d.",
+                       mBuffer[i].nRoi, mBuffer[i].nRoiTM, nRoi[0], nRoi[8]);
             gj.badRoiE++;
             gj.badRoi[b]++;
@@ -636,6 +630,5 @@
    //event does not yet exist; create it
    if (evFree < 0) {            //no space available in ctrl
-      snprintf (str, MXSTR, "No control slot to keep event %d", evID);
-      factOut (kError, 881, str);
+      factPrintf(kError, 881, "No control slot to keep event %d", evID);
       return -1;
    }
@@ -660,7 +653,6 @@
          if (runCtrl[k].roi0 != nRoi[0]
              || runCtrl[k].roi8 != nRoi[8]) {
-            snprintf (str, MXSTR, "Mismatch of roi within run. Expected roi=%d and roi_tm=%d, got %d and %d.",
-                      runCtrl[k].roi0, runCtrl[k].roi8, nRoi[0], nRoi[8]);
-            factOut (kError, 931, str);
+            factPrintf(kError, 931, "Mismatch of roi within run. Expected roi=%d and roi_tm=%d, got %d and %d.",
+                       runCtrl[k].roi0, runCtrl[k].roi8, nRoi[0], nRoi[8]);
             gj.badRoiR++;
             gj.badRoi[b]++;
@@ -679,13 +671,12 @@
 
    if (evFree < 0 && jold < 0) {
-      snprintf (str, MXSTR, "Not able to register the new run %d", runID);
-      factOut (kFatal, 883, str);
+      factPrintf(kFatal, 883, "Not able to register the new run %d", runID);
       return -1001;
-   } else {
+   }
+
       if (evFree < 0)
          evFree = jold;
-      snprintf (str, MXSTR, "New run %d (evFree=%d) registered with roi=%d and roi_tm=%d", runID,
-                evFree, nRoi[0], nRoi[8]);
-      factOut (kInfo, 503, str);
+      factPrintf(kInfo, 503, "New run %d (evFree=%d) registered with roi=%d and roi_tm=%d", runID,
+                 evFree, nRoi[0], nRoi[8]);
       runCtrl[evFree].runId = runID;
       runCtrl[evFree].roi0 = nRoi[0];
@@ -707,5 +698,4 @@
          runTail[evFree].nEventsBad =
          runTail[evFree].PCtime0 = runTail[evFree].PCtimeX = 0;
-   }
 
  RUNFOUND:
@@ -736,6 +726,5 @@
    mBuffer[i].buffer  = NULL;
    if (mBuffer[i].FADhead == NULL) {
-      snprintf (str, MXSTR, "malloc header failed for event %d", evID);
-      factOut (kError, 882, str);
+      factPrintf(kError, 882, "malloc header failed for event %d", evID);
       return -12;
    }
@@ -765,7 +754,5 @@
        else
        {
-           snprintf (str, MXSTR, "No memory left to keep event %6d sock %3d",
-                     evID, sk);
-           factOut (kDebug, 882, str);
+           factPrintf(kDebug, 882, "No memory left to keep event %6d sock %3d", evID, sk);
         }
 #endif
@@ -780,6 +767,5 @@
    mBuffer[i].buffer  = NULL;
    if (mBuffer[i].FADhead == NULL) {
-      snprintf (str, MXSTR, "malloc header failed for event %d", evID);
-      factOut (kError, 882, str);
+      factPrintf(kError, 882, "malloc header failed for event %d", evID);
       return -12;
    }
@@ -876,8 +862,6 @@
 
 #ifdef EVTDEBUG
-   snprintf (str, MXSTR,
-             "%5d %8d start new evt  %8d %8d sock %3d len %5d t %10d", evID,
-             runID, i, evtCtrl.lastPtr, sk, fadlen, mBuffer[i].pcTime);
-   factOut (kDebug, -11, str);
+   factPrintf(kDebug, -11, "%5d %8d start new evt  %8d %8d sock %3d len %5d t %10d",
+              evID, runID, i, evtCtrl.lastPtr, sk, fadlen, mBuffer[i].pcTime);
 #endif
    evtCtrl.lastPtr++;
@@ -1041,6 +1025,5 @@
 
 
-   snprintf (str, MXSTR, "Start initializing (readFAD)");
-   factOut (kInfo, -1, str);
+   factPrintf(kInfo, -1, "Start initializing (readFAD)");
 
 //   int cpu = 7;                 //read thread
@@ -1130,6 +1113,5 @@
       mBufInit ();              //initialize buffers
 
-      snprintf (str, MXSTR, "End   initializing (readFAD)");
-      factOut (kInfo, -1, str);
+      factPrintf(kInfo, -1, "End   initializing (readFAD)");
    }
 
@@ -1249,6 +1231,5 @@
                gi.numConn[b]++;
                gj.numConn[b]++;
-               snprintf (str, MXSTR, "New connection %d (number of connections: %d)", b, gi.numConn[b]);
-               factOut (kInfo, -1, str);
+               factPrintf(kInfo, -1, "New connection %d (number of connections: %d)", b, gi.numConn[b]);
             }
          }
@@ -1259,7 +1240,5 @@
              {
 #ifdef EVTDEBUG
-               snprintf (str, MXSTR, "do not read from socket %d  %d", i,
-                         rd[i].bufLen);
-               factOut (kDebug, 301, str);
+               factPrintf(kDebug, 301, "do not read from socket %d  %d", i, rd[i].bufLen);
 #endif
                  continue;
@@ -1278,15 +1257,11 @@
                   memcpy (&rd[i].xBuf->B[rd[i].bufPos],
                           &rd[i].rBuf->B[rd[i].bufPos], jrd);
-                  snprintf (str, MXSTR,
-                            "read sock %3d bytes %5d len %5d first %d %d", i,
-                            jrd, rd[i].bufLen, rd[i].rBuf->B[rd[i].bufPos],
-                            rd[i].rBuf->B[rd[i].bufPos + 1]);
-                  factOut (kDebug, 301, str);
+                  factPrintf(kDebug, 301, "read sock %3d bytes %5d len %5d first %d %d",
+                             i, jrd, rd[i].bufLen, rd[i].rBuf->B[rd[i].bufPos], rd[i].rBuf->B[rd[i].bufPos + 1]);
                }
 #endif
 
                if (jrd == 0) {  //connection has closed ...
-                  snprintf (str, MXSTR, "Socket %d closed by FAD", i);
-                  factOut (kInfo, 441, str);
+                  factPrintf(kInfo, 441, "Socket %d closed by FAD", i);
                   GenSock (s0, i, 0, NULL, &rd[i]);
                   gi.gotErr[b]++;
@@ -1298,6 +1273,5 @@
                } else if (jrd < 0) {    //did not read anything
                   if (errno != EAGAIN && errno != EWOULDBLOCK) {
-                      snprintf (str, MXSTR, "Reading from socket %d failed: %m (recv,rc=%d)", i, errno);
-                     factOut (kError, 442, str);
+                      factPrintf(kError, 442, "Reading from socket %d failed: %m (recv,rc=%d)", i, errno);
                      gi.gotErr[b]++;
                   } else
@@ -1329,7 +1303,5 @@
 //         just do nothing
 #ifdef EVTDEBUG
-               snprintf (str, MXSTR, "skipping %d bytes on socket %d", jrd,
-                         i);
-               factOut (kInfo, 301, str);
+               factPrintf(kInfo, 301, "skipping %d bytes on socket %d", jrd, i);
 #endif
                continue;
@@ -1363,9 +1335,7 @@
                       rd[i].rBuf->B[rd[i].fadLen - 2] != stop.B[1]) {
                      gi.evtErr++;
-                     snprintf (str, MXSTR,
-                               "End-of-event flag wrong on socket %3d for event %4d (len=%5d), expected %3d %3d, got %3d %3d",
-                               i, rd[i].evtID, rd[i].fadLen, stop.B[0], stop.B[1],
-                               rd[i].rBuf->B[rd[i].fadLen - 1], rd[i].rBuf->B[rd[i].fadLen - 2]);
-                     factOut (kError, 301, str);
+                     factPrintf(kError, 301, "End-of-event flag wrong on socket %3d for event %4d (len=%5d), expected %3d %3d, got %3d %3d",
+                                i, rd[i].evtID, rd[i].fadLen, stop.B[0], stop.B[1],
+                                rd[i].rBuf->B[rd[i].fadLen - 1], rd[i].rBuf->B[rd[i].fadLen - 2]);
                      //goto EndBuf;
                      continue;
@@ -1373,6 +1343,5 @@
 #ifdef EVTDEBUG
                   } else {
-                     snprintf (str, MXSTR,
-                               "good  end of buffer found sock %3d len %5d %d %d : %d %d - %d %d : %d %d",
+                     factPrintf(kDebug, 301, "good  end of buffer found sock %3d len %5d %d %d : %d %d - %d %d : %d %d",
                                i, rd[i].fadLen, rd[i].rBuf->B[0],
                                rd[i].rBuf->B[1], start.B[1], start.B[0],
@@ -1380,5 +1349,4 @@
                                rd[i].rBuf->B[rd[i].fadLen - 1], stop.B[1],
                                stop.B[0]);
-                     factOut (kDebug, 301, str);
 #endif
                   }
@@ -1446,6 +1414,5 @@
                       if (checkRoi != roi[0])
                       {
-                          snprintf (str, MXSTR, "Inconsistent Roi accross boards B=%d, expected %d, got %d", kr, checkRoi, roi[0]);
-                          factOut (kError, 1, str);
+                          factPrintf(kError, 1, "Inconsistent Roi accross boards B=%d, expected %d, got %d", kr, checkRoi, roi[0]);
 //                          goto EndBuf;
                           continue;
@@ -1463,6 +1430,5 @@
                          if (checkRoi != roi[jr])
                          {
-                             snprintf (str, MXSTR, "Inconsistent Roi accross patches B=%d P=%d, expected %d, got %d", kr, jr, roi[jr], checkRoi);
-                             factOut (kFatal, 1, str);
+                             factPrintf(kFatal, 1, "Inconsistent Roi accross patches B=%d P=%d, expected %d, got %d", kr, jr, roi[jr], checkRoi);
 //                             goto EndBuf;
                              continue;
@@ -1492,6 +1458,5 @@
 #ifdef EVTDEBUG
                      if (rd[i].bufLen != 0) {
-                        snprintf (str, MXSTR, "something screwed up");
-                        factOut (kFatal, 1, str);
+                        factPrintf(kFatal, 1, "something screwed up");
                      }
 #endif
@@ -1512,14 +1477,10 @@
                                      rd[i].fadLen);
                   if (xchk != 0) {
-                     snprintf (str, MXSTR, "ERROR OVERWRITE %d %d on port %d",
-                               xchk, rd[i].fadLen, i);
-                     factOut (kFatal, 1, str);
+                     factPrintf(kFatal, 1, "ERROR OVERWRITE %d %d on port %d", xchk, rd[i].fadLen, i);
 
                      uint iq;
                      for (iq = 0; iq < rd[i].fadLen; iq++) {
                         if (rd[i].rBuf->B[iq] != rd[i].xBuf->B[iq]) {
-                           snprintf (str, MXSTR, "ERROR %4d %4d %x %x", i, iq,
-                                     rd[i].rBuf->B[iq], rd[i].xBuf->B[iq]);
-                           factOut (kFatal, 1, str);
+                           factPrintf(kFatal, 1, "ERROR %4d %4d %x %x", i, iq, rd[i].rBuf->B[iq], rd[i].xBuf->B[iq]);
                         }
                      }
@@ -1531,16 +1492,13 @@
                   int fadCrate = fadBoard / 256;
                   if (boardId != (fadCrate * 10 + fadBoard % 256)) {
-                     snprintf (str, MXSTR, "Board ID mismatch. Expected %d, got %d (C=%d, B=%d)",
-                               boardId, fadBoard, fadCrate, fadBoard % 256);
-                     factOut (kWarn, 301, str);
+                     factPrintf(kWarn, 301, "Board ID mismatch. Expected %d, got %d (C=%d, B=%d)",
+                                boardId, fadBoard, fadCrate, fadBoard % 256);
                   }
                   if (mBuffer[evID].board[boardId] != -1) {
-                     snprintf (str, MXSTR,
-                               "Got event %5d from board %3d (i=%3d, len=%5d) twice: Starts with %3d %3d - ends with %3d %3d",
+                     factPrintf(kWarn, 501, "Got event %5d from board %3d (i=%3d, len=%5d) twice: Starts with %3d %3d - ends with %3d %3d",
                                evID, boardId, i, rd[i].fadLen,
                                rd[i].rBuf->B[0], rd[i].rBuf->B[1],
                                rd[i].rBuf->B[rd[i].fadLen - 2],
                                rd[i].rBuf->B[rd[i].fadLen - 1]);
-                     factOut (kWarn, 501, str);
 //                     goto EndBuf;       //--> skip Board
                      //rd[i].bufTyp = 0;     //ready to read next header
@@ -1614,8 +1572,6 @@
                               if (++runCtrl[ir].lastEvt == 0) {
                                  gotNewRun (actrun, mBuffer[evID].FADhead);
-                                 snprintf (str, MXSTR, "gotNewRun called for run %d, event %d",
-                                           mBuffer[evID].runNum,
-                                           mBuffer[evID].evNum);
-                                 factOut (kInfo, 1, str);
+                                 factPrintf(kInfo, 1, "gotNewRun called for run %d, event %d",
+                                            mBuffer[evID].runNum, mBuffer[evID].evNum);
                                  break;
                               }
@@ -1624,9 +1580,6 @@
                      }
 #ifdef EVTDEBUG
-                     snprintf (str, MXSTR,
-                               "%5d complete event roi %4d roiTM %d cpy %8d %5d",
-                               mBuffer[evID].evNum, roi[0], roi[8] - roi[0],
-                               qncpy, qnrun);
-                     factOut (kDebug, -1, str);
+                     factPrintf(kDebug, -1, "%5d complete event roi %4d roiTM %d cpy %8d %5d",
+                               mBuffer[evID].evNum, roi[0], roi[8] - roi[0], qncpy, qnrun);
 #endif
                      //complete event read ---> flag for next processing
@@ -1689,7 +1642,5 @@
                   //{
                      if (rd[i].skip > 0) {
-                        snprintf (str, MXSTR, "Skipped %d bytes on port %d",
-                                  rd[i].skip, i);
-                        factOut (kInfo, 666, str);
+                        factPrintf(kInfo, 666, "Skipped %d bytes on port %d", rd[i].skip, i);
                         rd[i].skip = 0;
                      }
@@ -1736,9 +1687,6 @@
                      int fadcrate = fadboard / 256;
                      fadboard = (fadcrate * 10 + fadboard % 256);
-                     snprintf (str, MXSTR,
-                               "sk %3d head: %5d %5d %5d %10d %4d %6d", i,
-                               rd[i].fadLen, rd[i].evtID, rd[i].ftmID,
-                               rd[i].runID, fadboard, jrd);
-                     factOut (kDebug, 1, str);
+                     factPrintf(kDebug, 1, "sk %3d head: %5d %5d %5d %10d %4d %6d",
+                                i, rd[i].fadLen, rd[i].evtID, rd[i].ftmID, rd[i].runID, fadboard, jrd);
 #endif
 
@@ -1762,6 +1710,5 @@
 
 #ifdef EVTDEBUG
-      snprintf (str, MXSTR, "Loop ---- %3d --- %8d", numokx, jrdx);
-      factOut (kDebug, -1, str);
+      factDebug(kDebug, -1, "Loop ---- %3d --- %8d", numokx, jrdx);
 #endif
 
@@ -1795,9 +1742,10 @@
                    && evtCtrl.pcTime[k0] < g_actTime - 30) {
                   int id = evtCtrl.evtBuf[k0];
-                  snprintf (str, MXSTR, "%5d skip incomplete evt %8d %8d %2d", mBuffer[id].evNum, evtCtrl.evtBuf[k0], k0,
-                            evtCtrl.evtStat[k0]);
-                  factOut (kWarn, 601, str);
+                  factPrintf(kWarn, 601, "%5d skip incomplete evt %8d %8d %2d",
+                            mBuffer[id].evNum, evtCtrl.evtBuf[k0], k0, evtCtrl.evtStat[k0]);
 
                   uint64_t report = 0;
+
+                  char str[1000];
 
                   int ik,ib,jb;
@@ -1821,5 +1769,5 @@
                   str[ik++] = '|';
                   str[ik]   = 0;
-                  factOut (kWarn, 601, str);
+                  factOut(kWarn, 601, str);
 
                   factReportIncomplete(report);
@@ -1835,7 +1783,5 @@
                int id = evtCtrl.evtBuf[k0];
 #ifdef EVTDEBUG
-               snprintf (str, MXSTR, "%5d free event buffer, nb=%3d",
-                         mBuffer[id].evNum, mBuffer[id].nBoard);
-               factOut (kDebug, -1, str);
+               factPrintf(kDebug, -1, "%5d free event buffer, nb=%3d", mBuffer[id].evNum, mBuffer[id].nBoard);
 #endif
                mBufFree (id);   //event written--> free memory
@@ -1891,6 +1837,5 @@
    }                            //and do next loop over all sockets ...
 
-   snprintf (str, MXSTR, "Stop reading ... RESET=%d", g_reset);
-   factOut (kInfo, -1, str);
+   factPrintf(kInfo, -1, "Stop reading ... RESET=%d", g_reset);
 
    if (g_reset > 0) {
@@ -1918,6 +1863,5 @@
    if (gi_resetS > 0) {
       //must close all open sockets ...
-      snprintf (str, MXSTR, "Close all sockets...");
-      factOut (kInfo, -1, str);
+      factPrintf(kInfo, -1, "Close all sockets...");
       for (i = 0; i < MAX_SOCK; i++) {
          if (rd[i].sockStat == 0) {
@@ -1960,10 +1904,9 @@
       if (gi_resetR == 1) {
          minclear = 900;
-         snprintf (str, MXSTR, "Drain all buffers ...");
+         factPrintf(kInfo, -1, "Drain all buffers ...");
       } else {
          minclear = 0;
-         snprintf (str, MXSTR, "Flush all buffers ...");
-      }
-      factOut (kInfo, -1, str);
+         factPrintf(kInfo, -1, "Flush all buffers ...");
+      }
 
       int numclear = 1;
@@ -1980,7 +1923,5 @@
                int id = evtCtrl.evtBuf[k0];
 #ifdef EVTDEBUG
-               snprintf (str, MXSTR, "ev %5d free event buffer, nb=%3d",
-                         mBuffer[id].evNum, mBuffer[id].nBoard);
-               factOut (kDebug, -1, str);
+               factPrintf(kDebug, -1, "ev %5d free event buffer, nb=%3d", mBuffer[id].evNum, mBuffer[id].nBoard);
 #endif
                mBufFree (id);   //event written--> free memory
@@ -2009,6 +1950,5 @@
       }
 
-      snprintf (str, MXSTR, "Continue read Process ...");
-      factOut (kInfo, -1, str);
+      factPrintf(kInfo, -1, "Continue read Process ...");
       gi_reset = 0;
       goto START;
@@ -2017,10 +1957,8 @@
 
 
-   snprintf (str, MXSTR, "Exit read Process...");
-   factOut (kInfo, -1, str);
+   factPrintf(kInfo, -1, "Exit read Process...");
 
 #ifdef THOMAS_MALLOC
-   snprintf (str, MXSTR, "%ld Bytes flaged as in-use.", tgb_inuse);
-   factOut (kInfo, -1, str);
+   factPrintf(kInfo, -1, "%ld Bytes flaged as in-use.", tgb_inuse);
 #endif
 
@@ -2045,6 +1983,5 @@
    threadID = (int64_t)thrid;
 
-   snprintf (str, MXSTR, "Starting sub-process-thread %ld", threadID);
-   factOut (kInfo, -1, str);
+   factPrintf(kInfo, -1, "Starting sub-process-thread %ld", threadID);
 
    while (g_runStat > -2) {     //in case of 'exit' we still must process pending events
@@ -2071,8 +2008,5 @@
 
                if (jret <= threadID) {
-                  snprintf (str, MXSTR,
-                            "Process %ld wants to send event to process %d... not allowed.",
-                            threadID, jret);
-                  factOut (kError, -1, str);
+                   factPrintf(kError, -1, "Process %ld wants to send event to process %d... not allowed.", threadID, jret);
                   jret = 5300;
                } else if (jret <= 0)
@@ -2090,6 +2024,5 @@
 
       if (gj.readStat < -10 && numWait == 0) {  //nothing left to do
-         snprintf (str, MXSTR, "Exit subProcessing in process %ld", threadID);
-         factOut (kInfo, -1, str);
+         factPrintf(kInfo, -1, "Exit subProcessing in process %ld", threadID);
          return 0;
       }
@@ -2102,6 +2035,5 @@
    }
 
-   snprintf (str, MXSTR, "Ending sub-process-thread %ld", threadID);
-   factOut (kInfo, -1, str);
+   factPrintf(kInfo, -1, "Ending sub-process-thread %ld", threadID);
    return 0;
 } /*-----------------------------------------------------------------*/
@@ -2115,7 +2047,4 @@
    int status, j;
    struct timespec xwait;
-   char str[MXSTR];
-
-
 
    int lastRun = 0;             //usually run from last event still valid
@@ -2124,7 +2053,5 @@
 //   int cpu = 1;                 //process thread  (will be several in final version)
 
-   snprintf (str, MXSTR, "Starting process-thread with %d subprocesses",
-             gi_maxProc);
-   factOut (kInfo, -1, str);
+   factPrintf(kInfo, -1, "Starting process-thread with %d subprocesses", gi_maxProc);
 
 /* CPU_ZERO initializes all the bits in the mask to zero. */
@@ -2184,8 +2111,5 @@
                   }
                   if (j >= MAX_RUN) {
-                     snprintf (str, MXSTR,
-                               "procEvt: Can not find run %d for event %d in %d",
-                               irun, ievt, id);
-                     factOut (kFatal, 901, str);
+                     factPrintf(kFatal, 901, "procEvt: Can not find run %d for event %d in %d", irun, ievt, id);
                   }
                   lastRun = j;
@@ -2200,8 +2124,5 @@
                         runCtrl[j1].procId = 2; //--> do no longer accept events for processing
 //----                  problem: processing still going on ==> must wait for closing ....
-                        snprintf (str, MXSTR,
-                                  "procEvt: Finished run since new one opened %d",
-                                  runCtrl[j1].runId);
-                        factOut (kInfo, -1, str);
+                        factPrintf(kInfo, -1, "procEvt: Finished run since new one opened %d", runCtrl[j1].runId);
                         runFinish1 (runCtrl[j1].runId);
                      }
@@ -2229,13 +2150,9 @@
                      runOpen (irun, &actRun, sizeof (actRun));
                   if (runCtrl[j].fileHd == NULL) {
-                     snprintf (str, MXSTR,
-                               "procEvt: Could not open a file for run %d (runOpen failed)", irun);
-                     factOut (kError, 502, str);
+                     factPrintf(kError, 502, "procEvt: Could not open a file for run %d (runOpen failed)", irun);
                      runCtrl[j].fileId = 91;
                      runCtrl[j].procId = 91;
                   } else {                                                 
-                      snprintf (str, MXSTR, "procEvt: Opened new file for run %d (evt=%d)",
-                               irun, ievt);
-                     factOut (kInfo, -1, str);
+                      factPrintf(kInfo, -1, "procEvt: Opened new file for run %d (evt=%d)", irun, ievt);
                      runCtrl[j].fileId = 0;
                      runCtrl[j].procId = 0;
@@ -2248,8 +2165,5 @@
                       || runCtrl[j].lastTime < g_actTime - 300
                       || runCtrl[j].maxEvt <= runCtrl[j].procEvt) {
-                     snprintf (str, MXSTR,
-                               "procEvt: Reached end of run condition for run %d",
-                               irun);
-                     factOut (kInfo, 502, str);
+                     factPrintf(kInfo, 502, "procEvt: Reached end of run condition for run %d", irun);
                      runFinish1 (runCtrl[j].runId);
                      runCtrl[j].procId = 1;
@@ -2258,8 +2172,5 @@
                if (runCtrl[j].procId != 0) {
 #ifdef EVTDEBUG
-                  snprintf (str, MXSTR,
-                            "procEvt: Skip event %d because no active run %d", ievt,
-                            irun);
-                  factOut (kDebug, 502, str);
+                  fcatPrintf(kDebug, 502, "procEvt: Skip event %d because no active run %d", ievt, irun);
 #endif
                   evtCtrl.evtStat[k0] = 9091;
@@ -2335,6 +2246,5 @@
 
       if (gj.readStat < -10 && numWait == 0) {  //nothing left to do
-         snprintf (str, MXSTR, "Exit Processing Process ...");
-         factOut (kInfo, -1, str);
+         factPrintf(kInfo, -1, "Exit Processing Process ...");
          gp_runStat = -22;      //==> we should exit
          gj.procStat = -22;     //==> we should exit
@@ -2356,6 +2266,5 @@
    //   when gi_runStat claims that all events are in the buffer...
 
-   snprintf (str, MXSTR, "Abort Processing Process ...");
-   factOut (kInfo, -1, str);
+   factPrintf(kInfo, -1, "Abort Processing Process ...");
    int kd = evtCtrl.lastPtr - evtCtrl.frstPtr;
    if (kd < 0)
@@ -2427,11 +2336,9 @@
    int k, j   ;
    struct timespec xwait;
-   char str[MXSTR];
 
 //   cpu_set_t mask;
 //   int cpu = 1;                 //write thread
 
-   snprintf (str, MXSTR, "Starting write-thread");
-   factOut (kInfo, -1, str);
+   factPrintf(kInfo, -1, "Starting write-thread");
 
 /* CPU_ZERO initializes all the bits in the mask to zero. */
@@ -2478,8 +2385,5 @@
                   }
                   if (j >= MAX_RUN) {
-                     snprintf (str, MXSTR,
-                               "writeEvt: Can not find run %d for event %d in %d",
-                               irun, ievt, id);
-                     factOut (kFatal, 901, str);
+                     factPrintf(kFatal, 901, "writeEvt: Can not find run %d for event %d in %d", irun, ievt, id);
                      gi.wrtErr++;
                   }
@@ -2508,12 +2412,8 @@
                      runOpen (irun, &actRun, sizeof (actRun));
                   if (runCtrl[j].fileHd == NULL) {
-                     snprintf (str, MXSTR,
-                               "writeEvt: Could not open a file for run %d (runOpen failed)", irun);
-                     factOut (kError, 502, str);
+                     factPrintf(kError, 502, "writeEvt: Could not open a file for run %d (runOpen failed)", irun);
                      runCtrl[j].fileId = 91;
                   } else {
-                      snprintf (str, MXSTR, "writeEvt: Opened new file for run %d (evt %d)",
-                               irun, ievt);
-                     factOut (kInfo, -1, str);
+                      factPrintf(kInfo, -1, "writeEvt: Opened new file for run %d (evt %d)", irun, ievt);
                      runCtrl[j].fileId = 0;
                   }
@@ -2523,17 +2423,11 @@
                if (runCtrl[j].fileId != 0) {
                   if (runCtrl[j].fileId < 0) {
-                     snprintf (str, MXSTR,
-                               "writeEvt: Never opened file for run %d", irun);
-                     factOut (kError, 123, str);
+                     factPrintf(kError, 123, "writeEvt: Never opened file for run %d", irun);
                   } else if (runCtrl[j].fileId < 100) {
-                      snprintf (str, MXSTR, "writeEvt: File for run %d is closed",
-                               irun);
-                     factOut (kWarn, 123, str);
+                      factPrintf(kWarn, 123, "writeEvt: File for run %d is closed", irun);
                      runCtrl[j].fileId += 100;
                   } else {
 #ifdef EVTDEBUG
-                      snprintf (str, MXSTR, "writeEvt: File for run %d is closed",
-                               irun);
-                     factOut (kDebug, 123, str);
+                      factPrintf(kDebug, 123, "writeEvt: File for run %d is closed", irun);
 #endif
                   }
@@ -2550,14 +2444,9 @@
                      evtCtrl.evtStat[k0] = 9901;
 #ifdef EVTDEBUG
-                     snprintf (str, MXSTR,
-                               "%5d successfully wrote for run %d id %5d",
-                               ievt, irun, k0);
-                     factOut (kDebug, 504, str);
+                     factPrintf(kDebug, 504, "%5d successfully wrote for run %d id %5d", ievt, irun, k0);
 #endif
 //               gj.writEvt++ ;
                   } else {
-                      snprintf (str, MXSTR, "writeEvt: Writing event for run %d failed (runWrite)",
-                               irun);
-                     factOut (kError, 503, str);
+                      factPrintf(kError, 503, "writeEvt: Writing event for run %d failed (runWrite)", irun);
                      evtCtrl.evtStat[k0] = 9902;
                      gi.wrtErr++;
@@ -2593,12 +2482,8 @@
                                    sizeof (runTail[j]));
                      if (i < 0) {
-                         snprintf (str, MXSTR, "writeEvt: Error closing run %d (runClose[1],i=%d)",
-                                  runCtrl[j].runId, i);
-                        factOut (kError, 503, str);
+                         factPrintf(kError, 503, "writeEvt: Error closing run %d (runClose[1],i=%d)", runCtrl[j].runId, i);
                         runCtrl[j].fileId = 92;
                      } else {
-                         snprintf (str, MXSTR, "writeEvt: Closed run %d (ii[1]=%d)", irun,
-                                  ii);
-                        factOut (kInfo, 503, str);
+                         factPrintf(kInfo, 503, "writeEvt: Closed run %d (ii[1]=%d)", irun, ii);
                         runCtrl[j].fileId = 93;
                      }
@@ -2633,6 +2518,5 @@
       if (runIdFound == 0)
       {
-          snprintf(str, MXSTR, "An Active run (number %u) has been registered, but it could not be found in the runs list", actrun);
-          factOut(kInfo, 000, str);
+          factPrintf(kInfo, 0, "An Active run (number %u) has been registered, but it could not be found in the runs list", actrun);
       }
 //	  snprintf(str, MXSTR, "Maximum runId: %d", maxStartedRun);
@@ -2647,6 +2531,5 @@
 			  (runCtrl[j].runId != 0))
           {
-              snprintf (str, MXSTR, "writeEvt: No file will be opened for run %ud. Last run: %ud (started)", runCtrl[j].runId, actrun);
-              factOut (kInfo, 000, str);
+              factPrintf(kInfo, 0, "writeEvt: No file will be opened for run %ud. Last run: %ud (started)", runCtrl[j].runId, actrun);
            ;//TODO notify that this run will never be opened
           }
@@ -2679,12 +2562,8 @@
                           sizeof (runTail[j]));
             if (i < 0) {
-                snprintf (str, MXSTR, "writeEvt: Error closing run %d (runClose[2],i=%d)",
-                         runCtrl[j].runId, i);
-               factOut (kError, 506, str);
+                factPrintf(kError, 506, "writeEvt: Error closing run %d (runClose[2],i=%d)", runCtrl[j].runId, i);
                runCtrl[j].fileId = 94;
             } else {
-                snprintf (str, MXSTR, "writeEvt: Closed run %d (ii[2]=%d)",
-                         runCtrl[j].runId, ii);
-               factOut (kInfo, 507, str);
+                factPrintf(kInfo, 507, "writeEvt: Closed run %d (ii[2]=%d)", runCtrl[j].runId, ii);
                runCtrl[j].fileId = 95;
             }
@@ -2700,6 +2579,5 @@
 
       if (gj.readStat < -10 && numWait == 0) {  //nothing left to do
-         snprintf (str, MXSTR, "Finish Write Process ...");
-         factOut (kInfo, -1, str);
+          factPrintf(kInfo, -1, "Finish Write Process ...");
          gw_runStat = -22;      //==> we should exit
          gj.writStat = -22;     //==> we should exit
@@ -2712,10 +2590,8 @@
 
    //must close all open files ....
-   snprintf (str, MXSTR, "Abort Writing Process ...");
-   factOut (kInfo, -1, str);
+   factPrintf(kInfo, -1, "Abort Writing Process ...");
 
  closerun:
-   snprintf (str, MXSTR, "Close all open files ...");
-   factOut (kInfo, -1, str);
+   factPrintf(kInfo, -1, "Close all open files ...");
    for (j = 0; j < MAX_RUN; j++)
       if (runCtrl[j].fileId == 0) {
@@ -2732,18 +2608,14 @@
          int ii = 0;
          if (runCtrl[j].closeTime < g_actTime)
-            ii = 2;
-         else if (runCtrl[j].lastTime < g_actTime - 300)
-            ii = 3;
-         else if (runCtrl[j].maxEvt <= runCtrl[j].actEvt)
-            ii = 4;
+            ii |= 1; // = 2
+         /*else*/ if (runCtrl[j].lastTime < g_actTime - 300)
+            ii |= 2; // = 3
+         /*else*/ if (runCtrl[j].maxEvt <= runCtrl[j].actEvt)
+            ii |= 4; // = 4
          if (i < 0) {
-             snprintf (str, MXSTR, "writeEvt: Error closing run %d (runClose[3],i=%d)",
-                      runCtrl[j].runId, i);
-            factOut (kError, 506, str);
+             factPrintf(kError, 506, "writeEvt: Error closing run %d (runClose[3],i=%d)", runCtrl[j].runId, i);
             runCtrl[j].fileId = 96;
          } else {
-             snprintf (str, MXSTR, "writeEvt: Closed run %d (ii[3]=%d)", runCtrl[j].runId,
-                      ii);
-            factOut (kInfo, 507, str);
+             factPrintf(kInfo, 507, "writeEvt: Closed run %d (ii[3]=%d)", runCtrl[j].runId, ii);
             runCtrl[j].fileId = 97;
          }
@@ -2752,6 +2624,5 @@
    gw_runStat = -99;
    gj.writStat = -99;
-   snprintf (str, MXSTR, "Exit Writing Process ...");
-   factOut (kInfo, -1, str);
+   factPrintf(kInfo, -1, "Exit Writing Process ...");
    return 0;
 
@@ -2775,6 +2646,5 @@
    gj.readStat = gj.procStat = gj.writStat = 0;
 
-   snprintf (str, MXSTR, "Starting EventBuilder V15.07 A");
-   factOut (kInfo, -1, str);
+   factPrintf(kInfo, -1, "Starting EventBuilder V15.07 A");
 
 //initialize run control logics 
@@ -2791,6 +2661,5 @@
    gi_maxProc = g_maxProc;
    if (gi_maxProc <= 0 || gi_maxProc > 90) {
-      snprintf (str, MXSTR, "Illegal number of processes %d", gi_maxProc);
-      factOut (kFatal, 301, str);
+      factPrintf(kFatal, 301, "Illegal number of processes %d", gi_maxProc);
       gi_maxProc = 1;
    }
