最近在建立爬虫的自动化测试体系,这块要能够监测apache的access-log做验证,也就是类似tailf的功能,下面就是一个实现:
int main(int argc, char **argv) {
char buffer[BUFSIZ];
size_t osize, nsize;
FILE *str;
const char *filename;
int count;
if (argc != 2) {
fprintf(stderr, "Usage: tailf logfile\n");
exit(1);
}
filename = argv[1];
//tailf(filename, 10);
for (osize = filesize(filename);;) {
nsize = filesize(filename);
if (nsize != osize) {
if (!(str = fopen(filename, "r"))) {
fprintf(stderr, "Cannot open \"%s\" for read\n", filename);
perror(argv[0]);
exit(1);
}
if (!fseek(str, osize, SEEK_SET))
while ((count = fread(buffer, 1, sizeof(buffer), str)) > 0)
fwrite(buffer, 1, count, stdout);
fflush(stdout);
fclose(str);
osize = nsize;
}
usleep(250000); /* 250mS */
}
return 0;
char buffer[BUFSIZ];
size_t osize, nsize;
FILE *str;
const char *filename;
int count;
if (argc != 2) {
fprintf(stderr, "Usage: tailf logfile\n");
exit(1);
}
filename = argv[1];
//tailf(filename, 10);
for (osize = filesize(filename);;) {
nsize = filesize(filename);
if (nsize != osize) {
if (!(str = fopen(filename, "r"))) {
fprintf(stderr, "Cannot open \"%s\" for read\n", filename);
perror(argv[0]);
exit(1);
}
if (!fseek(str, osize, SEEK_SET))
while ((count = fread(buffer, 1, sizeof(buffer), str)) > 0)
fwrite(buffer, 1, count, stdout);
fflush(stdout);
fclose(str);
osize = nsize;
}
usleep(250000); /* 250mS */
}
return 0;
}