From: Bradley Nicholes Date: Wed, 12 Jun 2002 21:46:50 +0000 (+0000) Subject: Added log rotation based on file size to the RotateLog support utility. X-Git-Tag: 2.0.38~88 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=689fc8157f4c4b77fdb01f3eb62729a543fe6396;p=apache Added log rotation based on file size to the RotateLog support utility. git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@95619 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/CHANGES b/CHANGES index f21b2e4477..f80eeab4d8 100644 --- a/CHANGES +++ b/CHANGES @@ -1,6 +1,9 @@ Changes with Apache 2.0.38 + *) Added log rotation based on file size to the RotateLog support + utility. [Brad Nicholes] + *) Fix some casting in mod_rewrite which broke random maps. PR 9770 [Allan Edwards, Greg Ames, Jeff Trawick] diff --git a/support/rotatelogs.c b/support/rotatelogs.c index 65bf9212dd..ed382ee315 100644 --- a/support/rotatelogs.c +++ b/support/rotatelogs.c @@ -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 @@ int main (int argc, const char * const argv[]) 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 @@ int main (int argc, const char * const argv[]) if (argc < 3 || argc > 4) { fprintf(stderr, "Usage: %s " - "[offset minutes from UTC]\n\n", + "[offset minutes from UTC] or \n\n", argv[0]); #ifdef OS2 fprintf(stderr, @@ -120,24 +122,40 @@ int main (int argc, const char * const argv[]) 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 @@ int main (int argc, const char * const argv[]) 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;