Jump to content
Sign in to follow this  
Euphoria

Reading a log file's update time.

Recommended Posts

Euphoria

Hi All,

I am still learning AutoIt and this is only my second script.

I have a log file which logs events from an application. I wanted to have an script to read the last line of this log file which I created. However now depending on conditions I have to read the log file in following scenarios:

A. Read the time for last 5 lines.

B. Read a specific string in last 5 lines and if found, return with success message displayed.

C. Can search for events after a specific Time in log file.

Appreciate in case somebody help me with these requirements.

TestingScript.au3

Share this post


Link to post
Share on other sites
Euphoria

Ok.. I have got a solution for reading last few lines of a log file. I tried to make it for last five lines. If I try this with a local log file it works, but on a remote server log file  it seems that it reads only last line (not the upper four). Below is the script I am trying. In case the remote server log file is around 8 MB if this matters....

DriveMapDel("M:")
DriveMapAdd("M:", "\\MyServer\logs", 0, "Domain\UserName", "password")
$file = FileOpen("M:\Test.log", 0)
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf
$lines = _FileCountLines($file)
For $i = $lines-4 To $lines
    If Not FileReadLine($file, $i) = "" Then $data = $data & FileReadLine($file, $i) & @CRLF
Next
MsgBox(0, "Test", StringTrimRight($data, 1))
DriveMapDel("M:")

Any help would be appreciated.

 

Share this post


Link to post
Share on other sites
Euphoria

Hi John,

Could you also be kind to solve my below query:

 

 

Ok.. I have got a solution for reading last few lines of a log file. I tried to make it for last five lines. If I try this with a local log file it works, but on a remote server log file  it seems that it reads only last line (not the upper four). Below is the script I am trying. In case the remote server log file is around 8 MB if this matters....

DriveMapDel("M:")
DriveMapAdd("M:", "\\MyServer\logs", 0, "Domain\UserName", "password")
$file = FileOpen("M:\Test.log", 0)
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf
$lines = _FileCountLines($file)
For $i = $lines-4 To $lines
    If Not FileReadLine($file, $i) = "" Then $data = $data & FileReadLine($file, $i) & @CRLF
Next
MsgBox(0, "Test", StringTrimRight($data, 1))
DriveMapDel("M:")

Any help would be appreciated.

 

I am not getting any hint why it is not working on a remote server log however works well with any local log file !!!!

Share this post


Link to post
Share on other sites
michaelslamet

If it works with local file, it should work with remote file too,

Try using _FileReadToArray like example below, is it works?

If yes then you can use _FileReadToArray plus you will need to add some error checkings:

#include<File.au3>

global $aArray, $sData

_FileReadToArray("c:\list.txt", $aArray)

For $i = Ubound($aArray)-5 To UBound($aArray)-1
    If Not $aArray[$i] = "" Then $sData &= $aArray[$i] & @CRLF
Next

MsgBox(0, "", $sData)

Share this post


Link to post
Share on other sites
sahsanu

I am not getting any hint why it is not working on a remote server log however works well with any local log file !!!!

 

As far as I know _FileCountLines() function needs a path and a filename instead of a handle, try this sample:

#include <File.au3>
Local $data
DriveMapDel("M:")
DriveMapAdd("M:", "\\MyServer\logs", 0, "Domain\UserName", "password")
$file="M:\Test.log"
;$file = FileOpen("M:\Test.log", 0)
;If $file = -1 Then
;   MsgBox(0, "Error", "Unable to open file.")
;   Exit
;EndIf
$lines = _FileCountLines($file) ;you should use a path and filename instead a file handle returned by FileOpen
For $i = $lines-4 To $lines
    If Not FileReadLine($file, $i) = "" Then $data = $data & FileReadLine($file, $i) & @CRLF
Next
DriveMapDel("M:")
MsgBox(0, "Test", StringTrimRight($data, 1))

I've tested it on my computer and a remote virtual machine and it is working as expected.

Cheers,

sahsanu

  • Like 1

Share this post


Link to post
Share on other sites
Euphoria

Thanks sahsanu,

This works if I do not use below:

;$file = FileOpen("M:Test.log", 0)

;If $file = -1 Then

;MsgBox(0, "Error", "Unable to open file.")

