Skip to content

Commit 5401e88

Browse files
author
Bradley Nicholes
committed
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
1 parent 9fcdf8b commit 5401e88

2 files changed

Lines changed: 65 additions & 17 deletions

File tree

CHANGES

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11

22
Changes with Apache 2.0.38
33

4+
*) Added log rotation based on file size to the RotateLog support
5+
utility. [Brad Nicholes]
6+
47
*) Fix some casting in mod_rewrite which broke random maps.
58
PR 9770 [Allan Edwards, Greg Ames, Jeff Trawick]
69

support/rotatelogs.c

Lines changed: 62 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,16 @@
9494
int main (int argc, const char * const argv[])
9595
{
9696
char buf[BUFSIZE], buf2[MAX_PATH], errbuf[ERRMSGSZ];
97-
int tLogEnd = 0, tRotation, utc_offset = 0;
97+
int tLogEnd = 0, tRotation = 0, utc_offset = 0;
98+
unsigned int sRotation = 0;
9899
int nMessCount = 0;
99100
apr_size_t nRead, nWrite;
100101
int use_strftime = 0;
101102
int now;
102103
const char *szLogRoot;
103104
apr_file_t *f_stdin, *nLogFD = NULL, *nLogFDprev = NULL;
104105
apr_pool_t *pool;
106+
char *ptr = NULL;
105107

106108
apr_app_initialize(&argc, &argv, NULL);
107109
atexit(apr_terminate);
@@ -110,7 +112,7 @@ int main (int argc, const char * const argv[])
110112
if (argc < 3 || argc > 4) {
111113
fprintf(stderr,
112114
"Usage: %s <logfile> <rotation time in seconds> "
113-
"[offset minutes from UTC]\n\n",
115+
"[offset minutes from UTC] or <rotation size in megabytes>\n\n",
114116
argv[0]);
115117
#ifdef OS2
116118
fprintf(stderr,
@@ -120,24 +122,40 @@ int main (int argc, const char * const argv[])
120122
fprintf(stderr,
121123
"Add this:\n\nTransferLog \"|%s /some/where 86400\"\n\n",
122124
argv[0]);
125+
fprintf(stderr,
126+
"or \n\nTransferLog \"|%s /some/where 5M\"\n\n", argv[0]);
123127
#endif
124128
fprintf(stderr,
125129
"to httpd.conf. The generated name will be /some/where.nnnn "
126130
"where nnnn is the\nsystem time at which the log nominally "
127-
"starts (N.B. this time will always be a\nmultiple of the "
128-
"rotation time, so you can synchronize cron scripts with it).\n"
129-
"At the end of each rotation time a new log is started.\n");
131+
"starts (N.B. if using a rotation time,\nthe time will always "
132+
"be a multiple of the rotation time, so you can synchronize\n"
133+
"cron scripts with it). At the end of each rotation time or "
134+
"when the file size\nis reached a new log is started.\n");
130135
exit(1);
131136
}
132137

133138
szLogRoot = argv[1];
134-
if (argc >= 4) {
135-
utc_offset = atoi(argv[3]) * 60;
139+
140+
ptr = strchr (argv[2], 'M');
141+
if (ptr) {
142+
if (*(ptr+1) == '\0') {
143+
sRotation = atoi(argv[2]) * 1048576;
144+
}
145+
if (sRotation == 0) {
146+
fprintf(stderr, "Invalid rotation size parameter\n");
147+
exit(1);
148+
}
136149
}
137-
tRotation = atoi(argv[2]);
138-
if (tRotation <= 0) {
139-
fprintf(stderr, "Rotation time must be > 0\n");
140-
exit(6);
150+
else {
151+
if (argc >= 4) {
152+
utc_offset = atoi(argv[3]) * 60;
153+
}
154+
tRotation = atoi(argv[2]);
155+
if (tRotation <= 0) {
156+
fprintf(stderr, "Rotation time must be > 0\n");
157+
exit(6);
158+
}
141159
}
142160

143161
use_strftime = (strchr(szLogRoot, '%') != NULL);
@@ -150,17 +168,44 @@ int main (int argc, const char * const argv[])
150168
nRead = sizeof(buf);
151169
if (apr_file_read(f_stdin, buf, &nRead) != APR_SUCCESS)
152170
exit(3);
153-
now = (int)(apr_time_now() / APR_USEC_PER_SEC) + utc_offset;
154171
if (nRead == 0)
155172
exit(3);
156-
if (nLogFD != NULL && (now >= tLogEnd || nRead < 0)) {
157-
nLogFDprev = nLogFD;
158-
nLogFD = NULL;
173+
if (tRotation) {
174+
now = (int)(apr_time_now() / APR_USEC_PER_SEC) + utc_offset;
175+
if (nLogFD != NULL && (now >= tLogEnd || nRead < 0)) {
176+
nLogFDprev = nLogFD;
177+
nLogFD = NULL;
178+
}
179+
}
180+
else if (sRotation) {
181+
apr_finfo_t finfo;
182+
unsigned int current_size = -1;
183+
184+
if ((nLogFD != NULL) &&
185+
(apr_file_info_get(&finfo, APR_FINFO_SIZE, nLogFD) == APR_SUCCESS)) {
186+
current_size = finfo.size;
187+
}
188+
189+
if (current_size > sRotation || nRead < 0) {
190+
nLogFDprev = nLogFD;
191+
nLogFD = NULL;
192+
}
159193
}
194+
else {
195+
fprintf(stderr, "No rotation time or size specified\n");
196+
exit(2);
197+
}
198+
160199
if (nLogFD == NULL) {
161-
int tLogStart = (now / tRotation) * tRotation;
200+
int tLogStart;
201+
202+
if (tRotation)
203+
tLogStart = (now / tRotation) * tRotation;
204+
else
205+
tLogStart = apr_time_now() / APR_USEC_PER_SEC;
206+
162207
if (use_strftime) {
163-
apr_time_t tNow = tLogStart * APR_USEC_PER_SEC;
208+
apr_time_t tNow = tLogStart * APR_USEC_PER_SEC;
164209
apr_time_exp_t e;
165210
apr_size_t rs;
166211

0 commit comments

Comments
 (0)