]> granicus.if.org Git - apache/commitdiff
Merge r1490493, r1490761 from trunk:
authorJim Jagielski <jim@apache.org>
Thu, 13 Jun 2013 15:33:13 +0000 (15:33 +0000)
committerJim Jagielski <jim@apache.org>
Thu, 13 Jun 2013 15:33:13 +0000 (15:33 +0000)
rotatelogs: add -n number-of-files option to roate through a number
of fixed-name logfiles.

don't truncate the very first file opened (unless the truncate flag is there too)

Submitted by: covener
Reviewed/backported by: jim

git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/branches/2.4.x@1492708 13f79535-47bb-0310-9956-ffa450edef68

CHANGES
STATUS
docs/manual/programs/rotatelogs.xml
support/rotatelogs.c

diff --git a/CHANGES b/CHANGES
index 0f840d368db8c55ab2c04506a2a937418da118a1..91e1872d1869a9eb8b0698998f529ddfa37862b9 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -1,4 +1,6 @@
                                                          -*- coding: utf-8 -*-
+  *) rotatelogs: add -n number-of-files option to roate through a number
+     of fixed-name logfiles. [Eric Covener]
 
 Changes with Apache 2.4.5
 
diff --git a/STATUS b/STATUS
index db979ad6914494864bff5e1c23beed012662b9ae..4eec6f9816afbdf7a2b64fcb7ee8b3eebf483f1e 100644 (file)
--- a/STATUS
+++ b/STATUS
@@ -90,11 +90,6 @@ RELEASE SHOWSTOPPERS:
 PATCHES ACCEPTED TO BACKPORT FROM TRUNK:
   [ start all new proposals below, under PATCHES PROPOSED. ]
  
-    * rotatelogs: support -n number-of-files for circular set of filenames
-      trunk patch: http://svn.apache.org/r1490493
-                   http://svn.apache.org/r1490761
-      2.4.x patch: trunk works + compat for manual
-      +1 covener, minfrin, jim
       
 
 PATCHES PROPOSED TO BACKPORT FROM TRUNK:
index bae96f84355d0f0de8ff7083ef660f79d99c31f1..f6c6aec5086df4008067bdbce6e5a54fd902ee62 100644 (file)
@@ -41,6 +41,7 @@
      [ -<strong>v</strong> ]
      [ -<strong>e</strong> ]
      [ -<strong>c</strong> ]
+     [ -<strong>n</strong> <var>number-of-files</var> ]
      <var>logfile</var>
      <var>rotationtime</var>|<var>filesize</var>(B|K|M|G)
      [ <var>offset</var> ]</code></p>
@@ -102,6 +103,11 @@ processed in real time by a further tool in the chain.</dd>
 <dt><code>-c</code></dt>
 <dd>Create log file for each interval, even if empty.</dd>
 
+<dt><code>-n <var>number-of-files</var></code></dt>
+<dd>Use a circular list of filenames without timestamps.
+With -n 3, the series of log files opened would be
+"logfile", "logfile.1", "logfile.2", then overwriting "logfile".</dd>
+
 <dt><code><var>logfile</var></code></dt>
 
 <dd><p>The path plus basename of the logfile.  If <var>logfile</var>
index 9bc3328599d39f48825fba371bb58155b9d91924..3f32f3cac4246b014876b4e2ece00027a5f37ab4 100644 (file)
@@ -99,6 +99,7 @@ struct rotate_config {
 #if APR_FILES_AS_SOCKETS
     int create_empty;
 #endif
+    int num_files;
 };
 
 typedef struct rotate_status rotate_status_t;
@@ -118,6 +119,7 @@ struct rotate_status {
     int rotateReason;
     int tLogEnd;
     int nMessCount;
+    int fileNum;
 };
 
 static rotate_config_t config;