;Exit

;EndIf

However not so sure for Error Handling messages.

One more query (or rather curiosity). If I can have this display Auto-update so I can display new events coming in from tail of the log file.... 

I know I am asking too much but bare with new kid.

Share this post


Link to post
Share on other sites
JLogan3o13

Euphoria, please wait at least 24 hours before bumping your thread. Everyone assisting in this forum is a volunteer; please give people time to respond.


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites
Euphoria
My bad.. I was ahead of Curiosity.

 

Thanks Jlogan3o13. Taken a note.

Share this post


Link to post
Share on other sites
sahsanu

Thanks sahsanu,

 

 You are welcome ;-)

This works if I do not use below:

;$file = FileOpen("M:Test.log", 0)

;If $file = -1 Then

;MsgBox(0, "Error", "Unable to open file.")

;Exit

;EndIf

 

Yeah, that is the reason I commented those lines :D

One more query (or rather curiosity). If I can have this display Auto-update so I can display new events coming in from tail of the log file.... 

 

Take a look to below script, it acts as tail command, first print the last x lines from your log in the remote machine and after that it will print the lines since the last change made to the remote file (just new lines). I couldn't perform too much tests so... use it as your own risk hehehe. Anyway, I'm not a good coder so I don't really now if there is another and more efficient way to perform the same task... but I'm pretty sure one of our forum buddies could write a better script.

#include <File.au3>
HotKeySet("{ESC}", "_Terminate");to terminate the script just pres ESC key

Global $size = 0, $lines = 0 ;declared to 0 to retrieve x last lines the first time it is executed
Global $mapdrive = "M:"
Global $filename = "test.log"
Global $filepath = $mapdrive & "\" & $filename

; Map remote share to drive X
DriveMapAdd($mapdrive, "\\MyServer\logs", 0, "Domain\UserName", "password")

While 1

    _tail($filepath, 15, 20) ;filepath, interval to check the log in seconds (default 30), number of last lines to be retreved the first time)
    If @error Then
        ConsoleWrite("_tail function returned error: " & @error & @CRLF)
        DriveMapDel($mapdrive)
        Exit
    EndIf

WEnd

; Remove previously mapped unit
DriveMapDel($mapdrive)

Func _tail($file, $wait = 30, $initiallastlines = 15)
    If $file = "" Then
        ConsoleWrite("I need a file path" & @CRLF)
        SetError(1)
        Return -1
    EndIf

    $filesize = FileGetSize($file) ;get filesize for comparing purposes
    If @error Then
        ConsoleWrite("Error retrieving file size. Exiting" & @CRLF)
        SetError(2)
        Return -1
    EndIf

    If $filesize <> $size Then ;check whether filesize is not equal to initial size
        If $lines = 0 Then ;this means that it is the first time qe try to get the log
            $linesnew = _FileCountLines($file)
            If @error Then
                ConsoleWrite("File cannot be opened or found. Exiting" & @CRLF)
                SetError(3)
                Return -1
            EndIf
            If $linesnew < $initiallastlines Then $initiallastlines=$linesnew
            For $i = $linesnew - $initiallastlines + 1 To $linesnew
                ConsoleWrite(FileReadLine($file, $i) & @CRLF) ;write last x lines on log
            Next
            $lines = $linesnew
        Else
            $linesnew = _FileCountLines($file)
            If @error Then
                ConsoleWrite("File cannot be opened or found. Exiting" & @CRLF)
                SetError(3)
                Return -1
            EndIf
            For $i = $lines + 1 To $linesnew
                ConsoleWrite(FileReadLine($file, $i) & @CRLF) ;write new lines added to log file
            Next
            $lines = $linesnew
        EndIf
        $size = $filesize
    EndIf

    Sleep($wait * 1000)
    Return 0

EndFunc   ;==>_tail

Func _Terminate()
    Exit 0
EndFunc   ;==>_Terminate

Note: To terminate program just press ESC key

Note2: The lines read from remote file are printed using ConsoleWrite so if you want them in a file you should code it ;)

I hope this helps you at least to start to write your own script.

Cheers,

sahsanu

 

Edited by sahsanu

Share this post


Link to post
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
Sign in to follow this  

×