--- rotatelogs.c 2009-11-09 14:57:18.000000000 -0500 +++ rotatelogs.c.new 2009-11-09 13:53:39.000000000 -0500 @@ -70,10 +70,15 @@ fprintf(stderr, "%s\n", reason); } fprintf(stderr, - "Usage: %s [-l] [-f] " + "Usage: %s [-N ] [-l] [-f] " "{|} " "[offset minutes from UTC]\n\n", argv0); + fprintf(stderr, + "if [-N ] is used, the log file will use the name specified without extensions (/some/where) \n" + " and roll the old logs to /some/where.n, total n files will be kept\n" + " n must be greater than 0 \n\n", + argv0); #ifdef OS2 fprintf(stderr, "Add this:\n\nTransferLog \"|%s.exe /some/where 86400\"\n\n", @@ -112,15 +117,18 @@ int main (int argc, const char * const argv[]) { - char buf[BUFSIZE], buf2[MAX_PATH], errbuf[ERRMSGSZ]; + char buf[BUFSIZE], buf2[MAX_PATH], errbuf[ERRMSGSZ], buf3[MAX_PATH], buf4[MAX_PATH]; int tLogEnd = 0, tRotation = 0, utc_offset = 0; unsigned int sRotation = 0; int nMessCount = 0; apr_size_t nRead, nWrite; int use_strftime = 0; int use_localtime = 0; + int use_num = 0; + int num_to_keep = 0; int bypass_io = 0; int now = 0; + int i = 0; const char *szLogRoot; apr_file_t *f_stdin, *nLogFD = NULL, *nLogFDprev = NULL; apr_pool_t *pool; @@ -137,7 +145,7 @@ apr_pool_create(&pool, NULL); apr_getopt_init(&opt, pool, argc, argv); - while ((rv = apr_getopt(opt, "lf", &c, &optarg)) == APR_SUCCESS) { + while ((rv = apr_getopt(opt, "lfN", &c, &optarg)) == APR_SUCCESS) { switch (c) { case 'l': use_localtime = 1; @@ -145,6 +153,13 @@ case 'f': bypass_io = 1; break; + case 'N': + use_num = 1; + num_to_keep = atoi(argv[opt->ind++]); + if ( num_to_keep <= 0 ) { + usage(argv[0], "Invalid 'number of old files' to keep parameter"); + }; + break; } } @@ -207,6 +222,14 @@ if (nLogFD != NULL && now >= tLogEnd) { nLogFDprev = nLogFD; nLogFD = NULL; + if (use_num) { + for ( i = num_to_keep; i > 1 ; i-- ) { + sprintf(buf4, "%s.%d", buf2, i); + sprintf(buf3, "%s.%d", buf2, i - 1); + rename(buf3, buf4); + } + rename(buf2, buf3); + } } } else if (sRotation) { @@ -221,6 +244,14 @@ if (current_size > sRotation) { nLogFDprev = nLogFD; nLogFD = NULL; + if (use_num) { + for ( i = num_to_keep; i > 1 ; i-- ) { + sprintf(buf4, "%s.%d", buf2, i); + sprintf(buf3, "%s.%d", buf2, i - 1); + rename(buf3, buf4); + } + rename(buf2, buf3); + } } } else { @@ -247,14 +278,23 @@ apr_time_exp_gmt(&e, tNow); apr_strftime(buf2, &rs, sizeof(buf2), szLogRoot, &e); } + else if (use_num) { + sprintf(buf2, "%s", szLogRoot); + } else { sprintf(buf2, "%s.%010d", szLogRoot, tLogStart); } tLogEnd = tLogStart + tRotation; pfile_prev = pfile; apr_pool_create(&pfile, pool); - rv = apr_file_open(&nLogFD, buf2, APR_WRITE | APR_CREATE | APR_APPEND, + if (use_num) { + rv = apr_file_open(&nLogFD, buf2, APR_WRITE | APR_CREATE, APR_OS_DEFAULT, pfile); + } + else { + rv = apr_file_open(&nLogFD, buf2, APR_WRITE | APR_CREATE | APR_APPEND, + APR_OS_DEFAULT, pfile); + } if (rv != APR_SUCCESS) { char error[120];