You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@httpd.apache.org by Brad Nicholes <BN...@novell.com> on 2002/06/12 20:25:26 UTC

[PATCH] Rotate logs based on file size...

    We have a requirement on NetWare to rotate the logs based on file
size rather than on a rotation time.  This patch modifies rotatelogs to
accept either a rotation time or a file size parameter.  If a file size
is specified, it will automatically rotate the log when the actual file
size reaches the specified file size.  I would like to check this patch
in if I can get enough votes.

thanks,
Brad



--- \tempapache\httpd-2.0\support\rotatelogs.c	Tue Jun 11 12:59:27
2002
+++ rotatelogs.c	Wed Jun 12 12:11:14 2002
@@ -94,7 +94,8 @@
 int main (int argc, const char * const argv[])
 {
     char buf[BUFSIZE], buf2[MAX_PATH], errbuf[ERRMSGSZ];
-    int tLogEnd = 0, tRotation, utc_offset = 0;
+    int tLogEnd = 0, tRotation = 0, utc_offset = 0;
+    unsigned int sRotation = 0;
     int nMessCount = 0;
     apr_size_t nRead, nWrite;
     int use_strftime = 0;
@@ -102,6 +103,7 @@
     const char *szLogRoot;
     apr_file_t *f_stdin, *nLogFD = NULL, *nLogFDprev = NULL;
     apr_pool_t *pool;
+    char *ptr = NULL;
 
     apr_app_initialize(&argc, &argv, NULL);
     atexit(apr_terminate);
@@ -110,7 +112,7 @@
     if (argc < 3 || argc > 4) {
         fprintf(stderr,
                 "Usage: %s <logfile> <rotation time in seconds> "
-                "[offset minutes from UTC]\n\n",
+                "[offset minutes from UTC] or <rotation size in
megabytes>\n\n",
                 argv[0]);
 #ifdef OS2
         fprintf(stderr,
@@ -120,24 +122,40 @@
         fprintf(stderr,
                 "Add this:\n\nTransferLog \"|%s /some/where
86400\"\n\n",
                 argv[0]);
+        fprintf(stderr,
+                "or \n\nTransferLog \"|%s /some/where 5M\"\n\n",
argv[0]);
 #endif
         fprintf(stderr,
                 "to httpd.conf. The generated name will be
/some/where.nnnn "
                 "where nnnn is the\nsystem time at which the log
nominally "
-                "starts (N.B. this time will always be a\nmultiple of
the "
-                "rotation time, so you can synchronize cron scripts
with it).\n"
-                "At the end of each rotation time a new log is
started.\n");
+                "starts (N.B. if using a rotation time,\nthe time will
always "
+                "be a multiple of the rotation time, so you can
synchronize\n"
+                "cron scripts with it). At the end of each rotation
time or "
+                "when the file size\nis reached a new log is
started.\n");
         exit(1);
     }
 
     szLogRoot = argv[1];
-    if (argc >= 4) {
-        utc_offset = atoi(argv[3]) * 60;
+
+    ptr = strchr (argv[2], 'M');
+    if (ptr) {
+        if (*(ptr+1) == '\0') {
+            sRotation = atoi(argv[2]) * 1048576;
+        }
+        if (sRotation == 0) {
+            fprintf(stderr, "Invalid rotation size parameter\n");
+            exit(1);
+        }
     }
-    tRotation = atoi(argv[2]);
-    if (tRotation <= 0) {
-        fprintf(stderr, "Rotation time must be > 0\n");
-        exit(6);
+    else {
+        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 = (strchr(szLogRoot, '%') != NULL);
@@ -150,17 +168,44 @@
         nRead = sizeof(buf);
         if (apr_file_read(f_stdin, buf, &nRead) != APR_SUCCESS)
             exit(3);
-        now = (int)(apr_time_now() / APR_USEC_PER_SEC) + utc_offset;
         if (nRead == 0)
             exit(3);
-        if (nLogFD != NULL && (now >= tLogEnd || nRead < 0)) {
-            nLogFDprev = nLogFD;
-            nLogFD = NULL;
+        if (tRotation) {
+            now = (int)(apr_time_now() / APR_USEC_PER_SEC) +
utc_offset;
+            if (nLogFD != NULL && (now >= tLogEnd || nRead < 0)) {
+                nLogFDprev = nLogFD;
+                nLogFD = NULL;
+            }
         }
+        else if (sRotation) {
+            apr_finfo_t finfo;+            unsigned int current_size =
-1;
+
+            if ((nLogFD != NULL) && 
+                (apr_file_info_get(&finfo, APR_FINFO_SIZE, nLogFD) ==
APR_SUCCESS)) {
+                current_size = finfo.size;
+            }
+
+            if (current_size > sRotation || nRead < 0) {
+                nLogFDprev = nLogFD;
+                nLogFD = NULL;
+            }
+        }
+        else {
+            fprintf(stderr, "No rotation time or size specified\n");
+            exit(2);
+        }
+
         if (nLogFD == NULL) {
-            int tLogStart = (now / tRotation) * tRotation;
+            int tLogStart;
+                
+            if (tRotation)
+                tLogStart = (now / tRotation) * tRotation;
+            else
+                tLogStart = apr_time_now() / APR_USEC_PER_SEC;
+
             if (use_strftime) {
-		apr_time_t tNow = tLogStart * APR_USEC_PER_SEC;
+		        apr_time_t tNow = tLogStart * APR_USEC_PER_SEC;
                 apr_time_exp_t e;
                 apr_size_t rs;
 


Brad Nicholes
Senior Software Engineer
Novell, Inc., the leading provider of Net business solutions
http://www.novell.com 

Re: [PATCH] Rotate logs based on file size...

Posted by Brian Pane <br...@cnet.com>.
Brad Nicholes wrote:

>    We have a requirement on NetWare to rotate the logs based on file
>size rather than on a rotation time.  This patch modifies rotatelogs to
>accept either a rotation time or a file size parameter.  If a file size
>is specified, it will automatically rotate the log when the actual file
>size reaches the specified file size.  I would like to check this patch
>in if I can get enough votes.
>

Based on a quick read through the patch, +1.

BTW, does anyone have strong opinions for or against adding log
rotation logic to mod_log_config as an alternative to rotatelogs?
IMHO, it would be more efficient, and simpler for the server
administrator, to have some configuration options for mod_log_config
that would enable it to start writing a new log file at a specified
time interval or file size threshold.

--Brian