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"
52 #if APR_HAVE_STRINGS_H
63 int main (int argc, const char * const argv[])
65 char buf[BUFSIZE], buf2[MAX_PATH], errbuf[ERRMSGSZ];
66 int tLogEnd = 0, tRotation = 0, utc_offset = 0;
67 unsigned int sRotation = 0;
69 apr_size_t nRead, nWrite;
71 int use_localtime = 0;
73 const char *szLogRoot;
74 apr_file_t *f_stdin, *nLogFD = NULL, *nLogFDprev = NULL;
82 apr_app_initialize(&argc, &argv, NULL);
83 atexit(apr_terminate);
85 apr_pool_create(&pool, NULL);
86 if ((argc > 2) && (strcmp(argv[1], "-l") == 0)) {
93 if (argc < (argBase + 3) || argc > (argBase + 4)) {
95 "Usage: %s [-l] <logfile> <rotation time in seconds> "
96 "[offset minutes from UTC] or <rotation size in megabytes>\n\n",
100 "Add this:\n\nTransferLog \"|%s.exe /some/where 86400\"\n\n",
104 "Add this:\n\nTransferLog \"|%s /some/where 86400\"\n\n",
107 "or \n\nTransferLog \"|%s /some/where 5M\"\n\n", argv[0]);
110 "to httpd.conf. The generated name will be /some/where.nnnn "
111 "where nnnn is the\nsystem time at which the log nominally "
112 "starts (N.B. if using a rotation time,\nthe time will always "
113 "be a multiple of the rotation time, so you can synchronize\n"
114 "cron scripts with it). At the end of each rotation time or "
115 "when the file size\nis reached a new log is started.\n");
119 szLogRoot = argv[argFile];
121 ptr = strchr(argv[argIntv], 'M');
123 if (*(ptr+1) == '\0') {
124 sRotation = atoi(argv[argIntv]) * 1048576;
126 if (sRotation == 0) {
127 fprintf(stderr, "Invalid rotation size parameter\n");
132 if (argc >= (argBase + 4)) {
133 utc_offset = atoi(argv[argOffset]) * 60;
135 tRotation = atoi(argv[argIntv]);
136 if (tRotation <= 0) {
137 fprintf(stderr, "Rotation time must be > 0\n");
142 use_strftime = (strchr(szLogRoot, '%') != NULL);
143 if (apr_file_open_stdin(&f_stdin, pool) != APR_SUCCESS) {
144 fprintf(stderr, "Unable to open stdin\n");
150 if (apr_file_read(f_stdin, buf, &nRead) != APR_SUCCESS) {
155 * Check for our UTC offset every time through the loop, since
156 * it might change if there's a switch between standard and
157 * daylight savings time.
161 apr_time_exp_lt(<, apr_time_now());
162 utc_offset = lt.tm_gmtoff;
164 now = (int)(apr_time_now() / APR_USEC_PER_SEC) + utc_offset;
165 if (nLogFD != NULL && now >= tLogEnd) {
170 else if (sRotation) {
172 apr_off_t current_size = -1;
174 if ((nLogFD != NULL) &&
175 (apr_file_info_get(&finfo, APR_FINFO_SIZE, nLogFD) == APR_SUCCESS)) {
176 current_size = finfo.size;
179 if (current_size > sRotation) {
185 fprintf(stderr, "No rotation time or size specified\n");
189 if (nLogFD == NULL) {
193 tLogStart = (now / tRotation) * tRotation;
196 tLogStart = (int)apr_time_sec(apr_time_now());
200 apr_time_t tNow = apr_time_from_sec(tLogStart);
204 apr_time_exp_gmt(&e, tNow);
205 apr_strftime(buf2, &rs, sizeof(buf2), szLogRoot, &e);
208 sprintf(buf2, "%s.%010d", szLogRoot, tLogStart);
210 tLogEnd = tLogStart + tRotation;
211 apr_file_open(&nLogFD, buf2, APR_READ | APR_WRITE | APR_CREATE | APR_APPEND,
212 APR_OS_DEFAULT, pool);
213 if (nLogFD == NULL) {
214 /* Uh-oh. Failed to open the new log file. Try to clear
215 * the previous log file, note the lost log entries,
216 * and keep on truckin'. */
217 if (nLogFDprev == NULL) {
218 fprintf(stderr, "1 Previous file handle doesn't exists %s\n", buf2);
224 "Resetting log file due to error opening "
225 "new log file. %10d messages lost.\n",
227 nWrite = strlen(errbuf);
228 apr_file_trunc(nLogFD, 0);
229 if (apr_file_write(nLogFD, errbuf, &nWrite) != APR_SUCCESS) {
230 fprintf(stderr, "Error writing to the file %s\n", buf2);
235 else if (nLogFDprev) {
236 apr_file_close(nLogFDprev);
241 apr_file_write(nLogFD, buf, &nWrite);
242 if (nWrite != nRead) {
245 "Error writing to log file. "
246 "%10d messages lost.\n",
248 nWrite = strlen(errbuf);
249 apr_file_trunc(nLogFD, 0);
250 if (apr_file_write(nLogFD, errbuf, &nWrite) != APR_SUCCESS) {
251 fprintf(stderr, "Error writing to the file %s\n", buf2);
259 /* Of course we never, but prevent compiler warnings */