34#define LOGFILE_BUF_LEN 65536
37rotate(
const char *path,
int rotate_count)
47 if (stat(path, &
sb) == 0)
48 if (S_ISREG(
sb.st_mode) == 0)
52 for (i = rotate_count; i > 1;) {
54 snprintf(from,
MAXPATHLEN,
"%s.%d", path, i - 1);
56#if _SQUID_OS2_ || _SQUID_WINDOWS_
59 fprintf(stderr,
"WARNING: remove '%s' failure: %s\n", to,
xstrerr(xerrno));
62 if (rename(from, to) < 0 && errno != ENOENT) {
64 fprintf(stderr,
"WARNING: rename '%s' to '%s' failure: %s\n", from, to,
xstrerr(xerrno));
67 if (rotate_count > 0) {
69#if _SQUID_OS2_ || _SQUID_WINDOWS_
72 fprintf(stderr,
"WARNING: remove '%s' failure: %s\n", to,
xstrerr(xerrno));
75 if (rename(path, to) < 0 && errno != ENOENT) {
77 fprintf(stderr,
"WARNING: rename %s to %s failure: %s\n", path, to,
xstrerr(xerrno));
94main(
int argc,
char *argv[])
99 int rotate_count = 10;
103 printf(
"Error: usage: %s <logfile>\n", argv[0]);
106 fp = fopen(argv[1],
"a");
111 setbuf(stdout,
nullptr);
116 t = open(_PATH_DEVNULL, O_RDWR);
124 if (buf[1] !=
'\0') {
125 fprintf(fp,
"%s", buf + 1);
127 int err = ferror(fp);
133 if (err == EFBIG || err == ENOSPC) {
134 fprintf(stderr,
"WARNING: %s writing %s. Attempting to recover via a log rotation.\n",
xstrerr(err),argv[1]);
136 rotate(argv[1], rotate_count);
137 fp = fopen(argv[1],
"a");
142 fprintf(fp,
"%s", buf + 1);
154 rotate(argv[1], rotate_count);
155 fp = fopen(argv[1],
"a");
167 rotate_count = atoi(buf + 1);
171 do_buffer = (buf[1] ==
'1');
178 fprintf(fp,
"%s", buf);
int main(int argc, char *argv[])
static void rotate(const char *path, int rotate_count)
const char * xstrerr(int error)