Commit 5401e88f authored by Bradley Nicholes's avatar Bradley Nicholes
Browse files

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
parent 9fcdf8b0
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
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]
+62 −17
Original line number Diff line number Diff line
@@ -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 <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,17 +122,32 @@ 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];

    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);
        }
    }
    else {
        if (argc >= 4) {
            utc_offset = atoi(argv[3]) * 60;
        }
@@ -139,6 +156,7 @@ int main (int argc, const char * const argv[])
            fprintf(stderr, "Rotation time must be > 0\n");
            exit(6);
        }
    }

    use_strftime = (strchr(szLogRoot, '%') != NULL);
    if (apr_file_open_stdin(&f_stdin, pool) != APR_SUCCESS) {
@@ -150,15 +168,42 @@ 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 (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_exp_t e;