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;
123 apr_pool_t *pfile = NULL;
124 apr_pool_t *pfile_prev = NULL;
131 apr_app_initialize(&argc, &argv, NULL);
132 atexit(apr_terminate);
134 apr_pool_create(&pool, NULL);
135 apr_getopt_init(&opt, pool, argc, argv);
136 while ((rv = apr_getopt(opt, "l", &c, &optarg)) == APR_SUCCESS) {
145 usage(argv[0], NULL /* specific error message already issued */ );
148 if (opt->ind + 2 != argc && opt->ind + 3 != argc) {
149 usage(argv[0], "Incorrect number of arguments");
152 szLogRoot = argv[opt->ind++];
154 ptr = strchr(argv[opt->ind], 'M');
155 if (ptr) { /* rotation based on file size */
156 if (*(ptr+1) == '\0') {
157 sRotation = atoi(argv[opt->ind]) * 1048576;
159 if (sRotation == 0) {
160 usage(argv[0], "Invalid rotation size parameter");
163 else { /* rotation based on elapsed time */
164 tRotation = atoi(argv[opt->ind]);
165 if (tRotation <= 0) {
166 usage(argv[0], "Invalid rotation time parameter");
171 if (opt->ind < argc) { /* have UTC offset */
173 usage(argv[0], "UTC offset parameter is not valid with -l");
175 utc_offset = atoi(argv[opt->ind]) * 60;
178 use_strftime = (strchr(szLogRoot, '%') != NULL);
179 if (apr_file_open_stdin(&f_stdin, pool) != APR_SUCCESS) {
180 fprintf(stderr, "Unable to open stdin\n");
186 if (apr_file_read(f_stdin, buf, &nRead) != APR_SUCCESS) {
190 now = get_now(use_localtime, utc_offset);
191 if (nLogFD != NULL && now >= tLogEnd) {
196 else if (sRotation) {
198 apr_off_t current_size = -1;
200 if ((nLogFD != NULL) &&
201 (apr_file_info_get(&finfo, APR_FINFO_SIZE, nLogFD) == APR_SUCCESS)) {
202 current_size = finfo.size;
205 if (current_size > sRotation) {
211 fprintf(stderr, "No rotation time or size specified\n");
215 if (nLogFD == NULL) {
220 tLogStart = (now / tRotation) * tRotation;
223 tLogStart = get_now(use_localtime, utc_offset);
227 apr_time_t tNow = apr_time_from_sec(tLogStart);
231 apr_time_exp_gmt(&e, tNow);
232 apr_strftime(buf2, &rs, sizeof(buf2), szLogRoot, &e);
235 sprintf(buf2, "%s.%010d", szLogRoot, tLogStart);
237 tLogEnd = tLogStart + tRotation;
239 apr_pool_create(&pfile, pool);
240 rv = apr_file_open(&nLogFD, buf2, APR_WRITE | APR_CREATE | APR_APPEND,
241 APR_OS_DEFAULT, pfile);
242 if (rv != APR_SUCCESS) {
245 apr_strerror(rv, error, sizeof error);
247 /* Uh-oh. Failed to open the new log file. Try to clear
248 * the previous log file, note the lost log entries,
249 * and keep on truckin'. */
250 if (nLogFDprev == NULL) {
251 fprintf(stderr, "Could not open log file '%s' (%s)\n", buf2, error);
256 apr_pool_destroy(pfile);
258 /* Try to keep this error message constant length
259 * in case it occurs several times. */
260 apr_snprintf(errbuf, sizeof errbuf,
261 "Resetting log file due to error opening "
262 "new log file, %10d messages lost: %-25.25s\n",
264 nWrite = strlen(errbuf);
265 apr_file_trunc(nLogFD, 0);
266 if (apr_file_write(nLogFD, errbuf, &nWrite) != APR_SUCCESS) {
267 fprintf(stderr, "Error writing to the file %s\n", buf2);
272 else if (nLogFDprev) {
273 apr_file_close(nLogFDprev);
275 apr_pool_destroy(pfile_prev);
281 apr_file_write(nLogFD, buf, &nWrite);
282 if (nWrite != nRead) {
285 "Error writing to log file. "
286 "%10d messages lost.\n",
288 nWrite = strlen(errbuf);
289 apr_file_trunc(nLogFD, 0);
290 if (apr_file_write(nLogFD, errbuf, &nWrite) != APR_SUCCESS) {
291 fprintf(stderr, "Error writing to the file %s\n", buf2);
299 /* Of course we never, but prevent compiler warnings */