strftime for innfeed

Alexander Bartolich alexander.bartolich at gmx.at
Sat Dec 13 23:00:18 UTC 2008


Hi,

With the one exception of function error_log_stderr_date (in misc.c)
innfeed currently uses ctime(3) to print date and time. I changed
that all to strftime(3) and made the format string configurable.



Index: doc/man/innfeed.conf.5
===================================================================
--- doc/man/innfeed.conf.5	(revision 8218)
+++ doc/man/innfeed.conf.5	(working copy)
@@ -190,6 +190,11 @@
  a positive value for ``\fBdebug-value\fP'', is used). This corresponds to
  the ``\fI\-l\fP'' command-line option. A relative pathname is relative to
  the ``\fBbacklog-directory\fP'' value.
+.TP
+.B log-time-format
+This key requires a format string suitable for strftime(3). It is used for
+messages sent via syslog(3) and to the \fBstatus-file\fP. Default value
+is "%a %b %d %H:%M:%S %Y".
  .\" .TP
  .\" .B initial-sleep
  .\" This key requires a positive integer value. It specifies how many seconds
Index: innfeed/connection.c
===================================================================
--- innfeed/connection.c	(revision 8218)
+++ innfeed/connection.c	(working copy)
@@ -4461,10 +4461,9 @@
        free (PointersFreedOnExit) ;
        freeTimeoutQueue () ;

-      strlcpy (dateString,ctime (&now), sizeof(dateString)) ;
+      timeToString (now, dateString, sizeof(dateString)) ;
+      notice ("ME finishing at %s\n", dateString) ;

-      notice ("ME finishing at %s", dateString) ;
-
        exit (0) ;
      }
  }
Index: innfeed/misc.h
===================================================================
--- innfeed/misc.h	(revision 8218)
+++ innfeed/misc.h	(working copy)
@@ -42,6 +42,9 @@
  /* debugging information */
  extern unsigned int loggingLevel ;     /* if 0 then d_printf is a no-op */

+/* used by timeToString with strftime(3) */
+extern char *timeToStringFormat ;
+
  /* the current count of file desccriptors */
  extern unsigned int openfds ;

@@ -114,6 +117,9 @@
  /* converts val into a printable string */
  const char *boolToString (bool val) ;

+/* strftime with "%a %b %d %H:%M:%S %Y" (like ctime without linefeed) */
+char* timeToString (time_t time, char* buffer, size_t size) ;
+
  /* memory leak checker helper. */
  void addPointerFreedOnExit (char *pointerToFree) ;

Index: innfeed/imap_connection.c
===================================================================
--- innfeed/imap_connection.c	(revision 8218)
+++ innfeed/imap_connection.c	(working copy)
@@ -4170,9 +4170,7 @@
        time_t now = theTime () ;
        char dateString [30] ;

-      strlcpy (dateString,ctime (&now),sizeof (dateString)) ;
-      dateString [24] = '\0' ;
-
+      timeToString (now, dateString, sizeof (dateString)) ;
        notice ("ME finishing at %s", dateString) ;

        exit (0) ;
Index: innfeed/host.c
===================================================================
--- innfeed/host.c	(revision 8218)
+++ innfeed/host.c	(working copy)
@@ -332,7 +332,7 @@

  static unsigned int hostHighwater = HOST_HIGHWATER ;
  static time_t start ;
-static char startTime [30] ;    /* for ctime(3) */
+static char startTime [30] ;    /* for timeToString */
  static pid_t myPid ;

  static char *statusFile = NULL ;
@@ -396,7 +396,7 @@
    else
      iv = DNS_EXPIRE_PERIOD ;
    dnsExpPeriod = (unsigned int) iv ;
-
+
    if (getBool (topScope,"gen-html",&bval,NO_INHERIT))
      genHtml = (bval ? true : false) ;
    else
@@ -410,13 +410,11 @@
    else
      hostSetStatusFile (INNFEED_STATUS) ;

-
    if (getBool (topScope,"connection-stats",&bval,NO_INHERIT))
      logConnectionStats = (bval ? true : false) ;
    else
      logConnectionStats = (LOG_CONNECTION_STATS ? true : false) ;

