Jump to content

log read problem


jing
 Share

Recommended Posts

I have an application which maintain a log file, so it adds new lines into this file continously. I need to make a program which gets the newly generated lines out and send to another machine through TCP.

My problem now is, how can I get the newly generated lines each time by reading the file?

I used the file modifaction time to check whether there is any changes, but which function can I use to get the latest update log? i have read the file API, but still don't know how to do it

Link to comment
Share on other sites

Link to comment
Share on other sites

Hi,

_FileCountLines [i think] reads the whole file each time, which might slow things down, depending on your needs and the size of the file.

You could have a var which keeps current file size, and whenever file size changes, only read the end of the file by the difference in size;

See "TailRW" in my sig for funcs which read the end of the file by binary read API, or refer Larry's API.

Best, randall

Link to comment
Share on other sites

Hiya, iwe created alot log parsers latly and i have this routine i like to use.

I have yett to see it halt, but it does drain some cpu power though but it works pretty well.

#include <file.au3>

Dim $CountLines,$lastline,$string,$log

$log = 'LOGGFILE.TXT'

$lastline = _FileCountLines($log)

While 1

    $CountLines = _FileCountLines($log)

        If $CountLines > $lastline Then
            $string = FileReadLine($log, $CountLines)
            
            ConsoleWrite($string&@CRLF)             

            $lastline = $CountLines 
        EndIf

    Sleep(10) ; Just so tailer does not drain 100% cpu, if it uppdates slower, upp this value and it drains even less power.

WEnd

Every time Log updates it will write uppdated string into console.

Edited by jokke
UDF:Crypter a file encrypt / decrypt tool with no need to remember a password again. Based on Caesar cipher using entire ASCII Table.Script's: PixelSearch Helper, quick and simple way to create a PixelSeach.Chatserver - simplified, not so complicated multi-socket server.AutoIT - Firewall, simple example on howto create a firewall with AutoIt.
Link to comment
Share on other sites

Thank you so much guys!

The file line count things can work definitely. It is a great idea. But it may work quite ineffeiently since it may need to scan the whole document which is not desirable.

randallc made a really good point. Thanks. I will try it.

Thanks again , guys.

Link to comment
Share on other sites

I have a better solution than counting the lines of the file many times every second... Why not just pull filesize or file-last-changed-time from the file, compare it to the value gotten 10 ms ago, if it changed to something else stay looping.

/edit: And, you can always keep a line counter variable that you add one to every time it finds the file changed, then you read that line using fileReadLine. :whistle:

Edited by SadBunny

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...