Commit 9e983d79 authored by Ken Coar's avatar Ken Coar
Browse files

	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
parent 7cb4bb71
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
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
+11 −5
Original line number Diff line number Diff line
.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
@@ -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)
.
+21 −5
Original line number Diff line number Diff line
@@ -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 <logfile> <rotation time in seconds>\n\n",
                "Usage: %s <logfile> <rotation time in seconds> "
                "[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;
            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) {