]>
git.ipfire.org Git - thirdparty/squid.git/blob - helpers/log_daemon/file/log_file_daemon.cc
16 #include <sys/param.h>
36 /* parse buffer - ie, length of longest expected line */
37 #define LOGFILE_BUF_LEN 65536
40 rotate(const char *path
, int rotate_count
)
46 char from
[MAXPATHLEN
];
50 if (stat(path
, &sb
) == 0)
51 if (S_ISREG(sb
.st_mode
) == 0)
54 /* Rotate numbers 0 through N up one */
55 for (i
= rotate_count
; i
> 1;) {
57 snprintf(from
, MAXPATHLEN
, "%s.%d", path
, i
- 1);
58 snprintf(to
, MAXPATHLEN
, "%s.%d", path
, i
);
59 #if defined(_SQUID_OS2_) || defined(_SQUID_WIN32_)
64 if (rotate_count
> 0) {
65 snprintf(to
, MAXPATHLEN
, "%s.%d", path
, 0);
66 #if defined(_SQUID_OS2_) || defined(_SQUID_WIN32_)
76 * L<data>\n - logfile data
81 * r<n>\n - set rotate count to <n>
82 * b<n>\n - 1 = buffer output, 0 = don't buffer output
85 main(int argc
, char *argv
[])
89 char buf
[LOGFILE_BUF_LEN
];
90 int rotate_count
= 10;
94 printf("Error: usage: %s <logfile>\n", argv
[0]);
97 fp
= fopen(argv
[1], "a");
102 setbuf(stdout
, NULL
);
104 t
= open(_PATH_DEVNULL
, O_RDWR
);
108 while (fgets(buf
, LOGFILE_BUF_LEN
, stdin
)) {
109 /* First byte indicates what we're logging! */
112 if (buf
[1] != '\0') {
113 fprintf(fp
, "%s", buf
+ 1);
114 /* try to detect the 32-bit file too big write error and rotate */
115 int err
= ferror(fp
);
118 /* file too big - recover by rotating the logs and starting a new one.
119 * out of device space - recover by rotating and hoping that rotation count drops a big one.
121 if (err
== EFBIG
|| err
== ENOSPC
) {
122 fprintf(stderr
, "WARNING: %s writing %s. Attempting to recover via a log rotation.\n",strerror(err
),argv
[1]);
124 rotate(argv
[1], rotate_count
);
125 fp
= fopen(argv
[1], "a");
130 fprintf(fp
, "%s", buf
+ 1);
142 rotate(argv
[1], rotate_count
);
143 fp
= fopen(argv
[1], "a");
154 //fprintf(fp, "SET ROTATE: %s\n", buf + 1);
155 rotate_count
= atoi(buf
+ 1);
158 //fprintf(fp, "SET BUFFERED: %s\n", buf + 1);
159 do_buffer
= (buf
[1] == '1');
165 /* Just in case .. */
166 fprintf(fp
, "%s", buf
);