2 * DEBUG: section 50 Log file handling
3 * AUTHOR: Adrian Chadd <adrian@squid-cache.org>
5 * SQUID Web Proxy Cache http://www.squid-cache.org/
6 * ----------------------------------------------------------
8 * Squid is the result of efforts by numerous individuals from
9 * the Internet community; see the CONTRIBUTORS file for full
10 * details. Many organizations have provided support for Squid's
11 * development; see the SPONSORS file for full details. Squid is
12 * Copyrighted (C) 2001 by the Regents of the University of
13 * California; see the COPYRIGHT file for full details. Squid
14 * incorporates software developed and/or copyrighted by other
15 * sources; see the CREDITS file for full details.
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
36 #include "log/Config.h"
38 #include "log/ModDaemon.h"
39 #include "SquidTime.h"
41 /* How many buffers to keep before we say we've buffered too much */
42 #define LOGFILE_MAXBUFS 128
44 /* Size of the logfile buffer */
46 * For optimal performance this should match LOGFILE_BUFSIZ in logfile-daemon.c
48 #define LOGFILE_BUFSZ 32768
50 /* How many seconds between warnings */
51 #define LOGFILE_WARN_TIME 30
53 static LOGWRITE logfile_mod_daemon_writeline
;
54 static LOGLINESTART logfile_mod_daemon_linestart
;
55 static LOGLINEEND logfile_mod_daemon_lineend
;
56 static LOGROTATE logfile_mod_daemon_rotate
;
57 static LOGFLUSH logfile_mod_daemon_flush
;
58 static LOGCLOSE logfile_mod_daemon_close
;
60 static void logfile_mod_daemon_append(Logfile
* lf
, const char *buf
, int len
);
72 typedef struct _l_daemon l_daemon_t
;
76 logfileNewBuffer(Logfile
* lf
)
78 l_daemon_t
*ll
= (l_daemon_t
*) lf
->data
;
81 debugs(50, 5, "logfileNewBuffer: " << lf
->path
<< ": new buffer");
83 b
= static_cast<logfile_buffer_t
*>(xcalloc(1, sizeof(logfile_buffer_t
)));
85 b
->buf
= static_cast<char*>(xcalloc(1, LOGFILE_BUFSZ
));
86 assert(b
->buf
!= NULL
);
87 b
->size
= LOGFILE_BUFSZ
;
90 dlinkAddTail(b
, &b
->node
, &ll
->bufs
);
95 logfileFreeBuffer(Logfile
* lf
, logfile_buffer_t
* b
)
97 l_daemon_t
*ll
= (l_daemon_t
*) lf
->data
;
99 dlinkDelete(&b
->node
, &ll
->bufs
);
106 logfileHandleWrite(int fd
, void *data
)
108 Logfile
*lf
= (Logfile
*) data
;
109 l_daemon_t
*ll
= (l_daemon_t
*) lf
->data
;
114 * We'll try writing the first entry until its done - if we
115 * get a partial write then we'll re-schedule until its completed.
116 * Its naive but it'll do for now.
118 b
= static_cast<logfile_buffer_t
*>(ll
->bufs
.head
->data
);
120 ll
->flush_pending
= 0;
122 ret
= FD_WRITE_METHOD(ll
->wfd
, b
->buf
+ b
->written_len
, b
->len
- b
->written_len
);
123 debugs(50, 3, "logfileHandleWrite: " << lf
->path
<< ": write returned " << ret
);
125 if (ignoreErrno(errno
)) {
126 /* something temporary */
129 debugs(50, DBG_IMPORTANT
,"logfileHandleWrite: " << lf
->path
<< ": error writing (" << xstrerror() << ")");
130 /* XXX should handle this better */
131 fatal("I don't handle this error well!");
135 debugs(50, DBG_IMPORTANT
, "logfileHandleWrite: " << lf
->path
<< ": wrote 0 bytes?");
136 /* XXX should handle this better */
137 fatal("I don't handle this error well!");
139 /* ret > 0, so something was written */
140 b
->written_len
+= ret
;
141 assert(b
->written_len
<= b
->len
);
142 if (b
->written_len
== b
->len
) {
143 /* written the whole buffer! */
144 logfileFreeBuffer(lf
, b
);
147 /* Is there more to write? */
148 if (ll
->bufs
.head
== NULL
) {
151 /* there is, so schedule more */
154 commSetSelect(ll
->wfd
, COMM_SELECT_WRITE
, logfileHandleWrite
, lf
, 0);
155 ll
->flush_pending
= 1;
161 logfileQueueWrite(Logfile
* lf
)
163 l_daemon_t
*ll
= (l_daemon_t
*) lf
->data
;
164 if (ll
->flush_pending
|| ll
->bufs
.head
== NULL
) {
167 ll
->flush_pending
= 1;
169 logfile_buffer_t
*b
= static_cast<logfile_buffer_t
*>(ll
->bufs
.head
->data
);
170 if (b
->len
+ 2 <= b
->size
)
171 logfile_mod_daemon_append(lf
, "F\n", 2);
173 /* Ok, schedule a write-event */
174 commSetSelect(ll
->wfd
, COMM_SELECT_WRITE
, logfileHandleWrite
, lf
, 0);
178 logfile_mod_daemon_append(Logfile
* lf
, const char *buf
, int len
)
180 l_daemon_t
*ll
= (l_daemon_t
*) lf
->data
;
184 /* Is there a buffer? If not, create one */
185 if (ll
->bufs
.head
== NULL
) {
186 logfileNewBuffer(lf
);
188 debugs(50, 3, "logfile_mod_daemon_append: " << lf
->path
<< ": appending " << len
<< " bytes");
189 /* Copy what can be copied */
191 b
= static_cast<logfile_buffer_t
*>(ll
->bufs
.tail
->data
);
192 debugs(50, 3, "logfile_mod_daemon_append: current buffer has " << b
->len
<< " of " << b
->size
<< " bytes before append");
193 s
= min(len
, (b
->size
- b
->len
));
194 xmemcpy(b
->buf
+ b
->len
, buf
, s
);
198 assert(b
->len
<= LOGFILE_BUFSZ
);
201 logfileNewBuffer(lf
);
207 * only schedule a flush (write) if one isn't scheduled.
210 logfileFlushEvent(void *data
)
212 Logfile
*lf
= static_cast<Logfile
*>(data
);
215 * This might work better if we keep track of when we wrote last and only
216 * schedule a write if we haven't done so in the last second or two.
218 logfileQueueWrite(lf
);
219 eventAdd("logfileFlush", logfileFlushEvent
, lf
, 1.0, 1);
226 logfile_mod_daemon_open(Logfile
* lf
, const char *path
, size_t bufsz
, int fatal_flag
)
232 lf
->f_close
= logfile_mod_daemon_close
;
233 lf
->f_linewrite
= logfile_mod_daemon_writeline
;
234 lf
->f_linestart
= logfile_mod_daemon_linestart
;
235 lf
->f_lineend
= logfile_mod_daemon_lineend
;
236 lf
->f_flush
= logfile_mod_daemon_flush
;
237 lf
->f_rotate
= logfile_mod_daemon_rotate
;
239 cbdataInternalLock(lf
); // WTF?
240 debugs(50, 1, "Logfile Daemon: opening log " << path
);
241 ll
= static_cast<l_daemon_t
*>(xcalloc(1, sizeof(*ll
)));
245 Ip::Address localhost
;
246 args
[0] = "(logfile-daemon)";
249 localhost
.SetLocalhost();
250 ll
->pid
= ipcCreate(IPC_STREAM
, Log::TheConfig
.logfile_daemon
, args
, "logfile-daemon", localhost
, &ll
->rfd
, &ll
->wfd
, NULL
);
252 fatal("Couldn't start logfile helper");
256 /* Queue the initial control data */
257 tmpbuf
= static_cast<char*>(xmalloc(BUFSIZ
));
258 snprintf(tmpbuf
, BUFSIZ
, "r%d\nb%d\n", Config
.Log
.rotateNumber
, Config
.onoff
.buffered_logs
);
259 logfile_mod_daemon_append(lf
, tmpbuf
, strlen(tmpbuf
));
262 /* Start the flush event */
263 eventAdd("logfileFlush", logfileFlushEvent
, lf
, 1.0, 1);
269 logfile_mod_daemon_close(Logfile
* lf
)
271 l_daemon_t
*ll
= static_cast<l_daemon_t
*>(lf
->data
);
272 debugs(50, 1, "Logfile Daemon: closing log " << lf
->path
);
274 if (ll
->rfd
== ll
->wfd
)
280 kill(ll
->pid
, SIGTERM
);
281 eventDelete(logfileFlushEvent
, lf
);
284 cbdataInternalUnlock(lf
); // WTF??
288 logfile_mod_daemon_rotate(Logfile
* lf
)
291 debugs(50, 1, "logfileRotate: " << lf
->path
);
295 logfile_mod_daemon_append(lf
, tb
, 2);
299 * This routine assumes that up to one line is written. Don't try to
300 * call this routine with more than one line or subsequent lines
301 * won't be prefixed with the command type and confuse the logging
305 logfile_mod_daemon_writeline(Logfile
* lf
, const char *buf
, size_t len
)
307 l_daemon_t
*ll
= static_cast<l_daemon_t
*>(lf
->data
);
308 /* Make sure the logfile buffer isn't too large */
309 if (ll
->nbufs
> LOGFILE_MAXBUFS
) {
310 if (ll
->last_warned
< squid_curtime
- LOGFILE_WARN_TIME
) {
311 ll
->last_warned
= squid_curtime
;
312 debugs(50, DBG_IMPORTANT
, "Logfile: " << lf
->path
<< ": queue is too large; some log messages have been lost.");
316 /* Append this data to the end buffer; create a new one if needed */
317 /* Are we eol? If so, prefix with our logfile command byte */
318 logfile_mod_daemon_append(lf
, buf
, len
);
322 logfile_mod_daemon_linestart(Logfile
* lf
)
324 l_daemon_t
*ll
= static_cast<l_daemon_t
*>(lf
->data
);
326 assert(ll
->eol
== 1);
330 logfile_mod_daemon_append(lf
, tb
, 1);
334 logfile_mod_daemon_lineend(Logfile
* lf
)
336 l_daemon_t
*ll
= static_cast<l_daemon_t
*>(lf
->data
);
338 assert(ll
->eol
== 0);
340 /* Kick a write off if the head buffer is -full- */
341 if (ll
->bufs
.head
!= NULL
) {
342 b
= static_cast<logfile_buffer_t
*>(ll
->bufs
.head
->data
);
343 if (b
->node
.next
!= NULL
|| !Config
.onoff
.buffered_logs
)
344 logfileQueueWrite(lf
);
349 logfile_mod_daemon_flush(Logfile
* lf
)
351 l_daemon_t
*ll
= static_cast<l_daemon_t
*>(lf
->data
);
352 if (commUnsetNonBlocking(ll
->wfd
)) {
353 debugs(50, DBG_IMPORTANT
, "Logfile Daemon: Couldn't set the pipe blocking for flush! You're now missing some log entries.");
356 while (ll
->bufs
.head
!= NULL
) {
357 logfileHandleWrite(ll
->wfd
, lf
);
359 if (commSetNonBlocking(ll
->wfd
)) {
360 fatalf("Logfile Daemon: %s: Couldn't set the pipe non-blocking for flush!\n", lf
->path
);