Jump to content

Read a log file opened and constantly updated by another program


Faalamva
 Share

Recommended Posts

Hi,

I'm trying to read a log file on Windows 7, opened and constantly updated by another application.

So far, I'm using a combination of FileGetSize to detect the apparition of new lines in the log (i.e. when the filesize changes), then FileOpen and FileSetPos to retrieve the new data starting from the last known end-position.

It works, HOWEVER I need to refresh (F5) the directory containing the file in Windows, otherwise the new content is not yet "seen" by my program.

I've found some solutions like here : https://stackoverflow.com/questions/4400517/how-can-i-read-a-file-even-when-getting-an-in-use-by-another-process-exception

But I don't know how to implement that in autoit.

I've also figured out that the solution may imply WinAPI. I'm pretty sure this kind of problem has already been treated before (monitoring logs in real-time seems a very classical problem to me) but I haven't been able to find an example so far (except the link above).

Would you please be kind enough to provide me with a very simple example ?

Thanks !

Link to comment
Share on other sites

Thank you, but unfortunately it's not enough.

It seems the new content is not visible until some kind of refresh, because the log file is opened by another application.

I can tell because I've already tried the code you linked (something similar).

EDIT : I've found a workaround, just before getting the FileSize, I do a FileCopy into a TMP file, then I work on the TMP file. It works, but it's VERY resource-consuming since I need to create that TMP file on a regular basis (every x seconds), and that means duplicating the whole file every x seconds... I really need something lighter and faster.

Link to comment
Share on other sites

  • Moderators

Personally I go for CMTrace, which is part of the SCCM toolkit. It is great for watching logs in real time, I even use it in a number of scripts so I can color code Info, Warnings and Errors.

https://www.microsoft.com/en-us/download/confirmation.aspx?id=50012

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

Global $g_iPid = 0 ;PID Handle to process writing the log
Global $g_sDebugFile = "\myfile.log"
Global $g_hDebugFile = FileOpen($g_sDebugFile, 0) ;Read Permissions ONLY!


While ProcessExists($g_iPid)
    If GetInfo($g_hDebugFile) <= 0 Then ExitLoop
WEnd

FileClose($g_hDebugFile)

Func GetInfo($hDebugFile)

    Static Local $sRead ;Everything Read so far
    Static Local $iRetry = 300 ;30 seconds
    Local $sRead_Current ;Just the new data
    Local $bHasData = False

    $sRead_Current = FileRead($hDebugFile, -1)
    If @extended = 0 Then
        $iRetry -= 1
        Sleep(100)
        ;No New Data
    Else
        $bHasData = True
        $sRead &= $sRead_Current
        ;ConsoleWrite($sRead & @CRLF)
        $iRetry = 300
    EndIf

    Return $iRetry
EndFunc

This works quite well for me

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...