Skip to content

Commit 9e983d7

Browse files
committed
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
1 parent 7cb4bb7 commit 9e983d7

3 files changed

Lines changed: 37 additions & 10 deletions

File tree

CHANGES

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
Changes with Apache 2.0.15-dev
2+
3+
*) Enhance rotatelogs so that a UTC offset can be specified, and
4+
the logfile name can be formatted using strftime(3). (Brought
5+
forward from 1.3.) [Ken Coar]
6+
27
*) Reimplement the Windows MPM (mpm_winnt.c) to eliminate calling
38
DuplicateHandle on an IOCompletionPort (a practice which
49
MS "discourages"). The new model does not rely on associating

docs/man/rotatelogs.8

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.TH rotatelogs 8 "March 1998"
1+
.TH rotatelogs 8 "March 2001"
22
.\" The Apache Software License, Version 1.1
33
.\"
44
.\" Copyright (c) 2000-2001 The Apache Software Foundation. All rights
@@ -56,14 +56,15 @@ rotatelogs \- rotate Apache logs without having to kill the server
5656
.B rotatelogs
5757
.I logfile
5858
.I rotationtime
59+
.I [offset]
5960
.PP
6061
.SH DESCRIPTION
6162
.B rotatelogs
6263
is a simple program for use in conjunction with Apache's piped logfile
6364
feature which can be used like this:
6465

6566
.fi
66-
TransferLog "|rotatelogs /path/to/logs/access_log 86400"
67+
TransferLog "| rotatelogs /path/to/logs/access_log 86400"
6768
.mf
6869

6970
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
7273
of each rotation time (here after 24 hours) a new log is started.
7374
.SH OPTIONS
7475
.IP \fB\fIlogfile\fP
75-
The path plus basename of the logfile. The suffix .nnnn is automatically
76-
added.
76+
The path plus basename of the logfile. If \fBlogfile\fP includes any
77+
'%' characters, it is treated as a format string for \fIstrftime(3)\fP.
78+
Otherwise, the suffix .nnnn is automatically added and is the time at which
79+
the logfile was created.
7780
.IP \fB\fIrotationtime\fP
7881
The rotation time in seconds.
82+
.IP \fB\fIoffset\fP
83+
The number of minutes offset from UTC. If omitted, zero is assumed and
84+
UTC is used. For example, to use local time in the zone UTC -5 hours,
85+
specify a value of \fI-300\fP for this argument.
7986
.PD
8087
.SH SEE ALSO
8188
.BR httpd(8)
82-
.

support/rotatelogs.c

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,15 @@ int main (int argc, char *argv[])
9090
char buf[BUFSIZE], buf2[MAX_PATH], errbuf[ERRMSGSZ];
9191
time_t tLogEnd = 0, tRotation;
9292
int nLogFD = -1, nLogFDprev = -1, nMessCount = 0, nRead, nWrite;
93+
int utc_offset = 0;
94+
int use_strftime = 0;
95+
time_t now;
9396
char *szLogRoot;
9497

95-
if (argc != 3) {
98+
if (argc < 3) {
9699
fprintf(stderr,
97-
"%s <logfile> <rotation time in seconds>\n\n",
100+
"Usage: %s <logfile> <rotation time in seconds> "
101+
"[offset minutes from UTC]\n\n",
98102
argv[0]);
99103
#ifdef OS2
100104
fprintf(stderr,
@@ -115,26 +119,38 @@ int main (int argc, char *argv[])
115119
}
116120

117121
szLogRoot = argv[1];
122+
if (argc >= 4) {
123+
utc_offset = atoi(argv[3]) * 60;
124+
}
118125
tRotation = atoi(argv[2]);
119126
if (tRotation <= 0) {
120127
fprintf(stderr, "Rotation time must be > 0\n");
121128
exit(6);
122129
}
123130

131+
use_strftime = (strstr(szLogRoot, "%") != NULL);
124132
for (;;) {
125133
nRead = read(0, buf, sizeof buf);
134+
now = time(NULL) + utc_offset;
126135
if (nRead == 0)
127136
exit(3);
128137
if (nRead < 0)
129138
if (errno != EINTR)
130139
exit(4);
131-
if (nLogFD >= 0 && (time(NULL) >= tLogEnd || nRead < 0)) {
140+
if (nLogFD >= 0 && (now >= tLogEnd || nRead < 0)) {
132141
nLogFDprev = nLogFD;
133142
nLogFD = -1;
134143
}
135144
if (nLogFD < 0) {
136-
time_t tLogStart = (time(NULL) / tRotation) * tRotation;
137-
sprintf(buf2, "%s.%010d", szLogRoot, (int) tLogStart);
145+
time_t tLogStart = (now / tRotation) * tRotation;
146+
if (use_strftime) {
147+
struct tm *tm_now;
148+
tm_now = gmtime(&tLogStart);
149+
strftime(buf2, sizeof(buf2), szLogRoot, tm_now);
150+
}
151+
else {
152+
sprintf(buf2, "%s.%010d", szLogRoot, (int) tLogStart);
153+
}
138154
tLogEnd = tLogStart + tRotation;
139155
nLogFD = open(buf2, O_WRONLY | O_CREAT | O_APPEND, 0666);
140156
if (nLogFD < 0) {

0 commit comments

Comments
 (0)