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.
35 #include "comm/Loops.h"
37 #include "log/Config.h"
39 #include "log/ModDaemon.h"
40 #include "SquidTime.h"
42 /* How many buffers to keep before we say we've buffered too much */
43 #define LOGFILE_MAXBUFS 128
45 /* Size of the logfile buffer */
47 * For optimal performance this should match LOGFILE_BUFSIZ in logfile-daemon.c
49 #define LOGFILE_BUFSZ 32768
51 /* How many seconds between warnings */
52 #define LOGFILE_WARN_TIME 30
54 static LOGWRITE logfile_mod_daemon_writeline
;
55 static LOGLINESTART logfile_mod_daemon_linestart
;
56 static LOGLINEEND logfile_mod_daemon_lineend
;
57 static LOGROTATE logfile_mod_daemon_rotate
;
58 static LOGFLUSH logfile_mod_daemon_flush
;
59 static LOGCLOSE logfile_mod_daemon_close
;
61 static void logfile_mod_daemon_append(Logfile
* lf
, const char *buf
, int len
);
73 typedef struct _l_daemon l_daemon_t
;
77 logfileNewBuffer(Logfile
* lf
)
79 l_daemon_t
*ll
= (l_daemon_t
*) lf
->data
;
82 debugs(50, 5, "logfileNewBuffer: " << lf
->path
<< ": new buffer");
84 b
= static_cast<logfile_buffer_t
*>(xcalloc(1, sizeof(logfile_buffer_t
)));
86 b
->buf
= static_cast<char*>(xcalloc(1, LOGFILE_BUFSZ
));
87 assert(b
->buf
!= NULL
);
88 b
->size
= LOGFILE_BUFSZ
;
91 dlinkAddTail(b
, &b
->node
, &ll
->bufs
);
96 logfileFreeBuffer(Logfile
* lf
, logfile_buffer_t
* b
)
98 l_daemon_t
*ll
= (l_daemon_t
*) lf
->data
;
100 dlinkDelete(&b
->node
, &ll
->bufs
);
107 logfileHandleWrite(int fd
, void *data
)
109 Logfile
*lf
= (Logfile
*) data
;
110 l_daemon_t
*ll
= (l_daemon_t
*) lf
->data
;
115 * We'll try writing the first entry until its done - if we
116 * get a partial write then we'll re-schedule until its completed.
117 * Its naive but it'll do for now.
119 b
= static_cast<logfile_buffer_t
*>(ll
->bufs
.head
->data
);
121 ll
->flush_pending
= 0;
123 ret
= FD_WRITE_METHOD(ll
->wfd
, b
->buf
+ b
->written_len
, b
->len
- b
->written_len
);
124 debugs(50, 3, "logfileHandleWrite: " << lf
->path
<< ": write returned " << ret
);
126 if (ignoreErrno(errno
)) {
127 /* something temporary */
130 debugs(50, DBG_IMPORTANT
,"logfileHandleWrite: " << lf
->path
<< ": error writing (" << xstrerror() << ")");
131 /* XXX should handle this better */
132 fatal("I don't handle this error well!");
136 debugs(50, DBG_IMPORTANT
, "logfileHandleWrite: " << lf
->path
<< ": wrote 0 bytes?");
137 /* XXX should handle this better */
138 fatal("I don't handle this error well!");
140 /* ret > 0, so something was written */
141 b
->written_len
+= ret
;
142 assert(b
->written_len
<= b
->len
);
143 if (b
->written_len
== b
->len
) {
144 /* written the whole buffer! */
145 logfileFreeBuffer(lf
, b
);
148 /* Is there more to write? */
149 if (ll
->bufs
.head
== NULL
) {
152 /* there is, so schedule more */
155 Comm::SetSelect(ll
->wfd
, COMM_SELECT_WRITE
, logfileHandleWrite
, lf
, 0);
156 ll
->flush_pending
= 1;
162 logfileQueueWrite(Logfile
* lf
)
164 l_daemon_t
*ll
= (l_daemon_t
*) lf
->data
;
165 if (ll
->flush_pending
|| ll
->bufs
.head
== NULL
) {
168 ll
->flush_pending
= 1;
170 logfile_buffer_t
*b
= static_cast<logfile_buffer_t
*>(ll
->bufs
.head
->data
);
171 if (b
->len
+ 2 <= b
->size
)
172 logfile_mod_daemon_append(lf
, "F\n", 2);
174 /* Ok, schedule a write-event */
175 Comm::SetSelect(ll
->wfd
, COMM_SELECT_WRITE
, logfileHandleWrite
, lf
, 0);
179 logfile_mod_daemon_append(Logfile
* lf
, const char *buf
, int len
)
181 l_daemon_t
*ll
= (l_daemon_t
*) lf
->data
;
185 /* Is there a buffer? If not, create one */
186 if (ll
->bufs
.head
== NULL
) {
187 logfileNewBuffer(lf
);
189 debugs(50, 3, "logfile_mod_daemon_append: " << lf
->path
<< ": appending " << len
<< " bytes");
190 /* Copy what can be copied */
192 b
= static_cast<logfile_buffer_t
*>(ll
->bufs
.tail
->data
);
193 debugs(50, 3, "logfile_mod_daemon_append: current buffer has " << b
->len
<< " of " << b
->size
<< " bytes before append");
194 s
= min(len
, (b
->size
- b
->len
));
195 memcpy(b
->buf
+ b
->len
, buf
, s
);
199 assert(b
->len
<= LOGFILE_BUFSZ
);
202 logfileNewBuffer(lf
);
208 * only schedule a flush (write) if one isn't scheduled.
211 logfileFlushEvent(void *data
)
213 Logfile
*lf
= static_cast<Logfile
*>(data
);
216 * This might work better if we keep track of when we wrote last and only
217 * schedule a write if we haven't done so in the last second or two.
219 logfileQueueWrite(lf
);
220 eventAdd("logfileFlush", logfileFlushEvent
, lf
, 1.0, 1);
227 logfile_mod_daemon_open(Logfile
* lf
, const char *path
, size_t bufsz
, int fatal_flag
)
233 lf
->f_close
= logfile_mod_daemon_close
;
234 lf
->f_linewrite
= logfile_mod_daemon_writeline
;
235 lf
->f_linestart
= logfile_mod_daemon_linestart
;
236 lf
->f_lineend
= logfile_mod_daemon_lineend
;
237 lf
->f_flush
= logfile_mod_daemon_flush
;
238 lf
->f_rotate
= logfile_mod_daemon_rotate
;
240 cbdataInternalLock(lf
); // WTF?
241 debugs(50, 1, "Logfile Daemon: opening log " << path
);
242 ll
= static_cast<l_daemon_t
*>(xcalloc(1, sizeof(*ll
)));
246 Ip::Address localhost
;
247 args
[0] = "(logfile-daemon)";
250 localhost
.SetLocalhost();
251 ll
->pid
= ipcCreate(IPC_STREAM
, Log::TheConfig
.logfile_daemon
, args
, "logfile-daemon", localhost
, &ll
->rfd
, &ll
->wfd
, NULL
);
253 fatal("Couldn't start logfile helper");
257 /* Queue the initial control data */
258 tmpbuf
= static_cast<char*>(xmalloc(BUFSIZ
));
259 snprintf(tmpbuf
, BUFSIZ
, "r%d\nb%d\n", Config
.Log
.rotateNumber
, Config
.onoff
.buffered_logs
);
260 logfile_mod_daemon_append(lf
, tmpbuf
, strlen(tmpbuf
));
263 /* Start the flush event */
264 eventAdd("logfileFlush", logfileFlushEvent
, lf
, 1.0, 1);
270 logfile_mod_daemon_close(Logfile
* lf
)
272 l_daemon_t
*ll
= static_cast<l_daemon_t
*>(lf
->data
);
273 debugs(50, 1, "Logfile Daemon: closing log " << lf
->path
);
275 if (ll
->rfd
== ll
->wfd
)
281 kill(ll
->pid
, SIGTERM
);
282 eventDelete(logfileFlushEvent
, lf
);
285 cbdataInternalUnlock(lf
); // WTF??
289 logfile_mod_daemon_rotate(Logfile
* lf
)
292 debugs(50, 1, "logfileRotate: " << lf
->path
);
296 logfile_mod_daemon_append(lf
, tb
, 2);
300 * This routine assumes that up to one line is written. Don't try to
301 * call this routine with more than one line or subsequent lines
302 * won't be prefixed with the command type and confuse the logging
306 logfile_mod_daemon_writeline(Logfile
* lf
, const char *buf
, size_t len
)
308 l_daemon_t
*ll
= static_cast<l_daemon_t
*>(lf
->data
);
309 /* Make sure the logfile buffer isn't too large */
310 if (ll
->nbufs
> LOGFILE_MAXBUFS
) {
311 if (ll
->last_warned
< squid_curtime
- LOGFILE_WARN_TIME
) {
312 ll
->last_warned
= squid_curtime
;
313 debugs(50, DBG_IMPORTANT
, "Logfile: " << lf
->path
<< ": queue is too large; some log messages have been lost.");
317 /* Append this data to the end buffer; create a new one if needed */
318 /* Are we eol? If so, prefix with our logfile command byte */
319 logfile_mod_daemon_append(lf
, buf
, len
);
323 logfile_mod_daemon_linestart(Logfile
* lf
)
325 l_daemon_t
*ll
= static_cast<l_daemon_t
*>(lf
->data
);
327 assert(ll
->eol
== 1);
331 logfile_mod_daemon_append(lf
, tb
, 1);
335 logfile_mod_daemon_lineend(Logfile
* lf
)
337 l_daemon_t
*ll
= static_cast<l_daemon_t
*>(lf
->data
);
339 assert(ll
->eol
== 0);
341 /* Kick a write off if the head buffer is -full- */
342 if (ll
->bufs
.head
!= NULL
) {
343 b
= static_cast<logfile_buffer_t
*>(ll
->bufs
.head
->data
);
344 if (b
->node
.next
!= NULL
|| !Config
.onoff
.buffered_logs
)
345 logfileQueueWrite(lf
);
350 logfile_mod_daemon_flush(Logfile
* lf
)
352 l_daemon_t
*ll
= static_cast<l_daemon_t
*>(lf
->data
);
353 if (commUnsetNonBlocking(ll
->wfd
)) {
354 debugs(50, DBG_IMPORTANT
, "Logfile Daemon: Couldn't set the pipe blocking for flush! You're now missing some log entries.");
357 while (ll
->bufs
.head
!= NULL
) {
358 logfileHandleWrite(ll
->wfd
, lf
);
360 if (commSetNonBlocking(ll
->wfd
)) {
361 fatalf("Logfile Daemon: %s: Couldn't set the pipe non-blocking for flush!\n", lf
->path
);