1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * Simple program to rotate Apache logs without having to kill the server.
20 * Contributed by Ben Laurie <ben algroup.co.uk>
24 * Ported to APR by Mladen Turk <mturk mappingsoft.com>
28 * -l option added 2004-06-11
30 * -l causes the use of local time rather than GMT as the base for the
31 * interval. NB: Using -l in an environment which changes the GMT offset
32 * (such as for BST or DST) can lead to unpredictable results!
39 #include "apr_strings.h"
40 #include "apr_errno.h"
41 #include "apr_file_io.h"
42 #include "apr_file_info.h"
43 #include "apr_general.h"
45 #include "apr_getopt.h"
53 #if APR_HAVE_STRINGS_H
64 static void usage(const char *argv0, const char *reason)
67 fprintf(stderr, "%s\n", reason);
70 "Usage: %s [-l] <logfile> "
71 "{<rotation time in seconds>|<rotation size in megabytes>} "
72 "[offset minutes from UTC]\n\n",
76 "Add this:\n\nTransferLog \"|%s.exe /some/where 86400\"\n\n",
80 "Add this:\n\nTransferLog \"|%s /some/where 86400\"\n\n",
83 "or \n\nTransferLog \"|%s /some/where 5M\"\n\n", argv0);
86 "to httpd.conf. The generated name will be /some/where.nnnn "
87 "where nnnn is the\nsystem time at which the log nominally "
88 "starts (N.B. if using a rotation time,\nthe time will always "
89 "be a multiple of the rotation time, so you can synchronize\n"
90 "cron scripts with it). At the end of each rotation time or "
91 "when the file size\nis reached a new log is started.\n");
95 static int get_now(int use_localtime, int utc_offset)
97 apr_time_t tNow = apr_time_now();
99 /* Check for our UTC offset before using it, since it might
100 * change if there's a switch between standard and daylight
104 apr_time_exp_lt(<, tNow);
105 utc_offset = lt.tm_gmtoff;
107 return (int)apr_time_sec(tNow) + utc_offset;
110 int main (int argc, const char * const argv[])
112 char buf[BUFSIZE], buf2[MAX_PATH], errbuf[ERRMSGSZ];
113 int tLogEnd = 0, tRotation = 0, utc_offset = 0;
114 unsigned int sRotation = 0;
116 apr_size_t nRead, nWrite;
117 int use_strftime = 0;
118 int use_localtime = 0;
120 const char *szLogRoot;
121 apr_file_t *f_stdin, *nLogFD = NULL, *nLogFDprev = NULL;
129 apr_app_initialize(&argc, &argv, NULL);
130 atexit(apr_terminate);
132 apr_pool_create(&pool, NULL);
133 apr_getopt_init(&opt, pool, argc, argv);
134 while ((rv = apr_getopt(opt, "l", &c, &optarg)) == APR_SUCCESS) {
143 usage(argv[0], NULL /* specific error message already issued */ );
146 if (opt->ind + 2 != argc && opt->ind + 3 != argc) {
147 usage(argv[0], "Incorrect number of arguments");
150 szLogRoot = argv[opt->ind++];
152 ptr = strchr(argv[opt->ind], 'M');
153 if (ptr) { /* rotation based on file size */
154 if (*(ptr+1) == '\0') {
155 sRotation = atoi(argv[opt->ind]) * 1048576;
157 if (sRotation == 0) {
158 usage(argv[0], "Invalid rotation size parameter");
161 else { /* rotation based on elapsed time */
162 tRotation = atoi(argv[opt->ind]);
163 if (tRotation <= 0) {
164 usage(argv[0], "Invalid rotation time parameter");
169 if (opt->ind < argc) { /* have UTC offset */
171 usage(argv[0], "UTC offset parameter is not valid with -l");
173 utc_offset = atoi(argv[opt->ind]) * 60;
176 use_strftime = (strchr(szLogRoot, '%') != NULL);
177 if (apr_file_open_stdin(&f_stdin, pool) != APR_SUCCESS) {
178 fprintf(stderr, "Unable to open stdin\n");
184 if (apr_file_read(f_stdin, buf, &nRead) != APR_SUCCESS) {
188 now = get_now(use_localtime, utc_offset);
189 if (nLogFD != NULL && now >= tLogEnd) {
194 else if (sRotation) {
196 apr_off_t current_size = -1;
198 if ((nLogFD != NULL) &&
199 (apr_file_info_get(&finfo, APR_FINFO_SIZE, nLogFD) == APR_SUCCESS)) {
200 current_size = finfo.size;
203 if (current_size > sRotation) {
209 fprintf(stderr, "No rotation time or size specified\n");
213 if (nLogFD == NULL) {
218 tLogStart = (now / tRotation) * tRotation;
221 tLogStart = get_now(use_localtime, utc_offset);
225 apr_time_t tNow = apr_time_from_sec(tLogStart);
229 apr_time_exp_gmt(&e, tNow);
230 apr_strftime(buf2, &rs, sizeof(buf2), szLogRoot, &e);
233 sprintf(buf2, "%s.%010d", szLogRoot, tLogStart);
235 tLogEnd = tLogStart + tRotation;
236 rv = apr_file_open(&nLogFD, buf2, APR_WRITE | APR_CREATE | APR_APPEND,
237 APR_OS_DEFAULT, pool);
238 if (rv != APR_SUCCESS) {
241 apr_strerror(rv, error, sizeof error);
243 /* Uh-oh. Failed to open the new log file. Try to clear
244 * the previous log file, note the lost log entries,
245 * and keep on truckin'. */
246 if (nLogFDprev == NULL) {
247 fprintf(stderr, "Could not open log file '%s' (%s)\n", buf2, error);
252 /* Try to keep this error message constant length
253 * in case it occurs several times. */
254 apr_snprintf(errbuf, sizeof errbuf,
255 "Resetting log file due to error opening "
256 "new log file, %10d messages lost: %-25.25s\n",
258 nWrite = strlen(errbuf);
259 apr_file_trunc(nLogFD, 0);
260 if (apr_file_write(nLogFD, errbuf, &nWrite) != APR_SUCCESS) {
261 fprintf(stderr, "Error writing to the file %s\n", buf2);
266 else if (nLogFDprev) {
267 apr_file_close(nLogFDprev);
272 apr_file_write(nLogFD, buf, &nWrite);
273 if (nWrite != nRead) {
276 "Error writing to log file. "
277 "%10d messages lost.\n",
279 nWrite = strlen(errbuf);
280 apr_file_trunc(nLogFD, 0);
281 if (apr_file_write(nLogFD, errbuf, &nWrite) != APR_SUCCESS) {
282 fprintf(stderr, "Error writing to the file %s\n", buf2);
290 /* Of course we never, but prevent compiler warnings */