Watch log

S

Salvatore Di Fazio

Hi guys,
I've an application that writes a log file.
I need to write an application with python that will read this file.

I would like wait until a new line will be write in the file.

Something like the "watch cat Log" command on Linux os.

How can I check the eof and restart the reading?
 
B

Bjoern Schliessmann

Salvatore said:
I would like wait until a new line will be write in the file.

Something like the "watch cat Log" command on Linux os.

Why not read the file continuously and only do something if a new
line is complete (i. e. a newline char is detected)?
How can I check the eof and restart the reading?

Why do you want to check for an EOF character?

Regards,


Björn
 
S

Salvatore Di Fazio

Bjoern Schliessmann ha scritto:
Why not read the file continuously and only do something if a new
line is complete (i. e. a newline char is detected)?

How can I read the file continuously?
 
L

Larry Bates

Salvatore said:
Hi guys,
I've an application that writes a log file.
I need to write an application with python that will read this file.

I would like wait until a new line will be write in the file.

Something like the "watch cat Log" command on Linux os.

How can I check the eof and restart the reading?
You didn't provide much info so here goes. If you use the
logger module built into Python you can set up a logger that
uses sockets. Have your program log to a socket and have the
other program read from the socket. That way you will always
have the latest log entries in the monitor program.

If you can't control the program that writes the log file,
have your monitoring program look for change in the length
of the logfile (you can use the os.stat module for this).
When it changes, open the file, seek to the old length and
read bytes up to the new length and close the logfile.
What is read will be the new log entry (or entries).
Reset the logfile length high water mark and loop.

Hope information helps.

-Larry Bates
 
T

Thinker

Salvatore said:
Bjoern Schliessmann ha scritto:



How can I read the file continuously?
What you want is something like 'tail -f' in linux.
If you look into it's source code, you will find it check file periodically.
When a read() reach the end of the file, read() will return a empty string.
You can try to read data from the file periodically after a EOF.
I think it is what you want.
 
M

Michele Simionato

Salvatore said:
Hi guys,
I've an application that writes a log file.
I need to write an application with python that will read this file.

You may begin from this:

$ cat showlog.py
import sys
from ScrolledText import ScrolledText

interval =50 # ms

def displayfile(f, textwidget):
textwidget.insert('end', f.readline())
textwidget.after(interval, displayfile, f, textwidget)

if __name__ == '__main__':
st = ScrolledText()
st.pack()
displayfile(sys.stdin, st)
st.mainloop()

$ python showlog.py < mylogfile.log

HTH,
Michele Simionato
 
S

Salvatore Di Fazio

Thinker ha scritto:
What you want is something like 'tail -f' in linux.
If you look into it's source code, you will find it check file periodically.
When a read() reach the end of the file, read() will return a empty string.
You can try to read data from the file periodically after a EOF.
I think it is what you want.

So every 2 sec:
- open the file
- read the file from the oldPosition to the EOF
- save the last position in file
- first point

mmm ok
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top