]> granicus.if.org Git - apache/commitdiff
Enhance customisability of rotatelogs: strftime(3)
authorKen Coar <coar@apache.org>
Tue, 13 Mar 2001 21:25:28 +0000 (21:25 +0000)
committerKen Coar <coar@apache.org>
Tue, 13 Mar 2001 21:25:28 +0000 (21:25 +0000)
formatting of filename and offset from UTC.

Reviewed by: Greg Stein, David Reid, OtherBill

git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@88511 13f79535-47bb-0310-9956-ffa450edef68

CHANGES
docs/man/rotatelogs.8
support/rotatelogs.c

diff --git a/CHANGES b/CHANGES
index da5c7516323a5218cec31e234c7d59f48f6f7d7b..9b0fa4389da669e5e1cda7b9147121cbc554947f 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -1,4 +1,9 @@
 Changes with Apache 2.0.15-dev
+
+  *) Enhance rotatelogs so that a UTC offset can be specified, and
+     the logfile name can be formatted using strftime(3).  (Brought
+     forward from 1.3.)  [Ken Coar]
+
   *) Reimplement the Windows MPM (mpm_winnt.c) to eliminate calling 
      DuplicateHandle on an IOCompletionPort (a practice which
      MS "discourages"). The new model does not rely on associating
index 465c14bdb51ca06a9e84e24e477590089df13ad7..b4e6e234472b2fedab8584fd325f73cb955272df 100644 (file)
@@ -1,4 +1,4 @@
-.TH rotatelogs 8 "March 1998"
+.TH rotatelogs 8 "March 2001"
 .\" The Apache Software License, Version 1.1
 .\"
 .\" Copyright (c) 2000-2001 The Apache Software Foundation.  All rights
@@ -56,6 +56,7 @@ rotatelogs \- rotate Apache logs without having to kill the server
 .B rotatelogs
 .I logfile
 .I rotationtime
+.I [offset]
 .PP
 .SH DESCRIPTION
 .B rotatelogs
@@ -63,7 +64,7 @@ is a simple program for use in conjunction with Apache's piped logfile
 feature which can be used like this:
 
 .fi
-   TransferLog "|rotatelogs /path/to/logs/access_log 86400"
+   TransferLog "| rotatelogs /path/to/logs/access_log 86400"
 .mf
 
 This creates the files /path/to/logs/access_log.nnnn where nnnn is the system
@@ -72,11 +73,16 @@ the rotation time, so you can synchronize cron scripts with it).  At the end
 of each rotation time (here after 24 hours) a new log is started.
 .SH OPTIONS
 .IP \fB\fIlogfile\fP
-The path plus basename of the logfile. The suffix .nnnn is automatically
-added.
+The path plus basename of the logfile.  If \fBlogfile\fP includes any
+'%' characters, it is treated as a format string for \fIstrftime(3)\fP.
+Otherwise, the suffix .nnnn is automatically added and is the time at which
+the logfile was created.
 .IP \fB\fIrotationtime\fP
 The rotation time in seconds.
+.IP \fB\fIoffset\fP
+The number of minutes offset from UTC.  If omitted, zero is assumed and
+UTC is used.  For example, to use local time in the zone UTC -5 hours,
+specify a value of \fI-300\fP for this argument.
 .PD
 .SH SEE ALSO
 .BR httpd(8)
-.
index 482680874397888de4f7c7259e87aeddc50989a1..349932870268ae0fe8925ae99cc06f5455d168e0 100644 (file)
@@ -90,11 +90,15 @@ int main (int argc, char *argv[])
     char buf[BUFSIZE], buf2[MAX_PATH], errbuf[ERRMSGSZ];
     time_t tLogEnd = 0, tRotation;
     int nLogFD = -1, nLogFDprev = -1, nMessCount = 0, nRead, nWrite;
+    int utc_offset = 0;
+    int use_strftime = 0;
+    time_t now;
     char *szLogRoot;
 
-    if (argc != 3) {
+    if (argc < 3) {
         fprintf(stderr,
-                "%s <logfile> <rotation time in seconds>\n\n",
+                "Usage: %s <logfile> <rotation time in seconds> "
+                "[offset minutes from UTC]\n\n",
                 argv[0]);
 #ifdef OS2
         fprintf(stderr,
@@ -115,26 +119,38 @@ int main (int argc, char *argv[])
     }
 
     szLogRoot = argv[1];
+    if (argc >= 4) {
+        utc_offset = atoi(argv[3]) * 60;
+    }
     tRotation = atoi(argv[2]);
     if (tRotation <= 0) {
         fprintf(stderr, "Rotation time must be > 0\n");
         exit(6);
     }
 
+    use_strftime = (strstr(szLogRoot, "%") != NULL);
     for (;;) {
         nRead = read(0, buf, sizeof buf);
+        now = time(NULL) + utc_offset;
         if (nRead == 0)
             exit(3);
         if (nRead < 0)
             if (errno != EINTR)
                 exit(4);
-        if (nLogFD >= 0 && (time(NULL) >= tLogEnd || nRead < 0)) {
+        if (nLogFD >= 0 && (now >= tLogEnd || nRead < 0)) {
             nLogFDprev = nLogFD;
             nLogFD = -1;
         }
         if (nLogFD < 0) {
-            time_t tLogStart = (time(NULL) / tRotation) * tRotation;
-            sprintf(buf2, "%s.%010d", szLogRoot, (int) tLogStart);
+            time_t tLogStart = (now / tRotation) * tRotation;
+            if (use_strftime) {
+                struct tm *tm_now;
+                tm_now = gmtime(&tLogStart);
+                strftime(buf2, sizeof(buf2), szLogRoot, tm_now);
+            }
+            else {
+                sprintf(buf2, "%s.%010d", szLogRoot, (int) tLogStart);
+            }
             tLogEnd = tLogStart + tRotation;
             nLogFD = open(buf2, O_WRONLY | O_CREAT | O_APPEND, 0666);
             if (nLogFD < 0) {