Jump to content

Reading a log file's update time.


Euphoria
 Share

Recommended Posts

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

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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)
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • Moderators

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.

"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

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