-
    if (getInteger (topScope,"host-queue-highwater", &iv,NO_INHERIT))
      {
        if (iv < 0)
@@ -1111,7 +1109,7 @@
    if (maxIpNameLen == 0)
      {
        start = theTime() ;
-      strlcpy (startTime,ctime (&start),sizeof (startTime)) ;
+      timeToString (start, startTime, sizeof (startTime)) ;
        myPid = getpid() ;
      }

@@ -1258,6 +1256,7 @@

  void printHostInfo (Host host, FILE *fp, unsigned int indentAmt)
  {
+  char dateString [30] ;
    char indent [INDENT_BUFFER_SIZE] ;
    unsigned int i ;
    ProcQElem qe ;
@@ -1404,12 +1403,17 @@
             100.0 * host->blFull / cnt) ;
    fprintf (fp,"%s      number of samples : %u\n", indent, host->blCount) ;

-  fprintf (fp,"%s    firstConnectTime : %s",indent,
-           ctime (&host->firstConnectTime));
-  fprintf (fp,"%s    connectTime : %s",indent,ctime (&host->connectTime));
-  fprintf (fp,"%s    spoolTime : %s",indent,ctime (&host->spoolTime)) ;
-  fprintf (fp,"%s    last-spool-time : %s",indent,
-           ctime (&host->lastSpoolTime)) ;
+  timeToString (host->firstConnectTime, dateString, sizeof (dateString));
+  fprintf (fp, "%s    firstConnectTime : %s\n", indent, dateString);
+
+  timeToString (host->connectTime, dateString, sizeof (dateString));
+  fprintf (fp, "%s    connectTime : %s\n", indent, dateString);
+
+  timeToString (host->spoolTime, dateString, sizeof (dateString));
+  fprintf (fp, "%s    spoolTime : %s\n", indent, dateString);
+
+  timeToString (host->lastSpoolTime, dateString, sizeof (dateString));
+  fprintf (fp, "%s    last-spool-time : %s\n", indent, dateString);

  #if 0
    fprintf (fp,"%s    tape {\n",indent) ;
@@ -3160,7 +3164,7 @@

        now = time (NULL) ;
        sec = (long) (now - start) ;
-      strlcpy (timeString,ctime (&now),sizeof (timeString)) ;
+      timeToString (now, timeString, sizeof (timeString)) ;

        if (genHtml)
          {
@@ -3173,7 +3177,7 @@
  	  fprintf (fp, "<PRE>\n");
  	}

-      fprintf (fp,"innfeed from %s\npid %d started %s\nUpdated: %s",
+      fprintf (fp,"innfeed from %s\npid %d started %s\n\nUpdated: %s\n",
                 INN_VERSION_STRING,(int) myPid,startTime,timeString) ;
        fprintf (fp,"(peers: %d active-cxns: %d sleeping-cxns: %d idle-cxns: %d)\n\n",
                 peerNum, actConn, slpConn,(maxcon - (actConn + slpConn))) ;
Index: innfeed/endpoint.c
===================================================================
--- innfeed/endpoint.c	(revision 8218)
+++ innfeed/endpoint.c	(working copy)
@@ -1495,12 +1495,12 @@
  static void printDate (TimeoutId tid, void *data) ;
  static void printDate (TimeoutId tid, void *data)
  {
-  time_t t ;
+  char dateString[30];
+  const time_t t = theTime() ;

-  t = theTime() ;
-
-  d_printf (1,"Timeout (%d) time now is %ld %s",
-           (int) tid,(long) t,ctime(&t)) ;
+  timeToString (t, dateString, sizeof (dateString)) ;
+  d_printf (1,"Timeout (%d) time now is %ld %s\n",
+           (int) tid, (long) t, dateString) ;

    if (timeoutQueue == NULL)
      {
@@ -1518,7 +1518,8 @@
    static int seeded ;
    static int howMany ;
    static int i ;
-  time_t t = theTime() ;
+  char dateString[30];
+  const time_t t = theTime() ;

    if ( !seeded )
      {
@@ -1526,8 +1527,9 @@
        seeded = 1 ;
      }

-  d_printf (1,"Timeout (%d) time now is %ld %s",
-           (int) tid, (long) t,ctime(&t)) ;
+  timeToString (t, dateString, sizeof (dateString)) ;
+  d_printf (1,"Timeout (%d) time now is %ld %s\n",
+           (int) tid, (long) t, dateString) ;

    if (timeoutQueue != NULL && timeoutQueue->next != NULL)
      d_printf (1,"%s timeout id %d\n",
Index: innfeed/main.c
===================================================================
--- innfeed/main.c	(revision 8218)
+++ innfeed/main.c	(working copy)
@@ -120,7 +120,7 @@
    bool checkConfig = false ;
    bool val;

-  strlcpy (dateString,ctime(&now),sizeof (dateString)) ;
+  timeToString (now, dateString, sizeof (dateString)) ;

    message_program_name = strrchr (argv [0],'/');
    if (message_program_name == NULL)
@@ -289,7 +289,7 @@

    if ( !checkConfig )
      {
-      notice ("ME starting at %s (%s)", dateString, INN_VERSION_STRING);
+      notice ("ME starting at %s\n (%s)", dateString, INN_VERSION_STRING);
      }

    val = true;
@@ -671,6 +671,7 @@
  {
    char *snapshotFile;
    FILE *fp;
+  char nowString[30];
    time_t now = theTime() ;

    snapshotFile = concatpath(innconf->pathlog, SNAPSHOT_FILE);
@@ -689,8 +690,9 @@
    setbuf (fp, NULL) ;
  #endif

-  fprintf (fp,"----------------------------System snaphot taken at: %s\n",
-           ctime (&now)) ;
+  timeToString (now, nowString, sizeof (nowString)) ;
+  fprintf (fp,"----------------------------System snaphot taken at: %s\n\n",
+           nowString) ;
    gPrintListenerInfo (fp,0) ;
    fprintf (fp,"\n\n\n\n") ;
    gPrintHostInfo (fp,0) ;
@@ -770,6 +772,12 @@
        free (p) ;
      }

+  if (getString (topScope,"log-time-format",&p,NO_INHERIT))
+    {
+      free(timeToStringFormat);
+      timeToStringFormat = p;
+    }
+
     /* For imap/lmtp delivering */
    if (getString (topScope,"deliver-username",&p, NO_INHERIT))
      {
Index: innfeed/innlistener.c
===================================================================
--- innfeed/innlistener.c	(revision 8218)
+++ innfeed/innlistener.c	(working copy)
@@ -230,8 +230,7 @@
        char dateString [30] ;

        gHostStats();
-      strlcpy (dateString,ctime (&now),sizeof (dateString)) ;
-      dateString [24] = '\0' ;
+      timeToString (now, dateString, sizeof (dateString)) ;

        if (fastExit)
          notice ("ME finishing (quickly) at %s", dateString) ;
Index: innfeed/misc.c
===================================================================
--- innfeed/misc.c	(revision 8218)
+++ innfeed/misc.c	(working copy)
@@ -40,6 +40,7 @@
  unsigned int loggingLevel ;
  char **PointersFreedOnExit ;

+char *timeToStringFormat = 0;
  bool debuggingDump = true ;
  extern void (*gPrintInfo) (void) ;
  void (*gCleanUp) (void) = 0 ;
@@ -115,6 +116,7 @@
    static pid_t myPid ;
    char timeString [30] ;
    time_t now ;
+  struct tm *tm ;
    va_list ap ;

    if (myPid == 0)
@@ -124,9 +126,8 @@
      return ;

    now = theTime() ;
-  /* strip off leading day name */
-  strlcpy (timeString, ctime (&now) + 4, sizeof (timeString)) ;
-  timeString [15] = '\0' ;      /* strip off trailing year and newline */
+  tm = localtime (&now);
+  strftime (timeString, sizeof(timeString), "%b %d %H:%M:%S", tm);

    va_start (ap, fmt) ;
    fprintf (stderr, "%s %s[%ld]: ",timeString,
@@ -591,6 +592,15 @@
    return val ? "true" : "false" ;
  }

+char* timeToString(time_t t, char* buffer, size_t size)
+{
+  static const char defaultFormat[] = "%a %b %d %H:%M:%S %Y" ;
+  const struct tm *const tm = localtime(&t);
+  strftime (buffer, size,
+    timeToStringFormat == 0 ? defaultFormat : timeToStringFormat, tm);
+  return buffer;
+}
+
  void addPointerFreedOnExit (char *pointerToFree)
  {
    static int totalPointers = 0 ;



More information about the inn-workers mailing list