使用c ...尾随日志文件 [英] tailing a log file using c ...

查看:56
本文介绍了使用c ...尾随日志文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


问题 - 需要读取日志(文本)文件查找某些条目,

这个文件仍然会被写入另一个第三方流程,并且

因此经常需要监控(我敢说拖尾?)


尾巴是否可行/实用/高效使用c的日志文件。

问题是:


1.如果我正在阅读文件并且我达到了EOF,我会看到新文件

写道 - 或者我是否需要重新打开文件才能看到它的新增内容?


2.我是否会使用unix''tail'命令输入ac程序?


3.有什么建议值得赞赏吗?


干杯


Tony

解决方案



" Tony Stock" < TS **** @ hotmail.com> schrieb im Newsbeitrag

news:7a ********************** @ posting.google.com ..。

1.如果我正在阅读文件并且我达到EOF,我会看到新文件写入 - 或者我是否必须重新打开文件以查看新增内容?


取决于操作系统。在很多UNIX上,在Windows上,是的,你这个
会看到新的写入。


你必须使用无缓冲的I在共享模式下打开文件/ O(open(),

read(),close())。

将read()中的数据复制到内部缓冲区并打印当它准备就绪时

(完整的一行)。


保持文件打开并尝试读取()超出EOF。如果错误是

返回,则没有新数据可用,否则请尽可能多地阅读
流程。


2我会使用unix''tail''命令输入ac程序吗?




您也可以这样做,调用system()发出tail命令并且

检索其输出。但另一种解决方案更简单。 :-)



" Ekkehard Morgenstern" < EK ****************** @ onlinehome.de>写道:

" Tony Stock" < TS **** @ hotmail.com> schrieb:

1.如果我正在阅读文件并且我达到EOF,我会看到新的
文件写入 - 或者我是否必须重新打开文件才能看到新的
添加内容对它?



它在标准C中无法保证。当你打开文件时它可能是完全锁定的
所以没有其他程序可以追加日志条目

到它。

取决于操作系统。在许多UNIX上,在Windows上,是的,你会看到新的写入。

你必须使用无缓冲的I / O以共享模式打开文件

(),read(),close())。




无缓冲的I / O不是标准C的一部分,并且在comp.lang.c上是偏离主题的。 。

打开,读取,关闭函数可以在comp.unix.programmer上讨论,

,即使你正在为Windows编写代码,因为它们是POSIX函数。 br />

-

Simon。


文章< 3f ******* ****************@news.optusnet.com.au> ,

Simon Biber< ne ** @ ralminNOSPAM.cc>写道:


无缓冲的I / O不是标准C的一部分,并且在comp.lang.c上是偏离主题的。
打开,读取,关闭函数可以是在comp.unix.programmer上讨论,
即使你正在为Windows编写代码,因为它们是POSIX函数。




我不认为真正。该标准定义了setvbuf()来设置/更改

流的缓冲模式。


#include< stdio.h>

int setvbuf(FILE * stream,char * buf,int mode,size_t size);


-

Rouben Rostamian


Hi all,

problem - need to read a log (text) file looking for certain entries,
this file will still be written to by another 3rd party process, and
hence constantly require monitoring (dare I say tailing ?)

Is it possible / practical / efficient to ''tail'' a log file using c.
Questions are :

1. If I was reading file and I reached EOF, would I see new file
writes - or would I have to re-open file to see new additions to it ?

2. Would I use unix ''tail'' command fed into a c program ?

3. Any advice appreciated ?

Cheers

Tony

解决方案


"Tony Stock" <ts****@hotmail.com> schrieb im Newsbeitrag
news:7a**********************@posting.google.com.. .

1. If I was reading file and I reached EOF, would I see new file
writes - or would I have to re-open file to see new additions to it ?
Depends on the operating system. On many UNIXes, and on Windows, yes, you
will see the new writes.

You have to open the file in a shared mode using unbuffered I/O (open(),
read(), close()).

Copy the data from read() to an internal buffer and print it when it''s ready
(complete line).

Keep the file open and attempt to read() beyond the EOF. If an error is
returned, no new data is available, otherwise read as much as you can
process.

2. Would I use unix ''tail'' command fed into a c program ?



You could also do that, calling system() to issue a tail command and
retrieving its output. But the other solution is simpler. :-)



"Ekkehard Morgenstern" <ek******************@onlinehome.de> wrote:

"Tony Stock" <ts****@hotmail.com> schrieb:

1. If I was reading file and I reached EOF, would I see new
file writes - or would I have to re-open file to see new
additions to it ?


It''s not guaranteed in standard C. When you open the file it may be
completely locked down so no other program could append log entries
to it.
Depends on the operating system. On many UNIXes, and on Windows,
yes, you will see the new writes.

You have to open the file in a shared mode using unbuffered I/O
(open(), read(), close()).



Unbuffered I/O is not part of Standard C, and is off-topic on comp.lang.c.
The open, read, close functions can be discussed on comp.unix.programmer,
even if you are writing code for Windows, since they are POSIX functions.

--
Simon.


In article <3f***********************@news.optusnet.com.au> ,
Simon Biber <ne**@ralminNOSPAM.cc> wrote:


Unbuffered I/O is not part of Standard C, and is off-topic on comp.lang.c.
The open, read, close functions can be discussed on comp.unix.programmer,
even if you are writing code for Windows, since they are POSIX functions.



I don''t think that true. The standard defines setvbuf() to set/change
the buffering mode of a stream.

#include <stdio.h>
int setvbuf(FILE *stream, char *buf, int mode , size_t size);

--
Rouben Rostamian


这篇关于使用c ...尾随日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