From: Ken Coar Date: Tue, 13 Mar 2001 21:25:28 +0000 (+0000) Subject: Enhance customisability of rotatelogs: strftime(3) X-Git-Tag: 2.0.15~43 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=43fbfa48f589f86ec1f2d73f759a70e648b8251d;p=thirdparty%2Fapache%2Fhttpd.git Enhance customisability of rotatelogs: strftime(3) formatting of filename and offset from UTC. Reviewed by: Greg Stein, David Reid, OtherBill git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@88511 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/CHANGES b/CHANGES index da5c7516323..9b0fa4389da 100644 --- a/CHANGES +++ b/CHANGES @@ -1,4 +1,9 @@ Changes with Apache 2.0.15-dev + + *) Enhance rotatelogs so that a UTC offset can be specified, and + the logfile name can be formatted using strftime(3). (Brought + forward from 1.3.) [Ken Coar] + *) Reimplement the Windows MPM (mpm_winnt.c) to eliminate calling DuplicateHandle on an IOCompletionPort (a practice which MS "discourages"). The new model does not rely on associating diff --git a/docs/man/rotatelogs.8 b/docs/man/rotatelogs.8 index 465c14bdb51..b4e6e234472 100644 --- a/docs/man/rotatelogs.8 +++ b/docs/man/rotatelogs.8 @@ -1,4 +1,4 @@ -.TH rotatelogs 8 "March 1998" +.TH rotatelogs 8 "March 2001" .\" The Apache Software License, Version 1.1 .\" .\" Copyright (c) 2000-2001 The Apache Software Foundation. All rights @@ -56,6 +56,7 @@ rotatelogs \- rotate Apache logs without having to kill the server .B rotatelogs .I logfile .I rotationtime +.I [offset] .PP .SH DESCRIPTION .B rotatelogs @@ -63,7 +64,7 @@ is a simple program for use in conjunction with Apache's piped logfile feature which can be used like this: .fi - TransferLog "|rotatelogs /path/to/logs/access_log 86400" + TransferLog "| rotatelogs /path/to/logs/access_log 86400" .mf This creates the files /path/to/logs/access_log.nnnn where nnnn is the system @@ -72,11 +73,16 @@ the rotation time, so you can synchronize cron scripts with it). At the end of each rotation time (here after 24 hours) a new log is started. .SH OPTIONS .IP \fB\fIlogfile\fP -The path plus basename of the logfile. The suffix .nnnn is automatically -added. +The path plus basename of the logfile. If \fBlogfile\fP includes any +'%' characters, it is treated as a format string for \fIstrftime(3)\fP. +Otherwise, the suffix .nnnn is automatically added and is the time at which +the logfile was created. .IP \fB\fIrotationtime\fP The rotation time in seconds. +.IP \fB\fIoffset\fP +The number of minutes offset from UTC. If omitted, zero is assumed and +UTC is used. For example, to use local time in the zone UTC -5 hours, +specify a value of \fI-300\fP for this argument. .PD .SH SEE ALSO .BR httpd(8) -. diff --git a/support/rotatelogs.c b/support/rotatelogs.c index 48268087439..34993287026 100644 --- a/support/rotatelogs.c +++ b/support/rotatelogs.c @@ -90,11 +90,15 @@ int main (int argc, char *argv[]) char buf[BUFSIZE], buf2[MAX_PATH], errbuf[ERRMSGSZ]; time_t tLogEnd = 0, tRotation; int nLogFD = -1, nLogFDprev = -1, nMessCount = 0, nRead, nWrite; + int utc_offset = 0; + int use_strftime = 0; + time_t now; char *szLogRoot; - if (argc != 3) { + if (argc < 3) { fprintf(stderr, - "%s \n\n", + "Usage: %s " + "[offset minutes from UTC]\n\n", argv[0]); #ifdef OS2 fprintf(stderr, @@ -115,26 +119,38 @@ int main (int argc, char *argv[]) } szLogRoot = argv[1]; + 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 = (strstr(szLogRoot, "%") != NULL); for (;;) { nRead = read(0, buf, sizeof buf); + now = time(NULL) + utc_offset; if (nRead == 0) exit(3); if (nRead < 0) if (errno != EINTR) exit(4); - if (nLogFD >= 0 && (time(NULL) >= tLogEnd || nRead < 0)) { + if (nLogFD >= 0 && (now >= tLogEnd || nRead < 0)) { nLogFDprev = nLogFD; nLogFD = -1; } if (nLogFD < 0) { - time_t tLogStart = (time(NULL) / tRotation) * tRotation; - sprintf(buf2, "%s.%010d", szLogRoot, (int) tLogStart); + time_t tLogStart = (now / tRotation) * tRotation; + if (use_strftime) { + struct tm *tm_now; + tm_now = gmtime(&tLogStart); + strftime(buf2, sizeof(buf2), szLogRoot, tm_now); + } + else { + sprintf(buf2, "%s.%010d", szLogRoot, (int) tLogStart); + } tLogEnd = tLogStart + tRotation; nLogFD = open(buf2, O_WRONLY | O_CREAT | O_APPEND, 0666); if (nLogFD < 0) {