]>
git.ipfire.org Git - thirdparty/squid.git/blob - helpers/log_daemon/file/log_file_daemon.cc
19 #include <sys/param.h>
39 /* parse buffer - ie, length of longest expected line */
40 #define LOGFILE_BUF_LEN 65536
43 rotate(const char *path
, int rotate_count
)
49 char from
[MAXPATHLEN
];
53 if (stat(path
, &sb
) == 0)
54 if (S_ISREG(sb
.st_mode
) == 0)
57 /* Rotate numbers 0 through N up one */
58 for (i
= rotate_count
; i
> 1;) {
60 snprintf(from
, MAXPATHLEN
, "%s.%d", path
, i
- 1);
61 snprintf(to
, MAXPATHLEN
, "%s.%d", path
, i
);
62 #if defined(_SQUID_OS2_) || defined(_SQUID_WIN32_)
67 if (rotate_count
> 0) {
68 snprintf(to
, MAXPATHLEN
, "%s.%d", path
, 0);
69 #if defined(_SQUID_OS2_) || defined(_SQUID_WIN32_)
79 * L<data>\n - logfile data
84 * r<n>\n - set rotate count to <n>
85 * b<n>\n - 1 = buffer output, 0 = don't buffer output
88 main(int argc
, char *argv
[])
92 char buf
[LOGFILE_BUF_LEN
];
93 int rotate_count
= 10;
97 printf("Error: usage: %s <logfile>\n", argv
[0]);
100 fp
= fopen(argv
[1], "a");
105 setbuf(stdout
, NULL
);
107 t
= open(_PATH_DEVNULL
, O_RDWR
);
111 while (fgets(buf
, LOGFILE_BUF_LEN
, stdin
)) {
112 /* First byte indicates what we're logging! */
115 if (buf
[1] != '\0') {
116 fprintf(fp
, "%s", buf
+ 1);
117 /* try to detect the 32-bit file too big write error and rotate */
118 int err
= ferror(fp
);
121 /* file too big - recover by rotating the logs and starting a new one.
122 * out of device space - recover by rotating and hoping that rotation count drops a big one.
124 if (err
== EFBIG
|| err
== ENOSPC
) {
125 fprintf(stderr
, "WARNING: %s writing %s. Attempting to recover via a log rotation.\n",strerror(err
),argv
[1]);
127 rotate(argv
[1], rotate_count
);
128 fp
= fopen(argv
[1], "a");
133 fprintf(fp
, "%s", buf
+ 1);
145 rotate(argv
[1], rotate_count
);
146 fp
= fopen(argv
[1], "a");
157 //fprintf(fp, "SET ROTATE: %s\n", buf + 1);
158 rotate_count
= atoi(buf
+ 1);
161 //fprintf(fp, "SET BUFFERED: %s\n", buf + 1);
162 do_buffer
= (buf
[1] == '1');
168 /* Just in case .. */
169 fprintf(fp
, "%s", buf
);