@@ -130,9 +132,9 @@ static void usage(const char *argv0, const char *reason)
     }
     fprintf(stderr,
 #if APR_FILES_AS_SOCKETS
-            "Usage: %s [-v] [-l] [-L linkname] [-p prog] [-f] [-t] [-e] [-c] <logfile> "
+            "Usage: %s [-v] [-l] [-L linkname] [-p prog] [-f] [-t] [-e] [-c] [-n number] <logfile> "
 #else
-            "Usage: %s [-v] [-l] [-L linkname] [-p prog] [-f] [-t] [-e] <logfile> "
+            "Usage: %s [-v] [-l] [-L linkname] [-p prog] [-f] [-t] [-e] [-n number] <logfile> "
 #endif
             "{<rotation time in seconds>|<rotation size>(B|K|M|G)} "
             "[offset minutes from UTC]\n\n",
@@ -374,6 +376,7 @@ static void doRotate(rotate_config_t *config, rotate_status_t *status)
     int tLogStart;
     apr_status_t rv;
     struct logfile newlog;
+    int thisLogNum = -1;
 
     status->rotateReason = ROTATE_NONE;
 
@@ -407,6 +410,16 @@ static void doRotate(rotate_config_t *config, rotate_status_t *status)
         if (config->truncate) {
             apr_snprintf(newlog.name, sizeof(newlog.name), "%s", config->szLogRoot);
         }
+        else if (config->num_files > 0) { 
+            if (status->fileNum == -1 || status->fileNum == (config->num_files - 1)) {
+                thisLogNum = 0;
+                apr_snprintf(newlog.name, sizeof(newlog.name), "%s", config->szLogRoot);
+            }
+            else { 
+                thisLogNum = status->fileNum + 1;
+                apr_snprintf(newlog.name, sizeof(newlog.name), "%s.%d", config->szLogRoot, thisLogNum);
+            }
+        }
         else {
             apr_snprintf(newlog.name, sizeof(newlog.name), "%s.%010d", config->szLogRoot,
                          tLogStart);
@@ -417,11 +430,13 @@ static void doRotate(rotate_config_t *config, rotate_status_t *status)
         fprintf(stderr, "Opening file %s\n", newlog.name);
     }
     rv = apr_file_open(&newlog.fd, newlog.name, APR_WRITE | APR_CREATE | APR_APPEND
-                       | (config->truncate ? APR_TRUNCATE : 0), APR_OS_DEFAULT, newlog.pool);
+                       | (config->truncate || (config->num_files > 0 && status->current.fd) ? APR_TRUNCATE : 0), 
+                       APR_OS_DEFAULT, newlog.pool);
     if (rv == APR_SUCCESS) {
         /* Handle post-rotate processing. */
         post_rotate(newlog.pool, &newlog, config, status);
 
+        status->fileNum = thisLogNum;
         /* Close out old (previously 'current') logfile, if any. */
         if (status->current.fd) {
             close_logfile(config, &status->current);
@@ -550,9 +565,9 @@ int main (int argc, const char * const argv[])
     apr_pool_create(&status.pool, NULL);
     apr_getopt_init(&opt, status.pool, argc, argv);
 #if APR_FILES_AS_SOCKETS
-    while ((rv = apr_getopt(opt, "lL:p:ftvec", &c, &opt_arg)) == APR_SUCCESS) {
+    while ((rv = apr_getopt(opt, "lL:p:ftvecn:", &c, &opt_arg)) == APR_SUCCESS) {
 #else
-    while ((rv = apr_getopt(opt, "lL:p:ftve", &c, &opt_arg)) == APR_SUCCESS) {
+    while ((rv = apr_getopt(opt, "lL:p:ftven:", &c, &opt_arg)) == APR_SUCCESS) {
 #endif
         switch (c) {
         case 'l':
@@ -581,6 +596,10 @@ int main (int argc, const char * const argv[])
             config.create_empty = 1;
             break;
 #endif
+        case 'n':
+            config.num_files = atoi(opt_arg);
+            status.fileNum = -1;
+            break;
         }
     }
 
@@ -609,6 +628,16 @@ int main (int argc, const char * const argv[])
 
     config.use_strftime = (strchr(config.szLogRoot, '%') != NULL);
 
+    if (config.use_strftime && config.num_files > 0) { 
+        fprintf(stderr, "Cannot use -n with %% in filename\n");
+        exit(1);
+    }
+
+    if (status.fileNum == -1 && config.num_files < 1) { 
+        fprintf(stderr, "Invalid -n argument\n");
+        exit(1);
+    }
+
     if (apr_file_open_stdin(&f_stdin, status.pool) != APR_SUCCESS) {
         fprintf(stderr, "Unable to open stdin\n");
         exit(1);