jing Posted March 11, 2007 Share Posted March 11, 2007 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 More sharing options...
ChrisL Posted March 11, 2007 Share Posted March 11, 2007 Like this #include <file.au3> $sFilePath = @WindowsDir & "\WindowsUpdate.log" $lastline = FIleReadLine($sFilePath,_FileCountLines ( $sFilePath ) - 1) Msgbox(0,"",$lastline) [u]Scripts[/u]Minimize gui to systray _ Fail safe source recoveryMsgbox UDF _ _procwatch() Stop your app from being closedLicensed/Trial software system _ Buffering Hotkeys_SQL.au3 ADODB.Connection _ Search 2d Arrays_SplashTextWithGraphicOn() _ Adjust Screen GammaTransparent Controls _ Eventlogs without the crap_GuiCtrlCreateFlash() _ Simple Interscript communication[u]Websites[/u]Curious Campers VW Hightops Lambert Plant Hire Link to comment Share on other sites More sharing options...
randallc Posted March 11, 2007 Share Posted March 11, 2007 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 ExcelCOM... AccessCom.. Word2... FileListToArrayNew...SearchMiner... Regexps...SQL...Explorer...Array2D.. _GUIListView...array problem...APITailRW Link to comment Share on other sites More sharing options...
jokke Posted March 11, 2007 Share Posted March 11, 2007 (edited) 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 March 11, 2007 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 More sharing options...
jing Posted March 12, 2007 Author Share Posted March 12, 2007 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 More sharing options...
SadBunny Posted March 12, 2007 Share Posted March 12, 2007 (edited) 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. Edited March 12, 2007 by SadBunny Roses are FF0000, violets are 0000FF... All my base are belong to you. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now